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