]> git.saurik.com Git - apple/security.git/blob - keychain/securityd/Regressions/sd-10-policytree.m
Security-59306.61.1.tar.gz
[apple/security.git] / keychain / securityd / Regressions / sd-10-policytree.m
1 /*
2 * Copyright (c) 2009,2012 Apple Inc. All Rights Reserved.
3 */
4
5 #include "trust/trustd/policytree.h"
6
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <libDER/oids.h>
10
11 #include "securityd_regressions.h"
12
13 #define DUMP_POLICY_TREE 0
14
15 int verbose = DUMP_POLICY_TREE;
16
17 static bool randomly_add_children(policy_tree_t node, void *ctx) {
18 int i, count;
19 uint32_t rnd = arc4random();
20 #if 1
21 count = rnd % 7;
22 if (count > 4)
23 count = 0;
24 #else
25 if (rnd < 0x40000000) {
26 count = 1;
27 } else if (rnd < 0x80000000) {
28 count = 2;
29 } else if (rnd < 0xc0000000) {
30 count = 3;
31 } else if (rnd < 0xf0000000) {
32 count = 4;
33 } else {
34 count = 0;
35 }
36 #endif
37
38 #if DUMP_POLICY_TREE
39 diag("node %p add %d children", node, count);
40 #endif
41 for (i = 1; i <= count ; ++i) {
42 policy_tree_add_child(node, &oidAnyPolicy, NULL);
43 //diag("node %p %d/%d children added", node, i, count);
44 //policy_tree_dump(node);
45 }
46 return count != 0;
47 }
48
49 static void tests(void)
50 {
51 policy_qualifier_t p_q = NULL;
52 policy_tree_t tree;
53 ok(tree = policy_tree_create(&oidAnyPolicy, p_q),
54 "create tree root");
55 if (verbose) policy_tree_dump(tree);
56
57 #if 0
58 int i, count = 4;
59 for (i = 1; i <= count ; ++i) {
60 policy_tree_add_child(tree, &oidAnyPolicy, NULL);
61 #if DUMP_POLICY_TREE
62 diag("node %p %d/%d children added", tree, i, count);
63 #endif
64 }
65 policy_tree_dump(tree);
66 #else
67 int depth;
68 for (depth = 0; tree && depth < 7; ++depth) {
69 bool added = false;
70 while (!added) {
71 added = policy_tree_walk_depth(tree, depth,
72 randomly_add_children, NULL);
73 #if DUMP_POLICY_TREE
74 diag("depth: %d %s", depth,
75 (added ? "added children" : "no children added"));
76 #endif
77 }
78 if (verbose) policy_tree_dump(tree);
79 #if DUMP_POLICY_TREE
80 diag("prune_childless depth: %d", depth);
81 #endif
82 policy_tree_prune_childless(&tree, depth);
83 if (verbose) {
84 if (tree)
85 policy_tree_dump(tree);
86 else {
87 #if DUMP_POLICY_TREE
88 diag("tree empty at depth: %d", depth);
89 #endif
90 break;
91 }
92 }
93 }
94 #endif
95 if (tree)
96 policy_tree_prune(&tree);
97 }
98
99 int sd_10_policytree(int argc, char *const *argv)
100 {
101 plan_tests(1);
102
103 tests();
104
105 return 0;
106 }