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 "usb_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 49 static int MatchFastboot(usb_ifc_info* info, const std::string& local_serial = ""); 50 bool UsbStillAvailible(); 51 bool UserSpaceFastboot(); 52 void ReconnectFastbootDevice(); 53 54 protected: 55 RetCode DownloadCommand(uint32_t size, std::string* response = nullptr, 56 std::vector<std::string>* info = nullptr); 57 58 RetCode SendBuffer(const std::vector<char>& buf); 59 RetCode HandleResponse(std::string* response = nullptr, 60 std::vector<std::string>* info = nullptr, int* dsize = nullptr); 61 62 void SetUp() override; 63 void TearDown() override; 64 void TearDownSerial(); 65 void SetLockState(bool unlock, bool assert_change = true); 66 67 std::unique_ptr<UsbTransportSniffer> transport; 68 std::unique_ptr<FastBootDriver> fb; 69 70 private: 71 // This is an annoying hack 72 static std::string cb_scratch; 73 static std::string device_path; 74 static std::string initial_slot; 75 }; 76 77 template <bool UNLOCKED> 78 class ModeTest : public FastBootTest { 79 protected: 80 void SetUp() override; 81 }; 82 83 class Fuzz : public ModeTest<true> { 84 protected: 85 void TearDown() override; 86 }; 87 88 // These derived classes without overrides serve no purpose other than to allow gtest to name them 89 // differently 90 class BasicFunctionality : public ModeTest<true> {}; 91 class Conformance : public ModeTest<true> {}; 92 class LogicalPartitionCompliance : public ModeTest<true> {}; 93 class UnlockPermissions : public ModeTest<true> {}; 94 class LockPermissions : public ModeTest<false> {}; 95 96 // Magic C++ double inheritance 97 class ExtensionsGetVarConformance 98 : public ModeTest<true>, 99 public ::testing::WithParamInterface< 100 std::pair<std::string, extension::Configuration::GetVar>> {}; 101 102 class ExtensionsOemConformance 103 : public ModeTest<true>, 104 public ::testing::WithParamInterface< 105 std::tuple<std::string, bool, extension::Configuration::CommandTest>> {}; 106 107 class ExtensionsPackedValid 108 : public ModeTest<true>, 109 public ::testing::WithParamInterface< 110 std::pair<std::string, extension::Configuration::PackedInfoTest>> {}; 111 112 class ExtensionsPackedInvalid 113 : public ModeTest<true>, 114 public ::testing::WithParamInterface< 115 std::pair<std::string, extension::Configuration::PackedInfoTest>> {}; 116 117 template <bool UNLOCKED> 118 class ExtensionsPartition 119 : public FastBootTest, 120 public ::testing::WithParamInterface< 121 std::pair<std::string, extension::Configuration::PartitionInfo>> { 122 protected: 123 void SetUp() override; 124 int64_t part_size; 125 int64_t max_flash; 126 int64_t max_dl; 127 std::vector<std::string> real_parts; // includes the slots 128 }; 129 130 class AnyPartition : public ExtensionsPartition<true> {}; 131 class WriteablePartition : public ExtensionsPartition<true> {}; 132 class WriteHashablePartition : public ExtensionsPartition<true> {}; 133 class WriteHashNonParsedPartition : public ExtensionsPartition<true> {}; 134 135 class FuzzWriteablePartition : public ExtensionsPartition<true> {}; 136 class FuzzWriteableParsedPartition : public ExtensionsPartition<true> {}; 137 class FuzzAnyPartitionLocked : public ExtensionsPartition<false> {}; 138 139 class UserdataPartition : public ExtensionsPartition<true> {}; 140 141 class SparseTestPartition : public ExtensionsPartition<true> {}; 142 143 } // end namespace fastboot 144