16cd6a6acSopenharmony_ci/*
26cd6a6acSopenharmony_ci * Author: Karl MacMillan <kmacmillan@tresys.com>
36cd6a6acSopenharmony_ci *
46cd6a6acSopenharmony_ci * Copyright (C) 2006 Tresys Technology, LLC
56cd6a6acSopenharmony_ci *
66cd6a6acSopenharmony_ci *  This library is free software; you can redistribute it and/or
76cd6a6acSopenharmony_ci *  modify it under the terms of the GNU Lesser General Public
86cd6a6acSopenharmony_ci *  License as published by the Free Software Foundation; either
96cd6a6acSopenharmony_ci *  version 2.1 of the License, or (at your option) any later version.
106cd6a6acSopenharmony_ci *
116cd6a6acSopenharmony_ci *  This library is distributed in the hope that it will be useful,
126cd6a6acSopenharmony_ci *  but WITHOUT ANY WARRANTY; without even the implied warranty of
136cd6a6acSopenharmony_ci *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
146cd6a6acSopenharmony_ci *  Lesser General Public License for more details.
156cd6a6acSopenharmony_ci *
166cd6a6acSopenharmony_ci *  You should have received a copy of the GNU Lesser General Public
176cd6a6acSopenharmony_ci *  License along with this library; if not, write to the Free Software
186cd6a6acSopenharmony_ci *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
196cd6a6acSopenharmony_ci */
206cd6a6acSopenharmony_ci
216cd6a6acSopenharmony_ci#include "test-cond.h"
226cd6a6acSopenharmony_ci#include "parse_util.h"
236cd6a6acSopenharmony_ci#include "helpers.h"
246cd6a6acSopenharmony_ci
256cd6a6acSopenharmony_ci#include <sepol/policydb/policydb.h>
266cd6a6acSopenharmony_ci#include <sepol/policydb/link.h>
276cd6a6acSopenharmony_ci#include <sepol/policydb/expand.h>
286cd6a6acSopenharmony_ci#include <sepol/policydb/conditional.h>
296cd6a6acSopenharmony_ci
306cd6a6acSopenharmony_cistatic policydb_t basemod;
316cd6a6acSopenharmony_cistatic policydb_t base_expanded;
326cd6a6acSopenharmony_ci
336cd6a6acSopenharmony_ciint cond_test_init(void)
346cd6a6acSopenharmony_ci{
356cd6a6acSopenharmony_ci	if (policydb_init(&base_expanded)) {
366cd6a6acSopenharmony_ci		fprintf(stderr, "out of memory!\n");
376cd6a6acSopenharmony_ci		policydb_destroy(&basemod);
386cd6a6acSopenharmony_ci		return -1;
396cd6a6acSopenharmony_ci	}
406cd6a6acSopenharmony_ci
416cd6a6acSopenharmony_ci	if (test_load_policy(&basemod, POLICY_BASE, 1, "test-cond", "refpolicy-base.conf"))
426cd6a6acSopenharmony_ci		goto cleanup;
436cd6a6acSopenharmony_ci
446cd6a6acSopenharmony_ci	if (link_modules(NULL, &basemod, NULL, 0, 0)) {
456cd6a6acSopenharmony_ci		fprintf(stderr, "link modules failed\n");
466cd6a6acSopenharmony_ci		goto cleanup;
476cd6a6acSopenharmony_ci	}
486cd6a6acSopenharmony_ci
496cd6a6acSopenharmony_ci	if (expand_module(NULL, &basemod, &base_expanded, 0, 1)) {
506cd6a6acSopenharmony_ci		fprintf(stderr, "expand module failed\n");
516cd6a6acSopenharmony_ci		goto cleanup;
526cd6a6acSopenharmony_ci	}
536cd6a6acSopenharmony_ci
546cd6a6acSopenharmony_ci	return 0;
556cd6a6acSopenharmony_ci
566cd6a6acSopenharmony_ci      cleanup:
576cd6a6acSopenharmony_ci	policydb_destroy(&basemod);
586cd6a6acSopenharmony_ci	policydb_destroy(&base_expanded);
596cd6a6acSopenharmony_ci	return -1;
606cd6a6acSopenharmony_ci}
616cd6a6acSopenharmony_ci
626cd6a6acSopenharmony_ciint cond_test_cleanup(void)
636cd6a6acSopenharmony_ci{
646cd6a6acSopenharmony_ci	policydb_destroy(&basemod);
656cd6a6acSopenharmony_ci	policydb_destroy(&base_expanded);
666cd6a6acSopenharmony_ci
676cd6a6acSopenharmony_ci	return 0;
686cd6a6acSopenharmony_ci}
696cd6a6acSopenharmony_ci
706cd6a6acSopenharmony_cistatic void test_cond_expr_equal(void)
716cd6a6acSopenharmony_ci{
726cd6a6acSopenharmony_ci	cond_node_t *a, *b;
736cd6a6acSopenharmony_ci
746cd6a6acSopenharmony_ci	a = base_expanded.cond_list;
756cd6a6acSopenharmony_ci	while (a) {
766cd6a6acSopenharmony_ci		b = base_expanded.cond_list;
776cd6a6acSopenharmony_ci		while (b) {
786cd6a6acSopenharmony_ci			if (a == b) {
796cd6a6acSopenharmony_ci				CU_ASSERT(cond_expr_equal(a, b));
806cd6a6acSopenharmony_ci			} else {
816cd6a6acSopenharmony_ci				CU_ASSERT(cond_expr_equal(a, b) == 0);
826cd6a6acSopenharmony_ci			}
836cd6a6acSopenharmony_ci			b = b->next;
846cd6a6acSopenharmony_ci		}
856cd6a6acSopenharmony_ci		a = a->next;
866cd6a6acSopenharmony_ci	}
876cd6a6acSopenharmony_ci}
886cd6a6acSopenharmony_ci
896cd6a6acSopenharmony_ciint cond_add_tests(CU_pSuite suite)
906cd6a6acSopenharmony_ci{
916cd6a6acSopenharmony_ci	if (NULL == CU_add_test(suite, "cond_expr_equal", test_cond_expr_equal)) {
926cd6a6acSopenharmony_ci		return CU_get_error();
936cd6a6acSopenharmony_ci	}
946cd6a6acSopenharmony_ci	return 0;
956cd6a6acSopenharmony_ci}
96