16cd6a6acSopenharmony_ci/*
26cd6a6acSopenharmony_ci * Author: Joshua Brindle <jbrindle@tresys.com>
36cd6a6acSopenharmony_ci *         Chad Sellers <csellers@tresys.com>
46cd6a6acSopenharmony_ci *
56cd6a6acSopenharmony_ci * Copyright (C) 2006 Tresys Technology, LLC
66cd6a6acSopenharmony_ci *
76cd6a6acSopenharmony_ci *  This library is free software; you can redistribute it and/or
86cd6a6acSopenharmony_ci *  modify it under the terms of the GNU Lesser General Public
96cd6a6acSopenharmony_ci *  License as published by the Free Software Foundation; either
106cd6a6acSopenharmony_ci *  version 2.1 of the License, or (at your option) any later version.
116cd6a6acSopenharmony_ci *
126cd6a6acSopenharmony_ci *  This library is distributed in the hope that it will be useful,
136cd6a6acSopenharmony_ci *  but WITHOUT ANY WARRANTY; without even the implied warranty of
146cd6a6acSopenharmony_ci *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
156cd6a6acSopenharmony_ci *  Lesser General Public License for more details.
166cd6a6acSopenharmony_ci *
176cd6a6acSopenharmony_ci *  You should have received a copy of the GNU Lesser General Public
186cd6a6acSopenharmony_ci *  License along with this library; if not, write to the Free Software
196cd6a6acSopenharmony_ci *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
206cd6a6acSopenharmony_ci */
216cd6a6acSopenharmony_ci
226cd6a6acSopenharmony_ci/* This has helper functions that are common between tests */
236cd6a6acSopenharmony_ci
246cd6a6acSopenharmony_ci#include "helpers.h"
256cd6a6acSopenharmony_ci#include "parse_util.h"
266cd6a6acSopenharmony_ci
276cd6a6acSopenharmony_ci#include <sepol/policydb/expand.h>
286cd6a6acSopenharmony_ci#include <sepol/policydb/avrule_block.h>
296cd6a6acSopenharmony_ci
306cd6a6acSopenharmony_ci#include <CUnit/Basic.h>
316cd6a6acSopenharmony_ci
326cd6a6acSopenharmony_ci#include <stdlib.h>
336cd6a6acSopenharmony_ci#include <limits.h>
346cd6a6acSopenharmony_ci
356cd6a6acSopenharmony_ciint test_load_policy(policydb_t * p, int policy_type, int mls, const char *test_name, const char *policy_name)
366cd6a6acSopenharmony_ci{
376cd6a6acSopenharmony_ci	char filename[PATH_MAX];
386cd6a6acSopenharmony_ci
396cd6a6acSopenharmony_ci	if (mls) {
406cd6a6acSopenharmony_ci		if (snprintf(filename, PATH_MAX, "policies/%s/%s.mls", test_name, policy_name) < 0) {
416cd6a6acSopenharmony_ci			return -1;
426cd6a6acSopenharmony_ci		}
436cd6a6acSopenharmony_ci	} else {
446cd6a6acSopenharmony_ci		if (snprintf(filename, PATH_MAX, "policies/%s/%s.std", test_name, policy_name) < 0) {
456cd6a6acSopenharmony_ci			return -1;
466cd6a6acSopenharmony_ci		}
476cd6a6acSopenharmony_ci	}
486cd6a6acSopenharmony_ci
496cd6a6acSopenharmony_ci	if (policydb_init(p)) {
506cd6a6acSopenharmony_ci		fprintf(stderr, "Out of memory");
516cd6a6acSopenharmony_ci		return -1;
526cd6a6acSopenharmony_ci	}
536cd6a6acSopenharmony_ci
546cd6a6acSopenharmony_ci	p->policy_type = policy_type;
556cd6a6acSopenharmony_ci	p->mls = mls;
566cd6a6acSopenharmony_ci
576cd6a6acSopenharmony_ci	if (read_source_policy(p, filename, test_name)) {
586cd6a6acSopenharmony_ci		fprintf(stderr, "failed to read policy %s\n", filename);
596cd6a6acSopenharmony_ci		policydb_destroy(p);
606cd6a6acSopenharmony_ci		return -1;
616cd6a6acSopenharmony_ci	}
626cd6a6acSopenharmony_ci
636cd6a6acSopenharmony_ci	return 0;
646cd6a6acSopenharmony_ci}
656cd6a6acSopenharmony_ci
666cd6a6acSopenharmony_ciavrule_decl_t *test_find_decl_by_sym(policydb_t * p, int symtab, const char *sym)
676cd6a6acSopenharmony_ci{
686cd6a6acSopenharmony_ci	scope_datum_t *scope = (scope_datum_t *) hashtab_search(p->scope[symtab].table, sym);
696cd6a6acSopenharmony_ci
706cd6a6acSopenharmony_ci	if (scope == NULL) {
716cd6a6acSopenharmony_ci		return NULL;
726cd6a6acSopenharmony_ci	}
736cd6a6acSopenharmony_ci	if (scope->scope != SCOPE_DECL) {
746cd6a6acSopenharmony_ci		return NULL;
756cd6a6acSopenharmony_ci	}
766cd6a6acSopenharmony_ci	if (scope->decl_ids_len != 1) {
776cd6a6acSopenharmony_ci		return NULL;
786cd6a6acSopenharmony_ci	}
796cd6a6acSopenharmony_ci
806cd6a6acSopenharmony_ci	return p->decl_val_to_struct[scope->decl_ids[0] - 1];
816cd6a6acSopenharmony_ci}
82