1 #include "keyword_vpd_parser.hpp"
2 #include "store.hpp"
3 #include "types.hpp"
4 
5 #include <exception>
6 #include <fstream>
7 
8 #include <gtest/gtest.h>
9 
10 using namespace vpd::keyword::parser;
11 using namespace openpower::vpd;
12 using namespace openpower::vpd::inventory;
13 using namespace std;
14 
15 class KeywordVpdParserTest : public ::testing::Test
16 {
17   protected:
18     Binary keywordVpdVector;
19     Binary bonoKwVpdVector;
20 
KeywordVpdParserTest()21     KeywordVpdParserTest()
22     {
23         // Open the kw VPD file in binary mode
24         std::ifstream kwVpdFile("vpd.dat", std::ios::binary);
25 
26         // Read the content of the binary file into a vector
27         keywordVpdVector.assign((std::istreambuf_iterator<char>(kwVpdFile)),
28                                 std::istreambuf_iterator<char>());
29         // Open the BONO type kw VPD file in binary mode
30         std::ifstream bonoKwVpdFile("bono.vpd", std::ios::binary);
31 
32         // Read the content of the binary file into a vector
33         bonoKwVpdVector.assign((std::istreambuf_iterator<char>(bonoKwVpdFile)),
34                                std::istreambuf_iterator<char>());
35     }
36 };
37 
TEST_F(KeywordVpdParserTest,GoodTestCase)38 TEST_F(KeywordVpdParserTest, GoodTestCase)
39 {
40     KeywordVpdParser parserObj1(std::move(keywordVpdVector));
41     KeywordVpdMap map1{
42         pair<std::string, Binary>{"WI", {0x00}},
43         pair<std::string, Binary>{"FL", {0x50, 0x32, 0x20, 0x20, 0x20}},
44         pair<std::string, Binary>{
45             "SM",
46             {0x82, 0x50, 0x32, 0x2d, 0x44, 0x34, 0x20, 0x20, 0x20, 0x20, 0x20,
47              0x20, 0x32, 0x53, 0x53, 0x43, 0x81, 0x50, 0x32, 0x2d, 0x44, 0x35,
48              0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x32, 0x53, 0x53, 0x43, 0x80,
49              0x50, 0x32, 0x2d, 0x44, 0x37, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
50              0x32, 0x53, 0x53, 0x43, 0x83, 0x50, 0x32, 0x2d, 0x44, 0x38, 0x20,
51              0x20, 0x20, 0x20, 0x20, 0x20, 0x32, 0x53, 0x53, 0x43}},
52         pair<std::string, Binary>{"B2",
53                                   {0x50, 0x05, 0x07, 0x60, 0x73, 0x00, 0x72,
54                                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55                                    0x01, 0x00}},
56         pair<std::string, Binary>{"MF", {0x00, 0x10}},
57         pair<std::string, Binary>{"VZ", {0x30, 0x33}},
58         pair<std::string, Binary>{"PN",
59                                   {0x30, 0x31, 0x4b, 0x55, 0x37, 0x32, 0x34}},
60         pair<std::string, Binary>{
61             "FN", {0x20, 0x30, 0x31, 0x4b, 0x55, 0x37, 0x32, 0x34}},
62         pair<std::string, Binary>{"CE", {0x31}},
63         pair<std::string, Binary>{"SN",
64                                   {0x59, 0x48, 0x33, 0x30, 0x42, 0x47, 0x37,
65                                    0x38, 0x42, 0x30, 0x31, 0x34}},
66         pair<std::string, Binary>{"CC", {0x32, 0x44, 0x33, 0x37}}};
67 
68     auto map2 = std::move(get<KeywordVpdMap>(parserObj1.parse()));
69     ASSERT_EQ(1, map1 == map2);
70 
71     // BONO TYPE VPD
72     KeywordVpdParser parserObj2(std::move(bonoKwVpdVector));
73     map1 = {
74         pair<std::string, Binary>{"B2",
75                                   {0x50, 0x0, 0xb3, 0xe0, 0x90, 0x0, 0x2, 0x50,
76                                    0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}},
77         pair<std::string, Binary>{"CC", {0x35, 0x39, 0x33, 0x42}},
78         pair<std::string, Binary>{"CT", {0x50, 0x37, 0x32, 0x0}},
79         pair<std::string, Binary>{"EC", {0x50, 0x34, 0x35, 0x35, 0x33, 0x37}},
80         pair<std::string, Binary>{"FN",
81                                   {0x30, 0x32, 0x44, 0x45, 0x33, 0x36, 0x35}},
82         pair<std::string, Binary>{"PN",
83                                   {0x30, 0x32, 0x44, 0x45, 0x33, 0x36, 0x36}},
84         pair<std::string, Binary>{"RV", {0xa1}},
85         pair<std::string, Binary>{
86             "SI", {0x31, 0x30, 0x31, 0x34, 0x30, 0x36, 0x37, 0x34}},
87         pair<std::string, Binary>{"SN",
88                                   {0x59, 0x4c, 0x35, 0x30, 0x48, 0x54, 0x39,
89                                    0x36, 0x4a, 0x30, 0x30, 0x38}},
90         pair<std::string, Binary>{"Z4", {0x30}},
91         pair<std::string, Binary>{"Z5", {0x30}},
92         pair<std::string, Binary>{
93             "Z6", {0x41, 0x31, 0x38, 0x30, 0x30, 0x32, 0x30, 0x30}}};
94 
95     map2 = std::move(get<KeywordVpdMap>(parserObj2.parse()));
96     ASSERT_EQ(1, map1 == map2);
97 }
98 
TEST_F(KeywordVpdParserTest,InvKwVpdTag)99 TEST_F(KeywordVpdParserTest, InvKwVpdTag)
100 {
101     // Invalid Large resource type Identifier String - corrupted at index[0]
102     keywordVpdVector[0] = 0x83;
103     KeywordVpdParser parserObj1(std::move(keywordVpdVector));
104     EXPECT_THROW(parserObj1.parse(), std::runtime_error);
105 
106     // For BONO type VPD
107     bonoKwVpdVector[0] = 0x83;
108     KeywordVpdParser parserObj2(std::move(bonoKwVpdVector));
109     EXPECT_THROW(parserObj2.parse(), std::runtime_error);
110 }
111 
TEST_F(KeywordVpdParserTest,InvKwValTag)112 TEST_F(KeywordVpdParserTest, InvKwValTag)
113 {
114     // Invalid Large resource type Vendor Defined - corrupted at index[19]
115     keywordVpdVector[19] = 0x85;
116     KeywordVpdParser parserObj1(std::move(keywordVpdVector));
117     EXPECT_THROW(parserObj1.parse(), std::runtime_error);
118 
119     // For BONO type VPD - corruputed at index[33]
120     bonoKwVpdVector[33] = 0x91;
121     KeywordVpdParser parserObj2(std::move(bonoKwVpdVector));
122     EXPECT_THROW(parserObj2.parse(), std::runtime_error);
123 }
124 
TEST_F(KeywordVpdParserTest,InvKwValSize)125 TEST_F(KeywordVpdParserTest, InvKwValSize)
126 {
127     // Badly formed keyword VPD data - corrupted at index[20]
128     keywordVpdVector[20] = 0x00;
129     KeywordVpdParser parserObj1(std::move(keywordVpdVector));
130     EXPECT_THROW(parserObj1.parse(), std::runtime_error);
131 
132     // For BONO type VPD - corruputed at index[34]
133     bonoKwVpdVector[34] = 0x00;
134     KeywordVpdParser parserObj2(std::move(bonoKwVpdVector));
135     EXPECT_THROW(parserObj2.parse(), std::runtime_error);
136 }
137 
TEST_F(KeywordVpdParserTest,InvKwValEndTag)138 TEST_F(KeywordVpdParserTest, InvKwValEndTag)
139 {
140     // Invalid Small resource type End - corrupted at index[177]
141     keywordVpdVector[177] = 0x80;
142     KeywordVpdParser parserObj1(std::move(keywordVpdVector));
143     EXPECT_THROW(parserObj1.parse(), std::runtime_error);
144 }
145 
TEST_F(KeywordVpdParserTest,InvChecksum)146 TEST_F(KeywordVpdParserTest, InvChecksum)
147 {
148     // Invalid Check sum - corrupted at index[178]
149     keywordVpdVector[178] = 0xb1;
150     KeywordVpdParser parserObj1(std::move(keywordVpdVector));
151     EXPECT_THROW(parserObj1.parse(), std::runtime_error);
152 }
153 
TEST_F(KeywordVpdParserTest,InvKwVpdEndTag)154 TEST_F(KeywordVpdParserTest, InvKwVpdEndTag)
155 {
156     // Invalid Small resource type Last End Of Data - corrupted at index[179]
157     keywordVpdVector[179] = 0x79;
158     KeywordVpdParser parserObj1(std::move(keywordVpdVector));
159     EXPECT_THROW(parserObj1.parse(), std::runtime_error);
160 
161     // For BONO type VPD - corrupted at index[147]
162     bonoKwVpdVector[147] = 0x79;
163     KeywordVpdParser parserObj2(std::move(bonoKwVpdVector));
164     EXPECT_THROW(parserObj2.parse(), std::runtime_error);
165 }
166 
TEST_F(KeywordVpdParserTest,OutOfBoundGreaterSize)167 TEST_F(KeywordVpdParserTest, OutOfBoundGreaterSize)
168 {
169     // Iterator Out of Bound - size is larger than the actual size - corrupted
170     // at index[24]
171     keywordVpdVector[24] = 0x32;
172     KeywordVpdParser parserObj1(std::move(keywordVpdVector));
173     EXPECT_THROW(parserObj1.parse(), std::runtime_error);
174 
175     // For BONO type VPD - corrupted at index[38]
176     bonoKwVpdVector[38] = 0x4D;
177     KeywordVpdParser parserObj2(std::move(bonoKwVpdVector));
178     EXPECT_THROW(parserObj2.parse(), std::runtime_error);
179 }
180 
TEST_F(KeywordVpdParserTest,OutOfBoundLesserSize)181 TEST_F(KeywordVpdParserTest, OutOfBoundLesserSize)
182 {
183     // Iterator Out of Bound - size is smaller than the actual size - corrupted
184     // at index[24]
185     keywordVpdVector[24] = 0x03;
186     KeywordVpdParser parserObj1(std::move(keywordVpdVector));
187     EXPECT_THROW(parserObj1.parse(), std::runtime_error);
188 
189     // For BONO type VPD - corrupted at index[38]
190     bonoKwVpdVector[38] = 0x04;
191     KeywordVpdParser parserObj2(std::move(bonoKwVpdVector));
192     EXPECT_THROW(parserObj2.parse(), std::runtime_error);
193 }
194 
TEST_F(KeywordVpdParserTest,BlankVpd)195 TEST_F(KeywordVpdParserTest, BlankVpd)
196 {
197     // Blank Kw Vpd
198     keywordVpdVector.clear();
199     KeywordVpdParser parserObj1(std::move(keywordVpdVector));
200     EXPECT_THROW(parserObj1.parse(), std::runtime_error);
201 
202     // Blank Bono Type Vpd
203     bonoKwVpdVector.clear();
204     KeywordVpdParser parserObj2(std::move(bonoKwVpdVector));
205     EXPECT_THROW(parserObj2.parse(), std::runtime_error);
206 }
207 
main(int argc,char ** argv)208 int main(int argc, char** argv)
209 {
210     ::testing::InitGoogleTest(&argc, argv);
211 
212     return RUN_ALL_TESTS();
213 }
214