xref: /netbsd-src/external/bsd/ntp/dist/sntp/tests/keyFile.c (revision 63aea4bd5b445e491ff0389fe27ec78b3099dba3)
1 /*	$NetBSD: keyFile.c,v 1.1.1.3 2015/10/23 17:47:43 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 
68 	TEST_ASSERT_EQUAL(0, auth_init(CreatePath("key-test-empty", INPUT_DIR), &keys));
69 	TEST_ASSERT_NULL(keys);
70 }
71 
72 
73 void
74 test_ReadASCIIKeys(void) {
75 	struct key* keys = NULL;
76 
77 	TEST_ASSERT_EQUAL(2, auth_init(CreatePath("key-test-ascii", INPUT_DIR), &keys));
78 
79 	TEST_ASSERT_NOT_NULL(keys);
80 
81 	struct key* result = NULL;
82 	get_key(40, &result);
83 	TEST_ASSERT_NOT_NULL(result);
84 	TEST_ASSERT_TRUE(CompareKeysAlternative(40, 11, "MD5", "asciikeyTwo", *result));
85 
86 	result = NULL;
87 	get_key(50, &result);
88 	TEST_ASSERT_NOT_NULL(result);
89 	TEST_ASSERT_TRUE(CompareKeysAlternative(50, 11, "MD5", "asciikeyOne", *result));
90 }
91 
92 
93 void
94 test_ReadHexKeys(void) {
95 	struct key* keys = NULL;
96 
97 	TEST_ASSERT_EQUAL(3, auth_init(CreatePath("key-test-hex", INPUT_DIR), &keys));
98 
99 	TEST_ASSERT_NOT_NULL(keys);
100 
101 	struct key* result = NULL;
102 	get_key(10, &result);
103 	TEST_ASSERT_NOT_NULL(result);
104 	TEST_ASSERT_TRUE(CompareKeysAlternative(10, 13, "MD5",
105 		 "\x01\x23\x45\x67\x89\xab\xcd\xef\x01\x23\x45\x67\x89", *result));
106 
107 	result = NULL;
108 	get_key(20, &result);
109 	TEST_ASSERT_NOT_NULL(result);
110 	char data1[15]; memset(data1, 0x11, 15);
111 	TEST_ASSERT_TRUE(CompareKeysAlternative(20, 15, "MD5", data1, *result));
112 
113 	result = NULL;
114 	get_key(30, &result);
115 	TEST_ASSERT_NOT_NULL(result);
116 	char data2[13]; memset(data2, 0x01, 13);
117 	TEST_ASSERT_TRUE(CompareKeysAlternative(30, 13, "MD5", data2, *result));
118 }
119 
120 
121 void
122 test_ReadKeyFileWithComments(void) {
123 	struct key* keys = NULL;
124 
125 	TEST_ASSERT_EQUAL(2, auth_init(CreatePath("key-test-comments", INPUT_DIR), &keys));
126 
127 	TEST_ASSERT_NOT_NULL(keys);
128 
129 	struct key* result = NULL;
130 	get_key(10, &result);
131 	TEST_ASSERT_NOT_NULL(result);
132 	char data[15]; memset(data, 0x01, 15);
133 	TEST_ASSERT_TRUE(CompareKeysAlternative(10, 15, "MD5", data, *result));
134 
135 	result = NULL;
136 	get_key(34, &result);
137 	TEST_ASSERT_NOT_NULL(result);
138 	TEST_ASSERT_TRUE(CompareKeysAlternative(34, 3, "MD5", "xyz", *result));
139 }
140 
141 
142 void
143 test_ReadKeyFileWithInvalidHex(void) {
144 	struct key* keys = NULL;
145 
146 	TEST_ASSERT_EQUAL(1, auth_init(CreatePath("key-test-invalid-hex", INPUT_DIR), &keys));
147 
148 	TEST_ASSERT_NOT_NULL(keys);
149 
150 	struct key* result = NULL;
151 	get_key(10, &result);
152 	TEST_ASSERT_NOT_NULL(result);
153 	char data[15]; memset(data, 0x01, 15);
154 	TEST_ASSERT_TRUE(CompareKeysAlternative(10, 15, "MD5", data, *result));
155 
156 	result = NULL;
157 	get_key(30, &result); // Should not exist, and result should remain NULL.
158 	TEST_ASSERT_NULL(result);
159 }
160