1 /* $NetBSD: keyFile.c,v 1.1.1.5 2016/01/08 21:21:33 christos Exp $ */ 2 3 #include "config.h" 4 #include "fileHandlingTest.h" 5 6 #include "ntp_stdlib.h" 7 #include "ntp_types.h" 8 #include "crypto.h" 9 10 #include "unity.h" 11 12 bool CompareKeys(struct key expected, struct key actual); 13 bool CompareKeysAlternative(int key_id,int key_len,const char* type,const char* key_seq,struct key actual); 14 void test_ReadEmptyKeyFile(void); 15 void test_ReadASCIIKeys(void); 16 void test_ReadHexKeys(void); 17 void test_ReadKeyFileWithComments(void); 18 void test_ReadKeyFileWithInvalidHex(void); 19 20 21 bool 22 CompareKeys(struct key expected, struct key actual) { 23 if (expected.key_id != actual.key_id){ 24 printf("Expected key_id: %d", expected.key_id); 25 printf(" but was: %d\n", actual.key_id); 26 return FALSE; 27 } 28 if (expected.key_len != actual.key_len){ 29 printf("Expected key_len: %d", expected.key_len); 30 printf(" but was: %d\n", actual.key_len); 31 return FALSE; 32 } 33 if (strcmp(expected.type, actual.type) != 0){ 34 printf("Expected key_type: %s", expected.type); 35 printf(" but was: %s\n", actual.type); 36 return FALSE; 37 38 } 39 if (memcmp(expected.key_seq, actual.key_seq, expected.key_len) != 0){ 40 printf("Key mismatch!\n"); 41 return FALSE; 42 } 43 return TRUE; 44 } 45 46 47 bool 48 CompareKeysAlternative(int key_id, 49 int key_len, 50 const char* type, 51 const char* key_seq, 52 struct key actual) { 53 struct key temp; 54 55 temp.key_id = key_id; 56 temp.key_len = key_len; 57 strlcpy(temp.type, type, sizeof(temp.type)); 58 memcpy(temp.key_seq, key_seq, key_len); 59 60 return CompareKeys(temp, actual); 61 } 62 63 64 void 65 test_ReadEmptyKeyFile(void) { 66 struct key* keys = NULL; 67 const char *path = CreatePath("key-test-empty", INPUT_DIR); 68 69 TEST_ASSERT_NOT_NULL(path); 70 TEST_ASSERT_EQUAL(0, auth_init(path, &keys)); 71 TEST_ASSERT_NULL(keys); 72 73 free((void *)path); 74 } 75 76 77 void 78 test_ReadASCIIKeys(void) { 79 struct key* keys = NULL; 80 const char *path = CreatePath("key-test-ascii", INPUT_DIR); 81 82 TEST_ASSERT_NOT_NULL(path); 83 TEST_ASSERT_EQUAL(2, auth_init(path, &keys)); 84 TEST_ASSERT_NOT_NULL(keys); 85 86 free((void *)path); 87 88 struct key* result = NULL; 89 get_key(40, &result); 90 TEST_ASSERT_NOT_NULL(result); 91 TEST_ASSERT_TRUE(CompareKeysAlternative(40, 11, "MD5", "asciikeyTwo", *result)); 92 93 result = NULL; 94 get_key(50, &result); 95 TEST_ASSERT_NOT_NULL(result); 96 TEST_ASSERT_TRUE(CompareKeysAlternative(50, 11, "MD5", "asciikeyOne", *result)); 97 } 98 99 100 void 101 test_ReadHexKeys(void) { 102 struct key* keys = NULL; 103 const char *path = CreatePath("key-test-hex", INPUT_DIR); 104 105 TEST_ASSERT_NOT_NULL(path); 106 TEST_ASSERT_EQUAL(3, auth_init(path, &keys)); 107 TEST_ASSERT_NOT_NULL(keys); 108 free((void *)path); 109 110 struct key* result = NULL; 111 get_key(10, &result); 112 TEST_ASSERT_NOT_NULL(result); 113 TEST_ASSERT_TRUE(CompareKeysAlternative(10, 13, "MD5", 114 "\x01\x23\x45\x67\x89\xab\xcd\xef\x01\x23\x45\x67\x89", *result)); 115 116 result = NULL; 117 get_key(20, &result); 118 TEST_ASSERT_NOT_NULL(result); 119 char data1[15]; memset(data1, 0x11, 15); 120 TEST_ASSERT_TRUE(CompareKeysAlternative(20, 15, "MD5", data1, *result)); 121 122 result = NULL; 123 get_key(30, &result); 124 TEST_ASSERT_NOT_NULL(result); 125 char data2[13]; memset(data2, 0x01, 13); 126 TEST_ASSERT_TRUE(CompareKeysAlternative(30, 13, "MD5", data2, *result)); 127 } 128 129 130 void 131 test_ReadKeyFileWithComments(void) { 132 struct key* keys = NULL; 133 const char *path = CreatePath("key-test-comments", INPUT_DIR); 134 135 TEST_ASSERT_NOT_NULL(path); 136 TEST_ASSERT_EQUAL(2, auth_init(path, &keys)); 137 TEST_ASSERT_NOT_NULL(keys); 138 free((void *)path); 139 140 struct key* result = NULL; 141 get_key(10, &result); 142 TEST_ASSERT_NOT_NULL(result); 143 char data[15]; memset(data, 0x01, 15); 144 TEST_ASSERT_TRUE(CompareKeysAlternative(10, 15, "MD5", data, *result)); 145 146 result = NULL; 147 get_key(34, &result); 148 TEST_ASSERT_NOT_NULL(result); 149 TEST_ASSERT_TRUE(CompareKeysAlternative(34, 3, "MD5", "xyz", *result)); 150 } 151 152 153 void 154 test_ReadKeyFileWithInvalidHex(void) { 155 struct key* keys = NULL; 156 const char *path = CreatePath("key-test-invalid-hex", INPUT_DIR); 157 158 TEST_ASSERT_NOT_NULL(path); 159 TEST_ASSERT_EQUAL(1, auth_init(path, &keys)); 160 TEST_ASSERT_NOT_NULL(keys); 161 free((void *)path); 162 163 struct key* result = NULL; 164 get_key(10, &result); 165 TEST_ASSERT_NOT_NULL(result); 166 char data[15]; memset(data, 0x01, 15); 167 TEST_ASSERT_TRUE(CompareKeysAlternative(10, 15, "MD5", data, *result)); 168 169 result = NULL; 170 get_key(30, &result); // Should not exist, and result should remain NULL. 171 TEST_ASSERT_NULL(result); 172 } 173