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