blob: a2304a2a0cb87087436ac217fe20b28fa3df18b4 [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
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -070011#include <sys/types.h>
12#include <sys/wait.h>
13
Will Drewrydecdfdc2011-09-27 15:13:54 -050014#include "test_harness.h"
15
16#include "libminijail.h"
17#include "libminijail-private.h"
18
Will Drewry3cb9f612011-10-21 20:52:08 -050019/* Prototypes needed only by test. */
20void *consumebytes(size_t length, char **buf, size_t *buflength);
21char *consumestr(char **buf, size_t *buflength);
22
Will Drewrydecdfdc2011-09-27 15:13:54 -050023/* Silence unused variable warnings. */
24TEST(silence_unused) {
25 EXPECT_STREQ(kLdPreloadEnvVar, kLdPreloadEnvVar);
26 EXPECT_STREQ(kFdEnvVar, kFdEnvVar);
27 EXPECT_STRNE(kFdEnvVar, kLdPreloadEnvVar);
28}
29
Will Drewry3cb9f612011-10-21 20:52:08 -050030TEST(consumebytes_zero) {
31 char buf[1024];
32 size_t len = sizeof(buf);
33 char *pos = &buf[0];
34 EXPECT_NE(NULL, consumebytes(0, &pos, &len));
35 EXPECT_EQ(&buf[0], pos);
36 EXPECT_EQ(sizeof(buf), len);
37}
38
39TEST(consumebytes_exact) {
40 char buf[1024];
41 size_t len = sizeof(buf);
42 char *pos = &buf[0];
43 /* One past the end since it consumes the whole buffer. */
44 char *end = &buf[sizeof(buf)];
45 EXPECT_NE(NULL, consumebytes(len, &pos, &len));
46 EXPECT_EQ((size_t)0, len);
47 EXPECT_EQ(end, pos);
48}
49
50TEST(consumebytes_half) {
51 char buf[1024];
52 size_t len = sizeof(buf);
53 char *pos = &buf[0];
54 /* One past the end since it consumes the whole buffer. */
55 char *end = &buf[sizeof(buf) / 2];
56 EXPECT_NE(NULL, consumebytes(len / 2, &pos, &len));
57 EXPECT_EQ(sizeof(buf) / 2, len);
58 EXPECT_EQ(end, pos);
59}
60
61TEST(consumebytes_toolong) {
62 char buf[1024];
63 size_t len = sizeof(buf);
64 char *pos = &buf[0];
65 /* One past the end since it consumes the whole buffer. */
66 EXPECT_EQ(NULL, consumebytes(len + 1, &pos, &len));
67 EXPECT_EQ(sizeof(buf), len);
68 EXPECT_EQ(&buf[0], pos);
69}
70
71TEST(consumestr_zero) {
72 char buf[1024];
73 size_t len = 0;
74 char *pos = &buf[0];
75 memset(buf, 0xff, sizeof(buf));
76 EXPECT_EQ(NULL, consumestr(&pos, &len));
77 EXPECT_EQ((size_t)0, len);
78 EXPECT_EQ(&buf[0], pos);
79}
80
81TEST(consumestr_nonul) {
82 char buf[1024];
83 size_t len = sizeof(buf);
84 char *pos = &buf[0];
85 memset(buf, 0xff, sizeof(buf));
86 EXPECT_EQ(NULL, consumestr(&pos, &len));
87 EXPECT_EQ(sizeof(buf), len);
88 EXPECT_EQ(&buf[0], pos);
89}
90
91TEST(consumestr_full) {
92 char buf[1024];
93 size_t len = sizeof(buf);
94 char *pos = &buf[0];
95 memset(buf, 0xff, sizeof(buf));
96 buf[sizeof(buf)-1] = '\0';
97 EXPECT_EQ((void *)buf, consumestr(&pos, &len));
98 EXPECT_EQ((size_t)0, len);
99 EXPECT_EQ(&buf[sizeof(buf)], pos);
100}
101
102TEST(consumestr_trailing_nul) {
103 char buf[1024];
104 size_t len = sizeof(buf) - 1;
105 char *pos = &buf[0];
106 memset(buf, 0xff, sizeof(buf));
107 buf[sizeof(buf)-1] = '\0';
108 EXPECT_EQ(NULL, consumestr(&pos, &len));
109 EXPECT_EQ(sizeof(buf) - 1, len);
110 EXPECT_EQ(&buf[0], pos);
111}
112
Will Drewrydecdfdc2011-09-27 15:13:54 -0500113FIXTURE(marshal) {
114 char buf[4096];
115 struct minijail *m;
116 struct minijail *j;
117 size_t size;
118};
119
120FIXTURE_SETUP(marshal) {
121 self->m = minijail_new();
122 self->j = minijail_new();
123 ASSERT_TRUE(self->m && self->j) TH_LOG("allocation failed");
124 self->size = minijail_size(self->m);
125 ASSERT_GT(sizeof(self->buf), self->size) {
126 TH_LOG("static buffer too small for test");
127 }
128}
129
130FIXTURE_TEARDOWN(marshal) {
131 minijail_destroy(self->m);
132 minijail_destroy(self->j);
133}
134
135TEST_F(marshal, empty) {
136 ASSERT_EQ(0, minijail_marshal(self->m, self->buf, sizeof(self->buf)));
137 EXPECT_EQ(0, minijail_unmarshal(self->j, self->buf, self->size));
138}
139
140TEST_F(marshal, 0xff) {
141 memset(self->buf, 0xff, sizeof(self->buf));
Will Drewry463c27a2011-10-21 15:52:43 -0500142 /* Should fail on the first consumestr since a NUL will never be found. */
143 EXPECT_EQ(-EINVAL, minijail_unmarshal(self->j, self->buf, sizeof(self->buf)));
Will Drewrydecdfdc2011-09-27 15:13:54 -0500144}
145
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700146TEST(test_minijail_run_pid_pipe) {
147 pid_t pid;
148 int child_stdin;
149 int mj_run_ret;
150 int write_ret;
151 int status;
152
153 struct minijail *j = minijail_new();
154 mj_run_ret = minijail_run_pid_pipe(j, "test/read_stdin",
155 NULL, &pid, &child_stdin);
156 EXPECT_EQ(mj_run_ret, 0);
157 write_ret = write(child_stdin, "test\n", strlen("test\n"));
158 EXPECT_GT(write_ret, -1);
159
160 waitpid(pid, &status, 0);
161
162 ASSERT_TRUE(WIFEXITED(status));
163 EXPECT_EQ(WEXITSTATUS(status), 0);
164
165 minijail_destroy(j);
Will Drewrydecdfdc2011-09-27 15:13:54 -0500166}
167
168TEST_HARNESS_MAIN