blob: c038aff40cb85fb51f9295d026be231fb58d52ae [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>
19#include <sys/un.h>
20
21#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
22#include <sys/_system_properties.h>
23
Tom Cherry8702dcb2017-10-13 16:20:19 -070024#include <android-base/properties.h>
Elliott Hughesb005d902017-02-22 14:54:15 -080025#include <gtest/gtest.h>
26
Tom Cherry8702dcb2017-10-13 16:20:19 -070027using android::base::SetProperty;
28
Tom Cherry81f5d3e2017-06-22 12:53:17 -070029namespace android {
30namespace init {
31
Elliott Hughesb005d902017-02-22 14:54:15 -080032TEST(property_service, very_long_name_35166374) {
33 // Connect to the property service directly...
34 int fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
35 ASSERT_NE(fd, -1);
36
37 static const char* property_service_socket = "/dev/socket/" PROP_SERVICE_NAME;
38 sockaddr_un addr = {};
39 addr.sun_family = AF_LOCAL;
40 strlcpy(addr.sun_path, property_service_socket, sizeof(addr.sun_path));
41
42 socklen_t addr_len = strlen(property_service_socket) + offsetof(sockaddr_un, sun_path) + 1;
43 ASSERT_NE(connect(fd, reinterpret_cast<sockaddr*>(&addr), addr_len), -1);
44
45 // ...so we can send it a malformed request.
46 uint32_t msg = PROP_MSG_SETPROP2;
47 uint32_t size = 0xffffffff;
Elliott Hughesb005d902017-02-22 14:54:15 -080048
49 ASSERT_EQ(static_cast<ssize_t>(sizeof(msg)), send(fd, &msg, sizeof(msg), 0));
50 ASSERT_EQ(static_cast<ssize_t>(sizeof(size)), send(fd, &size, sizeof(size), 0));
Tom Cherryb7ef7e72018-02-20 10:40:26 -080051 uint32_t result = 0;
52 ASSERT_EQ(static_cast<ssize_t>(sizeof(result)),
53 TEMP_FAILURE_RETRY(recv(fd, &result, sizeof(result), MSG_WAITALL)));
54 EXPECT_EQ(static_cast<uint32_t>(PROP_ERROR_READ_DATA), result);
Elliott Hughesb005d902017-02-22 14:54:15 -080055 ASSERT_EQ(0, close(fd));
56}
Tom Cherry81f5d3e2017-06-22 12:53:17 -070057
Tom Cherry8702dcb2017-10-13 16:20:19 -070058TEST(property_service, non_utf8_value) {
59 ASSERT_TRUE(SetProperty("property_service_utf8_test", "base_success"));
60 EXPECT_FALSE(SetProperty("property_service_utf8_test", "\x80"));
61 EXPECT_FALSE(SetProperty("property_service_utf8_test", "\xC2\x01"));
62 EXPECT_FALSE(SetProperty("property_service_utf8_test", "\xE0\xFF"));
63 EXPECT_FALSE(SetProperty("property_service_utf8_test", "\xE0\xA0\xFF"));
64 EXPECT_FALSE(SetProperty("property_service_utf8_test", "\xF0\x01\xFF"));
65 EXPECT_FALSE(SetProperty("property_service_utf8_test", "\xF0\x90\xFF"));
66 EXPECT_FALSE(SetProperty("property_service_utf8_test", "\xF0\x90\x80\xFF"));
67 EXPECT_FALSE(SetProperty("property_service_utf8_test", "\xF0\x90\x80"));
68 EXPECT_FALSE(SetProperty("property_service_utf8_test", "ab\xF0\x90\x80\x80qe\xF0\x90\x80"));
69 EXPECT_TRUE(SetProperty("property_service_utf8_test", "\xF0\x90\x80\x80"));
70}
71
Tom Cherry81f5d3e2017-06-22 12:53:17 -070072} // namespace init
73} // namespace android