|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Tenstorrent AI ULC |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +#include <errno.h> |
| 7 | +#include <stdlib.h> |
| 8 | + |
| 9 | +#include <zephyr/ztest.h> |
| 10 | + |
| 11 | +ZTEST(xsi_single_process, test_putenv) |
| 12 | +{ |
| 13 | + char buf[64]; |
| 14 | + |
| 15 | + { |
| 16 | + /* degenerate cases */ |
| 17 | + static const char *const cases[] = { |
| 18 | + NULL, "", "=", "abc", "42", "=abc", |
| 19 | + /* |
| 20 | + * Note: |
| 21 | + * There are many poorly-formatted environment variable names and values |
| 22 | + * that are invalid (from the perspective of a POSIX shell), but still |
| 23 | + * accepted by setenv() and subsequently putenv(). |
| 24 | + * |
| 25 | + * See also tests/posix/single_process/src/env.c |
| 26 | + * See also lib/posix/shell/env.c:101 |
| 27 | + */ |
| 28 | + }; |
| 29 | + |
| 30 | + ARRAY_FOR_EACH(cases, i) { |
| 31 | + char *s; |
| 32 | + |
| 33 | + if (cases[i] == NULL) { |
| 34 | + s = NULL; |
| 35 | + } else { |
| 36 | + strncpy(buf, cases[i], sizeof(buf)); |
| 37 | + buf[sizeof(buf) - 1] = '\0'; |
| 38 | + s = buf; |
| 39 | + } |
| 40 | + |
| 41 | + errno = 0; |
| 42 | + zexpect_equal(-1, putenv(s), "putenv(%s) unexpectedly succeeded", s); |
| 43 | + zexpect_not_equal(0, errno, "putenv(%s) did not set errno", s); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + { |
| 48 | + /* valid cases */ |
| 49 | + static const char *const cases[] = { |
| 50 | + "FOO=bar", |
| 51 | + }; |
| 52 | + |
| 53 | + ARRAY_FOR_EACH(cases, i) { |
| 54 | + strncpy(buf, cases[i], sizeof(buf)); |
| 55 | + buf[sizeof(buf) - 1] = '\0'; |
| 56 | + |
| 57 | + zexpect_ok(putenv(buf), "putenv(%s) failed: %d", buf, errno); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments