Skip to content

Commit 9a9dc9b

Browse files
committed
multipath-tools tests: adapations for cmocka 2.0
cmocka 2.0 has deprecated a number of previously defined functions and macros, and introduced others instead. Adapt our tests to work with both cmocka 1.1.x and 2.0.x. Signed-off-by: Martin Wilck <mwilck@suse.com>
1 parent dd853c3 commit 9a9dc9b

22 files changed

Lines changed: 375 additions & 344 deletions

tests/alias.c

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include <stdint.h>
33
#include <setjmp.h>
44
#include <stdio.h>
5-
#include <cmocka.h>
5+
#include "cmocka-compat.h"
66
#include "strbuf.h"
77
#include "util.h"
88
#include "alias.h"
@@ -59,9 +59,9 @@ ssize_t __wrap_write(int fd, const void *buf, size_t count)
5959
start = (const char *)buf + sizeof(BINDINGS_FILE_HEADER) - 1;
6060
else
6161
start = buf;
62-
binding = mock_ptr_type(char *);
62+
binding = mock_ptr_type(const char *);
6363
start = strstr(start, binding);
64-
check_expected(count);
64+
check_expected_uint(count);
6565
assert_ptr_not_equal(start, NULL);
6666
return set_errno__(mock_type(int));
6767
}
@@ -80,12 +80,12 @@ int __wrap_dm_get_wwid(const char *name, char *uuid, int uuid_len)
8080
{
8181
int ret;
8282

83-
check_expected(name);
84-
check_expected(uuid_len);
83+
check_expected_ptr(name);
84+
check_expected_int(uuid_len);
8585
assert_non_null(uuid);
8686
ret = mock_type(int);
8787
if (ret == DMP_OK)
88-
strcpy(uuid, mock_ptr_type(char *));
88+
strcpy(uuid, mock_ptr_type(const char *));
8989
return ret;
9090
}
9191

@@ -389,7 +389,7 @@ static void sd_fd_many(void **state)
389389

390390
for (i = 1; i < 5000; i++) {
391391
rc = format_devname__(buf, i, sizeof(buf), "MPATH");
392-
assert_in_range(rc, 6, 8);
392+
assert_int_in_range(rc, 6, 8);
393393
rc = scan_devname(buf, "MPATH");
394394
assert_int_equal(rc, i);
395395
}
@@ -404,7 +404,7 @@ static void sd_fd_random(void **state)
404404
for (i = 1; i < 1000; i++) {
405405
n = random() & 0xffff;
406406
rc = format_devname__(buf, n, sizeof(buf), "MPATH");
407-
assert_in_range(rc, 6, 9);
407+
assert_int_in_range(rc, 6, 9);
408408
rc = scan_devname(buf, "MPATH");
409409
assert_int_equal(rc, n);
410410
}
@@ -437,14 +437,14 @@ static int test_scan_devname(void)
437437
static void mock_unused_alias(const char *alias)
438438
{
439439
expect_string(__wrap_dm_get_wwid, name, alias);
440-
expect_value(__wrap_dm_get_wwid, uuid_len, WWID_SIZE);
440+
expect_int_value(__wrap_dm_get_wwid, uuid_len, WWID_SIZE);
441441
will_return(__wrap_dm_get_wwid, DMP_NOT_FOUND);
442442
}
443443

444444
static void mock_self_alias(const char *alias, const char *wwid)
445445
{
446446
expect_string(__wrap_dm_get_wwid, name, alias);
447-
expect_value(__wrap_dm_get_wwid, uuid_len, WWID_SIZE);
447+
expect_int_value(__wrap_dm_get_wwid, uuid_len, WWID_SIZE);
448448
will_return(__wrap_dm_get_wwid, DMP_OK);
449449
will_return(__wrap_dm_get_wwid, wwid);
450450
}
@@ -470,14 +470,14 @@ static void mock_self_alias(const char *alias, const char *wwid)
470470
#define mock_failed_alias(alias, wwid) \
471471
do { \
472472
expect_string(__wrap_dm_get_wwid, name, alias); \
473-
expect_value(__wrap_dm_get_wwid, uuid_len, WWID_SIZE); \
473+
expect_int_value(__wrap_dm_get_wwid, uuid_len, WWID_SIZE); \
474474
will_return(__wrap_dm_get_wwid, DMP_NOT_FOUND); \
475475
} while (0)
476476

