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