Linux: Fix USE_UCONTEXT detection
[openafs.git] / tests / auth / keys-t.c
index 49281dc..ba7391e 100644 (file)
@@ -38,6 +38,9 @@
 
 #include <tap/basic.h>
 
+#include "test.h"
+#include "common.h"
+
 static int
 copy(char *inFile, char *outFile)
 {
@@ -88,7 +91,6 @@ keyMatches(struct afsconf_typedKey *typedKey,
            memcmp(keyMaterial, buffer->val, buffer->len) == 0);
 }
 
-
 int main(int argc, char **argv)
 {
     struct afsconf_dir *dir;
@@ -97,44 +99,39 @@ int main(int argc, char **argv)
     struct rx_opaque *keyMaterial;
     struct afsconf_typedKey *typedKey;
     struct afsconf_typedKeyList *typedKeyList;
-
-    char buffer[1024];
-    char *dirEnd;
-    FILE *file;
+    char *dirname;
+    char *keyfile;
+    char *keyfilesrc;
     afs_int32 kvno;
     int code;
     int i;
 
-    plan(122);
+    plan(134);
 
     /* Create a temporary afs configuration directory */
-    snprintf(buffer, sizeof(buffer), "%s/afs_XXXXXX", gettmpdir());
-    mkdtemp(buffer);
-    dirEnd = buffer + strlen(buffer);
-
-    /* Create a CellServDB file */
-    strcpy(dirEnd, "/CellServDB");
-    file = fopen(buffer, "w");
-    fprintf(file, ">example.org # An example cell\n");
-    fprintf(file, "127.0.0.1 #test.example.org\n");
-    fclose(file);
-
-    /* Create a ThisCell file */
-    strcpy(dirEnd, "/ThisCell");
-    file = fopen(buffer, "w");
-    fprintf(file, "example.org\n");
-    fclose(file);
-
-    /* Firstly, copy in a known keyfile. */
-    strcpy(dirEnd, "/KeyFile");
-    code = copy("KeyFile", buffer);
-    if (code)
+
+    dirname = buildTestConfig();
+
+    if (asprintf(&keyfile, "%s/KeyFile", dirname) == -1)
        goto out;
 
-    *dirEnd='\0';
+    /* Work out the path to our KeyFile. If the test harness hasn't set
+     * the SOURCE environment variable, then assume it is in our CWD */
+    if (getenv("SOURCE") == NULL) {
+       keyfilesrc = strdup("KeyFile");
+    } else {
+       if (asprintf(&keyfilesrc, "%s/auth/KeyFile", getenv("SOURCE")) == -1)
+           goto out;
+    }
+
+    /* First, copy in a known keyfile */
+    code = copy(keyfilesrc, keyfile);
+    free(keyfilesrc);
+    if (code)
+       goto out;
 
     /* Start with a blank configuration directory */
-    dir = afsconf_Open(strdup(buffer));
+    dir = afsconf_Open(dirname);
     ok(dir != NULL, "Sucessfully re-opened config directory");
     if (dir == NULL)
        goto out;
@@ -160,6 +157,10 @@ int main(int argc, char **argv)
     ok(memcmp(&key, "\x19\x16\xfe\xe6\xba\x77\x2f\xfd", 8) == 0,
        " ... and correct key");
 
+    /* Check that GetLatestKey works if called with NULL parameters */
+    code = afsconf_GetLatestKey(dir, NULL, NULL);
+    is_int(0, code, "afsconf_GetLatestKey works if parameters are NULL");
+
     /* Verify that random access using GetKey works properly */
     code = afsconf_GetKey(dir, 2, &key);
     is_int(0, code, "afsconf_GetKey returns successfully");
@@ -238,8 +239,7 @@ int main(int argc, char **argv)
      * still have the same KeyFile */
     afsconf_Close(dir);
 
-    *dirEnd='\0';
-    dir = afsconf_Open(strdup(buffer));
+    dir = afsconf_Open(dirname);
     ok(dir != NULL, "Sucessfully re-opened config directory");
     if (dir == NULL)
        goto out;
@@ -336,14 +336,12 @@ int main(int argc, char **argv)
     is_int(AFSCONF_NOTFOUND, code, " ... and is really gone");
 
     /* Unlink the KeyFile */
-    strcpy(dirEnd, "/KeyFile");
-    unlink(buffer);
+    unlink(keyfile);
 
     /* Force a rebuild of the directory structure, just in case */
     afsconf_Close(dir);
 
-    *dirEnd='\0';
-    dir = afsconf_Open(strdup(buffer));
+    dir = afsconf_Open(dirname);
     ok(dir != NULL, "Sucessfully re-opened config directory");
     if (dir == NULL)
        goto out;
@@ -518,8 +516,7 @@ int main(int argc, char **argv)
      */
     afsconf_Close(dir);
 
-    *dirEnd='\0';
-    dir = afsconf_Open(strdup(buffer));
+    dir = afsconf_Open(dirname);
     ok(dir != NULL, "Sucessfully re-opened config directory");
     if (dir == NULL)
        goto out;
@@ -541,17 +538,52 @@ int main(int argc, char **argv)
        " ... with the right key in slot 1");
     afsconf_PutTypedKeyList(&typedKeyList);
 
