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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
#include <test.h>
#include <var_expressions.h>
static void test_plain_variable_with_no_stuff_in_it(void)
{
VarRef *ref = VarRefParse("foo");
assert_false(ref->ns);
assert_false(ref->scope);
assert_string_equal("foo", ref->lval);
assert_int_equal(0, ref->num_indices);
assert_false(ref->indices);
VarRefDestroy(ref);
}
static void test_scoped(void)
{
VarRef *ref = VarRefParse("scope.lval");
assert_false(ref->ns);
assert_string_equal("scope", ref->scope);
assert_string_equal("lval", ref->lval);
assert_int_equal(0, ref->num_indices);
assert_false(ref->indices);
VarRefDestroy(ref);
}
static void test_full(void)
{
VarRef *ref = VarRefParse("ns:scope.lval");
assert_string_equal("ns", ref->ns);
assert_string_equal("scope", ref->scope);
assert_string_equal("lval", ref->lval);
assert_int_equal(0, ref->num_indices);
assert_false(ref->indices);
VarRefDestroy(ref);
}
static void test_dotted_array(void)
{
VarRef *ref = VarRefParse("ns:scope.lval[la.la]");
assert_string_equal("ns", ref->ns);
assert_string_equal("scope", ref->scope);
assert_string_equal("lval", ref->lval);
assert_int_equal(1, ref->num_indices);
assert_string_equal("la.la", ref->indices[0]);
VarRefDestroy(ref);
}
static void test_levels(void)
{
VarRef *ref = VarRefParse("ns:scope.lval[x][y][z]");
assert_string_equal("ns", ref->ns);
assert_string_equal("scope", ref->scope);
assert_string_equal("lval", ref->lval);
assert_int_equal(3, ref->num_indices);
assert_string_equal("x", ref->indices[0]);
assert_string_equal("y", ref->indices[1]);
assert_string_equal("z", ref->indices[2]);
VarRefDestroy(ref);
}
static void test_unqualified_array(void)
{
VarRef *ref = VarRefParse("lval[x]");
assert_false(ref->ns);
assert_false(ref->scope);
assert_string_equal("lval", ref->lval);
assert_int_equal(1, ref->num_indices);
assert_string_equal("x", ref->indices[0]);
VarRefDestroy(ref);
}
static void test_qualified_array(void)
{
VarRef *ref = VarRefParse("scope.lval[x]");
assert_false(ref->ns);
assert_string_equal("scope", ref->scope);
assert_string_equal("lval", ref->lval);
assert_int_equal(1, ref->num_indices);
assert_string_equal("x", ref->indices[0]);
VarRefDestroy(ref);
}
static void test_nested_array(void)
{
VarRef *ref = VarRefParse("scope.lval[$(other[x])]");
assert_false(ref->ns);
assert_string_equal("scope", ref->scope);
assert_string_equal("lval", ref->lval);
assert_int_equal(1, ref->num_indices);
assert_string_equal("$(other[x])", ref->indices[0]);
VarRefDestroy(ref);
}
static void test_array_with_dot_colon_in_index(void)
{
VarRef *ref = VarRefParse("lval[x-x.x:x]");
assert_false(ref->ns);
assert_false(ref->scope);
assert_string_equal("lval", ref->lval);
assert_int_equal(1, ref->num_indices);
assert_string_equal("x-x.x:x", ref->indices[0]);
VarRefDestroy(ref);
}
static void test_special_scope(void)
{
Policy *p = PolicyNew();
Bundle *bp = PolicyAppendBundle(p, "ns", "b", "agent", NULL, NULL);
{
VarRef *ref = VarRefParseFromBundle("c.lval", bp);
assert_string_equal("ns", ref->ns);
assert_string_equal("c", ref->scope);
assert_string_equal("lval", ref->lval);
VarRefDestroy(ref);
}
{
VarRef *ref = VarRefParseFromBundle("sys.lval", bp);
assert_false(ref->ns);
assert_string_equal("sys", ref->scope);
assert_string_equal("lval", ref->lval);
VarRefDestroy(ref);
}
PolicyDestroy(p);
}
static void CheckToStringQualified(const char *str, const char *expect)
{
VarRef *ref = VarRefParse(str);
char *out = VarRefToString(ref, true);
assert_string_equal(expect, out);
free(out);
VarRefDestroy(ref);
}
static void test_to_string_qualified(void)
{
CheckToStringQualified("ns:scope.lval[x][y]", "ns:scope.lval[x][y]");
CheckToStringQualified("ns:scope.lval[x]", "ns:scope.lval[x]");
CheckToStringQualified("ns:scope.lval", "ns:scope.lval");
CheckToStringQualified("scope.lval", "default:scope.lval");
CheckToStringQualified("lval", "lval");
}
static void test_to_string_unqualified(void)
{
{
VarRef *ref = VarRefParse("ns:scope.lval[x][y]");
char *out = VarRefToString(ref, false);
assert_string_equal("lval[x][y]", out);
free(out);
VarRefDestroy(ref);
}
{
VarRef *ref = VarRefParse("ns:scope.lval[x]");
char *out = VarRefToString(ref, false);
assert_string_equal("lval[x]", out);
free(out);
VarRefDestroy(ref);
}
{
VarRef *ref = VarRefParse("scope.lval");
char *out = VarRefToString(ref, false);
assert_string_equal("lval", out);
free(out);
VarRefDestroy(ref);
}
{
VarRef *ref = VarRefParse("lval");
char *out = VarRefToString(ref, false);
assert_string_equal("lval", out);
free(out);
VarRefDestroy(ref);
}
}
int main()
{
PRINT_TEST_BANNER();
const UnitTest tests[] =
{
unit_test(test_plain_variable_with_no_stuff_in_it),
unit_test(test_scoped),
unit_test(test_full),
unit_test(test_dotted_array),
unit_test(test_levels),
unit_test(test_unqualified_array),
unit_test(test_qualified_array),
unit_test(test_nested_array),
unit_test(test_array_with_dot_colon_in_index),
unit_test(test_special_scope),
unit_test(test_to_string_qualified),
unit_test(test_to_string_unqualified),
};
return run_tests(tests);
}
|