|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Meta |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <errno.h> |
| 8 | +#include <unistd.h> |
| 9 | + |
| 10 | +#include <zephyr/ztest.h> |
| 11 | +#include <zephyr/sys/util.h> |
| 12 | + |
| 13 | +ZTEST(confstr, test_confstr) |
| 14 | +{ |
| 15 | + char buf[1]; |
| 16 | + |
| 17 | + /* degenerate cases */ |
| 18 | + { |
| 19 | + struct arg { |
| 20 | + int name; |
| 21 | + char *buf; |
| 22 | + size_t len; |
| 23 | + }; |
| 24 | + |
| 25 | + const struct arg arg1s[] = { |
| 26 | + {-1, NULL, 0}, |
| 27 | + {-1, NULL, sizeof(buf)}, |
| 28 | + {-1, buf, 0}, |
| 29 | + {-1, buf, sizeof(buf)}, |
| 30 | + }; |
| 31 | + |
| 32 | + const struct arg arg2s[] = { |
| 33 | + {_CS_PATH, NULL, 0}, |
| 34 | + {_CS_PATH, buf, 0}, |
| 35 | + }; |
| 36 | + |
| 37 | + const struct arg arg3s[] = { |
| 38 | + {_CS_PATH, NULL, sizeof(buf)}, |
| 39 | + }; |
| 40 | + |
| 41 | + ARRAY_FOR_EACH_PTR(arg1s, arg) { |
| 42 | + errno = 0; |
| 43 | + zassert_equal(0, confstr(arg->name, arg->buf, arg->len)); |
| 44 | + zassert_equal(errno, EINVAL); |
| 45 | + } |
| 46 | + |
| 47 | + ARRAY_FOR_EACH_PTR(arg2s, arg) { |
| 48 | + errno = 0; |
| 49 | + buf[0] = 0xff; |
| 50 | + zassert_true(confstr(arg->name, arg->buf, arg->len) > 0); |
| 51 | + zassert_equal(errno, 0); |
| 52 | + zassert_equal((uint8_t)buf[0], 0xff); |
| 53 | + } |
| 54 | + |
| 55 | + ARRAY_FOR_EACH_PTR(arg3s, arg) { |
| 56 | + errno = 0; |
| 57 | + zassert_true(confstr(arg->name, arg->buf, arg->len) > 0); |
| 58 | + zassert_equal(errno, 0); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + buf[0] = 0xff; |
| 63 | + zassert_true(confstr(_CS_PATH, buf, sizeof(buf) > 0)); |
| 64 | + zassert_equal(buf[0], '\0'); |
| 65 | +} |
| 66 | + |
| 67 | +ZTEST_SUITE(confstr, NULL, NULL, NULL, NULL, NULL); |
0 commit comments