OpenVPN 3 Core Library
Loading...
Searching...
No Matches
test_pushlex.cpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
17#include "test_common.hpp"
18
21
22using namespace openvpn;
23
24static std::string get_next(PushLex &pl)
25{
26 if (pl.defined())
27 return pl.next();
28 return std::string();
29}
30
31// parse a PUSH_UPDATE message
32TEST(Pushlex, Test1)
33{
34 const std::string csv = "PUSH_UPDATE,route 10.9.0.0 255.255.0.0,route 8.8.8.8,route-ipv6 fd69::/64";
35 PushLex pl(const_buf_from_string(csv), true);
36 ASSERT_EQ(get_next(pl), "route 10.9.0.0 255.255.0.0");
37 ASSERT_EQ(get_next(pl), "route 8.8.8.8");
38 ASSERT_EQ(get_next(pl), "route-ipv6 fd69::/64");
39 ASSERT_FALSE(pl.defined());
40}
41
42// parse a PUSH_UPDATE message with tortured StandardLex quoting
43TEST(Pushlex, Test2)
44{
45 const std::string csv = "PUSH_UPDATE,echo \"one,two,three\",,route 1.2.3.4,echo \\\",echo \"foo\",echo \\,,echo fin,";
46 PushLex pl(const_buf_from_string(csv), true);
47 ASSERT_EQ(get_next(pl), "echo \"one,two,three\"");
48 ASSERT_EQ(get_next(pl), "");
49 ASSERT_EQ(get_next(pl), "route 1.2.3.4");
50 ASSERT_EQ(get_next(pl), "echo \\\"");
51 ASSERT_EQ(get_next(pl), "echo \"foo\"");
52 ASSERT_EQ(get_next(pl), "echo \\,");
53 ASSERT_EQ(get_next(pl), "echo fin");
54 ASSERT_EQ(get_next(pl), "");
55 ASSERT_FALSE(pl.defined());
56}
57
58// test PushLex with discard_prefix == false
59TEST(Pushlex, Test3)
60{
61 const std::string csv = "PUSH_UPDATE,route 10.9.0.0 255.255.0.0,route 8.8.8.8,route-ipv6 fd69::/64";
62 PushLex pl(const_buf_from_string(csv), false);
63 ASSERT_EQ(get_next(pl), "PUSH_UPDATE"); // this is here because discard_prefix == false
64 ASSERT_EQ(get_next(pl), "route 10.9.0.0 255.255.0.0");
65 ASSERT_EQ(get_next(pl), "route 8.8.8.8");
66 ASSERT_EQ(get_next(pl), "route-ipv6 fd69::/64");
67 ASSERT_FALSE(pl.defined());
68}
69
70// test PushLex with a null message
71TEST(Pushlex, Test4)
72{
73 const std::string csv = "PUSH_UPDATE,";
74 PushLex pl(const_buf_from_string(csv), true);
75 ASSERT_FALSE(pl.defined());
76}
77
78// test PushLex with a null buffer
79TEST(Pushlex, Test5)
80{
81 ConstBuffer cbuf;
82 PushLex pl(cbuf, true);
83 ASSERT_FALSE(pl.defined());
84}
85
86// test that PushLex throws an exception when prefix
87// is unrecognized
88TEST(Pushlex, TestException1)
89{
91 {
92 const std::string csv = "FOO,route 10.9.0.0 255.255.0.0,route 8.8.8.8,route-ipv6 fd69::/64";
93 PushLex pl(const_buf_from_string(csv), true);
94 },
95 PushLex::pushlex_error,
96 "pushlex_error: not a valid PUSH_x message [1]");
97}
98
99// test that PushLex throws an exception when prefix
100// is not followed by a comma (",")
101TEST(Pushlex, TestException2)
102{
104 {
105 const std::string csv = "PUSH_FOO...";
106 PushLex pl(const_buf_from_string(csv), true);
107 },
108 PushLex::pushlex_error,
109 "pushlex_error: not a valid PUSH_x message [2]");
110}
bool defined() const
Definition pushlex.hpp:61
std::string next()
Definition pushlex.hpp:71
ConstBuffer const_buf_from_string(const std::string &str)
Definition bufstr.hpp:95
#define JY_EXPECT_THROW
TEST(Pushlex, Test1)
static std::string get_next(PushLex &pl)