xref: /netbsd-src/external/bsd/ntp/dist/tests/sec-2853/sec-2853.c (revision cdfa2a7ef92791ba9db70a584a1d904730e6fb46)
1 /*	$NetBSD: sec-2853.c,v 1.2 2020/05/25 20:47:37 christos Exp $	*/
2 
3 #include <config.h>
4 
5 #include <rc_cmdlength.h>
6 
7 #include "unity.h"
8 
9 void setUp(void);
10 void tearDown(void);
11 
12 void test_main( void );
13 int basic_good( void );
14 int embedded_nul( void );
15 int trailing_space( void );
16 
17 static int verbose = 1;        // if not 0, also print results if test passed
18 // static int exit_on_err = 0;    // if not 0, exit if test failed
19 
20 
setUp(void)21 void setUp(void)
22 {
23 }
24 
25 
tearDown(void)26 void tearDown(void)
27 {
28 }
29 
30 
31 /*
32  * Test function calling the remote config buffer checker
33  * http://bugs.ntp.org/show_bug.cgi?id=2853
34  *
35  * size_t remoteconfig_cmdlength(const char *src_buf, const char *src_end)
36  * - trims whitespace & garbage from the right
37  * then looks for only \tSP-\127 starting from the left.
38  * It returns the number of "good" characters it found.
39  */
40 
41 
test_main(void)42 void test_main( void )
43 {
44 	TEST_ASSERT_EQUAL(0, basic_good());
45 	TEST_ASSERT_EQUAL(0, embedded_nul());
46 	TEST_ASSERT_EQUAL(0, trailing_space());
47 }
48 
49 
basic_good(void)50 int basic_good( void )
51 {
52 	const char string[] = "good";
53 	const char *EOstring;
54 	size_t len;
55 	int failed;
56 
57 	EOstring = string + sizeof string;
58 
59 	len = remoteconfig_cmdlength(string, EOstring);
60 
61 	failed = ( 4 != len );
62 
63 	if ( failed || verbose )
64 		printf( "remoteconfig_cmdlength(\"%s\") returned %llu, expected %u: %s\n",
65 			string,
66 			(unsigned long long)len,
67 			4,
68 			failed ? "NO <<" : "yes" );
69 
70 	return failed ? -1 : 0;
71 }
72 
73 
embedded_nul(void)74 int embedded_nul( void )
75 {
76 	const char string[] = "nul\0 there";
77 	const char *EOstring;
78 	size_t len;
79 	int failed;
80 
81 	EOstring = string + sizeof string;
82 
83 	len = remoteconfig_cmdlength(string, EOstring);
84 
85 	failed = ( 3 != len );
86 
87 	if ( failed || verbose )
88 		printf( "remoteconfig_cmdlength(\"%s\") returned %llu, expected %u: %s\n",
89 			string,
90 			(unsigned long long)len,
91 			3,
92 			failed ? "NO <<" : "yes" );
93 
94 	return failed ? -1 : 0;
95 }
96 
97 
trailing_space(void)98 int trailing_space( void )
99 {
100 	const char string[] = "trailing space ";
101 	const char *EOstring;
102 	size_t len;
103 	int failed;
104 
105 	EOstring = string + sizeof string;
106 
107 	len = remoteconfig_cmdlength(string, EOstring);
108 
109 	failed = ( 14 != len );
110 
111 	if ( failed || verbose )
112 		printf( "remoteconfig_cmdlength(\"%s\") returned %llu, expected %u: %s\n",
113 			string,
114 			(unsigned long long)len,
115 			14,
116 			failed ? "NO <<" : "yes" );
117 
118 	return failed ? -1 : 0;
119 }
120