blob: 6ecbf908c1d6ab1b371a8bf31f494162eeab2b85 [file] [log] [blame]
Elliott Hughesf682b472015-02-06 12:19:48 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "util.h"
18
19#include <errno.h>
Mark Salyzyn62767fe2016-10-27 07:45:34 -070020#include <fcntl.h>
21#include <stdlib.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24#include <unistd.h>
25
26#include <cutils/files.h>
Elliott Hughesf682b472015-02-06 12:19:48 -080027#include <gtest/gtest.h>
Mark Salyzyn62767fe2016-10-27 07:45:34 -070028#include <selinux/android.h>
Elliott Hughesf682b472015-02-06 12:19:48 -080029
30TEST(util, read_file_ENOENT) {
31 std::string s("hello");
32 errno = 0;
33 EXPECT_FALSE(read_file("/proc/does-not-exist", &s));
34 EXPECT_EQ(ENOENT, errno);
35 EXPECT_EQ("", s); // s was cleared.
36}
37
38TEST(util, read_file_success) {
39 std::string s("hello");
40 EXPECT_TRUE(read_file("/proc/version", &s));
41 EXPECT_GT(s.length(), 6U);
42 EXPECT_EQ('\n', s[s.length() - 1]);
43 s[5] = 0;
44 EXPECT_STREQ("Linux", s.c_str());
45}
Elliott Hughes8d82ea02015-02-06 20:15:18 -080046
47TEST(util, decode_uid) {
48 EXPECT_EQ(0U, decode_uid("root"));
Nick Kralevichd2104df2015-06-18 17:46:54 -070049 EXPECT_EQ(UINT_MAX, decode_uid("toot"));
Elliott Hughes8d82ea02015-02-06 20:15:18 -080050 EXPECT_EQ(123U, decode_uid("123"));
51}
Mark Salyzyn62767fe2016-10-27 07:45:34 -070052
53struct selabel_handle *sehandle;
54
55TEST(util, create_file) {
56 if (!sehandle) sehandle = selinux_android_file_context_handle();
57
58 static const char path[] = "/data/local/tmp/util.create_file.test";
59 static const char key[] = ANDROID_FILE_ENV_PREFIX "_data_local_tmp_util_create_file_test";
60 EXPECT_EQ(unsetenv(key), 0);
61 unlink(path);
62
63 int fd;
64 uid_t uid = decode_uid("logd");
65 gid_t gid = decode_uid("system");
66 mode_t perms = S_IRWXU | S_IWGRP | S_IRGRP | S_IROTH;
67 static const char context[] = "u:object_r:misc_logd_file:s0";
68 EXPECT_GE(fd = create_file(path, O_RDWR | O_CREAT, perms, uid, gid, context), 0);
69 if (fd < 0) return;
70 static const char hello[] = "hello world\n";
71 static const ssize_t len = strlen(hello);
72 EXPECT_EQ(write(fd, hello, len), len);
73 char buffer[sizeof(hello)];
74 memset(buffer, 0, sizeof(buffer));
75 EXPECT_GE(lseek(fd, 0, SEEK_SET), 0);
76 EXPECT_EQ(read(fd, buffer, sizeof(buffer)), len);
77 EXPECT_EQ(strcmp(hello, buffer), 0);
78 char val[32];
79 snprintf(val, sizeof(val), "%d", fd);
80 EXPECT_EQ(android_get_control_file(path), -1);
81 setenv(key, val, true);
82 EXPECT_EQ(android_get_control_file(path), fd);
83 close(fd);
84 EXPECT_EQ(android_get_control_file(path), -1);
85 EXPECT_EQ(unsetenv(key), 0);
86 struct stat st;
87 EXPECT_EQ(stat(path, &st), 0);
88 EXPECT_EQ(st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO), perms);
89 EXPECT_EQ(st.st_uid, uid);
90 EXPECT_EQ(st.st_gid, gid);
91 security_context_t con;
92 EXPECT_GE(getfilecon(path, &con), 0);
93 EXPECT_NE(con, static_cast<security_context_t>(NULL));
94 if (con) {
95 EXPECT_EQ(context, std::string(con));
96 }
97 freecon(con);
98 EXPECT_EQ(unlink(path), 0);
99}