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