t-ntp_signd.c revision 290001
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
13#define HAVE_NTP_SIGND
14
15#include "ntp_signd.c"
16
17extern int ux_socket_connect(const char *name);
18
19
20//MOCKED FUNCTIONS
21
22//this connect function overrides/mocks connect() from  <sys/socket.h>
23int connect(int socket, const struct sockaddr *address,
24socklen_t address_len){
25	return 1;
26}
27
28//mocked write will only send 4 bytes at a time. This is so write_all can be properly tested
29ssize_t write(int fd, void const * buf, size_t len){
30	if(len >= 4){return 4;}
31	else return len;
32}
33
34ssize_t read(int fd, void * buf, size_t len){
35	if(len >= 4){return 4;}
36	else return len;
37}
38
39
40//END OF MOCKED FUNCTIONS
41
42int isGE(int a,int b){
43	if(a >= b) {return 1;}
44	else {return 0;}
45}
46
47
48void
49test_connect_incorrect_socket(void){
50	TEST_ASSERT_EQUAL(-1, ux_socket_connect(NULL));
51}
52
53void
54test_connect_correct_socket(void){
55
56
57
58	int temp = ux_socket_connect("/socket");
59
60	//risky, what if something is listening on :123, or localhost isnt 127.0.0.1?
61	//TEST_ASSERT_EQUAL(-1, ux_socket_connect("127.0.0.1:123"));
62
63	//printf("%d\n",temp);
64	TEST_ASSERT_TRUE(isGE(temp,0));
65
66	//write_all();
67	//char *socketName = "Random_Socket_Name";
68	//int length = strlen(socketName);
69
70}
71
72
73void
74test_write_all(void){
75	int fd = ux_socket_connect("/socket");
76	TEST_ASSERT_TRUE(isGE(fd,0));
77	char * str = "TEST123";
78	int temp = write_all(fd, str,strlen(str));
79	TEST_ASSERT_EQUAL(strlen(str),temp);
80}
81
82
83void
84test_send_packet(void){
85	int fd = ux_socket_connect("/socket");
86	char * str2 = "PACKET12345";
87	int temp = send_packet(fd, str2, strlen(str2));
88	TEST_ASSERT_EQUAL(0,temp);
89}
90
91
92void
93test_recv_packet(void){
94	int fd = ux_socket_connect("/socket");
95	int size = 256;
96	char str[size];
97
98	int temp = recv_packet(fd, &str, &size);
99	send_packet(fd, str, strlen(str));
100	TEST_ASSERT_EQUAL(0,temp); //0 because nobody sent us anything (yet!)
101}
102
103void
104test_send_via_ntp_signd(){
105
106	struct recvbuf *rbufp = (struct recvbuf *) malloc(sizeof(struct recvbuf));
107	int	xmode = 1;
108	keyid_t	xkeyid = 12345;
109	int flags =0;
110	struct pkt  *xpkt = (struct pkt *) malloc(sizeof(struct pkt)); //defined in ntp.h
111
112	//send_via_ntp_signd(NULL,NULL,NULL,NULL,NULL);	//doesn't work
113	send_via_ntp_signd(rbufp,xmode,xkeyid,flags,xpkt);
114
115
116}
117