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#if 0 143 int fd = ux_socket_connect("/socket"); 144 145 TEST_ASSERT_TRUE(isGE(fd, 0)); 146 147 uint32_t size = 256; 148 char *str = NULL; 149 int temp = recv_packet(fd, &str, &size); 150 151 send_packet(fd, str, strlen(str)); 152 free(str); 153 TEST_ASSERT_EQUAL(0,temp); //0 because nobody sent us anything (yet!) 154 155 (void)close(fd); 156#else 157 TEST_IGNORE_MESSAGE("test_recv_packet() needs work"); 158#endif 159 return; 160} 161 162void 163test_send_via_ntp_signd(void) 164{ 165 struct recvbuf *rbufp = (struct recvbuf *) malloc(sizeof(struct recvbuf)); 166 int xmode = 1; 167 keyid_t xkeyid = 12345; 168 int flags = 0; 169 struct pkt *xpkt = (struct pkt *) malloc(sizeof(struct pkt)); //defined in ntp.h 170 171 TEST_ASSERT_NOT_NULL(rbufp); 172 TEST_ASSERT_NOT_NULL(xpkt); 173 memset(xpkt, 0, sizeof(struct pkt)); 174 175 //send_via_ntp_signd(NULL,NULL,NULL,NULL,NULL); //doesn't work 176 /* 177 ** Send the xpkt to Samba, read the response back in rbufp 178 */ 179 send_via_ntp_signd(rbufp,xmode,xkeyid,flags,xpkt); 180 181 free(rbufp); 182 free(xpkt); 183 184 return; 185} 186