Alexander Hansen | 18b5e61 | 2025-08-08 11:35:40 +0200 | [diff] [blame^] | 1 | #include "../exampledevice/example_device.hpp" |
| 2 | |
| 3 | #include <phosphor-logging/lg2.hpp> |
| 4 | #include <sdbusplus/asio/connection.hpp> |
| 5 | #include <sdbusplus/asio/object_server.hpp> |
| 6 | #include <sdbusplus/async.hpp> |
| 7 | #include <sdbusplus/server.hpp> |
| 8 | |
| 9 | #include <memory> |
| 10 | #include <regex> |
| 11 | |
| 12 | #include <gtest/gtest.h> |
| 13 | |
| 14 | PHOSPHOR_LOG2_USING; |
| 15 | |
| 16 | using namespace phosphor::software; |
| 17 | using namespace phosphor::software::example_device; |
| 18 | |
| 19 | class SoftwareTest : public testing::Test |
| 20 | { |
| 21 | protected: |
| 22 | SoftwareTest() : |
| 23 | exampleUpdater(ctx, true, nullptr), device(exampleUpdater.getDevice()) |
| 24 | {} |
| 25 | ~SoftwareTest() noexcept override {} |
| 26 | |
| 27 | sdbusplus::async::context ctx; |
| 28 | ExampleCodeUpdater exampleUpdater; |
| 29 | std::unique_ptr<ExampleDevice>& device; |
| 30 | |
| 31 | public: |
| 32 | SoftwareTest(const SoftwareTest&) = delete; |
| 33 | SoftwareTest(SoftwareTest&&) = delete; |
| 34 | SoftwareTest& operator=(const SoftwareTest&) = delete; |
| 35 | SoftwareTest& operator=(SoftwareTest&&) = delete; |
| 36 | }; |
| 37 | |
| 38 | TEST_F(SoftwareTest, TestSoftwareConstructor) |
| 39 | { |
| 40 | // the software version is currently unknown |
| 41 | EXPECT_EQ(device->softwareCurrent, nullptr); |
| 42 | |
| 43 | auto sw = std::make_unique<Software>(ctx, *device); |
| 44 | |
| 45 | // since that software is not an update, there is no progress |
| 46 | EXPECT_EQ(sw->softwareActivationProgress, nullptr); |
| 47 | } |
| 48 | |
| 49 | TEST_F(SoftwareTest, TestVersionPurpose) |
| 50 | { |
| 51 | auto sw = std::make_unique<ExampleSoftware>(ctx, *device); |
| 52 | |
| 53 | EXPECT_EQ(sw->getPurpose(), std::nullopt); |
| 54 | |
| 55 | sw->setVersion("swVersion"); |
| 56 | EXPECT_EQ(sw->getPurpose(), SoftwareVersion::VersionPurpose::Unknown); |
| 57 | } |
| 58 | |
| 59 | TEST_F(SoftwareTest, TestSoftwareId) |
| 60 | { |
| 61 | auto sw = std::make_unique<Software>(ctx, *device); |
| 62 | |
| 63 | std::regex re("ExampleSoftware_[0-9]+"); |
| 64 | std::cmatch m; |
| 65 | |
| 66 | // design: Swid = <DeviceX>_<RandomId> |
| 67 | EXPECT_TRUE(std::regex_match(sw->swid.c_str(), m, re)); |
| 68 | } |
| 69 | |
| 70 | TEST_F(SoftwareTest, TestSoftwareObjectPath) |
| 71 | { |
| 72 | auto sw = std::make_unique<ExampleSoftware>(ctx, *device); |
| 73 | |
| 74 | debug("{PATH}", "PATH", sw->objectPath); |
| 75 | |
| 76 | // assert that the object path is as per the design |
| 77 | // design: /xyz/openbmc_project/Software/<SwId> |
| 78 | EXPECT_TRUE(std::string(sw->objectPath) |
| 79 | .starts_with("/xyz/openbmc_project/software/")); |
| 80 | } |