1 /*
2  * libdpkg - Debian packaging suite library routines
3  * t-pkg-hash.c - test pkg-hash implementation
4  *
5  * Copyright © 2018 Guillem Jover <guillem@debian.org>
6  *
7  * This is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  */
20 
21 #include <config.h>
22 #include <compat.h>
23 
24 #include <dpkg/test.h>
25 #include <dpkg/dpkg.h>
26 #include <dpkg/dpkg-db.h>
27 #include <dpkg/pkg.h>
28 
29 static void
test_pkg_hash(void)30 test_pkg_hash(void)
31 {
32 	struct dpkg_arch *arch;
33 	struct pkgset *set;
34 	struct pkginfo *pkg;
35 	struct pkg_hash_iter *iter;
36 	int pkginstance;
37 
38 	test_pass(pkg_hash_count_set() == 0);
39 	test_pass(pkg_hash_count_pkg() == 0);
40 
41 	set = pkg_hash_find_set("pkg-aa");
42 	test_pass(set != NULL);
43 	test_str(set->name, ==, "pkg-aa");
44 	test_pass(pkg_hash_count_set() == 1);
45 	test_pass(pkg_hash_count_pkg() == 1);
46 
47 	set = pkg_hash_find_set("pkg-aa");
48 	test_pass(set != NULL);
49 	test_str(set->name, ==, "pkg-aa");
50 	test_pass(pkg_hash_count_set() == 1);
51 	test_pass(pkg_hash_count_pkg() == 1);
52 
53 	set = pkg_hash_find_set("Pkg-AA");
54 	test_pass(set != NULL);
55 	test_str(set->name, ==, "pkg-aa");
56 	test_pass(pkg_hash_count_set() == 1);
57 	test_pass(pkg_hash_count_pkg() == 1);
58 
59 	set = pkg_hash_find_set("pkg-bb");
60 	pkg_set_status(&set->pkg, PKG_STAT_INSTALLED);
61 	test_pass(set != NULL);
62 	test_str(set->name, ==, "pkg-bb");
63 	test_pass(pkg_hash_count_set() == 2);
64 	test_pass(pkg_hash_count_pkg() == 2);
65 
66 	set = pkg_hash_find_set("pkg-cc");
67 	test_pass(set != NULL);
68 	test_str(set->name, ==, "pkg-cc");
69 	test_pass(pkg_hash_count_set() == 3);
70 	test_pass(pkg_hash_count_pkg() == 3);
71 
72 	arch = dpkg_arch_find("arch-xx");
73 	pkg = pkg_hash_find_pkg("pkg-aa", arch);
74 	pkg_set_status(pkg, PKG_STAT_INSTALLED);
75 	test_pass(pkg != NULL);
76 	test_str(pkg->set->name, ==, "pkg-aa");
77 	test_str(pkg->installed.arch->name, ==, "arch-xx");
78 	test_str(pkg->available.arch->name, ==, "arch-xx");
79 	test_pass(pkg_hash_count_set() == 3);
80 	test_pass(pkg_hash_count_pkg() == 3);
81 
82 	arch = dpkg_arch_find("arch-yy");
83 	pkg = pkg_hash_find_pkg("pkg-aa", arch);
84 	test_pass(pkg != NULL);
85 	test_str(pkg->set->name, ==, "pkg-aa");
86 	test_str(pkg->installed.arch->name, ==, "arch-yy");
87 	test_str(pkg->available.arch->name, ==, "arch-yy");
88 	test_pass(pkg_hash_count_set() == 3);
89 	test_pass(pkg_hash_count_pkg() == 4);
90 
91 	arch = dpkg_arch_find("arch-zz");
92 	pkg = pkg_hash_find_pkg("pkg-aa", arch);
93 	pkg_set_status(pkg, PKG_STAT_UNPACKED);
94 	test_pass(pkg != NULL);
95 	test_str(pkg->set->name, ==, "pkg-aa");
96 	test_str(pkg->installed.arch->name, ==, "arch-zz");
97 	test_str(pkg->available.arch->name, ==, "arch-zz");
98 	test_pass(pkg_hash_count_set() == 3);
99 	test_pass(pkg_hash_count_pkg() == 5);
100 
101 	arch = dpkg_arch_find("arch-xx");
102 	pkg = pkg_hash_find_pkg("pkg-aa", arch);
103 	test_pass(pkg != NULL);
104 	test_str(pkg->set->name, ==, "pkg-aa");
105 	test_str(pkg->installed.arch->name, ==, "arch-xx");
106 	test_str(pkg->available.arch->name, ==, "arch-xx");
107 	test_pass(pkg_hash_count_set() == 3);
108 	test_pass(pkg_hash_count_pkg() == 5);
109 
110 	set = pkg_hash_find_set("pkg-aa");
111 	test_str(set->name, ==, "pkg-aa");
112 	pkg = pkg_hash_get_singleton(set);
113 	test_pass(pkg == NULL);
114 	test_pass(pkg_hash_count_set() == 3);
115 	test_pass(pkg_hash_count_pkg() == 5);
116 
117 	pkg = pkg_hash_find_singleton("pkg-bb");
118 	test_pass(pkg != NULL);
119 	test_str(pkg->set->name, ==, "pkg-bb");
120 	test_pass(pkg_hash_count_set() == 3);
121 	test_pass(pkg_hash_count_pkg() == 5);
122 
123 	pkg = pkg_hash_find_singleton("pkg-cc");
124 	test_pass(pkg != NULL);
125 	test_str(pkg->set->name, ==, "pkg-cc");
126 	test_pass(pkg_hash_count_set() == 3);
127 	test_pass(pkg_hash_count_pkg() == 5);
128 
129 	iter = pkg_hash_iter_new();
130 	while ((set = pkg_hash_iter_next_set(iter))) {
131 		if (strcmp(set->name, "pkg-aa") == 0)
132 			test_str(set->name, ==, "pkg-aa");
133 		else if (strcmp(set->name, "pkg-bb") == 0)
134 			test_str(set->name, ==, "pkg-bb");
135 		else if (strcmp(set->name, "pkg-cc") == 0)
136 			test_str(set->name, ==, "pkg-cc");
137 		else
138 			test_fail("unknown fsys_namenode");
139 	}
140 	pkg_hash_iter_free(iter);
141 
142 	pkginstance = 0;
143 	iter = pkg_hash_iter_new();
144 	while ((pkg = pkg_hash_iter_next_pkg(iter))) {
145 		pkginstance++;
146 		if (strcmp(pkg->set->name, "pkg-aa") == 0) {
147 			struct pkgbin *pkgbin = &pkg->installed;
148 
149 			test_str(pkg->set->name, ==, "pkg-aa");
150 			if (strcmp(pkgbin->arch->name, "arch-xx") == 0)
151 				test_str(pkgbin->arch->name, ==, "arch-xx");
152 			else if (strcmp(pkgbin->arch->name, "arch-yy") == 0)
153 				test_str(pkgbin->arch->name, ==, "arch-yy");
154 			else if (strcmp(pkgbin->arch->name, "arch-zz") == 0)
155 				test_str(pkgbin->arch->name, ==, "arch-zz");
156 			else
157 				test_fail("unknown pkginfo instance");
158 		} else if (strcmp(pkg->set->name, "pkg-bb") == 0) {
159 			test_str(pkg->set->name, ==, "pkg-bb");
160 		} else if (strcmp(pkg->set->name, "pkg-cc") == 0) {
161 			test_str(pkg->set->name, ==, "pkg-cc");
162 		} else {
163 			test_fail("unknown fsys_namenode");
164 		}
165 	}
166 	pkg_hash_iter_free(iter);
167 
168 	pkg_hash_reset();
169 	test_pass(pkg_hash_count_set() == 0);
170 	test_pass(pkg_hash_count_pkg() == 0);
171 }
172 
TEST_ENTRY(test)173 TEST_ENTRY(test)
174 {
175 	test_plan(71);
176 
177 	test_pkg_hash();
178 }
179