| Elliott Hughes | b005d90 | 2017-02-22 14:54:15 -0800 | [diff] [blame] | 1 | /* |
| 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 Cherry | 8702dcb | 2017-10-13 16:20:19 -0700 | [diff] [blame] | 24 | #include <android-base/properties.h> |
| Nikita Ioffe | 92116e4 | 2020-03-31 01:28:35 +0100 | [diff] [blame^] | 25 | #include <android-base/scopeguard.h> |
| Elliott Hughes | b005d90 | 2017-02-22 14:54:15 -0800 | [diff] [blame] | 26 | #include <gtest/gtest.h> |
| 27 | |
| Nikita Ioffe | 92116e4 | 2020-03-31 01:28:35 +0100 | [diff] [blame^] | 28 | using android::base::GetProperty; |
| Tom Cherry | 8702dcb | 2017-10-13 16:20:19 -0700 | [diff] [blame] | 29 | using android::base::SetProperty; |
| 30 | |
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 31 | namespace android { |
| 32 | namespace init { |
| 33 | |
| Elliott Hughes | b005d90 | 2017-02-22 14:54:15 -0800 | [diff] [blame] | 34 | TEST(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 Hughes | b005d90 | 2017-02-22 14:54:15 -0800 | [diff] [blame] | 50 | |
| 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 Cherry | b7ef7e7 | 2018-02-20 10:40:26 -0800 | [diff] [blame] | 53 | 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 Hughes | b005d90 | 2017-02-22 14:54:15 -0800 | [diff] [blame] | 57 | ASSERT_EQ(0, close(fd)); |
| 58 | } |
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 59 | |
| Tom Cherry | 8702dcb | 2017-10-13 16:20:19 -0700 | [diff] [blame] | 60 | TEST(property_service, non_utf8_value) { |
| Tom Cherry | 17b2be0 | 2019-08-20 10:43:48 -0700 | [diff] [blame] | 61 | if (getuid() != 0) { |
| 62 | GTEST_SKIP() << "Skipping test, must be run as root."; |
| 63 | return; |
| 64 | } |
| 65 | |
| Tom Cherry | 8702dcb | 2017-10-13 16:20:19 -0700 | [diff] [blame] | 66 | 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 Ioffe | 92116e4 | 2020-03-31 01:28:35 +0100 | [diff] [blame^] | 79 | TEST(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 Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 93 | } // namespace init |
| 94 | } // namespace android |