blob: 112565a20bf5c0ed77fe55420e00d5e2f39680b1 [file] [log] [blame]
Patrick Venture3c086f22018-08-07 11:59:20 -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 FlashIpmiFlashDataTest : public ::testing::Test
12{
13 protected:
14 FlashIpmiFlashDataTest() = default;
15
16 void SetUp() override
17 {
18 name = std::tmpnam(nullptr);
19 }
20 void TearDown() override
21 {
22 (void)std::remove(name.c_str());
23 }
24
25 std::string name;
26};
27
28TEST_F(FlashIpmiFlashDataTest, CalledOutOfSequenceFails)
29{
30 // Verify that there is sanity checking.
31 std::vector<uint8_t> bytes = {0xaa, 0x55};
32
Patrick Venturefdc65b22018-08-07 14:37:58 -070033 FlashUpdate updater(name, "");
Patrick Venture3c086f22018-08-07 11:59:20 -070034 EXPECT_FALSE(updater.flashData(0, bytes));
35
36 // Verify the file doesn't exist.
37 auto file = std::fopen(name.c_str(), "r");
38 EXPECT_FALSE(file);
39}
40
41TEST_F(FlashIpmiFlashDataTest, CalledWithDataSucceeds)
42{
43 // Verify that under normal usage it writes the bytes.
44 std::vector<uint8_t> bytes = {0xaa, 0x55};
45
Patrick Venturefdc65b22018-08-07 14:37:58 -070046 FlashUpdate updater(name, "");
Patrick Venture3c086f22018-08-07 11:59:20 -070047 updater.start(THIRTYTWO_MIB);
48 EXPECT_TRUE(updater.flashData(0, bytes));
49
50 auto file = std::fopen(name.c_str(), "r");
51 EXPECT_TRUE(file);
52
53 uint8_t buffer[2];
54 auto read = std::fread(buffer, 1, bytes.size(), file);
55 EXPECT_EQ(read, bytes.size());
56 EXPECT_EQ(0, std::memcmp(buffer, bytes.data(), bytes.size()));
57 std::fclose(file);
58}
59
60TEST_F(FlashIpmiFlashDataTest, CalledNonZeroOffsetSucceeds)
61{
62 // Skipping bytes in POSIX can create a sparse file. In this case,
63 // let's allow the behavior. If we'd rather, we can have writeBlock
64 // check that the offset is where we expect.
65
66 std::vector<uint8_t> bytes = {0xaa, 0x55};
67
Patrick Venturefdc65b22018-08-07 14:37:58 -070068 FlashUpdate updater(name, "");
Patrick Venture3c086f22018-08-07 11:59:20 -070069 updater.start(THIRTYTWO_MIB);
70 EXPECT_TRUE(updater.flashData(2, bytes));
71
72 auto file = std::fopen(name.c_str(), "r");
73 EXPECT_TRUE(file);
74
75 uint8_t buffer[4];
76 auto read = std::fread(buffer, 1, sizeof(buffer), file);
77 EXPECT_EQ(read, sizeof(buffer));
78 EXPECT_EQ(0, std::memcmp(&buffer[2], bytes.data(), bytes.size()));
79 std::fclose(file);
80}