xref: /aosp_15_r20/system/core/init/property_service_test.cpp (revision 00c7fec1bb09f3284aad6a6f96d2f63dfc3650ad)
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/system_properties.h>
20 #include <sys/un.h>
21 
22 #include <android-base/properties.h>
23 #include <android-base/scopeguard.h>
24 #include <android-base/strings.h>
25 #include <gtest/gtest.h>
26 
27 using android::base::GetProperty;
28 using android::base::SetProperty;
29 
30 namespace android {
31 namespace init {
32 
TEST(property_service,very_long_name_35166374)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;
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));
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);
56   ASSERT_EQ(0, close(fd));
57 }
58 
TEST(property_service,non_utf8_value)59 TEST(property_service, non_utf8_value) {
60     if (getuid() != 0) {
61         GTEST_SKIP() << "Skipping test, must be run as root.";
62         return;
63     }
64 
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 
TEST(property_service,userspace_reboot_not_supported)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     }
83     EXPECT_FALSE(SetProperty("sys.powerctl", "reboot,userspace"));
84 }
85 
TEST(property_service,check_fingerprint_with_legacy_build_id)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);
94     std::string build_id = GetProperty("ro.build.id", "");
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 
120 }  // namespace init
121 }  // namespace android
122