Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Joe Hershberger | c04f8b0 | 2015-05-20 14:27:37 -0500 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 |
| 4 | * Joe Hershberger, National Instruments, joe.hershberger@ni.com |
Joe Hershberger | c04f8b0 | 2015-05-20 14:27:37 -0500 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <command.h> |
| 9 | #include <env_attr.h> |
| 10 | #include <test/env.h> |
| 11 | #include <test/ut.h> |
| 12 | |
| 13 | static int env_test_attrs_lookup(struct unit_test_state *uts) |
| 14 | { |
| 15 | char attrs[32]; |
| 16 | |
| 17 | ut_assertok(env_attr_lookup("foo:bar", "foo", attrs)); |
| 18 | ut_asserteq_str("bar", attrs); |
| 19 | |
| 20 | ut_assertok(env_attr_lookup(",foo:bar", "foo", attrs)); |
| 21 | ut_asserteq_str("bar", attrs); |
| 22 | |
| 23 | ut_assertok(env_attr_lookup(",foo:bar,", "foo", attrs)); |
| 24 | ut_asserteq_str("bar", attrs); |
| 25 | |
| 26 | ut_assertok(env_attr_lookup(" foo:bar", "foo", attrs)); |
| 27 | ut_asserteq_str("bar", attrs); |
| 28 | |
| 29 | ut_assertok(env_attr_lookup("foo : bar", "foo", attrs)); |
| 30 | ut_asserteq_str("bar", attrs); |
| 31 | |
| 32 | ut_assertok(env_attr_lookup(" foo: bar ", "foo", attrs)); |
| 33 | ut_asserteq_str("bar", attrs); |
| 34 | |
| 35 | ut_assertok(env_attr_lookup("foo:bar ", "foo", attrs)); |
| 36 | ut_asserteq_str("bar", attrs); |
| 37 | |
| 38 | ut_assertok(env_attr_lookup(",foo:bar,goo:baz", "foo", attrs)); |
| 39 | ut_asserteq_str("bar", attrs); |
| 40 | |
| 41 | ut_asserteq(-ENOENT, env_attr_lookup(",,", "foo", attrs)); |
| 42 | |
| 43 | ut_asserteq(-ENOENT, env_attr_lookup("goo:baz", "foo", attrs)); |
| 44 | |
| 45 | ut_assertok(env_attr_lookup("foo:bar,foo:bat,foo:baz", "foo", attrs)); |
| 46 | ut_asserteq_str("baz", attrs); |
| 47 | |
| 48 | ut_assertok(env_attr_lookup( |
| 49 | " foo : bar , foo : bat , foot : baz ", "foo", attrs)); |
| 50 | ut_asserteq_str("bat", attrs); |
| 51 | |
| 52 | ut_assertok(env_attr_lookup( |
| 53 | " foo : bar , foo : bat , ufoo : baz ", "foo", attrs)); |
| 54 | ut_asserteq_str("bat", attrs); |
| 55 | |
| 56 | ut_asserteq(-EINVAL, env_attr_lookup(NULL, "foo", attrs)); |
| 57 | ut_asserteq(-EINVAL, env_attr_lookup("foo:bar", "foo", NULL)); |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | ENV_TEST(env_test_attrs_lookup, 0); |
Joe Hershberger | 61a9507 | 2015-05-20 14:27:38 -0500 | [diff] [blame] | 62 | |
| 63 | #ifdef CONFIG_REGEX |
| 64 | static int env_test_attrs_lookup_regex(struct unit_test_state *uts) |
| 65 | { |
| 66 | char attrs[32]; |
| 67 | |
| 68 | ut_assertok(env_attr_lookup("foo1?:bar", "foo", attrs)); |
| 69 | ut_asserteq_str("bar", attrs); |
| 70 | |
| 71 | ut_assertok(env_attr_lookup("foo1?:bar", "foo1", attrs)); |
| 72 | ut_asserteq_str("bar", attrs); |
| 73 | |
| 74 | ut_assertok(env_attr_lookup(".foo:bar", ".foo", attrs)); |
| 75 | ut_asserteq_str("bar", attrs); |
| 76 | |
| 77 | ut_assertok(env_attr_lookup(".foo:bar", "ufoo", attrs)); |
| 78 | ut_asserteq_str("bar", attrs); |
| 79 | |
| 80 | ut_assertok(env_attr_lookup("\\.foo:bar", ".foo", attrs)); |
| 81 | ut_asserteq_str("bar", attrs); |
| 82 | |
| 83 | ut_asserteq(-ENOENT, env_attr_lookup("\\.foo:bar", "ufoo", attrs)); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | ENV_TEST(env_test_attrs_lookup_regex, 0); |
| 88 | #endif |