xref: /freebsd/contrib/bsnmp/tests/snmp_parse_server.cc (revision 0bf56da32d83fbd3b5db8d6c72cd1e7cc26fbc66)
1*0bf56da3SHartmut Brandt #include <iostream>
2*0bf56da3SHartmut Brandt #include <string>
3*0bf56da3SHartmut Brandt 
4*0bf56da3SHartmut Brandt extern "C" {
5*0bf56da3SHartmut Brandt #include "asn1.h"
6*0bf56da3SHartmut Brandt #include "snmp.h"
7*0bf56da3SHartmut Brandt #include "snmpclient.h"
8*0bf56da3SHartmut Brandt };
9*0bf56da3SHartmut Brandt 
10*0bf56da3SHartmut Brandt #include "catch.hpp"
11*0bf56da3SHartmut Brandt 
12*0bf56da3SHartmut Brandt using namespace std::string_literals;
13*0bf56da3SHartmut Brandt 
14*0bf56da3SHartmut Brandt static inline int
try_parse(struct snmp_client * sc,const char * str)15*0bf56da3SHartmut Brandt try_parse(struct snmp_client *sc, const char *str)
16*0bf56da3SHartmut Brandt {
17*0bf56da3SHartmut Brandt 	const int r = snmp_parse_server(sc, str);
18*0bf56da3SHartmut Brandt 	if (false && r != 0)
19*0bf56da3SHartmut Brandt 		std::cout << "snmp_parse_server: " << sc->error << "\n";
20*0bf56da3SHartmut Brandt 	return r;
21*0bf56da3SHartmut Brandt 
22*0bf56da3SHartmut Brandt }
23*0bf56da3SHartmut Brandt 
24*0bf56da3SHartmut Brandt TEST_CASE("snmp_parse_server: empty string", "[snmp_parse_server]") {
25*0bf56da3SHartmut Brandt 	struct snmp_client sc;
26*0bf56da3SHartmut Brandt 	snmp_client_init(&sc);
27*0bf56da3SHartmut Brandt 
28*0bf56da3SHartmut Brandt 	REQUIRE(try_parse(&sc, "") == 0);
29*0bf56da3SHartmut Brandt 	REQUIRE(sc.trans == SNMP_TRANS_UDP);
30*0bf56da3SHartmut Brandt 	REQUIRE(sc.chost == ""s);
31*0bf56da3SHartmut Brandt 	REQUIRE(sc.cport == "snmp"s);
32*0bf56da3SHartmut Brandt 	REQUIRE(sc.read_community == "public"s);
33*0bf56da3SHartmut Brandt 	REQUIRE(sc.write_community == "private"s);
34*0bf56da3SHartmut Brandt }
35*0bf56da3SHartmut Brandt 
36*0bf56da3SHartmut Brandt TEST_CASE("snmp_parse_server: hostname only", "[snmp_parse_server]") {
37*0bf56da3SHartmut Brandt 	struct snmp_client sc;
38*0bf56da3SHartmut Brandt 	snmp_client_init(&sc);
39*0bf56da3SHartmut Brandt 
40*0bf56da3SHartmut Brandt 	SECTION("simple name without special characters") {
41*0bf56da3SHartmut Brandt 		const auto str = "somehost"s;
42*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
43*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP);
44*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == str);
45*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == "snmp"s);
46*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
47*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
48*0bf56da3SHartmut Brandt 	}
49*0bf56da3SHartmut Brandt 	SECTION("complex host name without special characters") {
50*0bf56da3SHartmut Brandt 		const auto str = "some.host.domain"s;
51*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
52*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP);
53*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == str);
54*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == "snmp"s);
55*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
56*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
57*0bf56da3SHartmut Brandt 	}
58*0bf56da3SHartmut Brandt 	SECTION("complex host name with special characters") {
59*0bf56da3SHartmut Brandt 		const auto str = "some-mul.host-32.domain."s;
60*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
61*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP);
62*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == str);
63*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == "snmp"s);
64*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
65*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
66*0bf56da3SHartmut Brandt 	}
67*0bf56da3SHartmut Brandt 	SECTION("relative path name") {
68*0bf56da3SHartmut Brandt 		const auto str = "foo/bar"s;
69*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
70*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_LOC_DGRAM);
71*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == str);
72*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == ""s);
73*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
74*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
75*0bf56da3SHartmut Brandt 	}
76*0bf56da3SHartmut Brandt 	SECTION("absolute path name") {
77*0bf56da3SHartmut Brandt 		const auto str = "/foo/bar"s;
78*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
79*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_LOC_DGRAM);
80*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == str);
81*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == ""s);
82*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
83*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
84*0bf56da3SHartmut Brandt 	}
85*0bf56da3SHartmut Brandt }
86*0bf56da3SHartmut Brandt 
87*0bf56da3SHartmut Brandt TEST_CASE("snmp_parse_server: ipv6 address only", "[snmp_parse_server]") {
88*0bf56da3SHartmut Brandt 	struct snmp_client sc;
89*0bf56da3SHartmut Brandt 	snmp_client_init(&sc);
90*0bf56da3SHartmut Brandt 
91*0bf56da3SHartmut Brandt 	SECTION("in6_addr_any") {
92*0bf56da3SHartmut Brandt 		const auto host = "::"s;
93*0bf56da3SHartmut Brandt 		const auto str = "[" + host + "]";
94*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
95*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP6);
96*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
97*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == "snmp"s);
98*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
99*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
100*0bf56da3SHartmut Brandt 	}
101*0bf56da3SHartmut Brandt 	SECTION("localhost") {
102*0bf56da3SHartmut Brandt 		const auto host = "::1"s;
103*0bf56da3SHartmut Brandt 		const auto str = "[" + host + "]";
104*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
105*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP6);
106*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
107*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == "snmp"s);
108*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
109*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
110*0bf56da3SHartmut Brandt 	}
111*0bf56da3SHartmut Brandt 	SECTION("link local address") {
112*0bf56da3SHartmut Brandt 		const auto host = "fc00:0:12::1"s;
113*0bf56da3SHartmut Brandt 		const auto str = "[" + host + "]";
114*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
115*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP6);
116*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
117*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == "snmp"s);
118*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
119*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
120*0bf56da3SHartmut Brandt 	}
121*0bf56da3SHartmut Brandt 	SECTION("illegal address (bad character)") {
122*0bf56da3SHartmut Brandt 		const auto host = "fc00:0:1x::1"s;
123*0bf56da3SHartmut Brandt 		const auto str = "[" + host + "]";
124*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == -1);
125*0bf56da3SHartmut Brandt 		REQUIRE(sc.error == host + ": Name does not resolve");
126*0bf56da3SHartmut Brandt 	}
127*0bf56da3SHartmut Brandt 	SECTION("illegal address (two double colons)") {
128*0bf56da3SHartmut Brandt 		const auto host = "fc00::0:12::1"s;
129*0bf56da3SHartmut Brandt 		const auto str = "[" + host + "]";
130*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == -1);
131*0bf56da3SHartmut Brandt 		REQUIRE(sc.error == host + ": Name does not resolve");
132*0bf56da3SHartmut Brandt 	}
133*0bf56da3SHartmut Brandt 	SECTION("illegal address (two many colons)") {
134*0bf56da3SHartmut Brandt 		const auto host = "1:2:3:4:5:6:7:8:9"s;
135*0bf56da3SHartmut Brandt 		const auto str = "[" + host + "]";
136*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == -1);
137*0bf56da3SHartmut Brandt 		REQUIRE(sc.error == host + ": Name does not resolve");
138*0bf56da3SHartmut Brandt 	}
139*0bf56da3SHartmut Brandt 	SECTION("ipv6 address and junk") {
140*0bf56da3SHartmut Brandt 		const auto host = "::"s;
141*0bf56da3SHartmut Brandt 		const auto str = "[" + host + "]" + "xxx";
142*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == -1);
143*0bf56da3SHartmut Brandt 		REQUIRE(sc.error == "junk at end of server specification 'xxx'"s);
144*0bf56da3SHartmut Brandt 	}
145*0bf56da3SHartmut Brandt }
146*0bf56da3SHartmut Brandt 
147*0bf56da3SHartmut Brandt TEST_CASE("snmp_parse_server: hostname and port", "[snmp_parse_server]") {
148*0bf56da3SHartmut Brandt 	struct snmp_client sc;
149*0bf56da3SHartmut Brandt 	snmp_client_init(&sc);
150*0bf56da3SHartmut Brandt 
151*0bf56da3SHartmut Brandt 	SECTION("simple name and numeric port") {
152*0bf56da3SHartmut Brandt 		const auto host = "somehost"s;
153*0bf56da3SHartmut Brandt 		const auto port = "10007"s;
154*0bf56da3SHartmut Brandt 		const auto str = host + ":" + port;
155*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
156*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP);
157*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
158*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == port);
159*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
160*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
161*0bf56da3SHartmut Brandt 	}
162*0bf56da3SHartmut Brandt 	SECTION("simple name and string port") {
163*0bf56da3SHartmut Brandt 		const auto host = "somehost"s;
164*0bf56da3SHartmut Brandt 		const auto port = "telnet"s;
165*0bf56da3SHartmut Brandt 		const auto str = host + ":" + port;
166*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
167*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP);
168*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
169*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == port);
170*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
171*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
172*0bf56da3SHartmut Brandt 	}
173*0bf56da3SHartmut Brandt 	SECTION("name with embedded colon and numeric port") {
174*0bf56da3SHartmut Brandt 		const auto host = "somehost:foo"s;
175*0bf56da3SHartmut Brandt 		const auto port = "10007"s;
176*0bf56da3SHartmut Brandt 		const auto str = host + ":" + port;
177*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
178*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP);
179*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
180*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == port);
181*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
182*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
183*0bf56da3SHartmut Brandt 	}
184*0bf56da3SHartmut Brandt 	SECTION("FQDN with embedded colon and numeric port") {
185*0bf56da3SHartmut Brandt 		const auto host = "bla.blub:foo.baz."s;
186*0bf56da3SHartmut Brandt 		const auto port = "10007"s;
187*0bf56da3SHartmut Brandt 		const auto str = host + ":" + port;
188*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
189*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP);
190*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
191*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == port);
192*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
193*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
194*0bf56da3SHartmut Brandt 	}
195*0bf56da3SHartmut Brandt 	SECTION("simple name and empty port") {
196*0bf56da3SHartmut Brandt 		const auto host = "somehost"s;
197*0bf56da3SHartmut Brandt 		const auto port = ""s;
198*0bf56da3SHartmut Brandt 		const auto str = host + ":" + port;
199*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == -1);
200*0bf56da3SHartmut Brandt 		REQUIRE(sc.error == "empty port name"s);
201*0bf56da3SHartmut Brandt 	}
202*0bf56da3SHartmut Brandt }
203*0bf56da3SHartmut Brandt 
204*0bf56da3SHartmut Brandt TEST_CASE("snmp_parse_server: ipv6 and port", "[snmp_parse_server]") {
205*0bf56da3SHartmut Brandt 	struct snmp_client sc;
206*0bf56da3SHartmut Brandt 	snmp_client_init(&sc);
207*0bf56da3SHartmut Brandt 
208*0bf56da3SHartmut Brandt 	SECTION("ANY address and numeric port") {
209*0bf56da3SHartmut Brandt 		const auto host = "::"s;
210*0bf56da3SHartmut Brandt 		const auto port = "10007"s;
211*0bf56da3SHartmut Brandt 		const auto str = "[" + host + "]:" + port;
212*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
213*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP6);
214*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
215*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == port);
216*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
217*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
218*0bf56da3SHartmut Brandt 	}
219*0bf56da3SHartmut Brandt 	SECTION("localhost address and string port") {
220*0bf56da3SHartmut Brandt 		const auto host = "::1"s;
221*0bf56da3SHartmut Brandt 		const auto port = "snmp"s;
222*0bf56da3SHartmut Brandt 		const auto str = "[" + host + "]:" + port;
223*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
224*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP6);
225*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
226*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == port);
227*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
228*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
229*0bf56da3SHartmut Brandt 	}
230*0bf56da3SHartmut Brandt 	SECTION("some address name and empty port") {
231*0bf56da3SHartmut Brandt 		const auto host = "fc00:00:01::2:3"s;
232*0bf56da3SHartmut Brandt 		const auto port = ""s;
233*0bf56da3SHartmut Brandt 		const auto str = "[" + host + "]:" + port;
234*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == -1);
235*0bf56da3SHartmut Brandt 		REQUIRE(sc.error == "empty port name"s);
236*0bf56da3SHartmut Brandt 	}
237*0bf56da3SHartmut Brandt }
238*0bf56da3SHartmut Brandt 
239*0bf56da3SHartmut Brandt TEST_CASE("snmp_parse_server: IPv4 address only", "[snmp_parse_server]") {
240*0bf56da3SHartmut Brandt 	struct snmp_client sc;
241*0bf56da3SHartmut Brandt 	snmp_client_init(&sc);
242*0bf56da3SHartmut Brandt 
243*0bf56da3SHartmut Brandt 	SECTION("single octet address") {
244*0bf56da3SHartmut Brandt 		const auto host = "127"s;
245*0bf56da3SHartmut Brandt 		const auto str = host;
246*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
247*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP);
248*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
249*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == "snmp"s);
250*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
251*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
252*0bf56da3SHartmut Brandt 	}
253*0bf56da3SHartmut Brandt 	SECTION("two octet address") {
254*0bf56da3SHartmut Brandt 		const auto host = "127.1"s;
255*0bf56da3SHartmut Brandt 		const auto str = host;
256*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
257*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP);
258*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
259*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == "snmp"s);
260*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
261*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
262*0bf56da3SHartmut Brandt 	}
263*0bf56da3SHartmut Brandt 	SECTION("three octet address") {
264*0bf56da3SHartmut Brandt 		const auto host = "127.23.1"s;
265*0bf56da3SHartmut Brandt 		const auto str = host;
266*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
267*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP);
268*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
269*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == "snmp"s);
270*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
271*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
272*0bf56da3SHartmut Brandt 	}
273*0bf56da3SHartmut Brandt 	SECTION("four octet address") {
274*0bf56da3SHartmut Brandt 		const auto host = "127.18.23.1"s;
275*0bf56da3SHartmut Brandt 		const auto str = host;
276*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
277*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP);
278*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
279*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == "snmp"s);
280*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
281*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
282*0bf56da3SHartmut Brandt 	}
283*0bf56da3SHartmut Brandt 	SECTION("four octet octal address") {
284*0bf56da3SHartmut Brandt 		const auto host = "0300.077.0377.01"s;
285*0bf56da3SHartmut Brandt 		const auto str = host;
286*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
287*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP);
288*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
289*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == "snmp"s);
290*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
291*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
292*0bf56da3SHartmut Brandt 	}
293*0bf56da3SHartmut Brandt 	SECTION("four octet hex address") {
294*0bf56da3SHartmut Brandt 		const auto host = "x80.x12.xff.x1"s;
295*0bf56da3SHartmut Brandt 		const auto str = host;
296*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
297*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP);
298*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
299*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == "snmp"s);
300*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
301*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
302*0bf56da3SHartmut Brandt 	}
303*0bf56da3SHartmut Brandt }
304*0bf56da3SHartmut Brandt 
305*0bf56da3SHartmut Brandt TEST_CASE("snmp_parse_server: transport and hostname", "[snmp_parse_server]") {
306*0bf56da3SHartmut Brandt 	struct snmp_client sc;
307*0bf56da3SHartmut Brandt 	snmp_client_init(&sc);
308*0bf56da3SHartmut Brandt 
309*0bf56da3SHartmut Brandt 	SECTION("udp and host") {
310*0bf56da3SHartmut Brandt 		const auto trans = "udp"s;
311*0bf56da3SHartmut Brandt 		const auto host = "somehost"s;
312*0bf56da3SHartmut Brandt 		const auto str = trans + "::" + host;
313*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
314*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP);
315*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
316*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == "snmp"s);
317*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
318*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
319*0bf56da3SHartmut Brandt 	}
320*0bf56da3SHartmut Brandt 	SECTION("udp and ipv4 address") {
321*0bf56da3SHartmut Brandt 		const auto trans = "udp"s;
322*0bf56da3SHartmut Brandt 		const auto host = "240.0.1.2"s;
323*0bf56da3SHartmut Brandt 		const auto str = trans + "::" + host;
324*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
325*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP);
326*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
327*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == "snmp"s);
328*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
329*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
330*0bf56da3SHartmut Brandt 	}
331*0bf56da3SHartmut Brandt 	SECTION("udp6 and host") {
332*0bf56da3SHartmut Brandt 		const auto trans = "udp6"s;
333*0bf56da3SHartmut Brandt 		const auto host = "somehost"s;
334*0bf56da3SHartmut Brandt 		const auto str = trans + "::" + host;
335*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
336*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP6);
337*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
338*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == "snmp"s);
339*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
340*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
341*0bf56da3SHartmut Brandt 	}
342*0bf56da3SHartmut Brandt 	SECTION("udp6 and ipv6 address") {
343*0bf56da3SHartmut Brandt 		const auto trans = "udp6"s;
344*0bf56da3SHartmut Brandt 		const auto host = "fec0:0:2::17"s;
345*0bf56da3SHartmut Brandt 		const auto str = trans + "::[" + host + "]";
346*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
347*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP6);
348*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
349*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == "snmp"s);
350*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
351*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
352*0bf56da3SHartmut Brandt 	}
353*0bf56da3SHartmut Brandt 	SECTION("dgram and host") {
354*0bf56da3SHartmut Brandt 		const auto trans = "dgram"s;
355*0bf56da3SHartmut Brandt 		const auto host = "somehost"s;
356*0bf56da3SHartmut Brandt 		const auto str = trans + "::" + host;
357*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
358*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_LOC_DGRAM);
359*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
360*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == ""s);
361*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
362*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
363*0bf56da3SHartmut Brandt 	}
364*0bf56da3SHartmut Brandt 	SECTION("stream and host") {
365*0bf56da3SHartmut Brandt 		const auto trans = "stream"s;
366*0bf56da3SHartmut Brandt 		const auto host = "somehost"s;
367*0bf56da3SHartmut Brandt 		const auto str = trans + "::" + host;
368*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
369*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_LOC_STREAM);
370*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
371*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == ""s);
372*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
373*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
374*0bf56da3SHartmut Brandt 	}
375*0bf56da3SHartmut Brandt 	SECTION("unknown transport and host") {
376*0bf56da3SHartmut Brandt 		const auto trans = "foo"s;
377*0bf56da3SHartmut Brandt 		const auto host = "somehost"s;
378*0bf56da3SHartmut Brandt 		const auto str = trans + "::" + host;
379*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == -1);
380*0bf56da3SHartmut Brandt 		REQUIRE(sc.error == "unknown transport specifier '" + trans + "'");
381*0bf56da3SHartmut Brandt 	}
382*0bf56da3SHartmut Brandt 	SECTION("empty transport and host") {
383*0bf56da3SHartmut Brandt 		const auto trans = ""s;
384*0bf56da3SHartmut Brandt 		const auto host = "somehost"s;
385*0bf56da3SHartmut Brandt 		const auto str = trans + "::" + host;
386*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == -1);
387*0bf56da3SHartmut Brandt 		REQUIRE(sc.error == "empty transport specifier"s);
388*0bf56da3SHartmut Brandt 	}
389*0bf56da3SHartmut Brandt }
390*0bf56da3SHartmut Brandt 
391*0bf56da3SHartmut Brandt TEST_CASE("snmp_parse_server: transport, host and port", "[snmp_parse_server]") {
392*0bf56da3SHartmut Brandt 	struct snmp_client sc;
393*0bf56da3SHartmut Brandt 	snmp_client_init(&sc);
394*0bf56da3SHartmut Brandt 
395*0bf56da3SHartmut Brandt 	SECTION("udp, host and port") {
396*0bf56da3SHartmut Brandt 		const auto trans = "udp"s;
397*0bf56da3SHartmut Brandt 		const auto host = "somehost"s;
398*0bf56da3SHartmut Brandt 		const auto port = "ssh"s;
399*0bf56da3SHartmut Brandt 		const auto str = trans + "::" + host + ":" + port;
400*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
401*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP);
402*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
403*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == port);
404*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
405*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
406*0bf56da3SHartmut Brandt 	}
407*0bf56da3SHartmut Brandt 	SECTION("udp, host with colon and port") {
408*0bf56da3SHartmut Brandt 		const auto trans = "udp"s;
409*0bf56da3SHartmut Brandt 		const auto host = "somehost:foo"s;
410*0bf56da3SHartmut Brandt 		const auto port = "ssh"s;
411*0bf56da3SHartmut Brandt 		const auto str = trans + "::" + host + ":" + port;
412*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
413*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP);
414*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
415*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == port);
416*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
417*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
418*0bf56da3SHartmut Brandt 	}
419*0bf56da3SHartmut Brandt 	SECTION("udp and port") {
420*0bf56da3SHartmut Brandt 		const auto trans = "udp"s;
421*0bf56da3SHartmut Brandt 		const auto host = ""s;
422*0bf56da3SHartmut Brandt 		const auto port = "ssh"s;
423*0bf56da3SHartmut Brandt 		const auto str = trans + "::" + host + ":" + port;
424*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
425*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP);
426*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
427*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == port);
428*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
429*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
430*0bf56da3SHartmut Brandt 	}
431*0bf56da3SHartmut Brandt 	SECTION("udp6, ipv6 and port") {
432*0bf56da3SHartmut Brandt 		const auto trans = "udp6"s;
433*0bf56da3SHartmut Brandt 		const auto host = "::1:2"s;
434*0bf56da3SHartmut Brandt 		const auto port = "ssh"s;
435*0bf56da3SHartmut Brandt 		const auto str = trans + "::[" + host + "]:" + port;
436*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
437*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP6);
438*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
439*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == port);
440*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == "public"s);
441*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == "private"s);
442*0bf56da3SHartmut Brandt 	}
443*0bf56da3SHartmut Brandt }
444*0bf56da3SHartmut Brandt 
445*0bf56da3SHartmut Brandt TEST_CASE("snmp_parse_server: community and host", "[snmp_parse_server]")
446*0bf56da3SHartmut Brandt {
447*0bf56da3SHartmut Brandt 	struct snmp_client sc;
448*0bf56da3SHartmut Brandt 	snmp_client_init(&sc);
449*0bf56da3SHartmut Brandt 
450*0bf56da3SHartmut Brandt 	SECTION("community and host") {
451*0bf56da3SHartmut Brandt 		const auto comm = "public"s;
452*0bf56da3SHartmut Brandt 		const auto host = "server.com"s;
453*0bf56da3SHartmut Brandt 		const auto str = comm + "@" + host;
454*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
455*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP);
456*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
457*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == "snmp"s);
458*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == comm);
459*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == comm);
460*0bf56da3SHartmut Brandt 	}
461*0bf56da3SHartmut Brandt 	SECTION("community with @ and host") {
462*0bf56da3SHartmut Brandt 		const auto comm = "public@bla"s;
463*0bf56da3SHartmut Brandt 		const auto host = "server.com"s;
464*0bf56da3SHartmut Brandt 		const auto str = comm + "@" + host;
465*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
466*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP);
467*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
468*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == "snmp"s);
469*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == comm);
470*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == comm);
471*0bf56da3SHartmut Brandt 	}
472*0bf56da3SHartmut Brandt 	SECTION("empty community and host") {
473*0bf56da3SHartmut Brandt 		const auto comm = ""s;
474*0bf56da3SHartmut Brandt 		const auto host = "server.com"s;
475*0bf56da3SHartmut Brandt 		const auto str = comm + "@" + host;
476*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
477*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP);
478*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
479*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == "snmp"s);
480*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == comm);
481*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == comm);
482*0bf56da3SHartmut Brandt 	}
483*0bf56da3SHartmut Brandt }
484*0bf56da3SHartmut Brandt 
485*0bf56da3SHartmut Brandt TEST_CASE("snmp_parse_server: transport, community, host and port", "[snmp_parse_server]")
486*0bf56da3SHartmut Brandt {
487*0bf56da3SHartmut Brandt 	struct snmp_client sc;
488*0bf56da3SHartmut Brandt 	snmp_client_init(&sc);
489*0bf56da3SHartmut Brandt 
490*0bf56da3SHartmut Brandt 	SECTION("transport, community, host and numeric port") {
491*0bf56da3SHartmut Brandt 		const auto trans = "udp6"s;
492*0bf56da3SHartmut Brandt 		const auto comm = "public"s;
493*0bf56da3SHartmut Brandt 		const auto host = "server.com"s;
494*0bf56da3SHartmut Brandt 		const auto port = "65000"s;
495*0bf56da3SHartmut Brandt 		const auto str = trans + "::" + comm + "@" + host + ":" + port;
496*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
497*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP6);
498*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
499*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == port);
500*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == comm);
501*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == comm);
502*0bf56da3SHartmut Brandt 	}
503*0bf56da3SHartmut Brandt 	SECTION("transport, community, ipv4 and symbolic port") {
504*0bf56da3SHartmut Brandt 		const auto trans = "udp6"s;
505*0bf56da3SHartmut Brandt 		const auto comm = "public"s;
506*0bf56da3SHartmut Brandt 		const auto host = "127.1"s;
507*0bf56da3SHartmut Brandt 		const auto port = "ftp"s;
508*0bf56da3SHartmut Brandt 		const auto str = trans + "::" + comm + "@" + host + ":" + port;
509*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
510*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP6);
511*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
512*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == port);
513*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == comm);
514*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == comm);
515*0bf56da3SHartmut Brandt 	}
516*0bf56da3SHartmut Brandt 	SECTION("transport, community, ipv6 and symbolic port") {
517*0bf56da3SHartmut Brandt 		const auto trans = "udp"s;
518*0bf56da3SHartmut Brandt 		const auto comm = "public"s;
519*0bf56da3SHartmut Brandt 		const auto host = "fe80::1:2"s;
520*0bf56da3SHartmut Brandt 		const auto port = "ftp"s;
521*0bf56da3SHartmut Brandt 		const auto str = trans + "::" + comm + "@[" + host + "]:" + port;
522*0bf56da3SHartmut Brandt 		REQUIRE(try_parse(&sc, str.c_str()) == 0);
523*0bf56da3SHartmut Brandt 		REQUIRE(sc.trans == SNMP_TRANS_UDP6);
524*0bf56da3SHartmut Brandt 		REQUIRE(sc.chost == host);
525*0bf56da3SHartmut Brandt 		REQUIRE(sc.cport == port);
526*0bf56da3SHartmut Brandt 		REQUIRE(sc.read_community == comm);
527*0bf56da3SHartmut Brandt 		REQUIRE(sc.write_community == comm);
528*0bf56da3SHartmut Brandt 	}
529*0bf56da3SHartmut Brandt }
530