d02d6550e34412cf7a2a666d3115f6a21c40fdb6
[openafs.git] / tests / auth / keys-t.c
1 /*
2  * Copyright (c) 2010 Your File System Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
23  */
24
25 /*!
26  * Tests for the afsconf key handling functions
27  */
28
29 #include <afsconfig.h>
30 #include <afs/param.h>
31
32 #include <roken.h>
33
34 #include <afs/cellconfig.h>
35 #include <afs/keys.h>
36 #include <afs/afsutil.h>
37 #include <rx/rxkad.h>
38
39 #include <tap/basic.h>
40
41 #include "test.h"
42 #include "common.h"
43
44 static int
45 copy(char *inFile, char *outFile)
46 {
47     int in, out;
48     char *block;
49     size_t len;
50
51     in = open(inFile, O_RDONLY);
52     if (in<0)
53         return EIO;
54
55     out = open(outFile, O_WRONLY | O_CREAT, 0600);
56     if (out<0)
57         return EIO;
58
59     block = malloc(1024);
60     do {
61         len = read(in, block, 1024);
62         if (len > 0)
63             write(out, block, len);
64     } while (len > 0);
65     free(block);
66
67     close(in);
68     close(out);
69
70     if (len == -1)
71         return EIO;
72
73     return 0;
74 }
75
76 int
77 keyMatches(struct afsconf_typedKey *typedKey,
78            afsconf_keyType type, int kvno, int subType,
79            void *keyMaterial, size_t keyLen)
80 {
81     afsconf_keyType keyType;
82     int keyKvno;
83     int keySubType;
84     struct rx_opaque *buffer;
85
86     afsconf_typedKey_values(typedKey, &keyType, &keyKvno, &keySubType,
87                             &buffer);
88
89     return (keyType == type && keyKvno == kvno && keySubType == subType &&
90             buffer->len == keyLen &&
91             memcmp(keyMaterial, buffer->val, buffer->len) == 0);
92 }
93
94 int main(int argc, char **argv)
95 {
96     struct afsconf_dir *dir;
97     struct afsconf_keys keys;
98     struct ktc_encryptionKey key;
99     struct rx_opaque *keyMaterial;
100     struct afsconf_typedKey *typedKey;
101     struct afsconf_typedKeyList *typedKeyList;
102     char *dirname;
103     char *keyfile;
104     afs_int32 kvno;
105     int code;
106     int i;
107
108     plan(122);
109
110     /* Create a temporary afs configuration directory */
111
112     dirname = buildTestConfig();
113
114     if (asprintf(&keyfile, "%s/KeyFile", dirname) == -1)
115         goto out;
116
117     /* First, copy in a known keyfile */
118     code = copy("KeyFile", keyfile);
119     if (code)
120         goto out;
121
122     /* Start with a blank configuration directory */
123     dir = afsconf_Open(dirname);
124     ok(dir != NULL, "Sucessfully re-opened config directory");
125     if (dir == NULL)
126         goto out;
127
128     /* Verify that GetKeys returns the entire set of keys correctly */
129     code = afsconf_GetKeys(dir, &keys);
130     is_int(0, code, "afsconf_GetKeys returns successfully");
131     is_int(3, keys.nkeys, "... and returns the right number of keys");
132     is_int(1, keys.key[0].kvno, " ... first key number is correct");
133     is_int(2, keys.key[1].kvno, " ... second key number is correct");
134     is_int(4, keys.key[2].kvno, " ... third key number is correct");
135     ok(memcmp(keys.key[0].key, "\x01\x02\x04\x08\x10\x20\x40\x80", 8) == 0,
136        " ... first key matches");
137     ok(memcmp(keys.key[1].key, "\x04\x04\x04\x04\x04\x04\x04\x04", 8) == 0,
138        " ... second key matches");
139     ok(memcmp(keys.key[2].key, "\x19\x16\xfe\xe6\xba\x77\x2f\xfd", 8) == 0,
140        " ... third key matches");
141
142     /* Verify that GetLatestKey returns the newest key */
143     code = afsconf_GetLatestKey(dir, &kvno, &key);
144     is_int(0, code, "afsconf_GetLatestKey returns sucessfully");
145     is_int(4, kvno, " ... with correct key number");
146     ok(memcmp(&key, "\x19\x16\xfe\xe6\xba\x77\x2f\xfd", 8) == 0,
147        " ... and correct key");
148
149     /* Verify that random access using GetKey works properly */
150     code = afsconf_GetKey(dir, 2, &key);
151     is_int(0, code, "afsconf_GetKey returns successfully");
152     ok(memcmp(&key, "\x04\x04\x04\x04\x04\x04\x04\x04", 8) == 0,
153        " ... and with correct key");
154
155     /* And that it fails if the key number doesn't exist */
156     code = afsconf_GetKey(dir, 3, &key);
157     is_int(code, AFSCONF_NOTFOUND,
158            "afsconf_GetKey returns not found for missing key");
159
160     /* Check that AddKey can be used to add a new 'newest' key */
161     code = afsconf_AddKey(dir, 5, "\x08\x08\x08\x08\x08\x08\x08\x08", 0);
162     is_int(0, code, "afsconf_AddKey sucessfully adds a new key");
163
164     /* And that we can get it back with GetKeys, GetLatestKey and GetKey */
165     code = afsconf_GetKeys(dir, &keys);
166     is_int(0, code, " ... and GetKeys still works");
167     is_int(4, keys.nkeys, "... and has the correct number of keys");
168     is_int(5, keys.key[3].kvno, " ... and the fourth key has the correct kvno");
169     ok(memcmp(keys.key[3].key, "\x08\x08\x08\x08\x08\x08\x08\x08", 8) == 0,
170        " ... and is the correct key");
171
172     code = afsconf_GetLatestKey(dir, &kvno, &key);
173     is_int(0, code, " ... and GetLatestKey returns successfully");
174     is_int(5, kvno, " ... with the correct key number");
175     ok(memcmp(&key, "\x08\x08\x08\x08\x08\x08\x08\x08", 8) == 0,
176        " ... and the correct key");
177
178     code = afsconf_GetKey(dir, 5, &key);
179     is_int(0, code, " ... and GetKey still works");
180     ok(memcmp(&key, "\x08\x08\x08\x08\x08\x08\x08\x08", 8) == 0,
181        " ... and returns the correct key");
182
183     /* Check that AddKey without the overwrite flag won't overwrite an existing
184      * key */
185     code = afsconf_AddKey(dir, 5, "\x10\x10\x10\x10\x10\x10\x10", 0);
186     is_int(AFSCONF_KEYINUSE, code, "AddKey won't overwrite without being told to");
187
188     /* Check with GetKey that it didn't */
189     code = afsconf_GetKey(dir, 5, &key);
190     is_int(0, code, " ... and GetKey still works");
191     ok(memcmp(&key, "\x08\x08\x08\x08\x08\x08\x08\x08", 8) == 0,
192        " ... and key hasn't been overwritten");
193
194     /* Check that AddKey with the overwrite flag will overwrite an existing key */
195     code = afsconf_AddKey(dir, 5, "\x10\x10\x10\x10\x10\x10\x10\x10", 1);
196     is_int(0, code, "AddKey overwrites when asked");
197
198     /* Use GetKey to check that it did so */
199     code = afsconf_GetKey(dir, 5, &key);
200     is_int(0, code, " ... and GetKey still works");
201     ok(memcmp(&key, "\x10\x10\x10\x10\x10\x10\x10\x10", 8) == 0,
202        " ... and key has been overwritten");
203
204     /* Check that deleting a key that doesn't exist fails */
205     code = afsconf_DeleteKey(dir, 6);
206     is_int(AFSCONF_NOTFOUND, code,
207            "afsconf_DeleteKey returns NOTFOUND if key doesn't exist");
208
209     /* Check that we can delete a key using afsconf_DeleteKey */
210     code = afsconf_DeleteKey(dir, 2);
211     is_int(0, code, "afsconf_DeleteKey can delete a key");
212     code = afsconf_GetKey(dir, 2, &key);
213     is_int(AFSCONF_NOTFOUND, code, " ... and afsconf_GetKey can't find it");
214
215     /* Check that deleting it doesn't leave a hole in what GetKeys returns */
216     code = afsconf_GetKeys(dir, &keys);
217     is_int(0, code, "... and afsconf_GetKeys returns it");
218     is_int(3, keys.nkeys, "... and returns the right number of keys");
219     is_int(1, keys.key[0].kvno, " ... first key number is correct");
220     is_int(4, keys.key[1].kvno, " ... second key number is correct");
221     is_int(5, keys.key[2].kvno, " ... third key number is correct");
222
223     /* Make sure that if we drop the dir structure, and then rebuild it, we
224      * still have the same KeyFile */
225     afsconf_Close(dir);
226
227     dir = afsconf_Open(dirname);
228     ok(dir != NULL, "Sucessfully re-opened config directory");
229     if (dir == NULL)
230         goto out;
231
232     code = afsconf_GetKeys(dir, &keys);
233     is_int(0, code, "afsconf_GetKeys still works");
234     is_int(3, keys.nkeys, "... and returns the right number of keys");
235     is_int(1, keys.key[0].kvno, " ... first key number is correct");
236     is_int(4, keys.key[1].kvno, " ... second key number is correct");
237     is_int(5, keys.key[2].kvno, " ... third key number is correct");
238
239     /* Now check that we're limited to 8 keys */
240     for (i=0; i<5; i++) {
241         code = afsconf_AddKey(dir, 10+i, "\x10\x10\x10\x10\x10\x10\x10\x10",
242                               0);
243         is_int(0, code, "Adding %dth key with AddKey works", i+4);
244     }
245     code = afsconf_AddKey(dir, 20, "\x10\x10\x10\x10\x10\x10\x10\x10",0);
246     is_int(AFSCONF_FULL, code, "afsconf_AddKey fails once we've got 8 keys");
247
248     /* Check that the new interface also fails when we've got too many
249      * keys */
250     keyMaterial = rx_opaque_new("\x10\x10\x10\x10\x10\x10\x10\x10", 8);
251     typedKey = afsconf_typedKey_new(afsconf_rxkad, 20, 0, keyMaterial);
252     rx_opaque_free(&keyMaterial);
253     code = afsconf_AddTypedKey(dir, typedKey, 0);
254     afsconf_typedKey_put(&typedKey);
255     is_int(AFSCONF_FULL, code,
256            "afsconf_AddTypedKey fails for rxkad once we've got 8 keys");
257
258     /* Check the new accessors work for rxkad keys */
259     code = afsconf_GetKeyByTypes(dir, afsconf_rxkad, 4, 0, &typedKey);
260     is_int(0, code,
261            "afsconf_GetKeyByTypes works for rxkad");
262     ok(keyMatches(typedKey, afsconf_rxkad, 4, 0,
263                   "\x19\x16\xfe\xe6\xba\x77\x2f\xfd", 8),
264        " ... and returned key matches");
265
266     afsconf_typedKey_put(&typedKey);
267
268     code = afsconf_GetKeysByType(dir, afsconf_rxkad, 4, &typedKeyList);
269     is_int(0, code,
270            "afsconf_GetKeysByType works for rxkad");
271     is_int(1, typedKeyList->nkeys,
272            " ... and returns 1 key, as expected");
273     ok(keyMatches(typedKeyList->keys[0], afsconf_rxkad, 4, 0,
274                   "\x19\x16\xfe\xe6\xba\x77\x2f\xfd", 8),
275        " ... and returned key matches");
276
277     afsconf_PutTypedKeyList(&typedKeyList);
278
279     code = afsconf_GetLatestKeyByTypes(dir, afsconf_rxkad, 0, &typedKey);
280     is_int(0, code,
281            "afsconf_GetLatestKeyByTypes works for rxkad");
282     ok(keyMatches(typedKey, afsconf_rxkad, 14, 0,
283                   "\x10\x10\x10\x10\x10\x10\x10\x10", 8),
284        " ... and returned key matches");
285
286     afsconf_typedKey_put(&typedKey);
287
288     code = afsconf_GetLatestKeysByType(dir, afsconf_rxkad, &typedKeyList);
289     is_int(0, code,
290            "afsconf_GetLatestKeysByType works for rxkad");
291     is_int(1, typedKeyList->nkeys,
292            " ... and returns 1 key, as expected");
293     ok(keyMatches(typedKeyList->keys[0], afsconf_rxkad, 14, 0,
294                  "\x10\x10\x10\x10\x10\x10\x10\x10", 8),
295        " ... and returned key matches");
296     afsconf_PutTypedKeyList(&typedKeyList);
297
298     /* Check that we can't delete a key that doesn't exist */
299     code = afsconf_DeleteKeyByType(dir, afsconf_rxkad, 6);
300     is_int(AFSCONF_NOTFOUND, code,
301            "afsconf_DeleteKeyByType returns NOTFOUND if key doesn't exist");
302     code = afsconf_DeleteKeyBySubType(dir, afsconf_rxkad, 6, 0);
303     is_int(AFSCONF_NOTFOUND, code,
304            "afsconf_DeleteKeyBySubType returns NOTFOUND if key doesn't exist");
305     code = afsconf_DeleteKeyBySubType(dir, afsconf_rxkad, 14, 1);
306     is_int(AFSCONF_NOTFOUND, code,
307            "afsconf_DeleteKeyBySubType doesn't delete with wrong subtype");
308     code = afsconf_GetKeyByTypes(dir, afsconf_rxkad, 14, 0, &typedKey);
309     is_int(0, code, " ... and key is still there!");
310     afsconf_typedKey_put(&typedKey);
311
312     /* Check that we can delete a key that does */
313     code = afsconf_DeleteKeyByType(dir, afsconf_rxkad, 13);
314     is_int(0, code, "afsconf_DeleteKeyByType works");
315     code = afsconf_GetKeysByType(dir, afsconf_rxkad, 13, &typedKeyList);
316     is_int(AFSCONF_NOTFOUND, code, " ... and is really gone");
317
318     code = afsconf_DeleteKeyBySubType(dir, afsconf_rxkad, 14, 0);
319     is_int(0, code, "afsconf_DeleteKeyBySubType works");
320     code = afsconf_GetKeyByTypes(dir, afsconf_rxkad, 14, 0, &typedKey);
321     is_int(AFSCONF_NOTFOUND, code, " ... and is really gone");
322
323     /* Unlink the KeyFile */
324     unlink(keyfile);
325
326     /* Force a rebuild of the directory structure, just in case */
327     afsconf_Close(dir);
328
329     dir = afsconf_Open(dirname);
330     ok(dir != NULL, "Sucessfully re-opened config directory");
331     if (dir == NULL)
332         goto out;
333
334     /* Check that all of the various functions work properly if the file
335      * isn't there */
336     code = afsconf_GetKeys(dir, &keys);
337     is_int(0, code, "afsconf_GetKeys works with an empty KeyFile");
338     is_int(0, keys.nkeys, " ... and returns the right number of keys");
339     code = afsconf_GetKey(dir, 1, &key);
340     is_int(AFSCONF_NOTFOUND, code,
341            "afsconf_GetKey returns NOTFOUND with an empty KeyFile");
342     code = afsconf_DeleteKey(dir, 1);
343     is_int(AFSCONF_NOTFOUND, code,
344            "afsconf_DeleteKey returns NOTFOUND with an empty KeyFile");
345     code = afsconf_GetLatestKey(dir, &kvno, &key);
346     is_int(AFSCONF_NOTFOUND, code,
347            "afsconf_GetLatestKey returns NOTFOUND with an empty KeyFile");
348     code = afsconf_GetKeysByType(dir, afsconf_rxkad, 1, &typedKeyList);
349     is_int(AFSCONF_NOTFOUND, code,
350            "afsconf_GetKeysByType returns NOTFOUND with an empty KeyFile");
351     code = afsconf_GetKeyByTypes(dir, afsconf_rxkad, 1, 0, &typedKey);
352     is_int(AFSCONF_NOTFOUND, code,
353            "afsconf_GetKeyByTypes returns NOTFOUND with an empty KeyFile");
354     code = afsconf_GetLatestKeysByType(dir, afsconf_rxkad, &typedKeyList);
355     is_int(AFSCONF_NOTFOUND, code,
356            "afsconf_GetLatestKeysByType returns NOTFOUND with empty KeyFile");
357     code = afsconf_GetLatestKeyByTypes(dir, afsconf_rxkad, 0, &typedKey);
358     is_int(AFSCONF_NOTFOUND, code,
359            "afsconf_GetLatestKeyByTypes returns NOTFOUND with empty KeyFile");
360
361     /* Now try adding a key to an empty file */
362     code = afsconf_AddKey(dir, 1, "\x10\x10\x10\x10\x10\x10\x10\x10", 1);
363     is_int(0, code, "afsconf_AddKey succeeds with an empty KeyFile");
364     code = afsconf_GetLatestKey(dir, &kvno, &key);
365     is_int(0, code, " ... and afsconf_GetLatestKey succeeds");
366     is_int(1, kvno, " ... with correct kvno");
367     ok(memcmp(&key, "\x10\x10\x10\x10\x10\x10\x10\x10", 8) == 0,
368        " ... and key");
369
370     /* And adding a key using the new interface */
371
372     keyMaterial = rx_opaque_new("\x20\x20\x20\x20\x20\x20\x20\x20", 8);
373     typedKey = afsconf_typedKey_new(afsconf_rxkad, 2, 0, keyMaterial);
374     rx_opaque_free(&keyMaterial);
375     code = afsconf_AddTypedKey(dir, typedKey, 0);
376     afsconf_typedKey_put(&typedKey);
377     is_int(0, code, "afsconf_AddTypedKey works");
378     code = afsconf_GetLatestKey(dir, &kvno, &key);
379     is_int(0, code, " ... and afsconf_GetLatestKey succeeds");
380     is_int(2, kvno, " ... with correct kvno");
381     ok(memcmp(&key, "\x20\x20\x20\x20\x20\x20\x20\x20", 8) == 0,
382        " ... and key");
383     code = afsconf_GetLatestKeyByTypes(dir, afsconf_rxkad, 0, &typedKey);
384     is_int(0, code, " ... and so does afsconf_GetLatestKeyByTypes");
385     ok(keyMatches(typedKey, afsconf_rxkad, 2, 0,
386                   "\x20\x20\x20\x20\x20\x20\x20\x20", 8),
387        " ... with correct key");
388     afsconf_typedKey_put(&typedKey);
389
390     /* And that we can't add a key to an existing kvno and type */
391     keyMaterial = rx_opaque_new("\x30\x30\x30\x30\x30\x30\x30\x30", 8);
392     typedKey = afsconf_typedKey_new(afsconf_rxkad, 2, 0, keyMaterial);
393     rx_opaque_free(&keyMaterial);
394     code = afsconf_AddTypedKey(dir, typedKey, 0);
395     afsconf_typedKey_put(&typedKey);
396     is_int(AFSCONF_KEYINUSE, code,
397            "afsconf_AddTypedKey won't overwrite without being told to");
398     code = afsconf_GetKeyByTypes(dir, afsconf_rxkad, 2, 0, &typedKey);
399     is_int(0, code, " ... and key still exists");
400     ok(keyMatches(typedKey, afsconf_rxkad, 2, 0,
401                   "\x20\x20\x20\x20\x20\x20\x20\x20", 8),
402        " ... and hasn't changed");
403     afsconf_typedKey_put(&typedKey);
404
405     /* But we can if we force */
406     keyMaterial = rx_opaque_new("\x30\x30\x30\x30\x30\x30\x30\x30", 8);
407     typedKey = afsconf_typedKey_new(afsconf_rxkad, 2, 0, keyMaterial);
408     rx_opaque_free(&keyMaterial);
409     code = afsconf_AddTypedKey(dir, typedKey, 1);
410     afsconf_typedKey_put(&typedKey);
411     is_int(0, code,  "afsconf_AddTypedKey overwrites when asked");
412     code = afsconf_GetKeyByTypes(dir, afsconf_rxkad, 2, 0, &typedKey);
413     is_int(0, code, " ... and GetKeyByTypes retrieves new key");
414     ok(keyMatches(typedKey, afsconf_rxkad, 2, 0,
415                   "\x30\x30\x30\x30\x30\x30\x30\x30", 8),
416        " ... and it is the new key");
417
418     /* Check that we can't add bad rxkad keys */
419     keyMaterial = rx_opaque_new("\x30\x30\x30\x30\x30\x30\x30", 7);
420     typedKey = afsconf_typedKey_new(afsconf_rxkad, 3, 0, keyMaterial);
421     rx_opaque_free(&keyMaterial);
422     code = afsconf_AddTypedKey(dir, typedKey, 1);
423     afsconf_typedKey_put(&typedKey);
424     is_int(AFSCONF_BADKEY, code,
425            "afsconf_AddTypedKey won't add short rxkad keys");
426     keyMaterial = rx_opaque_new("\x30\x30\x30\x30\x30\x30\x30\x30\x30", 9);
427     typedKey = afsconf_typedKey_new(afsconf_rxkad, 3, 0, keyMaterial);
428     rx_opaque_free(&keyMaterial);
429     code = afsconf_AddTypedKey(dir, typedKey, 1);
430     afsconf_typedKey_put(&typedKey);
431     is_int(AFSCONF_BADKEY, code,
432            "afsconf_AddTypedKey won't add long rxkad keys");
433     keyMaterial = rx_opaque_new("\x30\x30\x30\x30\x30\x30\x30\x30", 8);
434     typedKey = afsconf_typedKey_new(afsconf_rxkad, 3, 1, keyMaterial);
435     rx_opaque_free(&keyMaterial);
436     code = afsconf_AddTypedKey(dir, typedKey, 1);
437     afsconf_typedKey_put(&typedKey);
438     is_int(AFSCONF_BADKEY, code,
439            "afsconf_AddTypedKey won't add rxkad keys with non-zero subtype");
440
441     /* Now, test things with other key types. */
442
443     /* Add a different key type, but with same kvno as rxkad */
444     keyMaterial = rx_opaque_new("\x01", 1);
445     typedKey = afsconf_typedKey_new(1, 2, 0, keyMaterial);
446     code = afsconf_AddTypedKey(dir, typedKey, 0);
447     afsconf_typedKey_put(&typedKey);
448     is_int(0, code,
449            "afsconf_AddTypedKey can add keys with different key type");
450
451     /* Add a different subtype, with same kvno */
452     keyMaterial = rx_opaque_new("\x02\x03", 2);
453     typedKey = afsconf_typedKey_new(1, 2, 1, keyMaterial);
454     code = afsconf_AddTypedKey(dir, typedKey, 0);
455     afsconf_typedKey_put(&typedKey);
456     is_int(0, code,
457            "afsconf_AddTypedKey can add keys with different sub type");
458
459     /* Check the GetKeyByTypes returns one of the keys */
460     code = afsconf_GetKeyByTypes(dir, 1, 2, 1, &typedKey);
461     is_int(0, code, "afsconf_GetKeyByTypes returns");
462     ok(keyMatches(typedKey, 1, 2, 1, "\x02\x03", 2),
463        " ... with the right key");
464
465     /* Check that GetKeysByType returns both of the keys */
466     code = afsconf_GetKeysByType(dir, 1, 2, &typedKeyList);
467     is_int(0, code, "afsconf_GetKeysByType returns");
468     is_int(2, typedKeyList->nkeys, " ... with correct number of keys");
469     ok(keyMatches(typedKeyList->keys[0], 1, 2, 0, "\x01", 1),
470        " ... with the right key in slot 0");
471     ok(keyMatches(typedKeyList->keys[1], 1, 2, 1, "\x02\x03", 2),
472        " ... with the right key in slot 1");
473     afsconf_PutTypedKeyList(&typedKeyList);
474
475     /* Add another key, before these ones, so we can check that
476      * latest really works */
477     keyMaterial = rx_opaque_new("\x03", 1);
478     typedKey = afsconf_typedKey_new(1, 1, 0, keyMaterial);
479     code = afsconf_AddTypedKey(dir, typedKey, 0);
480     afsconf_typedKey_put(&typedKey);
481     is_int(0, code, "afsconf_AddTypedKey worked again");
482
483     /* Check that GetLatestKeyByTypes returns one */
484     code = afsconf_GetLatestKeyByTypes(dir, 1, 1, &typedKey);
485     is_int(0, code, "afsconf_GetLatestKeyByTypes returns");
486     ok(keyMatches(typedKey, 1, 2, 1, "\x02\x03", 2),
487        " ... with the right key");
488
489     /* Check that GetLatestKeysByType returns both */
490     code = afsconf_GetLatestKeysByType(dir, 1, &typedKeyList);
491     is_int(0, code, "afsconf_GetLatestKeysByType returns");
492         is_int(2, typedKeyList->nkeys, " ... with correct number of keys");
493     ok(keyMatches(typedKeyList->keys[0], 1, 2, 0, "\x01", 1),
494        " ... with the right key in slot 0");
495     ok(keyMatches(typedKeyList->keys[1], 1, 2, 1, "\x02\x03", 2),
496        " ... with the right key in slot 1");
497     afsconf_PutTypedKeyList(&typedKeyList);
498
499     /* Check that closing this instance, and reopening, still has all of
500      * the required keys
501      */
502     afsconf_Close(dir);
503
504     dir = afsconf_Open(dirname);
505     ok(dir != NULL, "Sucessfully re-opened config directory");
506     if (dir == NULL)
507         goto out;
508
509     /* Check that GetKeysByType returns all of the keys */
510     code = afsconf_GetKeysByType(dir, 1, 1, &typedKeyList);
511     is_int(0, code, "afsconf_GetKeysByType returns after reopening");
512     is_int(1, typedKeyList->nkeys, " ... First kvno has correct number of keys");
513     ok(keyMatches(typedKeyList->keys[0], 1, 1, 0, "\x03", 1),
514        " ... and key material is correct");
515     afsconf_PutTypedKeyList(&typedKeyList);
516
517     code = afsconf_GetKeysByType(dir, 1, 2, &typedKeyList);
518     is_int(0, code, "afsconf_GetKeysByType returns after reopening");
519     is_int(2, typedKeyList->nkeys, " ... with correct number of keys");
520     ok(keyMatches(typedKeyList->keys[0], 1, 2, 0, "\x01", 1),
521        " ... with the right key in slot 0");
522     ok(keyMatches(typedKeyList->keys[1], 1, 2, 1, "\x02\x03", 2),
523        " ... with the right key in slot 1");
524     afsconf_PutTypedKeyList(&typedKeyList);
525
526 out:
527     unlinkTestConfig(dirname);
528
529     return 0;
530 }