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