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