blob: 53f32ad78252970b267aa6b477ee663857d3559b [file] [log] [blame]
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -05001#include "editor_impl.hpp"
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05002#include "ipz_parser.hpp"
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -05003
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -05004#include <nlohmann/json.hpp>
Patrick Williamsc78d8872023-05-10 07:50:56 -05005
6#include <algorithm>
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -05007#include <vector>
8
9#include <gtest/gtest.h>
10
11using namespace openpower::vpd;
12using namespace openpower::vpd::manager::editor;
13using namespace openpower::vpd::inventory;
14using namespace openpower::vpd::constants;
15
16class vpdManagerEditorTest : public ::testing::Test
17{
18 protected:
19 Binary vpd;
20
21 nlohmann::json jsonFile;
22
23 // map to hold the mapping of location code and inventory path
24 inventory::LocationCodeMap fruLocationCode;
25
26 public:
27 // constructor
28 vpdManagerEditorTest()
29 {
30 processJson();
31 }
32
33 void processJson();
34 void readFile(std::string pathToFile);
35};
36
37void vpdManagerEditorTest::readFile(std::string pathToFile)
38{
39 // read the json file and parse it
40 std::ifstream vpdFile(pathToFile, std::ios::binary);
41
42 if (!vpdFile)
43 {
44 throw std::runtime_error("json file not found");
45 }
46
47 vpd.assign((std::istreambuf_iterator<char>(vpdFile)),
48 std::istreambuf_iterator<char>());
49}
50
51void vpdManagerEditorTest::processJson()
52{
53 // read the json file and parse it
54 std::ifstream json("vpd-manager-test/vpd_editor_test.json",
55 std::ios::binary);
56
57 if (!json)
58 {
59 throw std::runtime_error("json file not found");
60 }
61
62 jsonFile = nlohmann::json::parse(json);
63
64 const nlohmann::json& groupFRUS =
65 jsonFile["frus"].get_ref<const nlohmann::json::object_t&>();
66 for (const auto& itemFRUS : groupFRUS.items())
67 {
68 const std::vector<nlohmann::json>& groupEEPROM =
69 itemFRUS.value().get_ref<const nlohmann::json::array_t&>();
70 for (const auto& itemEEPROM : groupEEPROM)
71 {
72 fruLocationCode.emplace(
Alpana Kumari414d5ae2021-03-04 21:06:35 +000073 itemEEPROM["extraInterfaces"][IBM_LOCATION_CODE_INF]
74 ["LocationCode"]
75 .get_ref<const nlohmann::json::string_t&>(),
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -050076 itemEEPROM["inventoryPath"]
77 .get_ref<const nlohmann::json::string_t&>());
78 }
79 }
80}
81
82TEST_F(vpdManagerEditorTest, InvalidFile)
83{
84 Binary dataToUodate{'M', 'O', 'D', 'I', 'F', 'Y',
85 'D', 'A', 'T', 'A', 'O', 'K'};
86
87 Binary emptyVpdFile;
88 try
89 {
90 // Invalid kwd name
91 EditorImpl edit("VINI", "SN", std::move(emptyVpdFile));
Santosh Puranika0b23912022-02-10 13:37:09 +053092 edit.updateKeyword(dataToUodate, 0, true);
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -050093 }
94 catch (const std::exception& e)
95 {
96 EXPECT_EQ(std::string(e.what()), std::string("Invalid File"));
97 }
98}
99
100TEST_F(vpdManagerEditorTest, InvalidHeader)
101{
102 Binary dataToUodate{'M', 'O', 'D', 'I', 'F', 'Y',
103 'D', 'A', 'T', 'A', 'O', 'K'};
104
105 readFile("vpd-manager-test/invalidHeaderFile.dat");
106 try
107 {
108 // the path is dummy
109 EditorImpl edit("VINI", "SN", std::move(vpd));
Santosh Puranika0b23912022-02-10 13:37:09 +0530110 edit.updateKeyword(dataToUodate, 0, true);
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -0500111 }
112 catch (const std::exception& e)
113 {
114 EXPECT_EQ(std::string(e.what()), std::string("VHDR record not found"));
115 }
116}
117
118TEST_F(vpdManagerEditorTest, InvalidRecordName)
119{
120 Binary dataToUodate{'M', 'O', 'D', 'I', 'F', 'Y',
121 'D', 'A', 'T', 'A', 'O', 'K'};
122
123 readFile("vpd-manager-test/vpdFile.dat");
124
125 try
126 {
127 // Invalid record name "VIN", path is dummy
128 EditorImpl edit("VIN", "SN", std::move(vpd));
Santosh Puranika0b23912022-02-10 13:37:09 +0530129 edit.updateKeyword(dataToUodate, 0, true);
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -0500130 }
131 catch (const std::exception& e)
132 {
133 EXPECT_EQ(std::string(e.what()), std::string("Record not found"));
134 }
135}
136
137TEST_F(vpdManagerEditorTest, InvalidKWdName)
138{
139 Binary dataToUodate{'M', 'O', 'D', 'I', 'F', 'Y',
140 'D', 'A', 'T', 'A', 'O', 'K'};
141
142 readFile("vpd-manager-test/vpdFile.dat");
143
144 try
145 {
146 // All valid data
147 EditorImpl edit("VINI", "Sn", std::move(vpd));
Santosh Puranika0b23912022-02-10 13:37:09 +0530148 edit.updateKeyword(dataToUodate, 0, true);
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -0500149 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500150 catch (const std::runtime_error& e)
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -0500151 {
152 EXPECT_EQ(std::string(e.what()), std::string("Keyword not found"));
153 }
154}
155
156TEST_F(vpdManagerEditorTest, UpdateKwd_Success)
157{
158 Binary dataToUodate{'M', 'O', 'D', 'I', 'F', 'Y',
159 'D', 'A', 'T', 'A', 'O', 'K'};
160
161 readFile("vpd-manager-test/vpdFile.dat");
162
163 try
164 {
Alpana Kumari0e34d352021-03-21 11:38:03 -0500165 // All valid data, but can't update with dummy ECC code
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -0500166 EditorImpl edit("VINI", "SN", std::move(vpd));
Santosh Puranika0b23912022-02-10 13:37:09 +0530167 edit.updateKeyword(dataToUodate, 0, true);
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -0500168 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500169 catch (const std::runtime_error& e)
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -0500170 {
Alpana Kumari0e34d352021-03-21 11:38:03 -0500171 EXPECT_EQ(std::string(e.what()), std::string("Ecc update failed"));
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -0500172 }
173}
174
175int main(int argc, char** argv)
176{
177 ::testing::InitGoogleTest(&argc, argv);
178
179 return RUN_ALL_TESTS();
Patrick Williamsc78d8872023-05-10 07:50:56 -0500180}