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 else
29 return std::string();
30}
31
32// parse a PUSH_UPDATE message
33TEST(pushlex, test_1)
34{
35 const std::string csv = "PUSH_UPDATE,route 10.9.0.0 255.255.0.0,route 8.8.8.8,route-ipv6 fd69::/64";
36 PushLex pl(const_buf_from_string(csv), true);
37 ASSERT_EQ(get_next(pl), "route 10.9.0.0 255.255.0.0");
38 ASSERT_EQ(get_next(pl), "route 8.8.8.8");
39 ASSERT_EQ(get_next(pl), "route-ipv6 fd69::/64");
40 ASSERT_FALSE(pl.defined());
41}
42
43// parse a PUSH_UPDATE message with tortured StandardLex quoting
44TEST(pushlex, test_2)
45{
46 const std::string csv = "PUSH_UPDATE,echo \"one,two,three\",,route 1.2.3.4,echo \\\",echo \"foo\",echo \\,,echo fin,";
47 PushLex pl(const_buf_from_string(csv), true);
48 ASSERT_EQ(get_next(pl), "echo \"one,two,three\"");
49 ASSERT_EQ(get_next(pl), "");
50 ASSERT_EQ(get_next(pl), "route 1.2.3.4");
51 ASSERT_EQ(get_next(pl), "echo \\\"");
52 ASSERT_EQ(get_next(pl), "echo \"foo\"");
53 ASSERT_EQ(get_next(pl), "echo \\,");
54 ASSERT_EQ(get_next(pl), "echo fin");
55 ASSERT_EQ(get_next(pl), "");
56 ASSERT_FALSE(pl.defined());
57}
58
59// test PushLex with discard_prefix == false
60TEST(pushlex, test_3)
61{
62 const std::string csv = "PUSH_UPDATE,route 10.9.0.0 255.255.0.0,route 8.8.8.8,route-ipv6 fd69::/64";
63 PushLex pl(const_buf_from_string(csv), false);
64 ASSERT_EQ(get_next(pl), "PUSH_UPDATE"); // this is here because discard_prefix == false
65 ASSERT_EQ(get_next(pl), "route 10.9.0.0 255.255.0.0");
66 ASSERT_EQ(get_next(pl), "route 8.8.8.8");
67 ASSERT_EQ(get_next(pl), "route-ipv6 fd69::/64");
68 ASSERT_FALSE(pl.defined());
69}
70
71// test PushLex with a null message
72TEST(pushlex, test_4)
73{
74 const std::string csv = "PUSH_UPDATE,";
75 PushLex pl(const_buf_from_string(csv), true);
76 ASSERT_FALSE(pl.defined());
77}
78
79// test PushLex with a null buffer
80TEST(pushlex, test_5)
81{
82 ConstBuffer cbuf;
83 PushLex pl(cbuf, true);
84 ASSERT_FALSE(pl.defined());
85}
86
87// test that PushLex throws an exception when prefix
88// is unrecognized
89TEST(pushlex, test_exception_1)
90{
92 {
93 const std::string csv = "FOO,route 10.9.0.0 255.255.0.0,route 8.8.8.8,route-ipv6 fd69::/64";
94 PushLex pl(const_buf_from_string(csv), true);
95 },
96 PushLex::pushlex_error,
97 "pushlex_error: not a valid PUSH_x message [1]");
98}
99
100// test that PushLex throws an exception when prefix
101// is not followed by a comma (",")
102TEST(pushlex, test_exception_2)
103{
105 {
106 const std::string csv = "PUSH_FOO...";
107 PushLex pl(const_buf_from_string(csv), true);
108 },
109 PushLex::pushlex_error,
110 "pushlex_error: not a valid PUSH_x message [2]");
111}
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, test_1)
static std::string get_next(PushLex &pl)