Patrick Venture | 605f75f | 2018-08-07 16:27:05 -0700 | [diff] [blame] | 1 | #include "flash-ipmi.hpp" |
| 2 | |
| 3 | #include <cstdio> |
| 4 | #include <cstring> |
Patrick Venture | 605f75f | 2018-08-07 16:27:05 -0700 | [diff] [blame] | 5 | #include <sdbusplus/test/sdbus_mock.hpp> |
| 6 | #include <string> |
| 7 | |
Patrick Venture | 1aedab2 | 2018-09-10 14:41:45 -0700 | [diff] [blame] | 8 | #include <gtest/gtest.h> |
| 9 | |
Patrick Venture | 605f75f | 2018-08-07 16:27:05 -0700 | [diff] [blame] | 10 | #define THIRTYTWO_MIB 33554432 |
| 11 | |
| 12 | using ::testing::IsNull; |
| 13 | using ::testing::NotNull; |
| 14 | using ::testing::Return; |
| 15 | using ::testing::StrEq; |
| 16 | |
| 17 | TEST(FlashIpmiAbortTest, VerifyItDeletesAndStopsServiced) |
| 18 | { |
| 19 | // Verify that once everything is started, the image and hash are deleted |
| 20 | // and it will try to stop the verification service. |
| 21 | |
| 22 | std::string iname = std::tmpnam(nullptr); |
| 23 | std::string vname = std::tmpnam(nullptr); |
| 24 | std::string hname = std::tmpnam(nullptr); |
| 25 | |
| 26 | sdbusplus::SdBusMock sdbus_mock; |
| 27 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 28 | |
| 29 | FlashUpdate updater(std::move(bus_mock), iname, vname, hname); |
| 30 | |
| 31 | std::vector<uint8_t> ibytes = {0xaa, 0x55}; |
| 32 | std::vector<uint8_t> hbytes = {0xcc, 0x65}; |
| 33 | |
| 34 | // Send the bytes up for the image. |
| 35 | EXPECT_TRUE(updater.start(THIRTYTWO_MIB)); |
| 36 | EXPECT_TRUE(updater.flashData(0, ibytes)); |
| 37 | EXPECT_TRUE(updater.flashFinish()); |
| 38 | |
| 39 | // Send the bytes up for the hash. |
| 40 | EXPECT_TRUE(updater.startHash(THIRTYTWO_MIB)); |
| 41 | EXPECT_TRUE(updater.hashData(0, hbytes)); |
| 42 | EXPECT_TRUE(updater.hashFinish()); |
| 43 | |
| 44 | // Verify the image bytes. |
| 45 | auto file = std::fopen(iname.c_str(), "r"); |
| 46 | EXPECT_TRUE(file); |
| 47 | uint8_t buffer[2]; |
| 48 | auto read = std::fread(buffer, 1, ibytes.size(), file); |
| 49 | EXPECT_EQ(read, ibytes.size()); |
| 50 | EXPECT_EQ(0, std::memcmp(buffer, ibytes.data(), ibytes.size())); |
| 51 | std::fclose(file); |
| 52 | |
| 53 | // Verify the hash bytes. |
| 54 | file = std::fopen(hname.c_str(), "r"); |
| 55 | EXPECT_TRUE(file); |
| 56 | read = std::fread(buffer, 1, hbytes.size(), file); |
| 57 | EXPECT_EQ(read, hbytes.size()); |
| 58 | EXPECT_EQ(0, std::memcmp(buffer, hbytes.data(), hbytes.size())); |
| 59 | std::fclose(file); |
| 60 | |
| 61 | EXPECT_CALL(sdbus_mock, |
| 62 | sd_bus_message_new_method_call( |
| 63 | IsNull(), NotNull(), StrEq("org.freedesktop.systemd1"), |
| 64 | StrEq("/org/freedesktop/systemd1"), |
| 65 | StrEq("org.freedesktop.systemd1.Manager"), |
| 66 | StrEq("StopUnit"))) |
| 67 | .WillOnce(Return(0)); |
| 68 | |
| 69 | // Send the abort. |
| 70 | EXPECT_TRUE(updater.abortUpdate()); |
| 71 | |
| 72 | // Verify the files are gone. |
| 73 | file = std::fopen(iname.c_str(), "r"); |
| 74 | EXPECT_FALSE(file); |
| 75 | file = std::fopen(hname.c_str(), "r"); |
| 76 | EXPECT_FALSE(file); |
| 77 | } |