1290001Sglebius#include "config.h" 2290001Sglebius#include "unity.h" 3290001Sglebius 4290001Sglebius#ifdef OPENSSL 5290001Sglebius# include "openssl/err.h" 6290001Sglebius# include "openssl/rand.h" 7290001Sglebius# include "openssl/evp.h" 8290001Sglebius#endif 9290001Sglebius#include "ntp.h" 10290001Sglebius#include "ntp_stdlib.h" 11290001Sglebius 12290001Sglebiusu_long current_time = 4; 13290001Sglebius 14290001Sglebius 15290001Sglebius/* 16290001Sglebius * Example packet with MD5 hash calculated manually. 17290001Sglebius */ 18290001Sglebiusconst int keytype = KEY_TYPE_MD5; 19293896Sglebiusconst u_char *key = (const u_char*)"abcdefgh"; 20290001Sglebiusconst u_short keyLength = 8; 21293896Sglebiusconst u_char *packet = (const u_char*)"ijklmnopqrstuvwx"; 22290001Sglebius#define packetLength 16 23290001Sglebius#define keyIdLength 4 24290001Sglebius#define digestLength 16 25293896Sglebius#define totalLength (packetLength + keyIdLength + digestLength) 26293896Sglebiusunion { 27293896Sglebius u_char u8 [totalLength]; 28293896Sglebius uint32_t u32[1]; 29293896Sglebius} expectedPacket = { 30293896Sglebius "ijklmnopqrstuvwx\0\0\0\0\x0c\x0e\x84\xcf\x0b\xb7\xa8\x68\x8e\x52\x38\xdb\xbc\x1c\x39\x53" 31293896Sglebius}; 32293896Sglebiusunion { 33293896Sglebius u_char u8 [totalLength]; 34293896Sglebius uint32_t u32[1]; 35293896Sglebius} invalidPacket = { 36293896Sglebius "ijklmnopqrstuvwx\0\0\0\0\x0c\x0e\x84\xcf\x0b\xb7\xa8\x68\x8e\x52\x38\xdb\xbc\x1c\x39\x54" 37293896Sglebius}; 38290001Sglebius 39290001Sglebius 40290001Sglebiusvoid test_Encrypt(void); 41290001Sglebiusvoid test_DecryptValid(void); 42290001Sglebiusvoid test_DecryptInvalid(void); 43290001Sglebiusvoid test_IPv4AddressToRefId(void); 44290001Sglebiusvoid test_IPv6AddressToRefId(void); 45290001Sglebius 46290001Sglebius 47290001Sglebiusvoid 48290001Sglebiustest_Encrypt(void) { 49293896Sglebius u_int32 *packetPtr; 50290001Sglebius int length; 51290001Sglebius 52310419Sdelphij packetPtr = emalloc_zero(totalLength * sizeof(*packetPtr)); 53290001Sglebius memcpy(packetPtr, packet, packetLength); 54290001Sglebius 55290001Sglebius cache_secretsize = keyLength; 56290001Sglebius 57293896Sglebius length = MD5authencrypt(keytype, key, packetPtr, packetLength); 58290001Sglebius 59293896Sglebius TEST_ASSERT_TRUE(MD5authdecrypt(keytype, key, packetPtr, packetLength, length)); 60290001Sglebius 61290001Sglebius TEST_ASSERT_EQUAL(20, length); 62293896Sglebius TEST_ASSERT_EQUAL_MEMORY(expectedPacket.u8, packetPtr, totalLength); 63290001Sglebius 64290001Sglebius free(packetPtr); 65290001Sglebius} 66290001Sglebius 67290001Sglebiusvoid 68290001Sglebiustest_DecryptValid(void) { 69290001Sglebius cache_secretsize = keyLength; 70293896Sglebius TEST_ASSERT_TRUE(MD5authdecrypt(keytype, key, expectedPacket.u32, packetLength, 20)); 71290001Sglebius} 72290001Sglebius 73290001Sglebiusvoid 74290001Sglebiustest_DecryptInvalid(void) { 75290001Sglebius cache_secretsize = keyLength; 76293896Sglebius TEST_ASSERT_FALSE(MD5authdecrypt(keytype, key, invalidPacket.u32, packetLength, 20)); 77290001Sglebius} 78290001Sglebius 79290001Sglebiusvoid 80290001Sglebiustest_IPv4AddressToRefId(void) { 81290001Sglebius sockaddr_u addr; 82290001Sglebius addr.sa4.sin_family = AF_INET; 83290001Sglebius u_int32 address; 84290001Sglebius 85290001Sglebius addr.sa4.sin_port = htons(80); 86290001Sglebius 87290001Sglebius address = inet_addr("192.0.2.1"); 88290001Sglebius addr.sa4.sin_addr.s_addr = address; 89290001Sglebius 90290001Sglebius TEST_ASSERT_EQUAL(address, addr2refid(&addr)); 91290001Sglebius} 92290001Sglebius 93290001Sglebiusvoid 94290001Sglebiustest_IPv6AddressToRefId(void) { 95293896Sglebius const int expected = 0x75cffd52; 96293896Sglebius const struct in6_addr address = { { { 97290001Sglebius 0x20, 0x01, 0x0d, 0xb8, 98290001Sglebius 0x85, 0xa3, 0x08, 0xd3, 99290001Sglebius 0x13, 0x19, 0x8a, 0x2e, 100290001Sglebius 0x03, 0x70, 0x73, 0x34 101293896Sglebius } } }; 102290001Sglebius sockaddr_u addr; 103290001Sglebius 104290001Sglebius addr.sa6.sin6_family = AF_INET6; 105290001Sglebius 106290001Sglebius addr.sa6.sin6_addr = address; 107290001Sglebius 108290001Sglebius 109290001Sglebius#if 0 110290001Sglebius TEST_ASSERT_EQUAL(expected, addr2refid(&addr)); 111290001Sglebius#else 112293896Sglebius (void)expected; 113290001Sglebius TEST_IGNORE_MESSAGE("Skipping because of big endian problem?"); 114290001Sglebius#endif 115290001Sglebius} 116