blob: e9f164d823f170bee515d536d3a9823701dc1305 [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
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080019#include <ctype.h>
Elliott Hughesf682b472015-02-06 12:19:48 -080020#include <errno.h>
Mark Salyzyn62767fe2016-10-27 07:45:34 -070021#include <fcntl.h>
22#include <stdlib.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <unistd.h>
26
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080027#include <android-base/stringprintf.h>
28#include <android-base/test_utils.h>
29#include <cutils/android_get_control_file.h>
Elliott Hughesf682b472015-02-06 12:19:48 -080030#include <gtest/gtest.h>
Mark Salyzyn62767fe2016-10-27 07:45:34 -070031#include <selinux/android.h>
Elliott Hughesf682b472015-02-06 12:19:48 -080032
33TEST(util, read_file_ENOENT) {
34 std::string s("hello");
35 errno = 0;
36 EXPECT_FALSE(read_file("/proc/does-not-exist", &s));
37 EXPECT_EQ(ENOENT, errno);
38 EXPECT_EQ("", s); // s was cleared.
39}
40
41TEST(util, read_file_success) {
42 std::string s("hello");
43 EXPECT_TRUE(read_file("/proc/version", &s));
44 EXPECT_GT(s.length(), 6U);
45 EXPECT_EQ('\n', s[s.length() - 1]);
46 s[5] = 0;
47 EXPECT_STREQ("Linux", s.c_str());
48}
Elliott Hughes8d82ea02015-02-06 20:15:18 -080049
50TEST(util, decode_uid) {
51 EXPECT_EQ(0U, decode_uid("root"));
Nick Kralevichd2104df2015-06-18 17:46:54 -070052 EXPECT_EQ(UINT_MAX, decode_uid("toot"));
Elliott Hughes8d82ea02015-02-06 20:15:18 -080053 EXPECT_EQ(123U, decode_uid("123"));
54}
Mark Salyzyn62767fe2016-10-27 07:45:34 -070055
56struct selabel_handle *sehandle;
57
58TEST(util, create_file) {
59 if (!sehandle) sehandle = selinux_android_file_context_handle();
60
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080061 TemporaryFile tf;
62 close(tf.fd);
63 EXPECT_GE(unlink(tf.path), 0);
Mark Salyzyn62767fe2016-10-27 07:45:34 -070064
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080065 std::string key(ANDROID_FILE_ENV_PREFIX);
66 key += tf.path;
67
68 std::for_each(key.begin(), key.end(), [] (char& c) { c = isalnum(c) ? c : '_'; });
69
70 EXPECT_EQ(unsetenv(key.c_str()), 0);
71
Mark Salyzyn62767fe2016-10-27 07:45:34 -070072 uid_t uid = decode_uid("logd");
73 gid_t gid = decode_uid("system");
74 mode_t perms = S_IRWXU | S_IWGRP | S_IRGRP | S_IROTH;
75 static const char context[] = "u:object_r:misc_logd_file:s0";
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080076 EXPECT_GE(tf.fd = create_file(tf.path, O_RDWR | O_CREAT, perms, uid, gid, context), 0);
77 if (tf.fd < 0) return;
Mark Salyzyn62767fe2016-10-27 07:45:34 -070078 static const char hello[] = "hello world\n";
79 static const ssize_t len = strlen(hello);
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080080 EXPECT_EQ(write(tf.fd, hello, len), len);
81 char buffer[sizeof(hello) + 1];
Mark Salyzyn62767fe2016-10-27 07:45:34 -070082 memset(buffer, 0, sizeof(buffer));
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080083 EXPECT_GE(lseek(tf.fd, 0, SEEK_SET), 0);
84 EXPECT_EQ(read(tf.fd, buffer, sizeof(buffer)), len);
85 EXPECT_EQ(std::string(hello), buffer);
86 EXPECT_EQ(android_get_control_file(tf.path), -1);
87 EXPECT_EQ(setenv(key.c_str(), android::base::StringPrintf("%d", tf.fd).c_str(), true), 0);
88 EXPECT_EQ(android_get_control_file(tf.path), tf.fd);
89 close(tf.fd);
90 EXPECT_EQ(android_get_control_file(tf.path), -1);
91 EXPECT_EQ(unsetenv(key.c_str()), 0);
Mark Salyzyn62767fe2016-10-27 07:45:34 -070092 struct stat st;
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080093 EXPECT_EQ(stat(tf.path, &st), 0);
Mark Salyzyn62767fe2016-10-27 07:45:34 -070094 EXPECT_EQ(st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO), perms);
95 EXPECT_EQ(st.st_uid, uid);
96 EXPECT_EQ(st.st_gid, gid);
97 security_context_t con;
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080098 EXPECT_GE(getfilecon(tf.path, &con), 0);
Mark Salyzyn62767fe2016-10-27 07:45:34 -070099 EXPECT_NE(con, static_cast<security_context_t>(NULL));
100 if (con) {
101 EXPECT_EQ(context, std::string(con));
102 }
103 freecon(con);
Mark Salyzyn52bd37e2016-11-07 09:39:30 -0800104 EXPECT_EQ(unlink(tf.path), 0);
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700105}