]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/ntpd/t-ntp_signd.c
Upgrade NTP to 4.2.8p4.
[FreeBSD/releng/10.2.git] / contrib / ntp / tests / ntpd / t-ntp_signd.c
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
17 extern int ux_socket_connect(const char *name);
18
19
20 //MOCKED FUNCTIONS
21
22 //this connect function overrides/mocks connect() from  <sys/socket.h>
23 int connect(int socket, const struct sockaddr *address,
24 socklen_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
29 ssize_t write(int fd, void const * buf, size_t len){
30         if(len >= 4){return 4;}
31         else return len;
32 }
33
34 ssize_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
42 int isGE(int a,int b){ 
43         if(a >= b) {return 1;}
44         else {return 0;}
45 }
46
47
48 void 
49 test_connect_incorrect_socket(void){
50         TEST_ASSERT_EQUAL(-1, ux_socket_connect(NULL));
51 }
52
53 void 
54 test_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
73 void
74 test_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
83 void
84 test_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
92 void
93 test_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
103 void 
104 test_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 }