blob: c6dcfa2573918b3a8e78970e5c71a0af44ae1757 [file] [log] [blame]
Elliott Hughesb005d902017-02-22 14:54:15 -08001/*
2 * Copyright (C) 2017 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 <errno.h>
18#include <sys/socket.h>
19#include <sys/un.h>
20
21#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
22#include <sys/_system_properties.h>
23
Tom Cherry8702dcb2017-10-13 16:20:19 -070024#include <android-base/properties.h>
Nikita Ioffe92116e42020-03-31 01:28:35 +010025#include <android-base/scopeguard.h>
Elliott Hughesb005d902017-02-22 14:54:15 -080026#include <gtest/gtest.h>
27
Nikita Ioffe92116e42020-03-31 01:28:35 +010028using android::base::GetProperty;
Tom Cherry8702dcb2017-10-13 16:20:19 -070029using android::base::SetProperty;
30
Tom Cherry81f5d3e2017-06-22 12:53:17 -070031namespace android {
32namespace init {
33
Elliott Hughesb005d902017-02-22 14:54:15 -080034TEST(property_service, very_long_name_35166374) {
35 // Connect to the property service directly...
36 int fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
37 ASSERT_NE(fd, -1);
38
39 static const char* property_service_socket = "/dev/socket/" PROP_SERVICE_NAME;
40 sockaddr_un addr = {};
41 addr.sun_family = AF_LOCAL;
42 strlcpy(addr.sun_path, property_service_socket, sizeof(addr.sun_path));
43
44 socklen_t addr_len = strlen(property_service_socket) + offsetof(sockaddr_un, sun_path) + 1;
45 ASSERT_NE(connect(fd, reinterpret_cast<sockaddr*>(&addr), addr_len), -1);
46
47 // ...so we can send it a malformed request.
48 uint32_t msg = PROP_MSG_SETPROP2;
49 uint32_t size = 0xffffffff;
Elliott Hughesb005d902017-02-22 14:54:15 -080050
51 ASSERT_EQ(static_cast<ssize_t>(sizeof(msg)), send(fd, &msg, sizeof(msg), 0));
52 ASSERT_EQ(static_cast<ssize_t>(sizeof(size)), send(fd, &size, sizeof(size), 0));
Tom Cherryb7ef7e72018-02-20 10:40:26 -080053 uint32_t result = 0;
54 ASSERT_EQ(static_cast<ssize_t>(sizeof(result)),
55 TEMP_FAILURE_RETRY(recv(fd, &result, sizeof(result), MSG_WAITALL)));
56 EXPECT_EQ(static_cast<uint32_t>(PROP_ERROR_READ_DATA), result);
Elliott Hughesb005d902017-02-22 14:54:15 -080057 ASSERT_EQ(0, close(fd));
58}
Tom Cherry81f5d3e2017-06-22 12:53:17 -070059
Tom Cherry8702dcb2017-10-13 16:20:19 -070060TEST(property_service, non_utf8_value) {
Tom Cherry17b2be02019-08-20 10:43:48 -070061 if (getuid() != 0) {
62 GTEST_SKIP() << "Skipping test, must be run as root.";
63 return;
64 }
65
Tom Cherry8702dcb2017-10-13 16:20:19 -070066 ASSERT_TRUE(SetProperty("property_service_utf8_test", "base_success"));
67 EXPECT_FALSE(SetProperty("property_service_utf8_test", "\x80"));
68 EXPECT_FALSE(SetProperty("property_service_utf8_test", "\xC2\x01"));
69 EXPECT_FALSE(SetProperty("property_service_utf8_test", "\xE0\xFF"));
70 EXPECT_FALSE(SetProperty("property_service_utf8_test", "\xE0\xA0\xFF"));
71 EXPECT_FALSE(SetProperty("property_service_utf8_test", "\xF0\x01\xFF"));
72 EXPECT_FALSE(SetProperty("property_service_utf8_test", "\xF0\x90\xFF"));
73 EXPECT_FALSE(SetProperty("property_service_utf8_test", "\xF0\x90\x80\xFF"));
74 EXPECT_FALSE(SetProperty("property_service_utf8_test", "\xF0\x90\x80"));
75 EXPECT_FALSE(SetProperty("property_service_utf8_test", "ab\xF0\x90\x80\x80qe\xF0\x90\x80"));
76 EXPECT_TRUE(SetProperty("property_service_utf8_test", "\xF0\x90\x80\x80"));
77}
78
Nikita Ioffe92116e42020-03-31 01:28:35 +010079TEST(property_service, userspace_reboot_not_supported) {
80 if (getuid() != 0) {
81 GTEST_SKIP() << "Skipping test, must be run as root.";
82 return;
83 }
84 const std::string original_value = GetProperty("init.userspace_reboot.is_supported", "");
85 auto guard = android::base::make_scope_guard([&original_value]() {
86 SetProperty("init.userspace_reboot.is_supported", original_value);
87 });
88
89 ASSERT_TRUE(SetProperty("init.userspace_reboot.is_supported", "false"));
90 EXPECT_FALSE(SetProperty("sys.powerctl", "reboot,userspace"));
91}
92
Tom Cherry81f5d3e2017-06-22 12:53:17 -070093} // namespace init
94} // namespace android