blob: 2d5e5b06647b59467ae15cf556b9d1abfd5e14b1 [file] [log] [blame]
Patrick Ventured5f590f2018-08-07 14:18:09 -07001#include "flash-ipmi.hpp"
2
3#include <cstdio>
4#include <cstring>
5#include <gtest/gtest.h>
Patrick Venture605f75f2018-08-07 16:27:05 -07006#include <sdbusplus/test/sdbus_mock.hpp>
Patrick Ventured5f590f2018-08-07 14:18:09 -07007#include <string>
8#include <vector>
9
10#define THIRTYTWO_MIB 33554432
11
12class FlashIpmiHashDataTest : public ::testing::Test
13{
14 protected:
15 FlashIpmiHashDataTest() = default;
16
17 void SetUp() override
18 {
19 name = std::tmpnam(nullptr);
20 name2 = std::tmpnam(nullptr);
21 }
22 void TearDown() override
23 {
24 (void)std::remove(name.c_str());
25 (void)std::remove(name2.c_str());
26 }
27
28 std::string name;
29 std::string name2;
30};
31
32TEST_F(FlashIpmiHashDataTest, CalledOutOfSequenceFails)
33{
34 // Verify that there is sanity checking
35 std::vector<uint8_t> bytes = {0xaa, 0x55};
36
Patrick Venture605f75f2018-08-07 16:27:05 -070037 sdbusplus::SdBusMock sdbus_mock;
38 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
39
40 FlashUpdate updater(std::move(bus_mock), name, "", name2);
Patrick Ventured5f590f2018-08-07 14:18:09 -070041 EXPECT_FALSE(updater.hashFinish());
42
43 // Verify the file doesn't exist.
44 auto file = std::fopen(name2.c_str(), "r");
45 EXPECT_FALSE(file);
46}
47
48TEST_F(FlashIpmiHashDataTest, CalledInSequenceSucceeds)
49{
50 // Verify that under normal usage it closes the file.
51 std::vector<uint8_t> bytes = {0xaa, 0x55};
52
Patrick Venture605f75f2018-08-07 16:27:05 -070053 sdbusplus::SdBusMock sdbus_mock;
54 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
55
56 FlashUpdate updater(std::move(bus_mock), name, "", name2);
Patrick Ventured5f590f2018-08-07 14:18:09 -070057 EXPECT_TRUE(updater.start(THIRTYTWO_MIB));
58 EXPECT_TRUE(updater.startHash(THIRTYTWO_MIB));
59 EXPECT_TRUE(updater.hashFinish());
60
61 // Verify we can open the file, so we know it didn't get deleted.
62 auto file = std::fopen(name2.c_str(), "r");
63 EXPECT_TRUE(file);
64 std::fclose(file);
65}
66
67TEST_F(FlashIpmiHashDataTest, CalledTwiceFails)
68{
69 // Verify that under normal usage it closes the file, and therefore cannot
70 // be closed twice.
71 std::vector<uint8_t> bytes = {0xaa, 0x55};
72
Patrick Venture605f75f2018-08-07 16:27:05 -070073 sdbusplus::SdBusMock sdbus_mock;
74 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
75
76 FlashUpdate updater(std::move(bus_mock), name, "", name2);
Patrick Ventured5f590f2018-08-07 14:18:09 -070077 EXPECT_TRUE(updater.start(THIRTYTWO_MIB));
78 EXPECT_TRUE(updater.startHash(THIRTYTWO_MIB));
79 EXPECT_TRUE(updater.hashFinish());
80
81 EXPECT_FALSE(updater.hashFinish());
82
83 // Verify we can open the file, so we know it didn't get deleted.
84 auto file = std::fopen(name2.c_str(), "r");
85 EXPECT_TRUE(file);
86 std::fclose(file);
87}