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