+    /* Check that GetAllKeys works as expected */
+    code = afsconf_GetAllKeys(dir, &typedKeyList);
+    is_int(0, code, "afsconf_GetAllKeys returns success");
+    is_int(5, typedKeyList->nkeys, " ... with the correct number of keys");
+    ok(keyMatches(typedKeyList->keys[0], afsconf_rxkad, 1, 0,
+                 "\x10\x10\x10\x10\x10\x10\x10\x10", 8),
+       " ... with right key in slot 0");
+    ok(keyMatches(typedKeyList->keys[1], afsconf_rxkad, 2, 0,
+                  "\x30\x30\x30\x30\x30\x30\x30\x30", 8),
+       " ... with right key in slot 1");
+    ok(keyMatches(typedKeyList->keys[2], 1, 1, 0, "\x03", 1),
+       " ... with right key in slot 2");
+    ok(keyMatches(typedKeyList->keys[3], 1, 2, 0, "\x01", 1),
+       " ... with right key in slot 3");
+    ok(keyMatches(typedKeyList->keys[4], 1, 2, 1, "\x02\03", 2),
+       " ... with right key in slot 4");
+
+    afsconf_Close(dir);
+
+    unlinkTestConfig(dirname);
+    free(dirname);
+    free(keyfile);
+
+    /* Start a new test configuration */
+    dirname = buildTestConfig();
+    dir = afsconf_Open(dirname);
+    ok(dir != NULL, "Sucessfully opened brand new config directory");
+    if (dir == NULL)
+       goto out;
+
+    /* Check that directories with just new style keys work */
+    keyMaterial = rx_opaque_new("\x02\x03", 2);
+    typedKey = afsconf_typedKey_new(1, 2, 1, keyMaterial);
+    code = afsconf_AddTypedKey(dir, typedKey, 0);
+    afsconf_typedKey_put(&typedKey);
+    is_int(0, code,
+          "afsconf_AddTypedKey can add keys with different sub type");
+
+    /* Check the GetKeyByTypes returns one of the keys */
+    code = afsconf_GetKeyByTypes(dir, 1, 2, 1, &typedKey);
+    is_int(0, code, "afsconf_GetKeyByTypes returns it");
+    ok(keyMatches(typedKey, 1, 2, 1, "\x02\x03", 2),
+       " ... with the right key");
+
 out:
-    strcpy(dirEnd, "/KeyFile");
-    unlink(buffer);
-    strcpy(dirEnd, "/CellServDB");
-    unlink(buffer);
-    strcpy(dirEnd, "/ThisCell");
-    unlink(buffer);
-    strcpy(dirEnd, "/UserList");
-    unlink(buffer);
-    *dirEnd='\0';
-    rmdir(buffer);
+    unlinkTestConfig(dirname);
 
     return 0;
 }