1 /*
2  * libdpkg - Debian packaging suite library routines
3  * t-arch.c - test dpkg_arch implementation
4  *
5  * Copyright © 2011 Linaro Limited
6  * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
7  * Copyright © 2011-2014 Guillem Jover <guillem@debian.org>
8  *
9  * This is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
21  */
22 
23 #include <config.h>
24 #include <compat.h>
25 
26 #include <dpkg/test.h>
27 #include <dpkg/varbuf.h>
28 #include <dpkg/arch.h>
29 
30 static void
test_dpkg_arch_name_is_illegal(void)31 test_dpkg_arch_name_is_illegal(void)
32 {
33 	/* Test invalid architecture names. */
34 	test_fail(dpkg_arch_name_is_illegal("") == NULL);
35 	test_fail(dpkg_arch_name_is_illegal("-i386") == NULL);
36 	test_fail(dpkg_arch_name_is_illegal(" i386") == NULL);
37 	test_fail(dpkg_arch_name_is_illegal(":any") == NULL);
38 	test_fail(dpkg_arch_name_is_illegal("amd64_test") == NULL);
39 	test_fail(dpkg_arch_name_is_illegal("i386:test") == NULL);
40 	test_fail(dpkg_arch_name_is_illegal("i386 amd64") == NULL);
41 	test_fail(dpkg_arch_name_is_illegal("i386,amd64") == NULL);
42 	test_fail(dpkg_arch_name_is_illegal("i386|amd64") == NULL);
43 
44 	/* Test valid architecture names. */
45 	test_pass(dpkg_arch_name_is_illegal("i386") == NULL);
46 	test_pass(dpkg_arch_name_is_illegal("amd64") == NULL);
47 	test_pass(dpkg_arch_name_is_illegal("hurd-i386") == NULL);
48 	test_pass(dpkg_arch_name_is_illegal("kfreebsd-i386") == NULL);
49 	test_pass(dpkg_arch_name_is_illegal("kfreebsd-amd64") == NULL);
50 	test_pass(dpkg_arch_name_is_illegal("ia64") == NULL);
51 	test_pass(dpkg_arch_name_is_illegal("alpha") == NULL);
52 	test_pass(dpkg_arch_name_is_illegal("armel") == NULL);
53 	test_pass(dpkg_arch_name_is_illegal("hppa") == NULL);
54 	test_pass(dpkg_arch_name_is_illegal("mips") == NULL);
55 	test_pass(dpkg_arch_name_is_illegal("mipsel") == NULL);
56 	test_pass(dpkg_arch_name_is_illegal("powerpc") == NULL);
57 	test_pass(dpkg_arch_name_is_illegal("s390") == NULL);
58 	test_pass(dpkg_arch_name_is_illegal("sparc") == NULL);
59 }
60 
61 static void
test_dpkg_arch_get_list(void)62 test_dpkg_arch_get_list(void)
63 {
64 	struct dpkg_arch *arch;
65 	int count = 1;
66 
67 	/* Must never return NULL. */
68 	arch = dpkg_arch_get_list();
69 	test_alloc(arch);
70 
71 	while ((arch = arch->next))
72 		count++;
73 
74 	/* The default list should contain 3 architectures. */
75 	test_pass(count == 3);
76 }
77 
78 static void
test_dpkg_arch_find(void)79 test_dpkg_arch_find(void)
80 {
81 	struct dpkg_arch *arch;
82 
83 	/* Test existence and initial values of default architectures. */
84 	arch = dpkg_arch_find("all");
85 	test_pass(arch->type == DPKG_ARCH_ALL);
86 	test_pass(dpkg_arch_get(DPKG_ARCH_ALL) == arch);
87 	arch = dpkg_arch_find(ARCHITECTURE);
88 	test_pass(arch->type == DPKG_ARCH_NATIVE);
89 	test_pass(dpkg_arch_get(DPKG_ARCH_NATIVE) == arch);
90 	arch = dpkg_arch_find("any");
91 	test_pass(arch->type == DPKG_ARCH_WILDCARD);
92 	test_pass(dpkg_arch_get(DPKG_ARCH_WILDCARD) == arch);
93 
94 	/* Test missing architecture. */
95 	arch = dpkg_arch_find(NULL);
96 	test_pass(arch->type == DPKG_ARCH_NONE);
97 	test_pass(dpkg_arch_get(DPKG_ARCH_NONE) == arch);
98 	test_str(arch->name, ==, "");
99 
100 	/* Test empty architectures. */
101 	arch = dpkg_arch_find("");
102 	test_pass(arch->type == DPKG_ARCH_EMPTY);
103 	test_pass(dpkg_arch_get(DPKG_ARCH_EMPTY) == arch);
104 	test_str(arch->name, ==, "");
105 
106 	/* Test for an unknown type. */
107 	test_pass(dpkg_arch_get(1000) == NULL);
108 
109 	/* New valid architectures are marked unknown. */
110 	arch = dpkg_arch_find("foobar");
111 	test_pass(arch->type == DPKG_ARCH_UNKNOWN);
112 	test_str(arch->name, ==, "foobar");
113 
114 	/* New illegal architectures are marked illegal. */
115 	arch = dpkg_arch_find("a:b");
116 	test_pass(arch->type == DPKG_ARCH_ILLEGAL);
117 	test_str(arch->name, ==, "a:b");
118 }
119 
120 static void
test_dpkg_arch_reset_list(void)121 test_dpkg_arch_reset_list(void)
122 {
123 	dpkg_arch_reset_list();
124 
125 	test_dpkg_arch_get_list();
126 }
127 
128 static void
test_dpkg_arch_modify(void)129 test_dpkg_arch_modify(void)
130 {
131 	struct dpkg_arch *arch;
132 
133 	dpkg_arch_reset_list();
134 
135 	/* Insert a new unknown arch. */
136 	arch = dpkg_arch_find("foo");
137 	test_pass(arch->type == DPKG_ARCH_UNKNOWN);
138 	test_str(arch->name, ==, "foo");
139 
140 	/* Check that existing unknown arch gets tagged. */
141 	arch = dpkg_arch_add("foo");
142 	test_pass(arch->type == DPKG_ARCH_FOREIGN);
143 	test_str(arch->name, ==, "foo");
144 
145 	/* Check that new unknown arch gets tagged. */
146 	arch = dpkg_arch_add("quux");
147 	test_pass(arch->type == DPKG_ARCH_FOREIGN);
148 	test_str(arch->name, ==, "quux");
149 
150 	/* Unmark foreign architectures. */
151 
152 	arch = dpkg_arch_find("foo");
153 	dpkg_arch_unmark(arch);
154 	test_pass(arch->type == DPKG_ARCH_UNKNOWN);
155 
156 	arch = dpkg_arch_find("bar");
157 	dpkg_arch_unmark(arch);
158 	test_pass(arch->type == DPKG_ARCH_UNKNOWN);
159 
160 	arch = dpkg_arch_find("quux");
161 	dpkg_arch_unmark(arch);
162 	test_pass(arch->type == DPKG_ARCH_UNKNOWN);
163 }
164 
165 static void
test_dpkg_arch_varbuf_archqual(void)166 test_dpkg_arch_varbuf_archqual(void)
167 {
168 	struct varbuf vb = VARBUF_INIT;
169 
170 	varbuf_add_archqual(&vb, dpkg_arch_get(DPKG_ARCH_NONE));
171 	varbuf_end_str(&vb);
172 	test_str(vb.buf, ==, "");
173 	varbuf_reset(&vb);
174 
175 	varbuf_add_archqual(&vb, dpkg_arch_get(DPKG_ARCH_EMPTY));
176 	varbuf_end_str(&vb);
177 	test_str(vb.buf, ==, "");
178 	varbuf_reset(&vb);
179 
180 	varbuf_add_archqual(&vb, dpkg_arch_get(DPKG_ARCH_ALL));
181 	varbuf_end_str(&vb);
182 	test_str(vb.buf, ==, ":all");
183 	varbuf_reset(&vb);
184 
185 	varbuf_add_archqual(&vb, dpkg_arch_get(DPKG_ARCH_WILDCARD));
186 	varbuf_end_str(&vb);
187 	test_str(vb.buf, ==, ":any");
188 	varbuf_reset(&vb);
189 }
190 
191 static void
test_dpkg_arch_describe(void)192 test_dpkg_arch_describe(void)
193 {
194 	struct dpkg_arch *arch;
195 
196 	arch = dpkg_arch_get(DPKG_ARCH_NONE);
197 	test_str(dpkg_arch_describe(arch), ==, "<none>");
198 
199 	arch = dpkg_arch_get(DPKG_ARCH_EMPTY);
200 	test_str(dpkg_arch_describe(arch), ==, "<empty>");
201 
202 	arch = dpkg_arch_get(DPKG_ARCH_ALL);
203 	test_str(dpkg_arch_describe(arch), ==, "all");
204 
205 	arch = dpkg_arch_get(DPKG_ARCH_WILDCARD);
206 	test_str(dpkg_arch_describe(arch), ==, "any");
207 
208 	arch = dpkg_arch_get(DPKG_ARCH_NATIVE);
209 	test_str(dpkg_arch_describe(arch), ==, ARCHITECTURE);
210 }
211 
TEST_ENTRY(test)212 TEST_ENTRY(test)
213 {
214 	test_plan(60);
215 
216 	test_dpkg_arch_name_is_illegal();
217 	test_dpkg_arch_get_list();
218 	test_dpkg_arch_find();
219 	test_dpkg_arch_reset_list();
220 	test_dpkg_arch_modify();
221 	test_dpkg_arch_varbuf_archqual();
222 	test_dpkg_arch_describe();
223 }
224