multipath tests: add blacklist tests
[multipath-tools/.git] / tests / blacklist.c
1 /*
2  * Copyright (c) 2018 Benjamin Marzinski, Redhat
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16  *
17  */
18 #include <stdarg.h>
19 #include <stddef.h>
20 #include <setjmp.h>
21 #include <cmocka.h>
22 #include "globals.c"
23 #include "blacklist.h"
24 #include "log.h"
25
26 struct udev_device {
27         const char *sysname;
28         char *property_list[];
29 };
30
31 const char *
32 __wrap_udev_device_get_sysname(struct udev_device *udev_device)
33 {
34         assert_non_null(udev_device);
35         assert_non_null(udev_device->sysname);
36         return udev_device->sysname;
37 }
38
39 struct udev_list_entry *
40 __wrap_udev_device_get_properties_list_entry(struct udev_device *udev_device)
41 {
42         assert_non_null(udev_device);
43         if (!udev_device->property_list)
44                 return NULL;
45         if (!*udev_device->property_list)
46                 return NULL;
47         return (struct udev_list_entry *)udev_device->property_list;
48 }
49
50 struct udev_list_entry *
51 __wrap_udev_list_entry_get_next(struct udev_list_entry *list_entry)
52 {
53         assert_non_null(list_entry);
54         if (!*((char **)list_entry + 1))
55                 return NULL;
56         return (struct udev_list_entry *)(((char **)list_entry) + 1);
57 }
58
59 const char *
60 __wrap_udev_list_entry_get_name(struct udev_list_entry *list_entry)
61 {
62         return *(const char **)list_entry;
63 }
64
65 void __wrap_dlog (int sink, int prio, const char * fmt, ...)
66 {
67         char buff[MAX_MSG_SIZE];
68         va_list ap;
69
70         assert_int_equal(prio, mock_type(int));
71         va_start(ap, fmt);
72         vsnprintf(buff, MAX_MSG_SIZE, fmt, ap);
73         va_end(ap);
74         assert_string_equal(buff, mock_ptr_type(char *));
75 }
76
77 void expect_condlog(int prio, char *string)
78 {
79         will_return(__wrap_dlog, prio);
80         will_return(__wrap_dlog, string);
81 }
82
83 vector blist_devnode_sdb;
84 vector blist_all;
85 vector blist_device_foo_bar;
86 vector blist_device_all;
87 vector blist_wwid_xyzzy;
88 vector blist_protocol_fcp;
89 vector blist_property_wwn;
90
91 static int setup(void **state)
92 {
93         blist_devnode_sdb = vector_alloc();
94         if (!blist_devnode_sdb ||
95             store_ble(blist_devnode_sdb, strdup("sdb"), ORIGIN_CONFIG))
96                 return -1;
97
98         blist_all = vector_alloc();
99         if (!blist_all || store_ble(blist_all, strdup(".*"), ORIGIN_CONFIG))
100                 return -1;
101
102         blist_device_foo_bar = vector_alloc();
103         if (!blist_device_foo_bar || alloc_ble_device(blist_device_foo_bar) ||
104             set_ble_device(blist_device_foo_bar, strdup("foo"), strdup("bar"),
105                            ORIGIN_CONFIG))
106                 return -1;
107
108         blist_device_all = vector_alloc();
109         if (!blist_device_all || alloc_ble_device(blist_device_all) ||
110             set_ble_device(blist_device_all, strdup(".*"), strdup(".*"),
111                            ORIGIN_CONFIG))
112                 return -1;
113
114         blist_wwid_xyzzy = vector_alloc();
115         if (!blist_wwid_xyzzy ||
116             store_ble(blist_wwid_xyzzy, strdup("xyzzy"), ORIGIN_CONFIG))
117                 return -1;
118
119         blist_protocol_fcp = vector_alloc();
120         if (!blist_protocol_fcp ||
121             store_ble(blist_protocol_fcp, strdup("scsi:fcp"), ORIGIN_CONFIG))
122                 return -1;
123
124         blist_property_wwn = vector_alloc();
125         if (!blist_property_wwn ||
126             store_ble(blist_property_wwn, strdup("ID_WWN"), ORIGIN_CONFIG))
127                 return -1;
128
129         return 0;
130 }
131
132 static int teardown(void **state)
133 {
134         free_blacklist(blist_devnode_sdb);
135         free_blacklist(blist_all);
136         free_blacklist_device(blist_device_foo_bar);
137         free_blacklist_device(blist_device_all);
138         free_blacklist(blist_wwid_xyzzy);
139         free_blacklist(blist_protocol_fcp);
140         free_blacklist(blist_property_wwn);
141         return 0;
142 }
143
144 static int reset_blists(void **state)
145 {
146         conf.blist_devnode = NULL;
147         conf.blist_wwid = NULL;
148         conf.blist_property = NULL;
149         conf.blist_protocol = NULL;
150         conf.blist_device = NULL;
151         conf.elist_devnode = NULL;
152         conf.elist_wwid = NULL;
153         conf.elist_property = NULL;
154         conf.elist_protocol = NULL;
155         conf.elist_device = NULL;
156         return 0;
157 }
158
159 static void test_devnode_blacklist(void **state)
160 {
161         expect_condlog(3, "sdb: device node name blacklisted\n");
162         assert_int_equal(filter_devnode(blist_devnode_sdb, NULL, "sdb"),
163                          MATCH_DEVNODE_BLIST);
164 }
165
166 static void test_devnode_whitelist(void **state)
167 {
168         expect_condlog(3, "sdb: device node name whitelisted\n");
169         assert_int_equal(filter_devnode(blist_all, blist_devnode_sdb, "sdb"),
170                          MATCH_DEVNODE_BLIST_EXCEPT);
171         expect_condlog(3, "sdc: device node name blacklisted\n");
172         assert_int_equal(filter_devnode(blist_all, blist_devnode_sdb, "sdc"),
173                          MATCH_DEVNODE_BLIST);
174 }
175
176 static void test_devnode_missing(void **state)
177 {
178         assert_int_equal(filter_devnode(blist_devnode_sdb, NULL, "sdc"),
179                          MATCH_NOTHING);
180 }
181
182 static void test_device_blacklist(void **state)
183 {
184         expect_condlog(3, "sdb: (foo:bar) vendor/product blacklisted\n");
185         assert_int_equal(filter_device(blist_device_foo_bar, NULL, "foo",
186                                        "bar", "sdb"),
187                          MATCH_DEVICE_BLIST);
188 }
189
190 static void test_device_whitelist(void **state)
191 {
192         expect_condlog(3, "sdb: (foo:bar) vendor/product whitelisted\n");
193         assert_int_equal(filter_device(blist_device_all, blist_device_foo_bar,
194                                        "foo", "bar", "sdb"),
195                          MATCH_DEVICE_BLIST_EXCEPT);
196         expect_condlog(3, "sdb: (foo:baz) vendor/product blacklisted\n");
197         assert_int_equal(filter_device(blist_device_all, blist_device_foo_bar,
198                                        "foo", "baz", "sdb"),
199                          MATCH_DEVICE_BLIST);
200 }
201
202 static void test_device_missing(void **state)
203 {
204         assert_int_equal(filter_device(blist_device_foo_bar, NULL, "foo",
205                                        "baz", "sdb"),
206                          MATCH_NOTHING);
207 }
208
209 static void test_wwid_blacklist(void **state)
210 {
211         expect_condlog(3, "sdb: wwid xyzzy blacklisted\n");
212         assert_int_equal(filter_wwid(blist_wwid_xyzzy, NULL, "xyzzy", "sdb"),
213                          MATCH_WWID_BLIST);
214 }
215
216 static void test_wwid_whitelist(void **state)
217 {
218         expect_condlog(3, "sdb: wwid xyzzy whitelisted\n");
219         assert_int_equal(filter_wwid(blist_all, blist_wwid_xyzzy,
220                                      "xyzzy", "sdb"),
221                          MATCH_WWID_BLIST_EXCEPT);
222         expect_condlog(3, "sdb: wwid plugh blacklisted\n");
223         assert_int_equal(filter_wwid(blist_all, blist_wwid_xyzzy,
224                                      "plugh", "sdb"),
225                          MATCH_WWID_BLIST);
226 }
227
228 static void test_wwid_missing(void **state)
229 {
230         assert_int_equal(filter_wwid(blist_wwid_xyzzy, NULL, "plugh", "sdb"),
231                          MATCH_NOTHING);
232 }
233
234 static void test_protocol_blacklist(void **state)
235 {
236         struct path pp = { .dev = "sdb", .bus = SYSFS_BUS_SCSI,
237                            .sg_id.proto_id = SCSI_PROTOCOL_FCP };
238         expect_condlog(3, "sdb: protocol scsi:fcp blacklisted\n");
239         assert_int_equal(filter_protocol(blist_protocol_fcp, NULL, &pp),
240                          MATCH_PROTOCOL_BLIST);
241 }
242
243 static void test_protocol_whitelist(void **state)
244 {
245         struct path pp1 = { .dev = "sdb", .bus = SYSFS_BUS_SCSI,
246                            .sg_id.proto_id = SCSI_PROTOCOL_FCP };
247         struct path pp2 = { .dev = "sdb", .bus = SYSFS_BUS_SCSI,
248                            .sg_id.proto_id = SCSI_PROTOCOL_ISCSI };
249         expect_condlog(3, "sdb: protocol scsi:fcp whitelisted\n");
250         assert_int_equal(filter_protocol(blist_all, blist_protocol_fcp, &pp1),
251                          MATCH_PROTOCOL_BLIST_EXCEPT);
252         expect_condlog(3, "sdb: protocol scsi:iscsi blacklisted\n");
253         assert_int_equal(filter_protocol(blist_all, blist_protocol_fcp, &pp2),
254                          MATCH_PROTOCOL_BLIST);
255 }
256
257 static void test_protocol_missing(void **state)
258 {
259         struct path pp = { .dev = "sdb", .bus = SYSFS_BUS_SCSI,
260                            .sg_id.proto_id = SCSI_PROTOCOL_ISCSI };
261         assert_int_equal(filter_protocol(blist_protocol_fcp, NULL, &pp),
262                          MATCH_NOTHING);
263 }
264
265 static void test_property_blacklist(void **state)
266 {
267         static struct udev_device udev = { "sdb", { "ID_FOO", "ID_WWN", "ID_BAR", NULL } };
268         conf.blist_property = blist_property_wwn;
269         expect_condlog(3, "sdb: udev property ID_WWN blacklisted\n");
270         assert_int_equal(filter_property(&conf, &udev), MATCH_PROPERTY_BLIST);
271 }
272
273 /* the property check works different in that you check all the property
274  * names, so setting a blacklist value will blacklist the device if any
275  * of the property on the blacklist are found before the property names
276  * in the whitelist.  This might be worth changing. although it would
277  * force multipath to go through the properties twice */
278 static void test_property_whitelist(void **state)
279 {
280         static struct udev_device udev = { "sdb", { "ID_FOO", "ID_WWN", "ID_BAR", NULL } };
281         conf.elist_property = blist_property_wwn;
282         expect_condlog(3, "sdb: udev property ID_WWN whitelisted\n");
283         assert_int_equal(filter_property(&conf, &udev),
284                          MATCH_PROPERTY_BLIST_EXCEPT);
285 }
286
287 static void test_property_missing(void **state)
288 {
289         static struct udev_device udev = { "sdb", { "ID_FOO", "ID_BAZ", "ID_BAR", NULL } };
290         conf.blist_property = blist_property_wwn;
291         expect_condlog(3, "sdb: blacklisted, udev property missing\n");
292         assert_int_equal(filter_property(&conf, &udev),
293                          MATCH_PROPERTY_BLIST_MISSING);
294 }
295
296 struct udev_device test_udev = { "sdb", { "ID_FOO", "ID_WWN", "ID_BAR", NULL } };
297
298 struct path test_pp = { .dev = "sdb", .bus = SYSFS_BUS_SCSI, .udev = &test_udev,
299                         .sg_id.proto_id = SCSI_PROTOCOL_FCP, .vendor_id = "foo",
300                         .product_id = "bar", .wwid = "xyzzy" };
301
302 static void test_filter_path_property(void **state)
303 {
304         conf.blist_property = blist_property_wwn;
305         expect_condlog(3, "sdb: udev property ID_WWN blacklisted\n");
306         assert_int_equal(filter_path(&conf, &test_pp), MATCH_PROPERTY_BLIST);
307 }
308
309 static void test_filter_path_devnode(void **state)
310 {
311         /* always must include property elist, to avoid "missing property"
312          * blacklisting */
313         conf.elist_property = blist_property_wwn;
314         conf.blist_devnode = blist_devnode_sdb;
315         expect_condlog(3, "sdb: udev property ID_WWN whitelisted\n");
316         expect_condlog(3, "sdb: device node name blacklisted\n");
317         assert_int_equal(filter_path(&conf, &test_pp), MATCH_DEVNODE_BLIST);
318 }
319
320 static void test_filter_path_device(void **state)
321 {
322         /* always must include property elist, to avoid "missing property"
323          * blacklisting */
324         conf.elist_property = blist_property_wwn;
325         conf.blist_device = blist_device_foo_bar;
326         expect_condlog(3, "sdb: udev property ID_WWN whitelisted\n");
327         expect_condlog(3, "sdb: (foo:bar) vendor/product blacklisted\n");
328         assert_int_equal(filter_path(&conf, &test_pp), MATCH_DEVICE_BLIST);
329 }
330
331 static void test_filter_path_protocol(void **state)
332 {
333         conf.elist_property = blist_property_wwn;
334         conf.blist_protocol = blist_protocol_fcp;
335         expect_condlog(3, "sdb: udev property ID_WWN whitelisted\n");
336         expect_condlog(3, "sdb: protocol scsi:fcp blacklisted\n");
337         assert_int_equal(filter_path(&conf, &test_pp), MATCH_PROTOCOL_BLIST);
338 }
339
340 static void test_filter_path_wwid(void **state)
341 {
342         conf.elist_property = blist_property_wwn;
343         conf.blist_wwid = blist_wwid_xyzzy;
344         expect_condlog(3, "sdb: udev property ID_WWN whitelisted\n");
345         expect_condlog(3, "sdb: wwid xyzzy blacklisted\n");
346         assert_int_equal(filter_path(&conf, &test_pp), MATCH_WWID_BLIST);
347 }
348
349 struct udev_device miss_udev = { "sdb", { "ID_FOO", "ID_BAZ", "ID_BAR", NULL } };
350
351 struct path miss1_pp = { .dev = "sdc", .bus = SYSFS_BUS_SCSI,
352                         .udev = &miss_udev,
353                         .sg_id.proto_id = SCSI_PROTOCOL_ISCSI,
354                         .vendor_id = "foo", .product_id = "baz",
355                         .wwid = "plugh" };
356
357 struct path miss2_pp = { .dev = "sdc", .bus = SYSFS_BUS_SCSI,
358                         .udev = &test_udev,
359                         .sg_id.proto_id = SCSI_PROTOCOL_ISCSI,
360                         .vendor_id = "foo", .product_id = "baz",
361                         .wwid = "plugh" };
362
363 static void test_filter_path_missing1(void **state)
364 {
365         conf.blist_property = blist_property_wwn;
366         conf.blist_devnode = blist_devnode_sdb;
367         conf.blist_device = blist_device_foo_bar;
368         conf.blist_protocol = blist_protocol_fcp;
369         conf.blist_wwid = blist_wwid_xyzzy;
370         expect_condlog(3, "sdb: blacklisted, udev property missing\n");
371         assert_int_equal(filter_path(&conf, &miss1_pp),
372                          MATCH_PROPERTY_BLIST_MISSING);
373 }
374
375 /* This one matches the property whitelist, to test the other missing
376  * functions */
377 static void test_filter_path_missing2(void **state)
378 {
379         conf.elist_property = blist_property_wwn;
380         conf.blist_devnode = blist_devnode_sdb;
381         conf.blist_device = blist_device_foo_bar;
382         conf.blist_protocol = blist_protocol_fcp;
383         conf.blist_wwid = blist_wwid_xyzzy;
384         expect_condlog(3, "sdb: udev property ID_WWN whitelisted\n");
385         assert_int_equal(filter_path(&conf, &miss2_pp),
386                          MATCH_NOTHING);
387 }
388
389 static void test_filter_path_whitelist(void **state)
390 {
391         conf.elist_property = blist_property_wwn;
392         conf.elist_devnode = blist_devnode_sdb;
393         conf.elist_device = blist_device_foo_bar;
394         conf.elist_protocol = blist_protocol_fcp;
395         conf.elist_wwid = blist_wwid_xyzzy;
396         expect_condlog(3, "sdb: udev property ID_WWN whitelisted\n");
397         expect_condlog(3, "sdb: device node name whitelisted\n");
398         expect_condlog(3, "sdb: (foo:bar) vendor/product whitelisted\n");
399         expect_condlog(3, "sdb: protocol scsi:fcp whitelisted\n");
400         expect_condlog(3, "sdb: wwid xyzzy whitelisted\n");
401         assert_int_equal(filter_path(&conf, &test_pp),
402                          MATCH_WWID_BLIST_EXCEPT);
403 }
404
405 static void test_filter_path_whitelist_property(void **state)
406 {
407         conf.blist_property = blist_property_wwn;
408         conf.elist_devnode = blist_devnode_sdb;
409         conf.elist_device = blist_device_foo_bar;
410         conf.elist_protocol = blist_protocol_fcp;
411         conf.elist_wwid = blist_wwid_xyzzy;
412         expect_condlog(3, "sdb: udev property ID_WWN blacklisted\n");
413         assert_int_equal(filter_path(&conf, &test_pp), MATCH_PROPERTY_BLIST);
414 }
415
416 static void test_filter_path_whitelist_devnode(void **state)
417 {
418         conf.elist_property = blist_property_wwn;
419         conf.blist_devnode = blist_devnode_sdb;
420         conf.elist_device = blist_device_foo_bar;
421         conf.elist_protocol = blist_protocol_fcp;
422         conf.elist_wwid = blist_wwid_xyzzy;
423         expect_condlog(3, "sdb: udev property ID_WWN whitelisted\n");
424         expect_condlog(3, "sdb: device node name blacklisted\n");
425         assert_int_equal(filter_path(&conf, &test_pp), MATCH_DEVNODE_BLIST);
426 }
427
428 static void test_filter_path_whitelist_device(void **state)
429 {
430         conf.elist_property = blist_property_wwn;
431         conf.elist_devnode = blist_devnode_sdb;
432         conf.blist_device = blist_device_foo_bar;
433         conf.elist_protocol = blist_protocol_fcp;
434         conf.elist_wwid = blist_wwid_xyzzy;
435         expect_condlog(3, "sdb: udev property ID_WWN whitelisted\n");
436         expect_condlog(3, "sdb: device node name whitelisted\n");
437         expect_condlog(3, "sdb: (foo:bar) vendor/product blacklisted\n");
438         assert_int_equal(filter_path(&conf, &test_pp), MATCH_DEVICE_BLIST);
439 }
440
441 static void test_filter_path_whitelist_protocol(void **state)
442 {
443         conf.elist_property = blist_property_wwn;
444         conf.elist_devnode = blist_devnode_sdb;
445         conf.elist_device = blist_device_foo_bar;
446         conf.blist_protocol = blist_protocol_fcp;
447         conf.elist_wwid = blist_wwid_xyzzy;
448         expect_condlog(3, "sdb: udev property ID_WWN whitelisted\n");
449         expect_condlog(3, "sdb: device node name whitelisted\n");
450         expect_condlog(3, "sdb: (foo:bar) vendor/product whitelisted\n");
451         expect_condlog(3, "sdb: protocol scsi:fcp blacklisted\n");
452         assert_int_equal(filter_path(&conf, &test_pp), MATCH_PROTOCOL_BLIST);
453 }
454
455 static void test_filter_path_whitelist_wwid(void **state)
456 {
457         conf.elist_property = blist_property_wwn;
458         conf.elist_devnode = blist_devnode_sdb;
459         conf.elist_device = blist_device_foo_bar;
460         conf.elist_protocol = blist_protocol_fcp;
461         conf.blist_wwid = blist_wwid_xyzzy;
462         expect_condlog(3, "sdb: udev property ID_WWN whitelisted\n");
463         expect_condlog(3, "sdb: device node name whitelisted\n");
464         expect_condlog(3, "sdb: (foo:bar) vendor/product whitelisted\n");
465         expect_condlog(3, "sdb: protocol scsi:fcp whitelisted\n");
466         expect_condlog(3, "sdb: wwid xyzzy blacklisted\n");
467         assert_int_equal(filter_path(&conf, &test_pp), MATCH_WWID_BLIST);
468 }
469
470 #define test_and_reset(x) cmocka_unit_test_teardown((x), reset_blists)
471
472 int test_blacklist(void)
473 {
474         const struct CMUnitTest tests[] = {
475                 cmocka_unit_test(test_devnode_blacklist),
476                 cmocka_unit_test(test_devnode_whitelist),
477                 cmocka_unit_test(test_devnode_missing),
478                 cmocka_unit_test(test_device_blacklist),
479                 cmocka_unit_test(test_device_whitelist),
480                 cmocka_unit_test(test_device_missing),
481                 cmocka_unit_test(test_wwid_blacklist),
482                 cmocka_unit_test(test_wwid_whitelist),
483                 cmocka_unit_test(test_wwid_missing),
484                 cmocka_unit_test(test_protocol_blacklist),
485                 cmocka_unit_test(test_protocol_whitelist),
486                 cmocka_unit_test(test_protocol_missing),
487                 test_and_reset(test_property_blacklist),
488                 test_and_reset(test_property_whitelist),
489                 test_and_reset(test_property_missing),
490                 test_and_reset(test_filter_path_property),
491                 test_and_reset(test_filter_path_devnode),
492                 test_and_reset(test_filter_path_device),
493                 test_and_reset(test_filter_path_protocol),
494                 test_and_reset(test_filter_path_wwid),
495                 test_and_reset(test_filter_path_missing1),
496                 test_and_reset(test_filter_path_missing2),
497                 test_and_reset(test_filter_path_whitelist),
498                 test_and_reset(test_filter_path_whitelist_property),
499                 test_and_reset(test_filter_path_whitelist_devnode),
500                 test_and_reset(test_filter_path_whitelist_device),
501                 test_and_reset(test_filter_path_whitelist_protocol),
502                 test_and_reset(test_filter_path_whitelist_wwid),
503         };
504         return cmocka_run_group_tests(tests, setup, teardown);
505 }
506
507 int main(void)
508 {
509         int ret = 0;
510         ret += test_blacklist();
511         return ret;
512 }