xref: /openbmc/openpower-vpd-parser/test/utest_keyword_parser.cpp (revision fa5e4d325ef9cea3c841fe89d202c340f92bd8c6)
1 #include "exceptions.hpp"
2 #include "keyword_vpd_parser.hpp"
3 #include "parser.hpp"
4 #include "types.hpp"
5 
6 #include <cstdint>
7 #include <exception>
8 #include <fstream>
9 
10 #include <gtest/gtest.h>
11 
12 using namespace vpd;
13 
TEST(KeywordVpdParserTest,GoodTestCase)14 TEST(KeywordVpdParserTest, GoodTestCase)
15 {
16     types::KeywordVpdMap l_keywordMap{
17         std::pair<std::string, types::BinaryVector>{"WI", {0x00}},
18         std::pair<std::string, types::BinaryVector>{
19             "FL", {0x50, 0x32, 0x20, 0x20, 0x20}},
20         std::pair<std::string, types::BinaryVector>{
21             "SM",
22             {0x82, 0x50, 0x32, 0x2d, 0x44, 0x34, 0x20, 0x20, 0x20, 0x20, 0x20,
23              0x20, 0x32, 0x53, 0x53, 0x43, 0x81, 0x50, 0x32, 0x2d, 0x44, 0x35,
24              0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x32, 0x53, 0x53, 0x43, 0x80,
25              0x50, 0x32, 0x2d, 0x44, 0x37, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
26              0x32, 0x53, 0x53, 0x43, 0x83, 0x50, 0x32, 0x2d, 0x44, 0x38, 0x20,
27              0x20, 0x20, 0x20, 0x20, 0x20, 0x32, 0x53, 0x53, 0x43}},
28         std::pair<std::string, types::BinaryVector>{
29             "B2",
30             {0x50, 0x05, 0x07, 0x60, 0x73, 0x00, 0x72, 0x00, 0x00, 0x00, 0x00,
31              0x00, 0x00, 0x00, 0x01, 0x00}},
32         std::pair<std::string, types::BinaryVector>{"MF", {0x00, 0x10}},
33         std::pair<std::string, types::BinaryVector>{"VZ", {0x30, 0x33}},
34         std::pair<std::string, types::BinaryVector>{
35             "PN", {0x30, 0x31, 0x4b, 0x55, 0x37, 0x32, 0x34}},
36         std::pair<std::string, types::BinaryVector>{
37             "FN", {0x20, 0x30, 0x31, 0x4b, 0x55, 0x37, 0x32, 0x34}},
38         std::pair<std::string, types::BinaryVector>{"CE", {0x31}},
39         std::pair<std::string, types::BinaryVector>{
40             "SN",
41             {0x59, 0x48, 0x33, 0x30, 0x42, 0x47, 0x37, 0x38, 0x42, 0x30, 0x31,
42              0x34}},
43         std::pair<std::string, types::BinaryVector>{"CC",
44                                                     {0x32, 0x44, 0x33, 0x37}}};
45 
46     nlohmann::json l_json;
47     std::string l_vpdFile("vpd_files/keyword.dat");
48     Parser l_vpdParser(l_vpdFile, l_json);
49 
50     ASSERT_EQ(1, std::get<types::KeywordVpdMap>(l_vpdParser.parse()) ==
51                      l_keywordMap);
52 }
53 
TEST(KeywordVpdParserTest,InvalidVpd)54 TEST(KeywordVpdParserTest, InvalidVpd)
55 {
56     // Invalid large resource type identifier string - corrupted at index[0]
57     nlohmann::json l_json;
58     std::string l_vpdFile("vpd_files/keyword_corrupted_index_0.dat");
59     Parser l_vpdParser(l_vpdFile, l_json);
60 
61     EXPECT_THROW(l_vpdParser.parse(), DataException);
62 }
63 
TEST(KeywordVpdParserTest,InvalidStartTag)64 TEST(KeywordVpdParserTest, InvalidStartTag)
65 {
66     // Invalid large resource type vendor defined - corrupted at index[19]
67     nlohmann::json l_json;
68     std::string l_vpdFile("vpd_files/keyword_corrupted_index_19.dat");
69     Parser l_vpdParser(l_vpdFile, l_json);
70 
71     EXPECT_THROW(l_vpdParser.parse(), DataException);
72 }
73 
TEST(KeywordVpdParserTest,InvalidSize)74 TEST(KeywordVpdParserTest, InvalidSize)
75 {
76     // Badly formed keyword VPD data - corrupted at index[20]
77     nlohmann::json l_json;
78     std::string l_vpdFile("vpd_files/keyword_corrupted_index_20.dat");
79     Parser l_vpdParser(l_vpdFile, l_json);
80 
81     EXPECT_THROW(l_vpdParser.parse(), DataException);
82 }
83 
TEST(KeywordVpdParserTest,InvalidEndTag)84 TEST(KeywordVpdParserTest, InvalidEndTag)
85 {
86     // Invalid small resource type end - corrupted at index[177]
87     nlohmann::json l_json;
88     std::string l_vpdFile("vpd_files/keyword_corrupted_index_177.dat");
89     Parser l_vpdParser(l_vpdFile, l_json);
90 
91     EXPECT_THROW(l_vpdParser.parse(), DataException);
92 }
93 
TEST(KeywordVpdParserTest,InvalidChecksum)94 TEST(KeywordVpdParserTest, InvalidChecksum)
95 {
96     // Invalid check sum - corrupted at index[178]
97     nlohmann::json l_json;
98     std::string l_vpdFile("vpd_files/keyword_corrupted_index_178.dat");
99     Parser l_vpdParser(l_vpdFile, l_json);
100 
101     EXPECT_THROW(l_vpdParser.parse(), DataException);
102 }
103 
TEST(KeywordVpdParserTest,InvalidLastEndTag)104 TEST(KeywordVpdParserTest, InvalidLastEndTag)
105 {
106     // Invalid small resource type last end of data - corrupted at index[179]
107     nlohmann::json l_json;
108     std::string l_vpdFile("vpd_files/keyword_corrupted_index_179.dat");
109     Parser l_vpdParser(l_vpdFile, l_json);
110 
111     EXPECT_THROW(l_vpdParser.parse(), DataException);
112 }
113 
TEST(KeywordVpdParserTest,OutOfBoundGreaterSize)114 TEST(KeywordVpdParserTest, OutOfBoundGreaterSize)
115 {
116     // Iterator out of bound - size is larger than the actual size - corrupted
117     // at index[24]
118     nlohmann::json l_json;
119     std::string l_vpdFile(
120         "vpd_files/keyword_corrupted_index_24_large_size.dat");
121     Parser l_vpdParser(l_vpdFile, l_json);
122 
123     EXPECT_THROW(l_vpdParser.parse(), DataException);
124 }
125 
TEST(KeywordVpdParserTest,OutOfBoundLesserSize)126 TEST(KeywordVpdParserTest, OutOfBoundLesserSize)
127 {
128     // Iterator out of bound - size is smaller than the actual size - corrupted
129     // at index[24]
130     nlohmann::json l_json;
131     std::string l_vpdFile(
132         "vpd_files/keyword_corrupted_index_24_small_size.dat");
133     Parser l_vpdParser(l_vpdFile, l_json);
134 
135     EXPECT_THROW(l_vpdParser.parse(), DataException);
136 }
137 
TEST(KeywordVpdParserTest,EmptyInput)138 TEST(KeywordVpdParserTest, EmptyInput)
139 {
140     // Blank keyword VPD
141     types::BinaryVector emptyVector{};
142     KeywordVpdParser l_keywordParser(std::move(emptyVector));
143 
144     EXPECT_THROW(l_keywordParser.parse(), std::exception);
145 }
146