blob: c12ff72e9705d3c7bc3436db35ba2e40ce1c7d76 [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>
Elliott Hughes30203af2024-08-09 15:55:38 +000019#include <sys/system_properties.h>
Elliott Hughesb005d902017-02-22 14:54:15 -080020#include <sys/un.h>
21
Tom Cherry8702dcb2017-10-13 16:20:19 -070022#include <android-base/properties.h>
Nikita Ioffe92116e42020-03-31 01:28:35 +010023#include <android-base/scopeguard.h>
Tianjie69e880e2021-05-05 20:00:58 -070024#include <android-base/strings.h>
Elliott Hughesb005d902017-02-22 14:54:15 -080025#include <gtest/gtest.h>
26
Nikita Ioffe92116e42020-03-31 01:28:35 +010027using android::base::GetProperty;
Tom Cherry8702dcb2017-10-13 16:20:19 -070028using android::base::SetProperty;
29
Tom Cherry81f5d3e2017-06-22 12:53:17 -070030namespace android {
31namespace init {
32
Elliott Hughesb005d902017-02-22 14:54:15 -080033TEST(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 Hughesb005d902017-02-22 14:54:15 -080049
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 Cherryb7ef7e72018-02-20 10:40:26 -080052 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 Hughesb005d902017-02-22 14:54:15 -080056 ASSERT_EQ(0, close(fd));
57}
Tom Cherry81f5d3e2017-06-22 12:53:17 -070058
Tom Cherry8702dcb2017-10-13 16:20:19 -070059TEST(property_service, non_utf8_value) {
Tom Cherry17b2be02019-08-20 10:43:48 -070060 if (getuid() != 0) {
61 GTEST_SKIP() << "Skipping test, must be run as root.";
62 return;
63 }
64
Tom Cherry8702dcb2017-10-13 16:20:19 -070065 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 Ioffe92116e42020-03-31 01:28:35 +010078TEST(property_service, userspace_reboot_not_supported) {
79 if (getuid() != 0) {
80 GTEST_SKIP() << "Skipping test, must be run as root.";
81 return;
82 }
Nikita Ioffe92116e42020-03-31 01:28:35 +010083 EXPECT_FALSE(SetProperty("sys.powerctl", "reboot,userspace"));
84}
85
Tianjie69e880e2021-05-05 20:00:58 -070086TEST(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);
Tianjiec8cf2a42021-10-28 21:49:05 -070094 std::string build_id = GetProperty("ro.build.id", "");
Tianjie69e880e2021-05-05 20:00:58 -070095 // 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 Cherry81f5d3e2017-06-22 12:53:17 -0700120} // namespace init
121} // namespace android