-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjob_policy_tests.cpp
107 lines (82 loc) · 3.74 KB
/
job_policy_tests.cpp
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2018 The Fuchsia Authors
//
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT
#include <object/job_policy.h>
#include <fbl/algorithm.h>
#include <lib/unittest/unittest.h>
namespace {
static bool initial_state() {
BEGIN_TEST;
JobPolicy p;
for (uint32_t pol = 0; pol < ZX_POL_MAX; ++pol) {
EXPECT_EQ(ZX_POL_ACTION_ALLOW, p.QueryBasicPolicy(pol), "");
}
const TimerSlack slack = p.GetTimerSlack();
EXPECT_TRUE(slack == kNoSlack, "");
END_TEST;
}
static bool add_basic_policy_absolute() {
BEGIN_TEST;
JobPolicy p;
zx_policy_basic_t repeated[2]{{ZX_POL_NEW_ANY, ZX_POL_ACTION_DENY},
{ZX_POL_NEW_ANY, ZX_POL_ACTION_DENY}};
ASSERT_EQ(ZX_OK, p.AddBasicPolicy(ZX_JOB_POL_ABSOLUTE, repeated, 2), "");
ASSERT_EQ(ZX_POL_ACTION_DENY, p.QueryBasicPolicy(ZX_POL_NEW_EVENT), "");
zx_policy_basic_t conflicting[2]{{ZX_POL_NEW_ANY, ZX_POL_ACTION_DENY},
{ZX_POL_NEW_ANY, ZX_POL_ACTION_ALLOW}};
ASSERT_EQ(ZX_ERR_ALREADY_EXISTS, p.AddBasicPolicy(ZX_JOB_POL_ABSOLUTE, conflicting, 2), "");
ASSERT_EQ(ZX_POL_ACTION_DENY, p.QueryBasicPolicy(ZX_POL_NEW_VMO), "");
END_TEST;
}
static bool add_basic_policy_relative() {
BEGIN_TEST;
JobPolicy p;
zx_policy_basic_t repeated[2]{{ZX_POL_NEW_ANY, ZX_POL_ACTION_DENY},
{ZX_POL_NEW_ANY, ZX_POL_ACTION_DENY}};
ASSERT_EQ(ZX_OK, p.AddBasicPolicy(ZX_JOB_POL_RELATIVE, repeated, 2), "");
ASSERT_EQ(ZX_POL_ACTION_DENY, p.QueryBasicPolicy(ZX_POL_NEW_TIMER), "");
zx_policy_basic_t conflicting[2]{{ZX_POL_NEW_ANY, ZX_POL_ACTION_DENY},
{ZX_POL_NEW_ANY, ZX_POL_ACTION_ALLOW}};
ASSERT_EQ(ZX_OK, p.AddBasicPolicy(ZX_JOB_POL_RELATIVE, conflicting, 2), "");
ASSERT_EQ(ZX_POL_ACTION_DENY, p.QueryBasicPolicy(ZX_POL_NEW_FIFO), "");
END_TEST;
}
// Test that AddBasicPolicy does not modify JobPolicy when it fails.
static bool add_basic_policy_unmodified_on_error() {
BEGIN_TEST;
JobPolicy p;
zx_policy_basic_t policy[2]{{ZX_POL_NEW_VMO,
ZX_POL_ACTION_ALLOW | ZX_POL_ACTION_EXCEPTION},
{ZX_POL_NEW_CHANNEL,
ZX_POL_ACTION_KILL}};
ASSERT_EQ(ZX_OK, p.AddBasicPolicy(ZX_JOB_POL_ABSOLUTE, policy, fbl::count_of(policy)), "");
ASSERT_EQ(ZX_POL_ACTION_ALLOW | ZX_POL_ACTION_EXCEPTION,
p.QueryBasicPolicy(ZX_POL_NEW_VMO), "");
ASSERT_EQ(ZX_POL_ACTION_KILL, p.QueryBasicPolicy(ZX_POL_NEW_CHANNEL), "");
const JobPolicy orig = p;
zx_policy_basic_t new_policy{ZX_POL_NEW_ANY, UINT32_MAX};
ASSERT_EQ(ZX_ERR_NOT_SUPPORTED, p.AddBasicPolicy(ZX_JOB_POL_ABSOLUTE, &new_policy, 1), "");
ASSERT_TRUE(orig == p, "");
new_policy = {ZX_POL_NEW_VMO, ZX_POL_ACTION_ALLOW};
ASSERT_EQ(ZX_ERR_ALREADY_EXISTS, p.AddBasicPolicy(ZX_JOB_POL_ABSOLUTE, &new_policy, 1), "");
ASSERT_TRUE(orig == p, "");
END_TEST;
}
static bool set_get_timer_slack() {
BEGIN_TEST;
JobPolicy p;
p.SetTimerSlack({1200, TIMER_SLACK_EARLY});
ASSERT_EQ(1200, p.GetTimerSlack().amount(), "");
ASSERT_EQ(TIMER_SLACK_EARLY, p.GetTimerSlack().mode(), "");
END_TEST;
}
} // namespace
UNITTEST_START_TESTCASE(job_policy_tests)
UNITTEST("initial_state", initial_state)
UNITTEST("add_basic_policy_absolute", add_basic_policy_absolute)
UNITTEST("add_basic_policy_relative", add_basic_policy_relative)
UNITTEST("add_basic_policy_unmodified_on_error", add_basic_policy_unmodified_on_error)
UNITTEST("set_get_timer_slack", set_get_timer_slack)
UNITTEST_END_TESTCASE(job_policy_tests, "job_policy", "JobPolicy tests");