| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include <catch2/catch_all.hpp> |
| 5 | #include <catch2/trompeloeil.hpp> |
| 6 | |
| 7 | #include "core/idatasource.h" |
| 8 | #include "core/info/common/gpuinfouevent.h" |
| 9 | #include "core/info/ihwidtranslator.h" |
| 10 | |
| 11 | extern template struct trompeloeil::reporter<trompeloeil::specialized>; |
| 12 | |
| 13 | using trompeloeil::_; |
| 14 | |
| 15 | namespace Tests::GPUInfoUevent { |
| 16 | |
| 17 | class GPUInfoUeventDataSourceStub |
| 18 | : public IDataSource<std::vector<std::string>, std::filesystem::path const> |
| 19 | { |
| 20 | public: |
| 21 | std::string source() const override |
| 22 | { |
| 23 | return "uevent"; |
| 24 | } |
| 25 | |
| 26 | bool read(std::vector<std::string> &data, std::filesystem::path const &) override |
| 27 | { |
| 28 | data.clear(); |
| 29 | data.emplace_back("DRIVER=driver"); |
| 30 | data.emplace_back("PCI_ID=1111:2222"); |
| 31 | data.emplace_back("PCI_SUBSYS_ID=3333:4444"); |
| 32 | data.emplace_back("PCI_SLOT_NAME=0000:01:00.0"); |
| 33 | return true; |
| 34 | } |
| 35 | }; |
| 36 | |
| 37 | class HWIDTranslatorMock : public IHWIDTranslator |
| 38 | { |
| 39 | public: |
| 40 | MAKE_CONST_MOCK1(vendor, std::string(std::string const &)); |
| 41 | MAKE_CONST_MOCK2(device, std::string(std::string const &, std::string const &)); |
| 42 | MAKE_CONST_MOCK4(subdevice, |
| 43 | std::string(std::string const &, std::string const &, |
| 44 | std::string const &, std::string const &)); |
| 45 | }; |
| 46 | |
| 47 | TEST_CASE("GPUInfoUevent tests", "[Info][GPUInfo][GPUInfoUevent]") |
| 48 | { |
| 49 | std::string vName("v_name"); |
| 50 | std::string dName("d_name"); |
| 51 | std::string sdName("s_name"); |
| 52 | |
| 53 | Vendor vendor(Vendor::AMD); |
| 54 | int const gpuIndex = 0; |
| 55 | IGPUInfo::Path path("_sys_", "_dev_"); |
| 56 | HWIDTranslatorMock hwIDTranslator; |
| 57 | ALLOW_CALL(hwIDTranslator, vendor(_)).LR_RETURN(vName); |
| 58 | ALLOW_CALL(hwIDTranslator, device(_, _)).LR_RETURN(dName); |
| 59 | ALLOW_CALL(hwIDTranslator, subdevice(_, _, _, _)).LR_RETURN(sdName); |
| 60 | |
| 61 | ::GPUInfoUevent ts(std::make_unique<GPUInfoUeventDataSourceStub>()); |
| 62 | |
| 63 | SECTION("Provides driver") |
| 64 | { |
| 65 | auto output = ts.provideInfo(vendor, gpuIndex, path, hwIDTranslator); |
| 66 | auto driver = std::make_pair(std::string(IGPUInfo::Keys::driver), |
| 67 | std::string("driver")); |
| 68 | |
| 69 | REQUIRE_THAT(output, Catch::Matchers::VectorContains(driver)); |
| 70 | } |
| 71 | |
| 72 | SECTION("Provides PCI slot") |
| 73 | { |
| 74 | auto output = ts.provideInfo(vendor, gpuIndex, path, hwIDTranslator); |
| 75 | auto driver = std::make_pair(std::string(IGPUInfo::Keys::pciSlot), |
| 76 | std::string("0000:01:00.0")); |
| 77 | |
| 78 | REQUIRE_THAT(output, Catch::Matchers::VectorContains(driver)); |
| 79 | } |
| 80 | |
| 81 | SECTION("Provides vendor id") |
| 82 | { |
| 83 | auto output = ts.provideInfo(vendor, gpuIndex, path, hwIDTranslator); |
| 84 | auto vendorID = std::make_pair(std::string(IGPUInfo::Keys::vendorID), |
| 85 | std::string("1111")); |
| 86 | |
| 87 | REQUIRE_THAT(output, Catch::Matchers::VectorContains(vendorID)); |
| 88 | } |
| 89 | |
| 90 | SECTION("Provides vendor name") |
| 91 | { |
| 92 | REQUIRE_CALL(hwIDTranslator, vendor("1111")).LR_RETURN(vName); |
| 93 | auto output = ts.provideInfo(vendor, gpuIndex, path, hwIDTranslator); |
| 94 | auto vendorName = std::make_pair(std::string(IGPUInfo::Keys::vendorName), |
| 95 | vName); |
| 96 | |
| 97 | REQUIRE_THAT(output, Catch::Matchers::VectorContains(vendorName)); |
| 98 | } |
| 99 | |
| 100 | SECTION("Provides device id") |
| 101 | { |
| 102 | auto output = ts.provideInfo(vendor, gpuIndex, path, hwIDTranslator); |
| 103 | auto deviceID = std::make_pair(std::string(IGPUInfo::Keys::deviceID), |
| 104 | std::string("2222")); |
| 105 | |
| 106 | REQUIRE_THAT(output, Catch::Matchers::VectorContains(deviceID)); |
| 107 | } |
| 108 | |
| 109 | SECTION("Provides device name") |
| 110 | { |
| 111 | REQUIRE_CALL(hwIDTranslator, device("1111", "2222")).LR_RETURN(dName); |
| 112 | |
| 113 | auto output = ts.provideInfo(vendor, gpuIndex, path, hwIDTranslator); |
| 114 | auto deviceName = std::make_pair(std::string(IGPUInfo::Keys::deviceName), |
| 115 | dName); |
| 116 | |
| 117 | REQUIRE_THAT(output, Catch::Matchers::VectorContains(deviceName)); |
| 118 | } |
| 119 | |
| 120 | SECTION("Provides subvendor id") |
| 121 | { |
| 122 | auto output = ts.provideInfo(vendor, gpuIndex, path, hwIDTranslator); |
| 123 | auto subvendorID = std::make_pair(std::string(IGPUInfo::Keys::subvendorID), |
| 124 | std::string("3333")); |
| 125 | |
| 126 | REQUIRE_THAT(output, Catch::Matchers::VectorContains(subvendorID)); |
| 127 | } |
| 128 | |
| 129 | SECTION("Provides subdevice id") |
| 130 | { |
| 131 | auto output = ts.provideInfo(vendor, gpuIndex, path, hwIDTranslator); |
| 132 | auto subdeviceID = std::make_pair(std::string(IGPUInfo::Keys::subdeviceID), |
| 133 | std::string("4444")); |
| 134 | |
| 135 | REQUIRE_THAT(output, Catch::Matchers::VectorContains(subdeviceID)); |
| 136 | } |
| 137 | |
| 138 | SECTION("Provides subdevice name") |
| 139 | { |
| 140 | REQUIRE_CALL(hwIDTranslator, subdevice("1111", "2222", "3333", "4444")) |
| 141 | .LR_RETURN(sdName); |
| 142 | |
| 143 | auto output = ts.provideInfo(vendor, gpuIndex, path, hwIDTranslator); |
| 144 | auto subdeviceName = std::make_pair( |
| 145 | std::string(IGPUInfo::Keys::subdeviceName), sdName); |
| 146 | |
| 147 | REQUIRE_THAT(output, Catch::Matchers::VectorContains(subdeviceName)); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | } // namespace Tests::GPUInfoUevent |