[apparmor] [patch 1/2] parser: refactor symtab unit tests, expand coverage, fix seenlist bug

Steve Beattie steve at nxnw.org
Fri Mar 28 18:14:20 UTC 2014


parser: refactor symtab unit tests, expand coverage, fix seenlist bug

This patch:
  - refactors the parser_symtab.c unit tests a bit in preparation for
    the patch to add an implicit autofilled @{profile_name} variable
  - expands coverage of the unit tests such that all code paths that
    don't result in an exit() or are due to memory allocation errors are
    exercised (this doesn't mean the tests are complete; the
    __expand_variable() could use more tests for correctness).
  - it fixes a bug where variables were not being removed from the
    seenlist when a problem was detected in __expand_variable().

Signed-off-by: Steve Beattie <steve at nxnw.org>
---
 parser/parser_symtab.c |  209 ++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 182 insertions(+), 27 deletions(-)

Index: b/parser/parser_symtab.c
===================================================================
--- a/parser/parser_symtab.c
+++ b/parser/parser_symtab.c
@@ -446,8 +446,9 @@ next:
 	}
 
 	symbol->expanded = expanded;
-	pop_seen_var(symbol->var_name);
+
 out:
+	pop_seen_var(symbol->var_name);
 	free_var_string(split);
 	return retval;
 }
@@ -545,12 +546,11 @@ void free_symtabs(void)
 
 #include "unit_test.h"
 
