2 * Copyright (c) 2010 Your File System Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * Tests for the afsconf key handling functions
29 #include <afsconfig.h>
30 #include <afs/param.h>
34 #include <afs/cellconfig.h>
36 #include <afs/afsutil.h>
40 copy(char *inFile, char *outFile)
47 in = open(inFile, O_RDONLY);
51 out = open(outFile, O_WRONLY | O_CREAT, 0600);
57 len = read(in, block, 1024);
59 write(out, block, len);
72 int main(int argc, char **argv)
74 struct afsconf_dir *dir;
75 struct afsconf_keys keys;
76 struct ktc_encryptionKey key;
85 /* Create a temporary afs configuration directory */
86 snprintf(buffer, sizeof(buffer), "%s/afs_XXXXXX", gettmpdir());
88 dirEnd = buffer + strlen(buffer);
90 /* Create a CellServDB file */
91 strcpy(dirEnd, "/CellServDB");
92 file = fopen(buffer, "w");
93 fprintf(file, ">example.org # An example cell\n");
94 fprintf(file, "127.0.0.1 #test.example.org\n");
97 /* Create a ThisCell file */
98 strcpy(dirEnd, "/ThisCell");
99 file = fopen(buffer, "w");
100 fprintf(file, "example.org\n");
103 /* Firstly, copy in a known keyfile. */
104 strcpy(dirEnd, "/KeyFile");
105 code = copy("KeyFile", buffer);
111 /* Start with a blank configuration directory */
112 dir = afsconf_Open(strdup(buffer));
114 fprintf(stderr, "Unable to open configuration directory.\n");
119 /* Verify that GetKeys returns the entire set of keys correctly */
120 code = afsconf_GetKeys(dir, &keys);
121 is_int(0, code, "afsconf_GetKeys returns successfully");
122 is_int(3, keys.nkeys, "... and returns the right number of keys");
123 is_int(1, keys.key[0].kvno, " ... first key number is correct");
124 is_int(2, keys.key[1].kvno, " ... second key number is correct");
125 is_int(4, keys.key[2].kvno, " ... third key number is correct");
126 ok(memcmp(keys.key[0].key, "\x01\x02\x04\x08\x10\x20\x40\x80", 8) == 0,
127 " ... first key matches");
128 ok(memcmp(keys.key[1].key, "\x04\x04\x04\x04\x04\x04\x04\x04", 8) == 0,
129 " ... second key matches");
130 ok(memcmp(keys.key[2].key, "\x19\x16\xfe\xe6\xba\x77\x2f\xfd", 8) == 0,
131 " ... third key matches");
133 /* Verify that GetLatestKey returns the newest key */
134 code = afsconf_GetLatestKey(dir, &kvno, &key);
135 is_int(0, code, "afsconf_GetLatestKey returns sucessfully");
136 is_int(4, kvno, " ... with correct key number");
137 ok(memcmp(&key, "\x19\x16\xfe\xe6\xba\x77\x2f\xfd", 8) == 0,
138 " ... and correct key");
140 /* Verify that random access using GetKey works properly */
141 code = afsconf_GetKey(dir, 2, &key);
142 is_int(0, code, "afsconf_GetKey returns successfully");
143 ok(memcmp(&key, "\x04\x04\x04\x04\x04\x04\x04\x04", 8) == 0,
144 " ... and with correct key");
146 /* And that it fails if the key number doesn't exist */
147 code = afsconf_GetKey(dir, 3, &key);
148 is_int(code, AFSCONF_NOTFOUND,
149 "afsconf_GetKey returns not found for missing key");
151 /* Check that AddKey can be used to add a new 'newest' key */
152 code = afsconf_AddKey(dir, 5, "\x08\x08\x08\x08\x08\x08\x08\x08", 0);
153 is_int(0, code, "afsconf_AddKey sucessfully adds a new key");
155 /* And that we can get it back with GetKeys, GetLatestKey and GetKey */
156 code = afsconf_GetKeys(dir, &keys);
157 is_int(0, code, " ... and GetKeys still works");
158 is_int(4, keys.nkeys, "... and has the correct number of keys");
159 is_int(5, keys.key[3].kvno, " ... and the fourth key has the correct kvno");
160 ok(memcmp(keys.key[3].key, "\x08\x08\x08\x08\x08\x08\x08\x08", 8) == 0,
161 " ... and is the correct key");
163 code = afsconf_GetLatestKey(dir, &kvno, &key);
164 is_int(0, code, " ... and GetLatestKey returns successfully");
165 is_int(5, kvno, " ... with the correct key number");
166 ok(memcmp(&key, "\x08\x08\x08\x08\x08\x08\x08\x08", 8) == 0,
167 " ... and the correct key");
169 code = afsconf_GetKey(dir, 5, &key);
170 is_int(0, code, " ... and GetKey still works");
171 ok(memcmp(&key, "\x08\x08\x08\x08\x08\x08\x08\x08", 8) == 0,
172 " ... and returns the correct key");
174 /* Check that AddKey without the overwrite flag won't overwrite an existing
176 code = afsconf_AddKey(dir, 5, "\x10\x10\x10\x10\x10\x10\x10", 0);
177 is_int(AFSCONF_KEYINUSE, code, "AddKey won't overwrite without being told to");
179 /* Check with GetKey that it didn't */
180 code = afsconf_GetKey(dir, 5, &key);
181 is_int(0, code, " ... and GetKey still works");
182 ok(memcmp(&key, "\x08\x08\x08\x08\x08\x08\x08\x08", 8) == 0,
183 " ... and key hasn't been overwritten");
185 /* Check that AddKey with the overwrite flag will overwrite an existing key */
186 code = afsconf_AddKey(dir, 5, "\x10\x10\x10\x10\x10\x10\x10\x10", 1);
187 is_int(0, code, "AddKey overwrites when asked");
189 /* Use GetKey to check that it did so */
190 code = afsconf_GetKey(dir, 5, &key);
191 is_int(0, code, " ... and GetKey still works");
192 ok(memcmp(&key, "\x10\x10\x10\x10\x10\x10\x10\x10", 8) == 0,
193 " ... and key has been overwritten");
195 /* Check that deleting a key that doesn't exist fails */
196 code = afsconf_DeleteKey(dir, 6);
197 is_int(AFSCONF_NOTFOUND, code,
198 "afsconf_DeleteKey returns NOTFOUND if key doesn't exist");
200 /* Check that we can delete a key using afsconf_DeleteKey */
201 code = afsconf_DeleteKey(dir, 2);
202 is_int(0, code, "afsconf_DeleteKey can delete a key");
203 code = afsconf_GetKey(dir, 2, &key);
204 is_int(AFSCONF_NOTFOUND, code, " ... and afsconf_GetKey can't find it");
206 /* Check that deleting it doesn't leave a hole in what GetKeys returns */
207 code = afsconf_GetKeys(dir, &keys);
208 is_int(0, code, "... and afsconf_GetKeys returns it");
209 is_int(3, keys.nkeys, "... and returns the right number of keys");
210 is_int(1, keys.key[0].kvno, " ... first key number is correct");
211 is_int(4, keys.key[1].kvno, " ... second key number is correct");
212 is_int(5, keys.key[2].kvno, " ... third key number is correct");
214 /* Unlink the KeyFile */
215 strcpy(dirEnd, "/KeyFile");
218 /* Force a rebuild of the directory structure, just in case */
222 dir = afsconf_Open(strdup(buffer));
224 fprintf(stderr, "Unable to open configuration directory.\n");
228 /* Check that all of the various functions work properly if the file
230 code = afsconf_GetKeys(dir, &keys);
231 is_int(0, code, "afsconf_GetKeys works with an empty KeyFile");
232 is_int(0, keys.nkeys, " ... and returns the right number of keys");
233 code = afsconf_GetKey(dir, 1, &key);
234 is_int(AFSCONF_NOTFOUND, code,
235 "afsconf_GetKey returns NOTFOUND with an empty KeyFile");
236 code = afsconf_DeleteKey(dir, 1);
237 is_int(AFSCONF_NOTFOUND, code,
238 "afsconf_DeleteKey returns NOTFOUND with an empty KeyFile");
239 code = afsconf_GetLatestKey(dir, &kvno, &key);
240 is_int(AFSCONF_NOTFOUND, code,
241 "afsconf_GetLatestKey returns NOTFOUND with an empty KeyFile");
243 /* Now try adding a key to an empty file */
244 code = afsconf_AddKey(dir, 1, "\x10\x10\x10\x10\x10\x10\x10\x10", 1);
245 is_int(0, code, "afsconf_AddKey succeeds with an empty KeyFile");
246 code = afsconf_GetLatestKey(dir, &kvno, &key);
247 is_int(0, code, " ... and afsconf_GetLatestKey succeeds");
248 is_int(1, kvno, " ... with correct kvno");
249 ok(memcmp(&key, "\x10\x10\x10\x10\x10\x10\x10\x10", 8) == 0,
253 strcpy(dirEnd, "/KeyFile");
255 strcpy(dirEnd, "/CellServDB");
257 strcpy(dirEnd, "/ThisCell");
259 strcpy(dirEnd, "/UserList");