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