blob: 9d420c1756b49997bf01007917dd46ffd51d92d8 [file] [log] [blame]
Will Drewrydecdfdc2011-09-27 15:13:54 -05001/* libminijail_unittest.c
2 * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 *
6 * Test platform independent logic of minijail.
7 */
8
Will Drewry463c27a2011-10-21 15:52:43 -05009#include <errno.h>
10
Will Drewrydecdfdc2011-09-27 15:13:54 -050011#include "test_harness.h"
12
13#include "libminijail.h"
14#include "libminijail-private.h"
15
Will Drewry3cb9f612011-10-21 20:52:08 -050016/* Prototypes needed only by test. */
17void *consumebytes(size_t length, char **buf, size_t *buflength);
18char *consumestr(char **buf, size_t *buflength);
19
Will Drewrydecdfdc2011-09-27 15:13:54 -050020/* Silence unused variable warnings. */
21TEST(silence_unused) {
22 EXPECT_STREQ(kLdPreloadEnvVar, kLdPreloadEnvVar);
23 EXPECT_STREQ(kFdEnvVar, kFdEnvVar);
24 EXPECT_STRNE(kFdEnvVar, kLdPreloadEnvVar);
25}
26
Will Drewry3cb9f612011-10-21 20:52:08 -050027TEST(consumebytes_zero) {
28 char buf[1024];
29 size_t len = sizeof(buf);
30 char *pos = &buf[0];
31 EXPECT_NE(NULL, consumebytes(0, &pos, &len));
32 EXPECT_EQ(&buf[0], pos);
33 EXPECT_EQ(sizeof(buf), len);
34}
35
36TEST(consumebytes_exact) {
37 char buf[1024];
38 size_t len = sizeof(buf);
39 char *pos = &buf[0];
40 /* One past the end since it consumes the whole buffer. */
41 char *end = &buf[sizeof(buf)];
42 EXPECT_NE(NULL, consumebytes(len, &pos, &len));
43 EXPECT_EQ((size_t)0, len);
44 EXPECT_EQ(end, pos);
45}
46
47TEST(consumebytes_half) {
48 char buf[1024];
49 size_t len = sizeof(buf);
50 char *pos = &buf[0];
51 /* One past the end since it consumes the whole buffer. */
52 char *end = &buf[sizeof(buf) / 2];
53 EXPECT_NE(NULL, consumebytes(len / 2, &pos, &len));
54 EXPECT_EQ(sizeof(buf) / 2, len);
55 EXPECT_EQ(end, pos);
56}
57
58TEST(consumebytes_toolong) {
59 char buf[1024];
60 size_t len = sizeof(buf);
61 char *pos = &buf[0];
62 /* One past the end since it consumes the whole buffer. */
63 EXPECT_EQ(NULL, consumebytes(len + 1, &pos, &len));
64 EXPECT_EQ(sizeof(buf), len);
65 EXPECT_EQ(&buf[0], pos);
66}
67
68TEST(consumestr_zero) {
69 char buf[1024];
70 size_t len = 0;
71 char *pos = &buf[0];
72 memset(buf, 0xff, sizeof(buf));
73 EXPECT_EQ(NULL, consumestr(&pos, &len));
74 EXPECT_EQ((size_t)0, len);
75 EXPECT_EQ(&buf[0], pos);
76}
77
78TEST(consumestr_nonul) {
79 char buf[1024];
80 size_t len = sizeof(buf);
81 char *pos = &buf[0];
82 memset(buf, 0xff, sizeof(buf));
83 EXPECT_EQ(NULL, consumestr(&pos, &len));
84 EXPECT_EQ(sizeof(buf), len);
85 EXPECT_EQ(&buf[0], pos);
86}
87
88TEST(consumestr_full) {
89 char buf[1024];
90 size_t len = sizeof(buf);
91 char *pos = &buf[0];
92 memset(buf, 0xff, sizeof(buf));
93 buf[sizeof(buf)-1] = '\0';
94 EXPECT_EQ((void *)buf, consumestr(&pos, &len));
95 EXPECT_EQ((size_t)0, len);
96 EXPECT_EQ(&buf[sizeof(buf)], pos);
97}
98
99TEST(consumestr_trailing_nul) {
100 char buf[1024];
101 size_t len = sizeof(buf) - 1;
102 char *pos = &buf[0];
103 memset(buf, 0xff, sizeof(buf));
104 buf[sizeof(buf)-1] = '\0';
105 EXPECT_EQ(NULL, consumestr(&pos, &len));
106 EXPECT_EQ(sizeof(buf) - 1, len);
107 EXPECT_EQ(&buf[0], pos);
108}
109
Will Drewrydecdfdc2011-09-27 15:13:54 -0500110FIXTURE(marshal) {
111 char buf[4096];
112 struct minijail *m;
113 struct minijail *j;
114 size_t size;
115};
116
117FIXTURE_SETUP(marshal) {
118 self->m = minijail_new();
119 self->j = minijail_new();
120 ASSERT_TRUE(self->m && self->j) TH_LOG("allocation failed");
121 self->size = minijail_size(self->m);
122 ASSERT_GT(sizeof(self->buf), self->size) {
123 TH_LOG("static buffer too small for test");
124 }
125}
126
127FIXTURE_TEARDOWN(marshal) {
128 minijail_destroy(self->m);
129 minijail_destroy(self->j);
130}
131
132TEST_F(marshal, empty) {
133 ASSERT_EQ(0, minijail_marshal(self->m, self->buf, sizeof(self->buf)));
134 EXPECT_EQ(0, minijail_unmarshal(self->j, self->buf, self->size));
135}
136
137TEST_F(marshal, 0xff) {
138 memset(self->buf, 0xff, sizeof(self->buf));
Will Drewry463c27a2011-10-21 15:52:43 -0500139 /* Should fail on the first consumestr since a NUL will never be found. */
140 EXPECT_EQ(-EINVAL, minijail_unmarshal(self->j, self->buf, sizeof(self->buf)));
Will Drewrydecdfdc2011-09-27 15:13:54 -0500141}
142
143TEST_F(marshal, short) {
144 ASSERT_EQ(0, minijail_marshal(self->m, self->buf, sizeof(self->buf)));
145 EXPECT_NE(0, minijail_unmarshal(self->j, self->buf, self->size / 2));
146}
147
148TEST_HARNESS_MAIN