tests: partition name to id function tests
[openafs.git] / tests / util / volutil-t.c
1 /*
2  * Copyright 2018, Sine Nomine Associates and others.
3  * All Rights Reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include <afsconfig.h>
27 #include <afs/param.h>
28
29 #include <afs/afsutil.h>
30 #include <tests/tap/basic.h>
31
32 char *out_of_range_names[] = {
33     "/vicepiv",
34     "/vicepzz",
35     "iv",
36     "zz",
37     "255",
38     "256",
39     NULL,
40 };
41
42 char *invalid_names[] = {
43     "",
44     "/vice",
45     "/vicep",
46     "/vicepbogus",
47     "/vicep0",
48     "foo",
49     "!?",
50     "-1",
51     NULL,
52 };
53
54 void
55 test_partition_name_to_id(void)
56 {
57     char x, y;
58     int id;
59     char part[32];
60     char **t;
61
62     /*
63      * Test conversion of XX to partition id,
64      * where XX is "a".."z", "aa"..."iu"
65      */
66     id = 0;
67     for (x = 'a'; x <= 'z'; x++, id++) {
68         sprintf(part, "%c", x);
69         is_int(id, volutil_GetPartitionID(part), "\"%s\" is %d", part, id);
70     }
71     for (x = 'a'; x <= 'i'; x++) {
72         for (y = 'a'; y <= 'z'; y++, id++) {
73             sprintf(part, "%c%c", x, y);
74             is_int(id, volutil_GetPartitionID(part), "\"%s\" is %d", part, id);
75             if (x == 'i' && y == 'u')
76                 break;
77         }
78     }
79
80     /*
81      * Test conversion of /vicepXX to partition id,
82      * where XX is "a".."z", "aa"..."iu".
83      */
84     id = 0;
85     for (x = 'a'; x <= 'z'; x++, id++) {
86         sprintf(part, "/vicep%c", x);
87         is_int(id, volutil_GetPartitionID(part), "\"%s\" is %d", part, id);
88     }
89     for (x = 'a'; x <= 'i'; x++) {
90         for (y = 'a'; y <= 'z'; y++, id++) {
91             sprintf(part, "/vicep%c%c", x,y );
92             is_int(id, volutil_GetPartitionID(part), "\"%s\" is %d", part, id);
93             if (x == 'i' && y == 'u')
94                 break;
95         }
96     }
97
98     /*
99      * Test conversion of vicepXX to partition id,
100      * where XX is "a".."z", "aa"..."iu".
101      */
102     id = 0;
103     for (x = 'a'; x <= 'z'; x++, id++) {
104         sprintf(part, "vicep%c", x);
105         is_int(id, volutil_GetPartitionID(part), "\"%s\" is %d", part, id);
106     }
107     for (x = 'a'; x <= 'i'; x++) {
108         for (y = 'a'; y <= 'z'; y++, id++) {
109             sprintf(part, "vicep%c%c", x,y );
110             is_int(id, volutil_GetPartitionID(part), "\"%s\" is %d", part, id);
111             if (x == 'i' && y == 'u')
112                 break;
113         }
114     }
115
116     /*
117      * Test numeric names in the range "0".."254" are passed through.
118      */
119     for (id = 0; id <= 254; id++) {
120         sprintf(part, "%d", id);
121         is_int(id, volutil_GetPartitionID(part), "\"%s\" is %d", part, id);
122     }
123
124     /*
125      * Test out of range names.
126      */
127     for (t = out_of_range_names; *t != NULL; t++) {
128         is_int(-1, volutil_GetPartitionID(*t), "\"%s\" is out of range", *t);
129     }
130
131     /*
132      * Test invalid names.
133      */
134     for (t = invalid_names; *t != NULL; t++) {
135         is_int(-1, volutil_GetPartitionID(*t), "\"%s\" is invalid", *t);
136     }
137 }
138
139 void
140 test_partition_id_to_name(void)
141 {
142     char x, y;
143     int id;
144     char part[32];
145
146     /*
147      * Test conversion of ids to partition names,
148      * for ids of 0 to 254.
149      */
150     id = 0;
151     for (x = 'a'; x <= 'z'; x++, id++) {
152         sprintf(part, "/vicep%c", x);
153         is_string(part, volutil_PartitionName(id), "%d is \"%s\"", id, part);
154     }
155     for (x = 'a'; x <= 'i'; x++) {
156         for (y = 'a'; y <= 'z'; y++, id++) {
157             sprintf(part, "/vicep%c%c", x,y );
158             is_string(part, volutil_PartitionName(id), "%d is \"%s\"", id, part);
159             if (x == 'i' && y == 'u')
160                 break;
161         }
162     }
163
164     /* Test out of range values. */
165     is_string("BAD VOLUME ID", volutil_PartitionName(-1), "-1 is invalid");
166     is_string("BAD VOLUME ID", volutil_PartitionName(255), "255 is invalid");
167     is_string("BAD VOLUME ID", volutil_PartitionName(256), "256 is invalid");
168
169     /* Test buffer too short (thread safe variant of volutil_PartitionName). */
170     is_string("SPC", volutil_PartitionName_r(0, part, 4), "buffer too short");
171 }
172
173 int
174 main(int argc, char **argv)
175 {
176     plan(1293);
177     test_partition_name_to_id();
178     test_partition_id_to_name();
179     return 0;
180 }