xref: /netbsd-src/external/bsd/ntp/dist/tests/libntp/refnumtoa.c (revision e6c7e151de239c49d2e38720a061ed9d1fa99309)
1 #include "config.h"
2 
3 #include "ntp_net.h"
4 #include "ntp_refclock.h"
5 
6 #include "unity.h"
7 
8 
9 /* Might need to be updated if a new refclock gets this id. */
10 static const int UNUSED_REFCLOCK_ID = 250;
11 
12 void setUp(void);
13 void test_LocalClock(void);
14 void test_UnknownId(void);
15 
16 
17 void
18 setUp(void)
19 {
20 	init_lib();
21 
22 	return;
23 }
24 
25 
26 void
27 test_LocalClock(void) {
28 #ifdef REFCLOCK		/* clockname() is useless otherwise */
29 	/* We test with a refclock address of type LOCALCLOCK.
30 	 * with id 8
31 	 */
32 	u_int32 addr = REFCLOCK_ADDR;
33 	addr |= REFCLK_LOCALCLOCK << 8;
34 	addr |= 0x8;
35 
36 	sockaddr_u address;
37 	address.sa4.sin_family = AF_INET;
38 	address.sa4.sin_addr.s_addr = htonl(addr);
39 
40 	char stringStart[100]= "";
41 
42 	strcat(stringStart, clockname(REFCLK_LOCALCLOCK));
43 	strcat(stringStart, "(8)");
44 
45 	char * expected = stringStart;
46 
47 	TEST_ASSERT_EQUAL_STRING(expected, refnumtoa(&address));
48 #else
49 	TEST_IGNORE_MESSAGE("REFCLOCK NOT DEFINED, SKIPPING TEST");
50 #endif	/* REFCLOCK */
51 }
52 
53 void
54 test_UnknownId(void) {
55 #ifdef REFCLOCK		/* refnumtoa() is useless otherwise */
56 	/* We test with a currently unused refclock ID */
57 	u_int32 addr = REFCLOCK_ADDR;
58 	addr |= UNUSED_REFCLOCK_ID << 8;
59 	addr |= 0x4;
60 
61 	sockaddr_u address;
62 	address.sa4.sin_family = AF_INET;
63 	address.sa4.sin_addr.s_addr = htonl(addr);
64 
65 	char stringStart[100]= "REFCLK(";
66 	char value[100] ;
67 	snprintf(value, sizeof(value), "%d", UNUSED_REFCLOCK_ID);
68 	strcat(stringStart,value);
69 	strcat(stringStart,",4)");
70 	char * expected = stringStart;
71 
72 	TEST_ASSERT_EQUAL_STRING(expected, refnumtoa(&address));
73 #else
74 	TEST_IGNORE_MESSAGE("REFCLOCK NOT DEFINED, SKIPPING TEST");
75 #endif	/* REFCLOCK */
76 }
77