tests: partition name to id function tests 76/13176/3
authorMichael Meffie <mmeffie@sinenomine.net>
Sat, 9 Jun 2018 04:39:49 +0000 (04:39 +0000)
committerBenjamin Kaduk <kaduk@mit.edu>
Fri, 14 Sep 2018 12:35:18 +0000 (08:35 -0400)
Add unit tests for the utility functions to convert between partition
names and partition ids.

Change-Id: I4b12f9d611cb9f3ce49909cda5cbcedd3e6c3d10
Reviewed-on: https://gerrit.openafs.org/13176
Reviewed-by: Benjamin Kaduk <kaduk@mit.edu>
Tested-by: Benjamin Kaduk <kaduk@mit.edu>

tests/TESTS
tests/util/.gitignore
tests/util/Makefile.in
tests/util/volutil-t.c [new file with mode: 0644]

index e59014e..29ed02d 100644 (file)
@@ -1,5 +1,6 @@
 util/ktime
 util/exec-alt
+util/volutil
 auth/keys
 auth/superuser
 auth/authcon
index 02663ae..b44d7d6 100644 (file)
@@ -5,3 +5,4 @@
 /exec-alt-t
 /ktime-t
 /queues-t
+/volutil-t
index 763ac89..08e8fda 100644 (file)
@@ -11,7 +11,7 @@ LIBS = ../tap/libtap.a \
        $(abs_top_builddir)/lib/util.a \
        $(abs_top_builddir)/lib/libopr.a
 
-tests = ktime-t exec-alt-t
+tests = ktime-t exec-alt-t volutil-t
 
 all check test tests: $(tests)
 
@@ -21,6 +21,9 @@ ktime-t: ktime-t.o $(LIBS)
 exec-alt-t: exec-alt-t.o $(LIBS)
        $(AFS_LDRULE) exec-alt-t.o $(LIBS) $(XLIBS)
 
+volutil-t: volutil-t.o $(LIBS)
+       $(AFS_LDRULE) volutil-t.o $(LIBS) $(LIB_roken) $(XLIBS)
+
 install:
 
 clean distclean:
diff --git a/tests/util/volutil-t.c b/tests/util/volutil-t.c
new file mode 100644 (file)
index 0000000..c598071
--- /dev/null
@@ -0,0 +1,180 @@
+/*
+ * Copyright 2018, Sine Nomine Associates and others.
+ * All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <afsconfig.h>
+#include <afs/param.h>
+
+#include <afs/afsutil.h>
+#include <tests/tap/basic.h>
+
+char *out_of_range_names[] = {
+    "/vicepiv",
+    "/vicepzz",
+    "iv",
+    "zz",
+    "255",
+    "256",
+    NULL,
+};
+
+char *invalid_names[] = {
+    "",
+    "/vice",
+    "/vicep",
+    "/vicepbogus",
+    "/vicep0",
+    "foo",
+    "!?",
+    "-1",
+    NULL,
+};
+
+void
+test_partition_name_to_id(void)
+{
+    char x, y;
+    int id;
+    char part[32];
+    char **t;
+
+    /*
+     * Test conversion of XX to partition id,
+     * where XX is "a".."z", "aa"..."iu"
+     */
+    id = 0;
+    for (x = 'a'; x <= 'z'; x++, id++) {
+        sprintf(part, "%c", x);
+        is_int(id, volutil_GetPartitionID(part), "\"%s\" is %d", part, id);
+    }
+    for (x = 'a'; x <= 'i'; x++) {
+        for (y = 'a'; y <= 'z'; y++, id++) {
+            sprintf(part, "%c%c", x, y);
+            is_int(id, volutil_GetPartitionID(part), "\"%s\" is %d", part, id);
+            if (x == 'i' && y == 'u')
+                break;
+        }
+    }
+
+    /*
+     * Test conversion of /vicepXX to partition id,
+     * where XX is "a".."z", "aa"..."iu".
+     */
+    id = 0;
+    for (x = 'a'; x <= 'z'; x++, id++) {
+        sprintf(part, "/vicep%c", x);
+        is_int(id, volutil_GetPartitionID(part), "\"%s\" is %d", part, id);
+    }
+    for (x = 'a'; x <= 'i'; x++) {
+        for (y = 'a'; y <= 'z'; y++, id++) {
+            sprintf(part, "/vicep%c%c", x,y );
+            is_int(id, volutil_GetPartitionID(part), "\"%s\" is %d", part, id);
+            if (x == 'i' && y == 'u')
+                break;
+        }
+    }
+
+    /*
+     * Test conversion of vicepXX to partition id,
+     * where XX is "a".."z", "aa"..."iu".
+     */
+    id = 0;
+    for (x = 'a'; x <= 'z'; x++, id++) {
+        sprintf(part, "vicep%c", x);
+        is_int(id, volutil_GetPartitionID(part), "\"%s\" is %d", part, id);
+    }
+    for (x = 'a'; x <= 'i'; x++) {
+        for (y = 'a'; y <= 'z'; y++, id++) {
+            sprintf(part, "vicep%c%c", x,y );
+            is_int(id, volutil_GetPartitionID(part), "\"%s\" is %d", part, id);
+            if (x == 'i' && y == 'u')
+                break;
+        }
+    }
+
+    /*
+     * Test numeric names in the range "0".."254" are passed through.
+     */
+    for (id = 0; id <= 254; id++) {
+        sprintf(part, "%d", id);
+        is_int(id, volutil_GetPartitionID(part), "\"%s\" is %d", part, id);
+    }
+
+    /*
+     * Test out of range names.
+     */
+    for (t = out_of_range_names; *t != NULL; t++) {
+        is_int(-1, volutil_GetPartitionID(*t), "\"%s\" is out of range", *t);
+    }
+
+    /*
+     * Test invalid names.
+     */
+    for (t = invalid_names; *t != NULL; t++) {
+        is_int(-1, volutil_GetPartitionID(*t), "\"%s\" is invalid", *t);
+    }
+}
+
+void
+test_partition_id_to_name(void)
+{
+    char x, y;
+    int id;
+    char part[32];
+
+    /*
+     * Test conversion of ids to partition names,
+     * for ids of 0 to 254.
+     */
+    id = 0;
+    for (x = 'a'; x <= 'z'; x++, id++) {
+        sprintf(part, "/vicep%c", x);
+        is_string(part, volutil_PartitionName(id), "%d is \"%s\"", id, part);
+    }
+    for (x = 'a'; x <= 'i'; x++) {
+        for (y = 'a'; y <= 'z'; y++, id++) {
+            sprintf(part, "/vicep%c%c", x,y );
+            is_string(part, volutil_PartitionName(id), "%d is \"%s\"", id, part);
+            if (x == 'i' && y == 'u')
+                break;
+        }
+    }
+
+    /* Test out of range values. */
+    is_string("BAD VOLUME ID", volutil_PartitionName(-1), "-1 is invalid");
+    is_string("BAD VOLUME ID", volutil_PartitionName(255), "255 is invalid");
+    is_string("BAD VOLUME ID", volutil_PartitionName(256), "256 is invalid");
+
+    /* Test buffer too short (thread safe variant of volutil_PartitionName). */
+    is_string("SPC", volutil_PartitionName_r(0, part, 4), "buffer too short");
+}
+
+int
+main(int argc, char **argv)
+{
+    plan(1293);
+    test_partition_name_to_id();
+    test_partition_id_to_name();
+    return 0;
+}