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