2 #include "fileHandlingTest.h"
4 #include "ntp_stdlib.h"
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);
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);
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);
31 if (strcmp(expected.type, actual.type) != 0){
32 printf("Expected key_type: %s", expected.type);
33 printf(" but was: %s\n", actual.type);
37 if (memcmp(expected.key_seq, actual.key_seq, expected.key_len) != 0){
38 printf("Key mismatch!\n");
46 CompareKeysAlternative(int 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);
58 return CompareKeys(temp, actual);
63 test_ReadEmptyKeyFile(void) {
64 struct key* keys = NULL;
65 const char *path = CreatePath("key-test-empty", INPUT_DIR);
67 TEST_ASSERT_NOT_NULL(path);
68 TEST_ASSERT_EQUAL(0, auth_init(path, &keys));
69 TEST_ASSERT_NULL(keys);
76 test_ReadASCIIKeys(void) {
77 struct key* keys = NULL;
78 const char *path = CreatePath("key-test-ascii", INPUT_DIR);
80 TEST_ASSERT_NOT_NULL(path);
81 TEST_ASSERT_EQUAL(2, auth_init(path, &keys));
82 TEST_ASSERT_NOT_NULL(keys);
86 struct key* result = NULL;
88 TEST_ASSERT_NOT_NULL(result);
89 TEST_ASSERT_TRUE(CompareKeysAlternative(40, 11, "MD5", "asciikeyTwo", *result));
93 TEST_ASSERT_NOT_NULL(result);
94 TEST_ASSERT_TRUE(CompareKeysAlternative(50, 11, "MD5", "asciikeyOne", *result));
99 test_ReadHexKeys(void) {
100 struct key* keys = NULL;
101 const char *path = CreatePath("key-test-hex", INPUT_DIR);
103 TEST_ASSERT_NOT_NULL(path);
104 TEST_ASSERT_EQUAL(3, auth_init(path, &keys));
105 TEST_ASSERT_NOT_NULL(keys);
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));
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));
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));
129 test_ReadKeyFileWithComments(void) {
130 struct key* keys = NULL;
131 const char *path = CreatePath("key-test-comments", INPUT_DIR);
133 TEST_ASSERT_NOT_NULL(path);
134 TEST_ASSERT_EQUAL(2, auth_init(path, &keys));
135 TEST_ASSERT_NOT_NULL(keys);
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));
145 get_key(34, &result);
146 TEST_ASSERT_NOT_NULL(result);
147 TEST_ASSERT_TRUE(CompareKeysAlternative(34, 3, "MD5", "xyz", *result));
152 test_ReadKeyFileWithInvalidHex(void) {
153 struct key* keys = NULL;
154 const char *path = CreatePath("key-test-invalid-hex", INPUT_DIR);
156 TEST_ASSERT_NOT_NULL(path);
157 TEST_ASSERT_EQUAL(1, auth_init(path, &keys));
158 TEST_ASSERT_NOT_NULL(keys);
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));
168 get_key(30, &result); // Should not exist, and result should remain NULL.
169 TEST_ASSERT_NULL(result);