blob: 8d5c0f1c3df49314f119b571cc5b682179feee0c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Joe Hershbergerb04d3552015-05-20 14:27:37 -05002/*
3 * (C) Copyright 2015
4 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
Joe Hershbergerb04d3552015-05-20 14:27:37 -05005 */
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
13static 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}
61ENV_TEST(env_test_attrs_lookup, 0);
Joe Hershbergerf68c2b22015-05-20 14:27:38 -050062
63#ifdef CONFIG_REGEX
64static 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}
87ENV_TEST(env_test_attrs_lookup_regex, 0);
88#endif