477477
#define mock_used_alias(alias, wwid) \
478478
do { \
479479
expect_string(__wrap_dm_get_wwid, name, alias); \
480-
expect_value(__wrap_dm_get_wwid, uuid_len, WWID_SIZE); \
480+
expect_int_value(__wrap_dm_get_wwid, uuid_len, WWID_SIZE); \
481481
will_return(__wrap_dm_get_wwid, DMP_OK); \
482482
will_return(__wrap_dm_get_wwid, "WWID_USED"); \
483483
expect_condlog(3, USED_STR(alias, wwid)); \
@@ -504,7 +504,7 @@ static void mock_bindings_file__(const char *content, bool conflict_ok)
504504
continue;
505505

506506
rc = add_binding(&global_bindings, alias, wwid);
507-
assert_in_set(rc, values, conflict_ok ? 2 : 1);
507+
assert_int_in_set(rc, values, (conflict_ok ? 2 : 1));
508508
}
509509
}
510510

@@ -1260,7 +1260,7 @@ static void al_a(void **state)
12601260
static const char ln[] = "MPATHa WWIDa\n";
12611261
char *alias;
12621262

1263-
expect_value(__wrap_write, count, strlen(BINDINGS_FILE_HEADER) + strlen(ln));
1263+
expect_uint_value(__wrap_write, count, strlen(BINDINGS_FILE_HEADER) + strlen(ln));
12641264
will_return(__wrap_write, ln);
12651265
will_return(__wrap_write, strlen(BINDINGS_FILE_HEADER) + strlen(ln));
12661266
will_return(__wrap_rename, 0);
@@ -1279,7 +1279,7 @@ static void al_zz(void **state)
12791279
static const char ln[] = "MPATHzz WWIDzz\n";
12801280
char *alias;
12811281

1282-
expect_value(__wrap_write, count, strlen(BINDINGS_FILE_HEADER) + strlen(ln));
1282+
expect_uint_value(__wrap_write, count, strlen(BINDINGS_FILE_HEADER) + strlen(ln));
12831283
will_return(__wrap_write, ln);
12841284
will_return(__wrap_write, strlen(BINDINGS_FILE_HEADER) + strlen(ln));
12851285
will_return(__wrap_rename, 0);
@@ -1318,10 +1318,10 @@ static void al_write_partial(void **state)
13181318
static const char ln[] = "MPATHa WWIDa\n";
13191319
char *alias;
13201320

1321-
expect_value(__wrap_write, count, strlen(BINDINGS_FILE_HEADER) + strlen(ln));
1321+
expect_uint_value(__wrap_write, count, strlen(BINDINGS_FILE_HEADER) + strlen(ln));
13221322
will_return(__wrap_write, ln);
13231323
will_return(__wrap_write, strlen(BINDINGS_FILE_HEADER) + strlen(ln) - 1);
1324-
expect_value(__wrap_write, count, 1);
1324+
expect_uint_value(__wrap_write, count, 1);
13251325
will_return(__wrap_write, ln + sizeof(ln) - 2);
13261326
will_return(__wrap_write, 1);
13271327
will_return(__wrap_rename, 0);
@@ -1340,10 +1340,10 @@ static void al_write_short(void **state)
13401340
static const char ln[] = "MPATHa WWIDa\n";
13411341
char *alias;
13421342

1343-
expect_value(__wrap_write, count, strlen(BINDINGS_FILE_HEADER) + strlen(ln));
1343+
expect_uint_value(__wrap_write, count, strlen(BINDINGS_FILE_HEADER) + strlen(ln));
13441344
will_return(__wrap_write, ln);
13451345
will_return(__wrap_write, strlen(BINDINGS_FILE_HEADER) + strlen(ln) - 1);
1346-
expect_value(__wrap_write, count, 1);
1346+
expect_uint_value(__wrap_write, count, 1);
13471347
will_return(__wrap_write, ln + sizeof(ln) - 2);
13481348
will_return(__wrap_write, 0);
13491349
expect_condlog(2, "write_bindings_file: short write");
@@ -1360,7 +1360,7 @@ static void al_write_err(void **state)
13601360
static const char ln[] = "MPATHa WWIDa\n";
13611361
char *alias;
13621362

1363-
expect_value(__wrap_write, count, strlen(BINDINGS_FILE_HEADER) + strlen(ln));
1363+
expect_uint_value(__wrap_write, count, strlen(BINDINGS_FILE_HEADER) + strlen(ln));
13641364
will_return(__wrap_write, ln);
13651365
will_return(__wrap_write, -EPERM);
13661366
expect_condlog(1, "failed to write new bindings file");
@@ -1376,7 +1376,7 @@ static void al_rename_err(void **state)
13761376
static const char ln[] = "MPATHa WWIDa\n";
13771377
char *alias;
13781378

