1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in 12 * the documentation and/or other materials provided with the 13 * distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 #pragma once 29 #include <gtest/gtest.h> 30 31 #include "fastboot_driver.h" 32 33 #include "extensions.h" 34 #include "transport_sniffer.h" 35 36 namespace fastboot { 37 38 const int USB_TIMEOUT = 30000; 39 40 constexpr char USB_PORT_GONE[] = 41 "The USB port has disappeared, this is usually due to the bootloader crashing"; 42 43 class FastBootTest : public testing::Test { 44 public: 45 static int serial_port; 46 static std::string device_serial; 47 static constexpr int MAX_USB_TRIES = 10; 48 static constexpr int MAX_TCP_TRIES = 6000; 49 50 static int MatchFastboot(usb_ifc_info* info, const std::string& local_serial = ""); 51 static bool IsFastbootOverTcp(); 52 bool UsbStillAvailible(); 53 bool UserSpaceFastboot(); 54 void ReconnectFastbootDevice(); 55 void ConnectTcpFastbootDevice(); 56 57 protected: 58 RetCode DownloadCommand(uint32_t size, std::string* response = nullptr, 59 std::vector<std::string>* info = nullptr); 60 61 RetCode SendBuffer(const std::vector<char>& buf); 62 RetCode HandleResponse(std::string* response = nullptr, 63 std::vector<std::string>* info = nullptr, int* dsize = nullptr); 64 65 void SetUp() override; 66 void TearDown() override; 67 void TearDownSerial(); 68 void SetLockState(bool unlock, bool assert_change = true); 69 70 std::unique_ptr<TransportSniffer> transport; 71 std::unique_ptr<FastBootDriver> fb; 72 73 private: 74 // This is an annoying hack 75 static std::string cb_scratch; 76 static std::string device_path; 77 static std::string initial_slot; 78 }; 79 80 template <bool UNLOCKED> 81 class ModeTest : public FastBootTest { 82 protected: 83 void SetUp() override; 84 }; 85 86 class Fuzz : public ModeTest<true> { 87 protected: 88 void TearDown() override; 89 }; 90 91 // These derived classes without overrides serve no purpose other than to allow gtest to name them 92 // differently 93 class BasicFunctionality : public ModeTest<true> {}; 94 class Conformance : public ModeTest<true> {}; 95 class LogicalPartitionCompliance : public ModeTest<true> {}; 96 class UnlockPermissions : public ModeTest<true> {}; 97 class LockPermissions : public ModeTest<false> {}; 98 99 // Magic C++ double inheritance 100 class ExtensionsGetVarConformance 101 : public ModeTest<true>, 102 public ::testing::WithParamInterface< 103 std::pair<std::string, extension::Configuration::GetVar>> {}; 104 105 class ExtensionsOemConformance 106 : public ModeTest<true>, 107 public ::testing::WithParamInterface< 108 std::tuple<std::string, bool, extension::Configuration::CommandTest>> {}; 109 110 class ExtensionsPackedValid 111 : public ModeTest<true>, 112 public ::testing::WithParamInterface< 113 std::pair<std::string, extension::Configuration::PackedInfoTest>> {}; 114 115 class ExtensionsPackedInvalid 116 : public ModeTest<true>, 117 public ::testing::WithParamInterface< 118 std::pair<std::string, extension::Configuration::PackedInfoTest>> {}; 119 120 template <bool UNLOCKED> 121 class ExtensionsPartition 122 : public FastBootTest, 123 public ::testing::WithParamInterface< 124 std::pair<std::string, extension::Configuration::PartitionInfo>> { 125 protected: 126 void SetUp() override; 127 int64_t part_size; 128 int64_t max_flash; 129 int64_t max_dl; 130 std::vector<std::string> real_parts; // includes the slots 131 }; 132 133 class AnyPartition : public ExtensionsPartition<true> {}; 134 class WriteablePartition : public ExtensionsPartition<true> {}; 135 class WriteHashablePartition : public ExtensionsPartition<true> {}; 136 class WriteHashNonParsedPartition : public ExtensionsPartition<true> {}; 137 138 class FuzzWriteablePartition : public ExtensionsPartition<true> {}; 139 class FuzzWriteableParsedPartition : public ExtensionsPartition<true> {}; 140 class FuzzAnyPartitionLocked : public ExtensionsPartition<false> {}; 141 142 class UserdataPartition : public ExtensionsPartition<true> {}; 143 144 class SparseTestPartition : public ExtensionsPartition<true> {}; 145 146 } // end namespace fastboot 147