tests: Add tests for afsconf_'s key functions
[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 static int
40 copy(char *inFile, char *outFile)
41 {
42     int in, out;
43     int code, written;
44     char *block;
45     size_t len;
46
47     in = open(inFile, O_RDONLY);
48     if (in<0)
49         return EIO;
50
51     out = open(outFile, O_WRONLY | O_CREAT, 0600);
52     if (out<0)
53         return EIO;
54
55     block = malloc(1024);
56     do {
57         len = read(in, block, 1024);
58         if (len > 0)
59             write(out, block, len);
60     } while (len > 0);
61     free(block);
62
63     close(in);
64     close(out);
65
66     if (len == -1)
67         return EIO;
68
69     return 0;
70 }
71
72 int main(int argc, char **argv)
73 {
74     struct afsconf_dir *dir;
75     struct afsconf_keys keys;
76     struct ktc_encryptionKey key;
77     char buffer[1024];
78     char *dirEnd;
79     FILE *file;
80     afs_int32 kvno;
81     int code;
82
83     plan(47);
84
85     /* Create a temporary afs configuration directory */
86     snprintf(buffer, sizeof(buffer), "%s/afs_XXXXXX", gettmpdir());
87     mkdtemp(buffer);
88     dirEnd = buffer + strlen(buffer);
89
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");
95     fclose(file);
96
97     /* Create a ThisCell file */
98     strcpy(dirEnd, "/ThisCell");
99     file = fopen(buffer, "w");
100     fprintf(file, "example.org\n");
101     fclose(file);
102
103     /* Firstly, copy in a known keyfile. */
104     strcpy(dirEnd, "/KeyFile");
105     code = copy("KeyFile", buffer);
106     if (code)
107         goto out;
108
109     *dirEnd='\0';
110
111     /* Start with a blank configuration directory */
112     dir = afsconf_Open(strdup(buffer));
113     if (dir == NULL) {
114         fprintf(stderr, "Unable to open configuration directory.\n");
115         exit(1);
116     }
117
118
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");
132
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");
139
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");
145
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");
150
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");
154
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");
162
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");
168
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");
173
174     /* Check that AddKey without the overwrite flag won't overwrite an existing
175      * key */
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");
178
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");
184
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");
188
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");
194
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");
199
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");
205
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");
213
214     /* Unlink the KeyFile */
215     strcpy(dirEnd, "/KeyFile");
216     unlink(buffer);
217
218     /* Force a rebuild of the directory structure, just in case */
219     afsconf_Close(dir);
220
221     *dirEnd='\0';
222     dir = afsconf_Open(strdup(buffer));
223     if (dir == NULL) {
224         fprintf(stderr, "Unable to open configuration directory.\n");
225         goto out;
226     }
227
228     /* Check that all of the various functions work properly if the file
229      * isn't there */
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");
242
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,
250        " ... and key");
251
252 out:
253     strcpy(dirEnd, "/KeyFile");
254     unlink(buffer);
255     strcpy(dirEnd, "/CellServDB");
256     unlink(buffer);
257     strcpy(dirEnd, "/ThisCell");
258     unlink(buffer);
259     strcpy(dirEnd, "/UserList");
260     unlink(buffer);
261     *dirEnd='\0';
262     rmdir(buffer);
263 }