-int main(void)
+int test_compare_symtab(void)
 {
 	int rc = 0;
 	int retval;
-	struct set_value *retptr;
-	struct symtab *a, *b;
+	struct symtab *a, *b, *c;
 
 	a = new_symtab_entry("blah");
 	b = new_symtab_entry("suck");
@@ -565,6 +565,181 @@ int main(void)
 	retval = compare_symtabs(b, a);
 	MY_TEST(retval != 0, "comparison 3");
 
+	retval = compare_symtabs(b, b);
+	MY_TEST(retval == 0, "comparison 4");
+
+	c = new_symtab_entry("blah");
+	retval = compare_symtabs(a, c);
+	MY_TEST(retval == 0, "comparison 5");
+
+	free_symtab(a);
+	free_symtab(b);
+	free_symtab(c);
+
+	return rc;
+}
+
+int test_seenlist(void)
+{
+	int rc = 0;
+
+	MY_TEST(!is_seen("oogabooga"), "lookup unseen variable");
+
+	push_seen_var("oogabooga");
+	MY_TEST(is_seen("oogabooga"), "lookup seen variable 1");
+	MY_TEST(!is_seen("not_seen"), "lookup unseen variable 2");
+
+	push_seen_var("heebiejeebie");
+	MY_TEST(is_seen("oogabooga"), "lookup seen variable 2");
+	MY_TEST(is_seen("heebiejeebie"), "lookup seen variable 3");
+	MY_TEST(!is_seen("not_seen"), "lookup unseen variable 3");
+
+	pop_seen_var("oogabooga");
+	MY_TEST(!is_seen("oogabooga"), "lookup unseen variable 4");
+	MY_TEST(is_seen("heebiejeebie"), "lookup seen variable 4");
+	MY_TEST(!is_seen("not_seen"), "lookup unseen variable 5");
+
+	pop_seen_var("heebiejeebie");
+	MY_TEST(!is_seen("heebiejeebie"), "lookup unseen variable 6");
+
+	//pop_seen_var("not_seen"); /* triggers assert */
+
+	return rc;
+}
+
+int test_add_set_to_boolean(void)
+{
+	int rc = 0;
+	int retval;
+
+	/* test adding a set value to a boolean variable */
+	retval = add_boolean_var("not_a_set_variable", 1);
+	MY_TEST(retval == 0, "new boolean variable 3");
+	retval = add_set_value("not_a_set_variable", "a set value");
+	MY_TEST(retval != 0, "add set value to boolean");
+
+	free_symtabs();
+
+	return rc;
+}
+
+int test_expand_bool_within_set(void)
+{
+	int rc = 0;
+	int retval;
+	struct symtab *retsym;
+
+	/* test expanding a boolean var within a set variable */
+	retval = add_boolean_var("not_a_set_variable", 1);
+	MY_TEST(retval == 0, "new boolean variable 4");
+	retval = new_set_var("set_variable", "set_value@{not_a_set_variable}");
+	MY_TEST(retval == 0, "add set value with embedded boolean");
+	retsym = lookup_existing_symbol("set_variable");
+	MY_TEST(retsym != NULL, "get set variable w/boolean");
+	retval = __expand_variable(retsym);
+	MY_TEST(retval != 0, "expand set variable with embedded boolean");
+
+	free_symtabs();
+
+	return rc;
+}
+
+int test_expand_recursive_set_vars(void)
+{
+	int rc = 0;
+	int retval;
+	struct symtab *retsym;
+
+	/* test expanding a recursive var within a set variable */
+	retval = new_set_var("recursive_1", "set_value@{recursive_2}");
+	MY_TEST(retval == 0, "new recursive set variable 1");
+	retval = new_set_var("recursive_2", "set_value@{recursive_3}");
+	MY_TEST(retval == 0, "new recursive set variable 2");
+	retval = new_set_var("recursive_3", "set_value@{recursive_1}");
+	MY_TEST(retval == 0, "new recursive set variable 3");
+	retsym = lookup_existing_symbol("recursive_1");
+	MY_TEST(retsym != NULL, "get recursive set variable");
+	retval = __expand_variable(retsym);
+	MY_TEST(retval != 0, "expand recursive set variable");
+
+	free_symtabs();
+
+	return rc;
+}
+
+int test_expand_undefined_set_var(void)
+{
+	int rc = 0;
+	int retval;
+	struct symtab *retsym;
+
+	/* test expanding an undefined var within a set variable */
+	retval = new_set_var("defined_var", "set_value@{undefined_var}");
+	MY_TEST(retval == 0, "new undefined test set variable");
+	retsym = lookup_existing_symbol("defined_var");
+	MY_TEST(retsym != NULL, "get undefined test set variable");
+	retval = __expand_variable(retsym);
+	MY_TEST(retval != 0, "expand undefined set variable");
+
+	free_symtabs();
+
+	return rc;
+}
+
+int test_expand_set_var_during_dump(void)
+{
+	int rc = 0;
+	int retval;
+	struct symtab *retsym;
+
+	/* test expanding an defined var within a set variable during var dump*/
+	retval = new_set_var("set_var_1", "set_value@{set_var_2}");
+	MY_TEST(retval == 0, "new dump expansion set variable 1");
+	retval = new_set_var("set_var_2", "some other set_value");
+	MY_TEST(retval == 0, "new dump expansion set variable 2");
+	retsym = lookup_existing_symbol("set_var_1");
+	MY_TEST(retsym != NULL, "get dump expansion set variable 1");
+	__dump_symtab_entry(retsym, 0);
+	__dump_symtab_entry(retsym, 1);
+	__dump_symtab_entry(retsym, 0);
+
+	free_symtabs();
+
+	return rc;
+}
+
+int main(void)
+{
+	int rc = 0;
+	int retval;
+	struct set_value *retptr;
+
+	rc = test_compare_symtab();
+
+	retval = test_seenlist();
+	if (rc == 0)
+		rc = retval;
+
+	retval = test_add_set_to_boolean();
+	if (rc == 0)
+		rc = retval;
+
+	retval = test_expand_bool_within_set();
+	if (rc == 0)
+		rc = retval;
+
+	retval = test_expand_recursive_set_vars();
+	if (rc == 0)
+		rc = retval;
+
+	retval = test_expand_undefined_set_var();
+	if (rc == 0)
+		rc = retval;
+
+	retval = test_expand_set_var_during_dump();
+	if (rc == 0)
+		rc = retval;
+
 	retval = new_set_var("test", "test value");
 	MY_TEST(retval == 0, "new set variable 1");
 
@@ -619,35 +794,13 @@ int main(void)
 	retptr = get_set_var("abuse");
 	MY_TEST(retptr == NULL, "get set variable that's declared a boolean");
 
+	/* test walking set values */
 	retptr = get_set_var("monopuff");
 	MY_TEST(retptr != NULL, "get set variable 1");
-
 	retval = strcmp(get_next_set_value(&retptr), "Mockingbird");
 	MY_TEST(retval == 0, "get set value 1");
-
 	MY_TEST(get_next_set_value(&retptr) == NULL, "get no more set values 1");
 
-	MY_TEST(!is_seen("oogabooga"), "lookup unseen variable");
-
-	push_seen_var("oogabooga");
-	MY_TEST(is_seen("oogabooga"), "lookup seen variable 1");
-	MY_TEST(!is_seen("not_seen"), "lookup unseen variable 2");
-
-	push_seen_var("heebiejeebie");
-	MY_TEST(is_seen("oogabooga"), "lookup seen variable 2");
-	MY_TEST(is_seen("heebiejeebie"), "lookup seen variable 3");
-	MY_TEST(!is_seen("not_seen"), "lookup unseen variable 3");
-
-	pop_seen_var("oogabooga");
-	MY_TEST(!is_seen("oogabooga"), "lookup unseen variable 4");
-	MY_TEST(is_seen("heebiejeebie"), "lookup seen variable 4");
-	MY_TEST(!is_seen("not_seen"), "lookup unseen variable 5");
-
-	pop_seen_var("heebiejeebie");
-	MY_TEST(!is_seen("heebiejeebie"), "lookup unseen variable 6");
-
-	//pop_seen_var("not_seen"); /* triggers assert */
-
 	retval = new_set_var("eek", "Mocking@{monopuff}bir@{stereopuff}d@{stereopuff}");
 	MY_TEST(retval == 0, "new set variable 4");
 
@@ -656,6 +809,8 @@ int main(void)
 	dump_symtab();
 	dump_expanded_symtab();
 
+	free_symtabs();
+
 	return rc;
 }
 #endif /* UNIT_TEST */
-- 
Steve Beattie
<sbeattie at ubuntu.com>
http://NxNW.org/~steve/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <https://lists.ubuntu.com/archives/apparmor/attachments/20140328/b5bfa3b8/attachment.pgp>


More information about the AppArmor mailing list