lin
2025-07-30 fcd736bf35fd93b563e9bbf594f2aa7b62028cc9
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
/*
 * Author: Joshua Brindle <jbrindle@tresys.com>
 *         Chad Sellers <csellers@tresys.com>
 *
 * Copyright (C) 2006 Tresys Technology, LLC
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
 
/* This has helper functions that are common between tests */
 
#include "helpers.h"
#include "parse_util.h"
 
#include <sepol/policydb/expand.h>
#include <sepol/policydb/avrule_block.h>
 
#include <CUnit/Basic.h>
 
#include <stdlib.h>
#include <limits.h>
 
int test_load_policy(policydb_t * p, int policy_type, int mls, const char *test_name, const char *policy_name)
{
   char filename[PATH_MAX];
 
   if (mls) {
       if (snprintf(filename, PATH_MAX, "policies/%s/%s.mls", test_name, policy_name) < 0) {
           return -1;
       }
   } else {
       if (snprintf(filename, PATH_MAX, "policies/%s/%s.std", test_name, policy_name) < 0) {
           return -1;
       }
   }
 
   if (policydb_init(p)) {
       fprintf(stderr, "Out of memory");
       return -1;
   }
 
   p->policy_type = policy_type;
   p->mls = mls;
 
   if (read_source_policy(p, filename, test_name)) {
       fprintf(stderr, "failed to read policy %s\n", filename);
       policydb_destroy(p);
       return -1;
   }
 
   return 0;
}
 
avrule_decl_t *test_find_decl_by_sym(policydb_t * p, int symtab, const char *sym)
{
   scope_datum_t *scope = (scope_datum_t *) hashtab_search(p->scope[symtab].table, sym);
 
   if (scope == NULL) {
       return NULL;
   }
   if (scope->scope != SCOPE_DECL) {
       return NULL;
   }
   if (scope->decl_ids_len != 1) {
       return NULL;
   }
 
   return p->decl_val_to_struct[scope->decl_ids[0] - 1];
}