| 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> |
| Elliott Hughes | 30203af | 2024-08-09 15:55:38 +0000 | [diff] [blame] | 19 | #include <sys/system_properties.h> |
| Elliott Hughes | b005d90 | 2017-02-22 14:54:15 -0800 | [diff] [blame] | 20 | #include <sys/un.h> |
| 21 | |
| Tom Cherry | 8702dcb | 2017-10-13 16:20:19 -0700 | [diff] [blame] | 22 | #include <android-base/properties.h> |
| Nikita Ioffe | 92116e4 | 2020-03-31 01:28:35 +0100 | [diff] [blame] | 23 | #include <android-base/scopeguard.h> |
| Tianjie | 69e880e | 2021-05-05 20:00:58 -0700 | [diff] [blame] | 24 | #include <android-base/strings.h> |
| Elliott Hughes | b005d90 | 2017-02-22 14:54:15 -0800 | [diff] [blame] | 25 | #include <gtest/gtest.h> |
| 26 | |
| Nikita Ioffe | 92116e4 | 2020-03-31 01:28:35 +0100 | [diff] [blame] | 27 | using android::base::GetProperty; |
| Tom Cherry | 8702dcb | 2017-10-13 16:20:19 -0700 | [diff] [blame] | 28 | using android::base::SetProperty; |
| 29 | |
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace init { |
| 32 | |
| Elliott Hughes | b005d90 | 2017-02-22 14:54:15 -0800 | [diff] [blame] | 33 | TEST(property_service, very_long_name_35166374) { |
| 34 | // Connect to the property service directly... |
| 35 | int fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 36 | ASSERT_NE(fd, -1); |
| 37 | |
| 38 | static const char* property_service_socket = "/dev/socket/" PROP_SERVICE_NAME; |
| 39 | sockaddr_un addr = {}; |
| 40 | addr.sun_family = AF_LOCAL; |
| 41 | strlcpy(addr.sun_path, property_service_socket, sizeof(addr.sun_path)); |
| 42 | |
| 43 | socklen_t addr_len = strlen(property_service_socket) + offsetof(sockaddr_un, sun_path) + 1; |
| 44 | ASSERT_NE(connect(fd, reinterpret_cast<sockaddr*>(&addr), addr_len), -1); |
| 45 | |
| 46 | // ...so we can send it a malformed request. |
| 47 | uint32_t msg = PROP_MSG_SETPROP2; |
| 48 | uint32_t size = 0xffffffff; |
| Elliott Hughes | b005d90 | 2017-02-22 14:54:15 -0800 | [diff] [blame] | 49 | |
| 50 | ASSERT_EQ(static_cast<ssize_t>(sizeof(msg)), send(fd, &msg, sizeof(msg), 0)); |
| 51 | 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] | 52 | uint32_t result = 0; |
| 53 | ASSERT_EQ(static_cast<ssize_t>(sizeof(result)), |
| 54 | TEMP_FAILURE_RETRY(recv(fd, &result, sizeof(result), MSG_WAITALL))); |
| 55 | EXPECT_EQ(static_cast<uint32_t>(PROP_ERROR_READ_DATA), result); |
| Elliott Hughes | b005d90 | 2017-02-22 14:54:15 -0800 | [diff] [blame] | 56 | ASSERT_EQ(0, close(fd)); |
| 57 | } |
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 58 | |
| Tom Cherry | 8702dcb | 2017-10-13 16:20:19 -0700 | [diff] [blame] | 59 | TEST(property_service, non_utf8_value) { |
| Tom Cherry | 17b2be0 | 2019-08-20 10:43:48 -0700 | [diff] [blame] | 60 | if (getuid() != 0) { |
| 61 | GTEST_SKIP() << "Skipping test, must be run as root."; |
| 62 | return; |
| 63 | } |
| 64 | |
| Tom Cherry | 8702dcb | 2017-10-13 16:20:19 -0700 | [diff] [blame] | 65 | ASSERT_TRUE(SetProperty("property_service_utf8_test", "base_success")); |
| 66 | EXPECT_FALSE(SetProperty("property_service_utf8_test", "\x80")); |
| 67 | EXPECT_FALSE(SetProperty("property_service_utf8_test", "\xC2\x01")); |
| 68 | EXPECT_FALSE(SetProperty("property_service_utf8_test", "\xE0\xFF")); |
| 69 | EXPECT_FALSE(SetProperty("property_service_utf8_test", "\xE0\xA0\xFF")); |
| 70 | EXPECT_FALSE(SetProperty("property_service_utf8_test", "\xF0\x01\xFF")); |
| 71 | EXPECT_FALSE(SetProperty("property_service_utf8_test", "\xF0\x90\xFF")); |
| 72 | EXPECT_FALSE(SetProperty("property_service_utf8_test", "\xF0\x90\x80\xFF")); |
| 73 | EXPECT_FALSE(SetProperty("property_service_utf8_test", "\xF0\x90\x80")); |
| 74 | EXPECT_FALSE(SetProperty("property_service_utf8_test", "ab\xF0\x90\x80\x80qe\xF0\x90\x80")); |
| 75 | EXPECT_TRUE(SetProperty("property_service_utf8_test", "\xF0\x90\x80\x80")); |
| 76 | } |
| 77 | |
| Nikita Ioffe | 92116e4 | 2020-03-31 01:28:35 +0100 | [diff] [blame] | 78 | TEST(property_service, userspace_reboot_not_supported) { |
| 79 | if (getuid() != 0) { |
| 80 | GTEST_SKIP() << "Skipping test, must be run as root."; |
| 81 | return; |
| 82 | } |
| Nikita Ioffe | 92116e4 | 2020-03-31 01:28:35 +0100 | [diff] [blame] | 83 | EXPECT_FALSE(SetProperty("sys.powerctl", "reboot,userspace")); |
| 84 | } |
| 85 | |
| Tianjie | 69e880e | 2021-05-05 20:00:58 -0700 | [diff] [blame] | 86 | TEST(property_service, check_fingerprint_with_legacy_build_id) { |
| 87 | std::string legacy_build_id = GetProperty("ro.build.legacy.id", ""); |
| 88 | if (legacy_build_id.empty()) { |
| 89 | GTEST_SKIP() << "Skipping test, legacy build id isn't set."; |
| 90 | } |
| 91 | |
| 92 | std::string vbmeta_digest = GetProperty("ro.boot.vbmeta.digest", ""); |
| 93 | ASSERT_GE(vbmeta_digest.size(), 8u); |
| Tianjie | c8cf2a4 | 2021-10-28 21:49:05 -0700 | [diff] [blame] | 94 | std::string build_id = GetProperty("ro.build.id", ""); |
| Tianjie | 69e880e | 2021-05-05 20:00:58 -0700 | [diff] [blame] | 95 | // Check that the build id is constructed with the prefix of vbmeta digest |
| 96 | std::string expected_build_id = legacy_build_id + "." + vbmeta_digest.substr(0, 8); |
| 97 | ASSERT_EQ(expected_build_id, build_id); |
| 98 | // Check that the fingerprint is constructed with the expected format. |
| 99 | std::string fingerprint = GetProperty("ro.build.fingerprint", ""); |
| 100 | std::vector<std::string> fingerprint_fields = { |
| 101 | GetProperty("ro.product.brand", ""), |
| 102 | "/", |
| 103 | GetProperty("ro.product.name", ""), |
| 104 | "/", |
| 105 | GetProperty("ro.product.device", ""), |
| 106 | ":", |
| 107 | GetProperty("ro.build.version.release_or_codename", ""), |
| 108 | "/", |
| 109 | expected_build_id, |
| 110 | "/", |
| 111 | GetProperty("ro.build.version.incremental", ""), |
| 112 | ":", |
| 113 | GetProperty("ro.build.type", ""), |
| 114 | "/", |
| 115 | GetProperty("ro.build.tags", "")}; |
| 116 | |
| 117 | ASSERT_EQ(android::base::Join(fingerprint_fields, ""), fingerprint); |
| 118 | } |
| 119 | |
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 120 | } // namespace init |
| 121 | } // namespace android |