1 /*	$NetBSD: t-ntp_signd.c,v 1.2 2020/05/25 20:47:36 christos Exp $	*/
2 
3 #include "config.h"
4 
5 #include "ntp.h"
6 #include "ntp_calendar.h"
7 #include "ntp_stdlib.h"
8 
9 #include "unity.h"
10 
11 #include "test-libntp.h"
12 
13 
14 #define HAVE_NTP_SIGND
15 
16 #include "ntp_signd.c"
17 
18 extern int ux_socket_connect(const char *name);
19 
20 
21 //MOCKED FUNCTIONS
22 
23 //this connect function overrides/mocks connect() from  <sys/socket.h>
24 int
connect(int socket,const struct sockaddr * address,socklen_t address_len)25 connect(int socket, const struct sockaddr *address, socklen_t address_len)
26 {
27 	return 1;
28 }
29 
30 /*
31 ** Mocked read() and write() calls.
32 **
33 ** These will only operate 4 bytes at a time.
34 **
35 ** This is so write_all can be properly tested.
36 */
37 
38 static char rw_buf[4];
39 
40 ssize_t
write(int fd,void const * buf,size_t len)41 write(int fd, void const * buf, size_t len)
42 {
43 	REQUIRE(0 <= len);
44 	if (len >= 4) len = 4;	/* 4 bytes, max */
45 	(void)memcpy(rw_buf, buf, len);
46 
47 	return len;
48 }
49 
50 ssize_t
read(int fd,void * buf,size_t len)51 read(int fd, void * buf, size_t len)
52 {
53 	REQUIRE(0 <= len);
54 	if (len >= 4) len = 4;
55 	(void)memcpy(buf, rw_buf, len);
56 	return len;
57 }
58 
59 
60 //END OF MOCKED FUNCTIONS
61 
62 static int
isGE(int a,int b)63 isGE(int a,int b)
64 {
65 	if (a >= b) {return 1;}
66 	else {return 0;}
67 }
68 
69 extern void test_connect_incorrect_socket(void);
70 extern void test_connect_correct_socket(void);
71 extern void test_write_all(void);
72 extern void test_send_packet(void);
73 extern void test_recv_packet(void);
74 extern void test_send_via_ntp_signd(void);
75 
76 
77 void
test_connect_incorrect_socket(void)78 test_connect_incorrect_socket(void)
79 {
80 	TEST_ASSERT_EQUAL(-1, ux_socket_connect(NULL));
81 
82 	return;
83 }
84 
85 void
test_connect_correct_socket(void)86 test_connect_correct_socket(void)
87 {
88 	int temp = ux_socket_connect("/socket");
89 
90 	//risky, what if something is listening on :123, or localhost isnt 127.0.0.1?
91 	//TEST_ASSERT_EQUAL(-1, ux_socket_connect("127.0.0.1:123"));
92 
93 	//printf("%d\n",temp);
94 	TEST_ASSERT_TRUE(isGE(temp,0));
95 
96 	//write_all();
97 	//char *socketName = "Random_Socket_Name";
98 	//int length = strlen(socketName);
99 
100 	return;
101 }
102 
103 
104 void
test_write_all(void)105 test_write_all(void)
106 {
107 	int fd = ux_socket_connect("/socket");
108 
109 	TEST_ASSERT_TRUE(isGE(fd, 0));
110 
111 	char * str = "TEST123";
112 	int temp = write_all(fd, str,strlen(str));
113 	TEST_ASSERT_EQUAL(strlen(str), temp);
114 
115 	(void)close(fd);
116 	return;
117 }
118 
119 
120 void
test_send_packet(void)121 test_send_packet(void)
122 {
123 	int fd = ux_socket_connect("/socket");
124 
125 	TEST_ASSERT_TRUE(isGE(fd, 0));
126 
127 	char * str2 = "PACKET12345";
128 	int temp = send_packet(fd, str2, strlen(str2));
129 
130 	TEST_ASSERT_EQUAL(0,temp);
131 
132 	(void)close(fd);
133 	return;
134 }
135 
136 
137 /*
138 ** HMS: What's going on here?
139 ** Looks like this needs more work.
140 */
141 void
test_recv_packet(void)142 test_recv_packet(void)
143 {
144 #if 0
145 	int fd = ux_socket_connect("/socket");
146 
147 	TEST_ASSERT_TRUE(isGE(fd, 0));
148 
149 	uint32_t size = 256;
150 	char *str = NULL;
151 	int temp = recv_packet(fd, &str, &size);
152 
153 	send_packet(fd, str, strlen(str));
154 	free(str);
155 	TEST_ASSERT_EQUAL(0,temp); //0 because nobody sent us anything (yet!)
156 
157 	(void)close(fd);
158 #else
159 	TEST_IGNORE_MESSAGE("test_recv_packet() needs work");
160 #endif
161 	return;
162 }
163 
164 void
test_send_via_ntp_signd(void)165 test_send_via_ntp_signd(void)
166 {
167 	struct recvbuf *rbufp = (struct recvbuf *) malloc(sizeof(struct recvbuf));
168 	int	xmode = 1;
169 	keyid_t	xkeyid = 12345;
170 	int	flags = 0;
171 	struct pkt  *xpkt = (struct pkt *) malloc(sizeof(struct pkt)); //defined in ntp.h
172 
173 	TEST_ASSERT_NOT_NULL(rbufp);
174 	TEST_ASSERT_NOT_NULL(xpkt);
175 	memset(xpkt, 0, sizeof(struct pkt));
176 
177 	//send_via_ntp_signd(NULL,NULL,NULL,NULL,NULL);	//doesn't work
178 	/*
179 	** Send the xpkt to Samba, read the response back in rbufp
180 	*/
181 	send_via_ntp_signd(rbufp,xmode,xkeyid,flags,xpkt);
182 
183 	free(rbufp);
184 	free(xpkt);
185 
186 	return;
187 }
188