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);
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);
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);
35 if (strcmp(expected.typen, actual.typen) != 0) {
36 printf("Expected key_type: %s but was: %s\n",
37 expected.typen, actual.typen);
41 if (memcmp(expected.key_seq, actual.key_seq, expected.key_len) != 0) {
42 printf("Key mismatch!\n");
50 CompareKeysAlternative(
61 temp.key_len = key_len;
62 strlcpy(temp.typen, type, sizeof(temp.typen));
63 memcpy(temp.key_seq, key_seq, key_len);
65 return CompareKeys(temp, actual);
70 test_ReadEmptyKeyFile(void)
72 struct key * keys = NULL;
73 const char * path = CreatePath("key-test-empty", INPUT_DIR);
75 TEST_ASSERT_NOT_NULL(path);
76 TEST_ASSERT_EQUAL(0, auth_init(path, &keys));
77 TEST_ASSERT_NULL(keys);
84 test_ReadASCIIKeys(void)
86 struct key * keys = NULL;
87 struct key * result = NULL;
88 const char * path = CreatePath("key-test-ascii", INPUT_DIR);
90 TEST_ASSERT_NOT_NULL(path);
91 TEST_ASSERT_EQUAL(2, auth_init(path, &keys));
92 TEST_ASSERT_NOT_NULL(keys);
97 TEST_ASSERT_NOT_NULL(result);
98 TEST_ASSERT_TRUE(CompareKeysAlternative(40, 11, "MD5", "asciikeyTwo", *result));
101 get_key(50, &result);
102 TEST_ASSERT_NOT_NULL(result);
103 TEST_ASSERT_TRUE(CompareKeysAlternative(50, 11, "MD5", "asciikeyOne", *result));
108 test_ReadHexKeys(void)
110 struct key * keys = NULL;
111 struct key * result = NULL;
112 const char * path = CreatePath("key-test-hex", INPUT_DIR);
116 TEST_ASSERT_NOT_NULL(path);
117 TEST_ASSERT_EQUAL(3, auth_init(path, &keys));
118 TEST_ASSERT_NOT_NULL(keys);
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));
127 get_key(20, &result);
128 TEST_ASSERT_NOT_NULL(result);
130 memset(data1, 0x11, 15);
131 TEST_ASSERT_TRUE(CompareKeysAlternative(20, 15, "MD5", data1, *result));
134 get_key(30, &result);
135 TEST_ASSERT_NOT_NULL(result);
137 memset(data2, 0x01, 13);
138 TEST_ASSERT_TRUE(CompareKeysAlternative(30, 13, "MD5", data2, *result));
143 test_ReadKeyFileWithComments(void)
145 struct key * keys = NULL;
146 struct key * result = NULL;
147 const char * path = CreatePath("key-test-comments", INPUT_DIR);
150 TEST_ASSERT_NOT_NULL(path);
151 TEST_ASSERT_EQUAL(2, auth_init(path, &keys));
152 TEST_ASSERT_NOT_NULL(keys);
155 get_key(10, &result);
156 TEST_ASSERT_NOT_NULL(result);
158 memset(data, 0x01, 15);
159 TEST_ASSERT_TRUE(CompareKeysAlternative(10, 15, "MD5", data, *result));
162 get_key(34, &result);
163 TEST_ASSERT_NOT_NULL(result);
164 TEST_ASSERT_TRUE(CompareKeysAlternative(34, 3, "MD5", "xyz", *result));
169 test_ReadKeyFileWithInvalidHex(void)
171 struct key * keys = NULL;
172 struct key * result = NULL;
173 const char * path = CreatePath("key-test-invalid-hex", INPUT_DIR);
176 TEST_ASSERT_NOT_NULL(path);
177 TEST_ASSERT_EQUAL(1, auth_init(path, &keys));
178 TEST_ASSERT_NOT_NULL(keys);
181 get_key(10, &result);
182 TEST_ASSERT_NOT_NULL(result);
184 memset(data, 0x01, 15);
185 TEST_ASSERT_TRUE(CompareKeysAlternative(10, 15, "MD5", data, *result));
188 get_key(30, &result); /* Should not exist, and result should remain NULL. */
189 TEST_ASSERT_NULL(result);