Home | History | Annotate | Download | only in init
      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 <gtest/gtest.h>
     26 
     27 using android::base::SetProperty;
     28 
     29 namespace android {
     30 namespace init {
     31 
     32 TEST(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;
     48 
     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));
     51   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);
     55   ASSERT_EQ(0, close(fd));
     56 }
     57 
     58 TEST(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 
     72 }  // namespace init
     73 }  // namespace android
     74