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