forked from opensvc/multipath-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsysfs.c
495 lines (424 loc) · 13.6 KB
/
sysfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
* Copyright (c) 2021 SUSE LLC
* SPDX-License-Identifier: GPL-2.0-only
*/
#define _GNU_SOURCE
#include <stdbool.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdlib.h>
#include <cmocka.h>
#include <libudev.h>
#include <fcntl.h>
#include <errno.h>
#include "debug.h"
#include "globals.c"
#include "test-log.h"
#include "sysfs.h"
#include "util.h"
#include "wrap64.h"
#define TEST_FD 123
char *__wrap_udev_device_get_syspath(struct udev_device *ud)
{
char *val = mock_ptr_type(char *);
return val;
}
int WRAP_FUNC(open)(const char *pathname, int flags)
{
int ret;
check_expected(pathname);
check_expected(flags);
ret = mock_type(int);
return ret;
}
ssize_t __wrap_read(int fd, void *buf, size_t count)
{
ssize_t ret;
char *val;
check_expected(fd);
check_expected(count);
ret = mock_type(int);
val = mock_ptr_type(char *);
if (ret >= (ssize_t)count)
ret = count;
if (ret >= 0 && val) {
fprintf(stderr, "%s: '%s' -> %zd\n", __func__, val, ret);
memcpy(buf, val, ret);
}
return ret;
}
ssize_t __wrap_write(int fd, void *buf, size_t count)
{
ssize_t ret;
check_expected(fd);
check_expected(count);
ret = mock_type(int);
if (ret >= (ssize_t)count)
ret = count;
return ret;
}
int __real_close(int fd);
int __wrap_close(int fd) {
if (fd != TEST_FD)
return __real_close(fd);
return mock_type(int);
}
static int setup(void **state)
{
udev = udev_new();
return 0;
}
static int teardown(void **state)
{
udev_unref(udev);
return 0;
}
static void expect_sagv_invalid(void)
{
expect_condlog(1, "__sysfs_attr_get_value: invalid parameters");
}
static void test_sagv_invalid(void **state)
{
expect_sagv_invalid();
assert_int_equal(sysfs_attr_get_value(NULL, NULL, NULL, 0), -EINVAL);
expect_sagv_invalid();
assert_int_equal(sysfs_bin_attr_get_value(NULL, NULL, NULL, 0), -EINVAL);
expect_sagv_invalid();
assert_int_equal(sysfs_attr_get_value(NULL, (void *)state, (void *)state, 1),
-EINVAL);
expect_sagv_invalid();
assert_int_equal(sysfs_bin_attr_get_value(NULL, (void *)state, (void *)state, 1),
-EINVAL);
expect_sagv_invalid();
assert_int_equal(sysfs_attr_get_value((void *)state, NULL, (void *)state, 1),
-EINVAL);
expect_sagv_invalid();
assert_int_equal(sysfs_bin_attr_get_value((void *)state, NULL, (void *)state, 1),
-EINVAL);
expect_sagv_invalid();
assert_int_equal(sysfs_attr_get_value((void *)state, (void *)state, NULL, 1),
-EINVAL);
expect_sagv_invalid();
assert_int_equal(sysfs_bin_attr_get_value((void *)state, (void *)state, NULL, 1),
-EINVAL);
expect_sagv_invalid();
assert_int_equal(sysfs_attr_get_value((void *)state, (void *)state,
(void *)state, 0), -EINVAL);
expect_sagv_invalid();
assert_int_equal(sysfs_bin_attr_get_value((void *)state, (void *)state,
(void *)state, 0), -EINVAL);
}
static void test_sagv_bad_udev(void **state)
{
will_return(__wrap_udev_device_get_syspath, NULL);
expect_condlog(3, "__sysfs_attr_get_value: invalid udevice");
assert_int_equal(sysfs_attr_get_value((void *)state, (void *)state,
(void *)state, 1), -EINVAL);
will_return(__wrap_udev_device_get_syspath, NULL);
expect_condlog(3, "__sysfs_attr_get_value: invalid udevice");
assert_int_equal(sysfs_bin_attr_get_value((void *)state, (void *)state,
(void *)state, 1), -EINVAL);
}
static void test_sagv_bad_snprintf(void **state)
{
char longstr[PATH_MAX + 1];
char buf[1];
memset(longstr, 'a', sizeof(longstr) - 1);
longstr[sizeof(longstr) - 1] = '\0';
will_return(__wrap_udev_device_get_syspath, "/foo");
expect_condlog(3, "__sysfs_attr_get_value: devpath overflow");
assert_int_equal(sysfs_attr_get_value((void *)state, longstr,
buf, sizeof(buf)), -EOVERFLOW);
will_return(__wrap_udev_device_get_syspath, "/foo");
expect_condlog(3, "__sysfs_attr_get_value: devpath overflow");
assert_int_equal(sysfs_bin_attr_get_value((void *)state, longstr,
(unsigned char *)buf, sizeof(buf)),
-EOVERFLOW);
}
static void test_sagv_open_fail(void **state)
{
char buf[1];
will_return(__wrap_udev_device_get_syspath, "/foo");
expect_condlog(4, "open '/foo/bar'");
expect_string(WRAP_FUNC(open), pathname, "/foo/bar");
expect_value(WRAP_FUNC(open), flags, O_RDONLY);
errno = ENOENT;
wrap_will_return(WRAP_FUNC(open), -1);
expect_condlog(3, "__sysfs_attr_get_value: attribute '/foo/bar' cannot be opened");
assert_int_equal(sysfs_attr_get_value((void *)state, "bar",
buf, sizeof(buf)), -ENOENT);
}
static void test_sagv_read_fail(void **state)
{
char buf[1];
will_return(__wrap_udev_device_get_syspath, "/foo");
expect_condlog(4, "open '/foo/bar'");
expect_string(WRAP_FUNC(open), pathname, "/foo/bar");
expect_value(WRAP_FUNC(open), flags, O_RDONLY);
wrap_will_return(WRAP_FUNC(open), TEST_FD);
expect_value(__wrap_read, fd, TEST_FD);
expect_value(__wrap_read, count, sizeof(buf));
errno = EISDIR;
will_return(__wrap_read, -1);
will_return(__wrap_read, NULL);
expect_condlog(3, "__sysfs_attr_get_value: read from /foo/bar failed:");
will_return(__wrap_close, 0);
assert_int_equal(sysfs_attr_get_value((void *)state, "bar",
buf, sizeof(buf)), -EISDIR);
will_return(__wrap_udev_device_get_syspath, "/foo");
expect_condlog(4, "open '/foo/baz'");
expect_string(WRAP_FUNC(open), pathname, "/foo/baz");
expect_value(WRAP_FUNC(open), flags, O_RDONLY);
wrap_will_return(WRAP_FUNC(open), TEST_FD);
expect_value(__wrap_read, fd, TEST_FD);
expect_value(__wrap_read, count, sizeof(buf));
errno = EPERM;
will_return(__wrap_read, -1);
will_return(__wrap_read, NULL);
expect_condlog(3, "__sysfs_attr_get_value: read from /foo/baz failed:");
will_return(__wrap_close, 0);
assert_int_equal(sysfs_bin_attr_get_value((void *)state, "baz",
(unsigned char *)buf, sizeof(buf)),
-EPERM);
}
static void _test_sagv_read(void **state, unsigned int bufsz)
{
char buf[16];
char input[] = "01234567";
unsigned int n, trunc;
assert_in_range(bufsz, 1, sizeof(buf));
memset(buf, '.', sizeof(buf));
will_return(__wrap_udev_device_get_syspath, "/foo");
expect_condlog(4, "open '/foo/bar'");
expect_string(WRAP_FUNC(open), pathname, "/foo/bar");
expect_value(WRAP_FUNC(open), flags, O_RDONLY);
wrap_will_return(WRAP_FUNC(open), TEST_FD);
expect_value(__wrap_read, fd, TEST_FD);
expect_value(__wrap_read, count, bufsz);
will_return(__wrap_read, sizeof(input) - 1);
will_return(__wrap_read, input);
/* If the buffer is too small, input will be truncated by a 0 byte */
if (bufsz <= sizeof(input) - 1) {
n = bufsz;
trunc = 1;
expect_condlog(3, "__sysfs_attr_get_value: overflow reading from /foo/bar");
} else {
n = sizeof(input) - 1;
trunc = 0;
}
will_return(__wrap_close, 0);
assert_int_equal(sysfs_attr_get_value((void *)state, "bar",
buf, bufsz), n);
assert_memory_equal(buf, input, n - trunc);
assert_int_equal(buf[n - trunc], '\0');
/* Binary input is not truncated */
memset(buf, '.', sizeof(buf));
will_return(__wrap_udev_device_get_syspath, "/foo");
expect_condlog(4, "open '/foo/baz'");
expect_string(WRAP_FUNC(open), pathname, "/foo/baz");
expect_value(WRAP_FUNC(open), flags, O_RDONLY);
wrap_will_return(WRAP_FUNC(open), TEST_FD);
expect_value(__wrap_read, fd, TEST_FD);
expect_value(__wrap_read, count, bufsz);
will_return(__wrap_read, sizeof(input) - 1);
will_return(__wrap_read, input);
will_return(__wrap_close, 0);
n = bufsz < sizeof(input) - 1 ? bufsz : sizeof(input) - 1;
assert_int_equal(sysfs_bin_attr_get_value((void *)state, "baz",
(unsigned char *)buf,
bufsz),
n);
assert_memory_equal(buf, input, n);
}
static void test_sagv_read_overflow_8(void **state)
{
_test_sagv_read(state, 8);
}
static void test_sagv_read_overflow_4(void **state)
{
_test_sagv_read(state, 4);
}
static void test_sagv_read_overflow_1(void **state)
{
_test_sagv_read(state, 1);
}
static void test_sagv_read_good_9(void **state)
{
_test_sagv_read(state, 9);
}
static void test_sagv_read_good_15(void **state)
{
_test_sagv_read(state, 15);
}
static void _test_sagv_read_zeroes(void **state, unsigned int bufsz)
{
char buf[16];
char input[] = { '\0','\0','\0','\0','\0','\0','\0','\0' };
unsigned int n;
assert_in_range(bufsz, 1, sizeof(buf));
memset(buf, '.', sizeof(buf));
will_return(__wrap_udev_device_get_syspath, "/foo");
expect_condlog(4, "open '/foo/bar'");
expect_string(WRAP_FUNC(open), pathname, "/foo/bar");
expect_value(WRAP_FUNC(open), flags, O_RDONLY);
wrap_will_return(WRAP_FUNC(open), TEST_FD);
expect_value(__wrap_read, fd, TEST_FD);
expect_value(__wrap_read, count, bufsz);
will_return(__wrap_read, sizeof(input) - 1);
will_return(__wrap_read, input);
if (bufsz <= sizeof(input) - 1) {
n = bufsz;
expect_condlog(3, "__sysfs_attr_get_value: overflow reading from /foo/bar");
} else
n = 0;
will_return(__wrap_close, 0);
assert_int_equal(sysfs_attr_get_value((void *)state, "bar",
buf, bufsz), n);
/*
* The return value of sysfs_attr_get_value ignores zero bytes,
* but the read data should have been copied to the buffer
*/
assert_memory_equal(buf, input, n == 0 ? bufsz : n);
}
static void test_sagv_read_zeroes_4(void **state)
{
_test_sagv_read_zeroes(state, 4);
}
static void expect_sasv_invalid(void)
{
expect_condlog(1, "sysfs_attr_set_value: invalid parameters");
}
static void test_sasv_invalid(void **state)
{
expect_sasv_invalid();
assert_int_equal(sysfs_attr_set_value(NULL, NULL, NULL, 0), -EINVAL);
expect_sasv_invalid();
assert_int_equal(sysfs_attr_set_value(NULL, (void *)state, (void *)state, 1),
-EINVAL);
expect_sasv_invalid();
assert_int_equal(sysfs_attr_set_value((void *)state, NULL, (void *)state, 1),
-EINVAL);
expect_sasv_invalid();
assert_int_equal(sysfs_attr_set_value((void *)state, (void *)state, NULL, 1),
-EINVAL);
expect_sasv_invalid();
assert_int_equal(sysfs_attr_set_value((void *)state, (void *)state,
(void *)state, 0), -EINVAL);
}
static void test_sasv_bad_udev(void **state)
{
will_return(__wrap_udev_device_get_syspath, NULL);
expect_condlog(3, "sysfs_attr_set_value: invalid udevice");
assert_int_equal(sysfs_attr_set_value((void *)state, (void *)state,
(void *)state, 1), -EINVAL);
}
static void test_sasv_bad_snprintf(void **state)
{
char longstr[PATH_MAX + 1];
char buf[1];
memset(longstr, 'a', sizeof(longstr) - 1);
longstr[sizeof(longstr) - 1] = '\0';
will_return(__wrap_udev_device_get_syspath, "/foo");
expect_condlog(3, "sysfs_attr_set_value: devpath overflow");
assert_int_equal(sysfs_attr_set_value((void *)state, longstr,
buf, sizeof(buf)), -EOVERFLOW);
}
static void test_sasv_open_fail(void **state)
{
char buf[1];
will_return(__wrap_udev_device_get_syspath, "/foo");
expect_condlog(4, "open '/foo/bar'");
expect_string(WRAP_FUNC(open), pathname, "/foo/bar");
expect_value(WRAP_FUNC(open), flags, O_WRONLY);
errno = EPERM;
wrap_will_return(WRAP_FUNC(open), -1);
expect_condlog(3, "sysfs_attr_set_value: attribute '/foo/bar' cannot be opened");
assert_int_equal(sysfs_attr_set_value((void *)state, "bar",
buf, sizeof(buf)), -EPERM);
}
static void test_sasv_write_fail(void **state)
{
char buf[1];
will_return(__wrap_udev_device_get_syspath, "/foo");
expect_condlog(4, "open '/foo/bar'");
expect_string(WRAP_FUNC(open), pathname, "/foo/bar");
expect_value(WRAP_FUNC(open), flags, O_WRONLY);
wrap_will_return(WRAP_FUNC(open), TEST_FD);
expect_value(__wrap_write, fd, TEST_FD);
expect_value(__wrap_write, count, sizeof(buf));
errno = EISDIR;
will_return(__wrap_write, -1);
expect_condlog(3, "sysfs_attr_set_value: write to /foo/bar failed:");
will_return(__wrap_close, 0);
assert_int_equal(sysfs_attr_set_value((void *)state, "bar",
buf, sizeof(buf)), -EISDIR);
}
static void _test_sasv_write(void **state, unsigned int n_written)
{
char buf[8];
assert_in_range(n_written, 0, sizeof(buf));
will_return(__wrap_udev_device_get_syspath, "/foo");
expect_condlog(4, "open '/foo/bar'");
expect_string(WRAP_FUNC(open), pathname, "/foo/bar");
expect_value(WRAP_FUNC(open), flags, O_WRONLY);
wrap_will_return(WRAP_FUNC(open), TEST_FD);
expect_value(__wrap_write, fd, TEST_FD);
expect_value(__wrap_write, count, sizeof(buf));
will_return(__wrap_write, n_written);
if (n_written < sizeof(buf))
expect_condlog(3, "sysfs_attr_set_value: underflow writing");
will_return(__wrap_close, 0);
assert_int_equal(sysfs_attr_set_value((void *)state, "bar",
buf, sizeof(buf)),
n_written);
}
static void test_sasv_write_0(void **state)
{
_test_sasv_write(state, 0);
}
static void test_sasv_write_4(void **state)
{
_test_sasv_write(state, 4);
}
static void test_sasv_write_7(void **state)
{
_test_sasv_write(state, 7);
}
static void test_sasv_write_8(void **state)
{
_test_sasv_write(state, 8);
}
static int test_sysfs(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_sagv_invalid),
cmocka_unit_test(test_sagv_bad_udev),
cmocka_unit_test(test_sagv_bad_snprintf),
cmocka_unit_test(test_sagv_open_fail),
cmocka_unit_test(test_sagv_read_fail),
cmocka_unit_test(test_sagv_read_overflow_1),
cmocka_unit_test(test_sagv_read_overflow_4),
cmocka_unit_test(test_sagv_read_overflow_8),
cmocka_unit_test(test_sagv_read_good_9),
cmocka_unit_test(test_sagv_read_good_15),
cmocka_unit_test(test_sagv_read_zeroes_4),
cmocka_unit_test(test_sasv_invalid),
cmocka_unit_test(test_sasv_bad_udev),
cmocka_unit_test(test_sasv_bad_snprintf),
cmocka_unit_test(test_sasv_open_fail),
cmocka_unit_test(test_sasv_write_fail),
cmocka_unit_test(test_sasv_write_0),
cmocka_unit_test(test_sasv_write_4),
cmocka_unit_test(test_sasv_write_7),
cmocka_unit_test(test_sasv_write_8),
};
return cmocka_run_group_tests(tests, setup, teardown);
}
int main(void)
{
int ret = 0;
init_test_verbosity(4);
ret += test_sysfs();
return ret;
}