blob: 12ff7a89b42317e0bbd34eb8d2fe09c3fdda2201 [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>
6#include <string>
7#include <vector>
8
9#define THIRTYTWO_MIB 33554432
10
11class FlashIpmiHashDataTest : public ::testing::Test
12{
13 protected:
14 FlashIpmiHashDataTest() = default;
15
16 void SetUp() override
17 {
18 name = std::tmpnam(nullptr);
19 name2 = std::tmpnam(nullptr);
20 }
21 void TearDown() override
22 {
23 (void)std::remove(name.c_str());
24 (void)std::remove(name2.c_str());
25 }
26
27 std::string name;
28 std::string name2;
29};
30
31TEST_F(FlashIpmiHashDataTest, CalledOutOfSequenceFails)
32{
33 // Verify that there is sanity checking
34 std::vector<uint8_t> bytes = {0xaa, 0x55};
35
Patrick Venturefdc65b22018-08-07 14:37:58 -070036 FlashUpdate updater(name, "", name2);
Patrick Ventured5f590f2018-08-07 14:18:09 -070037 EXPECT_FALSE(updater.hashFinish());
38
39 // Verify the file doesn't exist.
40 auto file = std::fopen(name2.c_str(), "r");
41 EXPECT_FALSE(file);
42}
43
44TEST_F(FlashIpmiHashDataTest, CalledInSequenceSucceeds)
45{
46 // Verify that under normal usage it closes the file.
47 std::vector<uint8_t> bytes = {0xaa, 0x55};
48
Patrick Venturefdc65b22018-08-07 14:37:58 -070049 FlashUpdate updater(name, "", name2);
Patrick Ventured5f590f2018-08-07 14:18:09 -070050 EXPECT_TRUE(updater.start(THIRTYTWO_MIB));
51 EXPECT_TRUE(updater.startHash(THIRTYTWO_MIB));
52 EXPECT_TRUE(updater.hashFinish());
53
54 // Verify we can open the file, so we know it didn't get deleted.
55 auto file = std::fopen(name2.c_str(), "r");
56 EXPECT_TRUE(file);
57 std::fclose(file);
58}
59
60TEST_F(FlashIpmiHashDataTest, CalledTwiceFails)
61{
62 // Verify that under normal usage it closes the file, and therefore cannot
63 // be closed twice.
64 std::vector<uint8_t> bytes = {0xaa, 0x55};
65
Patrick Venturefdc65b22018-08-07 14:37:58 -070066 FlashUpdate updater(name, "", name2);
Patrick Ventured5f590f2018-08-07 14:18:09 -070067 EXPECT_TRUE(updater.start(THIRTYTWO_MIB));
68 EXPECT_TRUE(updater.startHash(THIRTYTWO_MIB));
69 EXPECT_TRUE(updater.hashFinish());
70
71 EXPECT_FALSE(updater.hashFinish());
72
73 // Verify we can open the file, so we know it didn't get deleted.
74 auto file = std::fopen(name2.c_str(), "r");
75 EXPECT_TRUE(file);
76 std::fclose(file);
77}