1379-
expect_value(__wrap_write, count, strlen(BINDINGS_FILE_HEADER) + strlen(ln));
1379+
expect_uint_value(__wrap_write, count, strlen(BINDINGS_FILE_HEADER) + strlen(ln));
13801380
will_return(__wrap_write, ln);
13811381
will_return(__wrap_write, strlen(BINDINGS_FILE_HEADER) + strlen(ln));
13821382
will_return(__wrap_rename, -EROFS);
@@ -1408,7 +1408,7 @@ static int test_allocate_binding(void)
14081408
do { \
14091409
static const char ln[] = BINDING_STR(alias, wwid); \
14101410
\
1411-
expect_value(__wrap_write, count, \
1411+
expect_uint_value(__wrap_write, count, \
14121412
strlen(BINDINGS_FILE_HEADER) + (len) + strlen(ln)); \
14131413
will_return(__wrap_write, ln); \
14141414
will_return(__wrap_write, \

tests/blacklist.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <stdarg.h>
66
#include <stddef.h>
77
#include <setjmp.h>
8-
#include <cmocka.h>
8+
#include "cmocka-compat.h"
99
#include "globals.c"
1010
#include "blacklist.h"
1111
#include "test-log.h"

tests/cli.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <stddef.h>
99
#include <setjmp.h>
1010
#include <stdlib.h>
11-
#include <cmocka.h>
11+
#include "cmocka-compat.h"
1212

1313
#include <errno.h>
1414

@@ -49,7 +49,7 @@ static void client_test_##NAME(void **state) \
4949
assert_int_equal(get_cmdvec(cmd, &v, false), R); \
5050
if (R == 0) { \
5151
assert_ptr_not_equal(v, NULL); \
52-
assert_int_equal(fingerprint(v), FPR); \
52+
assert_uint_equal(fingerprint(v), FPR); \
5353
if (GOOD) \
5454
assert_ptr_not_equal(find_handler_for_cmdvec(v), NULL); \
5555
else \
@@ -73,7 +73,7 @@ static void client_param_##NAME(void **state) \
7373
\
7474
assert_int_equal(get_cmdvec(cmd, &v, false), 0); \
7575
assert_ptr_not_equal(v, NULL); \
76-
assert_int_equal(fingerprint(v), FPR); \
76+
assert_uint_equal(fingerprint(v), FPR); \
7777
assert_ptr_not_equal(find_handler_for_cmdvec(v), NULL); \
7878
assert_string_equal(((struct key *)VECTOR_SLOT(v, 1))->param, PAR); \
7979
free_keys(v); \
@@ -95,7 +95,7 @@ static void client_2param_##NAME(void **state) \
9595
\
9696
assert_int_equal(get_cmdvec(cmd, &v, false), 0); \
9797
assert_ptr_not_equal(v, NULL); \
98-
assert_int_equal(fingerprint(v), FPR); \
98+
assert_uint_equal(fingerprint(v), FPR); \
9999
assert_ptr_not_equal(find_handler_for_cmdvec(v), NULL); \
100100
assert_string_equal(((struct key *)VECTOR_SLOT(v, 1))->param, PAR1); \
101101
assert_string_equal(((struct key *)VECTOR_SLOT(v, N))->param, PARN); \

tests/cmocka-compat.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Copyright (c) 2015 Martin Wilck, SUSE LLC
4+
*/
5+
#include <cmocka.h>
6+
7+
#if CMOCKA_VERSION < 0x020000
8+
#define assert_int_in_range(x, low, high) assert_in_range(x, low, high)
9+
#define assert_uint_in_range(x, low, high) assert_in_range(x, low, high)
10+
#define assert_uint_equal(x, y) assert_int_equal(x, y)
11+
#define check_expected_int(x) check_expected(x)
12+
#define check_expected_uint(x) check_expected(x)
13+
#define check_expected_ptr(x) check_expected(x)
14+
#define expect_int_value(f, x, y) expect_value(f, x, y)
15+
#define expect_uint_value(f, x, y) expect_value(f, x, y)
16+
#define assert_int_in_set(x, vals, count) assert_in_set(x, vals, count)
17+
#endif

tests/devt.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <stddef.h>
99
#include <setjmp.h>
1010
#include <stdlib.h>
11-
#include <cmocka.h>
11+
#include "cmocka-compat.h"
1212
#include "mt-udev-wrap.h"
1313
#include <sys/sysmacros.h>
1414
#include <fcntl.h>
@@ -173,7 +173,7 @@ static void test_devt2devname_real(void **state)
173173
enm = udev_enumerate_new(udev);
174174
assert_non_null(enm);
175175
r = udev_enumerate_add_match_subsystem(enm, "block");
176-
assert_in_range(r, 0, INT_MAX);
176+
assert_int_in_range(r, 0, INT_MAX);
177177
r = udev_enumerate_scan_devices(enm);
178178
first = udev_enumerate_get_list_entry(enm);
179179
udev_list_entry_foreach(item, first) {

tests/directio.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <stddef.h>
1111
#include <setjmp.h>
1212
#include <stdlib.h>
13-
#include <cmocka.h>
13+
#include "cmocka-compat.h"
1414
#include "wrap64.h"
1515
#include "globals.c"
1616
#include "../libmultipath/checkers/directio.c"
@@ -38,7 +38,7 @@ int WRAP_IOCTL(int fd, ioctl_request_t request, void *argp)
3838
int *blocksize = (int *)argp;
3939

4040
if (test_dev) {
41-
mock_type(int);
41+
(void)mock_type(int);
4242
return REAL_IOCTL(fd, request, argp);
4343
}
4444

@@ -51,7 +51,12 @@ int WRAP_IOCTL(int fd, ioctl_request_t request, void *argp)
5151
* BLKSZGET must be cast to "int" and back to "unsigned long",
5252
* otherwise the assertion below will fail.
5353
*/
54+
#ifdef __GLIBC__
55+
assert_uint_equal(request, (ioctl_request_t)BLKBSZGET);
56+
#else
5457
assert_int_equal(request, (ioctl_request_t)BLKBSZGET);
58+
#endif
59+
5560
assert_non_null(blocksize);
5661
*blocksize = mock_type(int);
5762
return 0;
@@ -138,9 +143,9 @@ int WRAP_IO_GETEVENTS(io_context_t ctx, long min_nr, long nr,
138143
struct io_event *events, struct timespec *timeout)
139144
{
140145
int nr_evs;
141-
struct timespec *sleep_tmo;
146+
const struct timespec *sleep_tmo;
142147
int i;
143-
struct io_event *evs;
148+
const struct io_event *evs;
144149

145150
assert_non_null(timeout);
146151
nr_evs = mock_type(int);
@@ -149,8 +154,8 @@ int WRAP_IO_GETEVENTS(io_context_t ctx, long min_nr, long nr,
149154
return 0;
150155
if (test_dev) {
151156
int n = 0;
152-
mock_ptr_type(struct timespec *);
153-
mock_ptr_type(struct io_event *);
157+
(void)mock_ptr_type(const struct timespec *);
158+
(void)mock_ptr_type(const struct io_event *);
154159

155160
condlog(2, "min_nr = %ld nr_evs = %d", min_nr, nr_evs);
156161
while (n < nr_evs) {
@@ -160,7 +165,7 @@ int WRAP_IO_GETEVENTS(io_context_t ctx, long min_nr, long nr,
160165
}
161166
assert_int_equal(nr_evs, n);
162167
} else {
163-
sleep_tmo = mock_ptr_type(struct timespec *);
168+
sleep_tmo = mock_ptr_type(const struct timespec *);
164169
if (sleep_tmo) {
165170
if (sleep_tmo->tv_sec < 0)
166171
nanosleep(timeout, NULL);
@@ -171,7 +176,7 @@ int WRAP_IO_GETEVENTS(io_context_t ctx, long min_nr, long nr,
171176
errno = -nr_evs;
172177
return -1;
173178
}
174-
evs = mock_ptr_type(struct io_event *);
179+
evs = mock_ptr_type(const struct io_event *);
175180
for (i = 0; i < nr_evs; i++)
176181
events[i] = evs[i];
177182
}
@@ -259,7 +264,7 @@ static void do_libcheck_init(struct checker *c, int blocksize, int timeout,
259264
*req = ct->req;
260265
if (!test_dev)
261266
/* don't check fake blocksize on real devices */
262-
assert_int_equal(ct->req->blksize, blocksize);
267+
assert_uint_equal(ct->req->blksize, blocksize);
263268
}
264269

265270
static int is_checker_running(struct checker *c)

0 commit comments

Comments
 (0)