aboutsummaryrefslogtreecommitdiff
path: root/test/env/attr.c
blob: 8d5c0f1c3df49314f119b571cc5b682179feee0c (plain)
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
// SPDX-License-Identifier: GPL-2.0
/*
 * (C) Copyright 2015
 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
 */

#include <common.h>
#include <command.h>
#include <env_attr.h>
#include <test/env.h>
#include <test/ut.h>

static int env_test_attrs_lookup(struct unit_test_state *uts)
{
	char attrs[32];

	ut_assertok(env_attr_lookup("foo:bar", "foo", attrs));
	ut_asserteq_str("bar", attrs);

	ut_assertok(env_attr_lookup(",foo:bar", "foo", attrs));
	ut_asserteq_str("bar", attrs);

	ut_assertok(env_attr_lookup(",foo:bar,", "foo", attrs));
	ut_asserteq_str("bar", attrs);

	ut_assertok(env_attr_lookup(" foo:bar", "foo", attrs));
	ut_asserteq_str("bar", attrs);

	ut_assertok(env_attr_lookup("foo : bar", "foo", attrs));
	ut_asserteq_str("bar", attrs);

	ut_assertok(env_attr_lookup(" foo: bar ", "foo", attrs));
	ut_asserteq_str("bar", attrs);

	ut_assertok(env_attr_lookup("foo:bar ", "foo", attrs));
	ut_asserteq_str("bar", attrs);

	ut_assertok(env_attr_lookup(",foo:bar,goo:baz", "foo", attrs));
	ut_asserteq_str("bar", attrs);

	ut_asserteq(-ENOENT, env_attr_lookup(",,", "foo", attrs));

	ut_asserteq(-ENOENT, env_attr_lookup("goo:baz", "foo", attrs));

	ut_assertok(env_attr_lookup("foo:bar,foo:bat,foo:baz", "foo", attrs));
	ut_asserteq_str("baz", attrs);

	ut_assertok(env_attr_lookup(
		" foo : bar , foo : bat , foot : baz ", "foo", attrs));
	ut_asserteq_str("bat", attrs);

	ut_assertok(env_attr_lookup(
		" foo : bar , foo : bat , ufoo : baz ", "foo", attrs));
	ut_asserteq_str("bat", attrs);

	ut_asserteq(-EINVAL, env_attr_lookup(NULL, "foo", attrs));
	ut_asserteq(-EINVAL, env_attr_lookup("foo:bar", "foo", NULL));

	return 0;
}
ENV_TEST(env_test_attrs_lookup, 0);

#ifdef CONFIG_REGEX
static int env_test_attrs_lookup_regex(struct unit_test_state *uts)
{
	char attrs[32];

	ut_assertok(env_attr_lookup("foo1?:bar", "foo", attrs));
	ut_asserteq_str("bar", attrs);

	ut_assertok(env_attr_lookup("foo1?:bar", "foo1", attrs));
	ut_asserteq_str("bar", attrs);

	ut_assertok(env_attr_lookup(".foo:bar", ".foo", attrs));
	ut_asserteq_str("bar", attrs);

	ut_assertok(env_attr_lookup(".foo:bar", "ufoo", attrs));
	ut_asserteq_str("bar", attrs);

	ut_assertok(env_attr_lookup("\\.foo:bar", ".foo", attrs));
	ut_asserteq_str("bar", attrs);

	ut_asserteq(-ENOENT, env_attr_lookup("\\.foo:bar", "ufoo", attrs));

	return 0;
}
ENV_TEST(env_test_attrs_lookup_regex, 0);
#endif