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