blob: 8d7be774e0301a81eb4cd96f270c1be3f3caf7c3 [file] [log] [blame]
Patrick Venturecbe51492018-08-07 14:09:17 -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 if you try to write to the hash before starting, it'll fail.
34 // Presently, start() will open the hash file.
35
36 std::vector<uint8_t> bytes = {0xaa, 0x55};
37
Patrick Venturefdc65b22018-08-07 14:37:58 -070038 FlashUpdate updater(name, "", name2);
Patrick Venturecbe51492018-08-07 14:09:17 -070039 EXPECT_FALSE(updater.hashData(0, bytes));
40}
41
42TEST_F(FlashIpmiHashDataTest, CalledWithDataSucceeds)
43{
44 // Verify the normal use case works.
45 std::vector<uint8_t> bytes = {0xaa, 0x55};
46
Patrick Venturefdc65b22018-08-07 14:37:58 -070047 FlashUpdate updater(name, "", name2);
Patrick Venturecbe51492018-08-07 14:09:17 -070048 EXPECT_TRUE(updater.start(THIRTYTWO_MIB));
49 EXPECT_TRUE(updater.startHash(THIRTYTWO_MIB));
50 EXPECT_TRUE(updater.hashData(0, bytes));
51
52 auto file = std::fopen(name2.c_str(), "r");
53 EXPECT_TRUE(file);
54
55 uint8_t buffer[2];
56 auto read = std::fread(buffer, 1, bytes.size(), file);
57 EXPECT_EQ(read, bytes.size());
58 EXPECT_EQ(0, std::memcmp(buffer, bytes.data(), bytes.size()));
59 std::fclose(file);
60}
61
62TEST_F(FlashIpmiHashDataTest, CalledNonZeroOffsetSucceeds)
63{
64 // Skipping bytes in POSIX can create a sparse file. In this case,
65 // let's allow the behavior. If we'd rather, we can have writeBlock
66 // check that the offset is where we expect.
67
68 std::vector<uint8_t> bytes = {0xaa, 0x55};
69
Patrick Venturefdc65b22018-08-07 14:37:58 -070070 FlashUpdate updater(name, "", name2);
Patrick Venturecbe51492018-08-07 14:09:17 -070071 EXPECT_TRUE(updater.start(THIRTYTWO_MIB));
72 EXPECT_TRUE(updater.startHash(THIRTYTWO_MIB));
73 EXPECT_TRUE(updater.hashData(2, bytes));
74
75 auto file = std::fopen(name2.c_str(), "r");
76 EXPECT_TRUE(file);
77
78 uint8_t buffer[4];
79 auto read = std::fread(buffer, 1, sizeof(buffer), file);
80 EXPECT_EQ(read, sizeof(buffer));
81 EXPECT_EQ(0, std::memcmp(&buffer[2], bytes.data(), bytes.size()));
82 std::fclose(file);
83}