1 /*
2  * Copyright (C) 2023 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 "restart_service.h"
18 
19 #include <time.h>
20 #include <string>
21 
22 #include <android-base/properties.h>
23 #include <android-base/stringprintf.h>
24 #include <android-base/strings.h>
25 #include <gtest/gtest.h>
26 
27 #include "services.h"
28 #include "sysdeps.h"
29 #include "test_utils/test_utils.h"
30 
31 using namespace test_utils;
32 
33 // Test successful execution of tcp restart.
TEST(RestartServiceTest,RestartTcpServiceValidPortSuccess)34 TEST(RestartServiceTest, RestartTcpServiceValidPortSuccess) {
35     // Identify an available port from the system allocated pool.
36     // The objective is to do a best guess attempt at randomizing the
37     // specified port on which the restart service listens.
38     unique_fd dontcare;
39     const int assigned_port(test_utils::GetUnassignedPort(dontcare));
40 
41     unique_fd command_fd_ = create_service_thread(
42             "tcp", std::bind(restart_tcp_service, std::placeholders::_1, assigned_port));
43     EXPECT_GE(command_fd_.get(), 0);
44     test_utils::ExpectLinesEqual(
45             ReadRaw(command_fd_),
46             {android::base::StringPrintf("restarting in TCP mode port: %d", assigned_port)});
47 
48     EXPECT_EQ(android::base::GetProperty("service.adb.tcp.port", ""),
49               std::to_string(assigned_port));
50 }
51 
52 // Test failure path of  tcp restart.
TEST(RestartServiceTest,RestartTcpServiceInvalidPortFailure)53 TEST(RestartServiceTest, RestartTcpServiceInvalidPortFailure) {
54     const std::string port_str = android::base::GetProperty("service.adb.tcp.port", "");
55 
56     const int port = -5;
57     unique_fd command_fd_ = create_service_thread(
58             "tcp", std::bind(restart_tcp_service, std::placeholders::_1, port));
59     EXPECT_GE(command_fd_, 0);
60     test_utils::ExpectLinesEqual(ReadRaw(command_fd_),
61                                  {android::base::StringPrintf("invalid port %d", port)});
62 
63     // Validate that there's no mutation.
64     EXPECT_EQ(android::base::GetProperty("service.adb.tcp.port", ""), port_str);
65 }
66 
67 // Test successful execution of usb restart.
TEST(RestartServiceTest,RestartUsbServiceSuccess)68 TEST(RestartServiceTest, RestartUsbServiceSuccess) {
69     unique_fd command_fd_ = create_service_thread("usb", restart_usb_service);
70     EXPECT_GE(command_fd_, 0);
71 
72     test_utils::ExpectLinesEqual(ReadRaw(command_fd_),
73                                  {android::base::StringPrintf("restarting in USB mode")});
74 
75     EXPECT_EQ(android::base::GetProperty("service.adb.tcp.port", ""), "0");
76 }
77