xref: /openbmc/openpower-vpd-parser/test/ipz_parser/parser.cpp (revision c78d887ccac761a70f2682fe3d5b948383cf56bd)
1e12b181bSSunnySrivastava1984 #include "ipz_parser.hpp"
2e12b181bSSunnySrivastava1984 
318bb985cSgirik #include <const.hpp>
426a74af7SAlpana Kumari #include <defines.hpp>
526a74af7SAlpana Kumari #include <impl.hpp>
626a74af7SAlpana Kumari #include <store.hpp>
726a74af7SAlpana Kumari 
8*c78d887cSPatrick Williams #include <cassert>
9*c78d887cSPatrick Williams #include <fstream>
10*c78d887cSPatrick Williams #include <iterator>
11*c78d887cSPatrick Williams 
1226a74af7SAlpana Kumari #include <gtest/gtest.h>
1326a74af7SAlpana Kumari 
1426a74af7SAlpana Kumari using namespace openpower::vpd;
1518bb985cSgirik using namespace openpower::vpd::constants;
1618bb985cSgirik 
1718bb985cSgirik constexpr uint32_t vpdOffset = 0;
1826a74af7SAlpana Kumari 
TEST(IpzVpdParserApp,vpdGoodPath)1926a74af7SAlpana Kumari TEST(IpzVpdParserApp, vpdGoodPath)
2026a74af7SAlpana Kumari {
2126a74af7SAlpana Kumari     // Create a vpd
2226a74af7SAlpana Kumari     Binary vpd = {
2326a74af7SAlpana Kumari         0x00, 0x0f, 0x17, 0xba, 0x42, 0xca, 0x82, 0xd7, 0x7b, 0x77, 0x1e, 0x84,
2426a74af7SAlpana Kumari         0x28, 0x00, 0x52, 0x54, 0x04, 0x56, 0x48, 0x44, 0x52, 0x56, 0x44, 0x02,
2526a74af7SAlpana Kumari         0x30, 0x31, 0x50, 0x54, 0x0e, 0x56, 0x54, 0x4f, 0x43, 0xd5, 0x00, 0x37,
2626a74af7SAlpana Kumari         0x00, 0x4c, 0x00, 0x97, 0x05, 0x13, 0x00, 0x50, 0x46, 0x08, 0x00, 0x00,
2726a74af7SAlpana Kumari         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x84, 0x48, 0x00, 0x52, 0x54,
2826a74af7SAlpana Kumari         0x04, 0x56, 0x54, 0x4f, 0x43, 0x50, 0x54, 0x0e, 0x56, 0x49, 0x4e, 0x49,
2926a74af7SAlpana Kumari         0xd5, 0x00, 0x52, 0x00, 0x90, 0x00, 0x73, 0x05, 0x24, 0x00, 0x84, 0x8c,
3026a74af7SAlpana Kumari         0x00, 0x52, 0x54, 0x04, 0x56, 0x49, 0x4e, 0x49, 0x44, 0x52, 0x10, 0x41,
3126a74af7SAlpana Kumari         0x50, 0x53, 0x53, 0x20, 0x26, 0x20, 0x54, 0x50, 0x4d, 0x20, 0x20, 0x43,
3226a74af7SAlpana Kumari         0x41, 0x52, 0x44, 0x43, 0x45, 0x01, 0x31, 0x56, 0x5a, 0x02, 0x30, 0x31,
3326a74af7SAlpana Kumari         0x46, 0x4e, 0x07, 0x30, 0x31, 0x44, 0x48, 0x32, 0x30, 0x30, 0x50, 0x4e,
3426a74af7SAlpana Kumari         0x07, 0x30, 0x31, 0x44, 0x48, 0x32, 0x30, 0x31, 0x53, 0x4e, 0x0c, 0x59,
3526a74af7SAlpana Kumari         0x4c, 0x33, 0x30, 0x42, 0x47, 0x37, 0x43, 0x46, 0x30, 0x33, 0x50, 0x43,
3626a74af7SAlpana Kumari         0x43, 0x04, 0x36, 0x42, 0x36, 0x36, 0x50, 0x52, 0x08, 0x00, 0x00, 0x00,
3726a74af7SAlpana Kumari         0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x45, 0x04, 0x30, 0x30, 0x30, 0x31,
3826a74af7SAlpana Kumari         0x43, 0x54, 0x04, 0x40, 0xb8, 0x02, 0x03, 0x48, 0x57, 0x02, 0x00, 0x01,
3926a74af7SAlpana Kumari         0x42, 0x33, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x34, 0x01,
4026a74af7SAlpana Kumari         0x00, 0x42, 0x37, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4126a74af7SAlpana Kumari         0x00, 0x00, 0x00, 0x00, 0x50, 0x46, 0x02, 0x00, 0x00, 0x78, 0x84, 0xdc,
4226a74af7SAlpana Kumari         0x00, 0x52, 0x54, 0x04};
4326a74af7SAlpana Kumari 
4426a74af7SAlpana Kumari     // call app for this vpd
4518bb985cSgirik     parser::Impl p(std::move(vpd), std::string{}, systemVpdFilePath, vpdOffset);
4626a74af7SAlpana Kumari     Store vpdStore = p.run();
4726a74af7SAlpana Kumari 
4826a74af7SAlpana Kumari     static const std::string record = "VINI";
4926a74af7SAlpana Kumari     static const std::string keyword = "DR";
5026a74af7SAlpana Kumari 
5126a74af7SAlpana Kumari     // TODO 2: Move this as an utility to store.hpp
5226a74af7SAlpana Kumari     std::string dataFound;
5326a74af7SAlpana Kumari     Parsed st_bin = vpdStore.getVpdMap();
5426a74af7SAlpana Kumari 
5526a74af7SAlpana Kumari     auto kw = st_bin.find(record);
5626a74af7SAlpana Kumari     if (st_bin.end() != kw)
5726a74af7SAlpana Kumari     {
5826a74af7SAlpana Kumari         auto value = (kw->second).find(keyword);
5926a74af7SAlpana Kumari         if ((kw->second).end() != value)
6026a74af7SAlpana Kumari         {
6126a74af7SAlpana Kumari             dataFound = value->second;
6226a74af7SAlpana Kumari         }
6326a74af7SAlpana Kumari     }
6426a74af7SAlpana Kumari 
6526a74af7SAlpana Kumari     ASSERT_EQ(dataFound, "APSS & TPM  CARD");
6626a74af7SAlpana Kumari }
6726a74af7SAlpana Kumari 
TEST(IpzVpdParserApp,vpdBadPathEmptyVPD)6826a74af7SAlpana Kumari TEST(IpzVpdParserApp, vpdBadPathEmptyVPD)
6926a74af7SAlpana Kumari {
7026a74af7SAlpana Kumari     Binary vpd = {};
7126a74af7SAlpana Kumari 
7226a74af7SAlpana Kumari     // VPD is empty
7318bb985cSgirik     parser::Impl p(std::move(vpd), std::string{}, systemVpdFilePath, vpdOffset);
7426a74af7SAlpana Kumari 
7526a74af7SAlpana Kumari     // Expecting a throw here
7626a74af7SAlpana Kumari     EXPECT_THROW(p.run(), std::runtime_error);
7726a74af7SAlpana Kumari }
7826a74af7SAlpana Kumari 
TEST(IpzVpdParserApp,vpdBadPathMissingHeader)7926a74af7SAlpana Kumari TEST(IpzVpdParserApp, vpdBadPathMissingHeader)
8026a74af7SAlpana Kumari {
8126a74af7SAlpana Kumari     Binary vpd = {
8226a74af7SAlpana Kumari         0x00, 0x0f, 0x17, 0xba, 0x42, 0xca, 0x82, 0xd7, 0x7b, 0x77, 0x1e, 0x84,
8326a74af7SAlpana Kumari         0x28, 0x00, 0x52, 0x54, 0x04, 0x56, 0x48, 0x44, 0x52, 0x56, 0x44, 0x02,
8426a74af7SAlpana Kumari         0x30, 0x31, 0x50, 0x54, 0x0e, 0x56, 0x54, 0x4f, 0x43, 0xd5, 0x00, 0x37,
8526a74af7SAlpana Kumari         0x00, 0x4c, 0x00, 0x97, 0x05, 0x13, 0x00, 0x50, 0x46, 0x08, 0x00, 0x00,
8626a74af7SAlpana Kumari         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x84, 0x48, 0x00, 0x52, 0x54,
8726a74af7SAlpana Kumari         0x04, 0x56, 0x54, 0x4f, 0x43, 0x50, 0x54, 0x0e, 0x56, 0x49, 0x4e, 0x49,
8826a74af7SAlpana Kumari         0xd5, 0x00, 0x52, 0x00, 0x90, 0x00, 0x73, 0x05, 0x24, 0x00, 0x84, 0x8c,
8926a74af7SAlpana Kumari         0x00, 0x52, 0x54, 0x04, 0x56, 0x49, 0x4e, 0x49, 0x44, 0x52, 0x10, 0x41,
9026a74af7SAlpana Kumari         0x50, 0x53, 0x53, 0x20, 0x26, 0x20, 0x54, 0x50, 0x4d, 0x20, 0x20, 0x43,
9126a74af7SAlpana Kumari         0x41, 0x52, 0x44, 0x43, 0x45, 0x01, 0x31, 0x56, 0x5a, 0x02, 0x30, 0x31,
9226a74af7SAlpana Kumari         0x46, 0x4e, 0x07, 0x30, 0x31, 0x44, 0x48, 0x32, 0x30, 0x30, 0x50, 0x4e,
9326a74af7SAlpana Kumari         0x07, 0x30, 0x31, 0x44, 0x48, 0x32, 0x30, 0x31, 0x53, 0x4e, 0x0c, 0x59,
9426a74af7SAlpana Kumari         0x4c, 0x33, 0x30, 0x42, 0x47, 0x37, 0x43, 0x46, 0x30, 0x33, 0x50, 0x43,
9526a74af7SAlpana Kumari         0x43, 0x04, 0x36, 0x42, 0x36, 0x36, 0x50, 0x52, 0x08, 0x00, 0x00, 0x00,
9626a74af7SAlpana Kumari         0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x45, 0x04, 0x30, 0x30, 0x30, 0x31,
9726a74af7SAlpana Kumari         0x43, 0x54, 0x04, 0x40, 0xb8, 0x02, 0x03, 0x48, 0x57, 0x02, 0x00, 0x01,
9826a74af7SAlpana Kumari         0x42, 0x33, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x34, 0x01,
9926a74af7SAlpana Kumari         0x00, 0x42, 0x37, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
10026a74af7SAlpana Kumari         0x00, 0x00, 0x00, 0x00, 0x50, 0x46, 0x02, 0x00, 0x00, 0x78, 0x84, 0xdc,
10126a74af7SAlpana Kumari         0x00, 0x52, 0x54, 0x04};
10226a74af7SAlpana Kumari 
10326a74af7SAlpana Kumari     // corrupt the VHDR
10426a74af7SAlpana Kumari     vpd[17] = 0x00;
10526a74af7SAlpana Kumari 
10618bb985cSgirik     parser::Impl p(std::move(vpd), std::string{}, systemVpdFilePath, vpdOffset);
10726a74af7SAlpana Kumari 
10826a74af7SAlpana Kumari     // Expecting a throw here
10926a74af7SAlpana Kumari     EXPECT_THROW(p.run(), std::runtime_error);
11026a74af7SAlpana Kumari }
11126a74af7SAlpana Kumari 
TEST(IpzVpdParserApp,vpdBadPathMissingVTOC)11226a74af7SAlpana Kumari TEST(IpzVpdParserApp, vpdBadPathMissingVTOC)
11326a74af7SAlpana Kumari {
11426a74af7SAlpana Kumari     Binary vpd = {
11526a74af7SAlpana Kumari         0x00, 0x0f, 0x17, 0xba, 0x42, 0xca, 0x82, 0xd7, 0x7b, 0x77, 0x1e, 0x84,
11626a74af7SAlpana Kumari         0x28, 0x00, 0x52, 0x54, 0x04, 0x56, 0x48, 0x44, 0x52, 0x56, 0x44, 0x02,
11726a74af7SAlpana Kumari         0x30, 0x31, 0x50, 0x54, 0x0e, 0x56, 0x54, 0x4f, 0x43, 0xd5, 0x00, 0x37,
11826a74af7SAlpana Kumari         0x00, 0x4c, 0x00, 0x97, 0x05, 0x13, 0x00, 0x50, 0x46, 0x08, 0x00, 0x00,
11926a74af7SAlpana Kumari         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x84, 0x48, 0x00, 0x52, 0x54,
12026a74af7SAlpana Kumari         0x04, 0x56, 0x54, 0x4f, 0x43, 0x50, 0x54, 0x0e, 0x56, 0x49, 0x4e, 0x49,
12126a74af7SAlpana Kumari         0xd5, 0x00, 0x52, 0x00, 0x90, 0x00, 0x73, 0x05, 0x24, 0x00, 0x84, 0x8c,
12226a74af7SAlpana Kumari         0x00, 0x52, 0x54, 0x04, 0x56, 0x49, 0x4e, 0x49, 0x44, 0x52, 0x10, 0x41,
12326a74af7SAlpana Kumari         0x50, 0x53, 0x53, 0x20, 0x26, 0x20, 0x54, 0x50, 0x4d, 0x20, 0x20, 0x43,
12426a74af7SAlpana Kumari         0x41, 0x52, 0x44, 0x43, 0x45, 0x01, 0x31, 0x56, 0x5a, 0x02, 0x30, 0x31,
12526a74af7SAlpana Kumari         0x46, 0x4e, 0x07, 0x30, 0x31, 0x44, 0x48, 0x32, 0x30, 0x30, 0x50, 0x4e,
12626a74af7SAlpana Kumari         0x07, 0x30, 0x31, 0x44, 0x48, 0x32, 0x30, 0x31, 0x53, 0x4e, 0x0c, 0x59,
12726a74af7SAlpana Kumari         0x4c, 0x33, 0x30, 0x42, 0x47, 0x37, 0x43, 0x46, 0x30, 0x33, 0x50, 0x43,
12826a74af7SAlpana Kumari         0x43, 0x04, 0x36, 0x42, 0x36, 0x36, 0x50, 0x52, 0x08, 0x00, 0x00, 0x00,
12926a74af7SAlpana Kumari         0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x45, 0x04, 0x30, 0x30, 0x30, 0x31,
13026a74af7SAlpana Kumari         0x43, 0x54, 0x04, 0x40, 0xb8, 0x02, 0x03, 0x48, 0x57, 0x02, 0x00, 0x01,
13126a74af7SAlpana Kumari         0x42, 0x33, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x34, 0x01,
13226a74af7SAlpana Kumari         0x00, 0x42, 0x37, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
13326a74af7SAlpana Kumari         0x00, 0x00, 0x00, 0x00, 0x50, 0x46, 0x02, 0x00, 0x00, 0x78, 0x84, 0xdc,
13426a74af7SAlpana Kumari         0x00, 0x52, 0x54, 0x04};
13526a74af7SAlpana Kumari 
13626a74af7SAlpana Kumari     // corrupt the VTOC
13726a74af7SAlpana Kumari     vpd[61] = 0x00;
13826a74af7SAlpana Kumari 
13918bb985cSgirik     parser::Impl p(std::move(vpd), std::string{}, systemVpdFilePath, vpdOffset);
14026a74af7SAlpana Kumari 
14126a74af7SAlpana Kumari     // Expecting a throw here
14226a74af7SAlpana Kumari     EXPECT_THROW(p.run(), std::runtime_error);
14326a74af7SAlpana Kumari }
14426a74af7SAlpana Kumari 
main(int argc,char ** argv)14526a74af7SAlpana Kumari int main(int argc, char** argv)
14626a74af7SAlpana Kumari {
14726a74af7SAlpana Kumari     ::testing::InitGoogleTest(&argc, argv);
14826a74af7SAlpana Kumari 
14926a74af7SAlpana Kumari     return RUN_ALL_TESTS();
15026a74af7SAlpana Kumari }
151