blob: 5c197dd303c1bae7e819daf1dc270afbe7e8de6d [file] [log] [blame]
George Liue53193f2020-02-24 09:23:26 +08001#include "libpldmresponder/pdr_utils.hpp"
Deepak Kodihallic682fe22020-03-04 00:42:54 -06002#include "libpldmresponder/platform.hpp"
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -05003
4#include "libpldm/platform.h"
5
6#include <gtest/gtest.h>
7
8using namespace pldm::responder;
Deepak Kodihallic682fe22020-03-04 00:42:54 -06009using namespace pldm::responder::platform;
10using namespace pldm::responder::pdr;
11using namespace pldm::responder::pdr_utils;
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050012
13TEST(GeneratePDR, testGoodJson)
14{
Deepak Kodihallic682fe22020-03-04 00:42:54 -060015 auto inPDRRepo = pldm_pdr_init();
16 auto outPDRRepo = pldm_pdr_init();
17 Repo outRepo(outPDRRepo);
18 Handler handler("./pdr_jsons/state_effecter/good", inPDRRepo);
19 Repo inRepo(inPDRRepo);
20 getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050021
22 // 2 entries
Deepak Kodihallic682fe22020-03-04 00:42:54 -060023 ASSERT_EQ(outRepo.getRecordCount(), 2);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050024
25 // Check first PDR
George Liue53193f2020-02-24 09:23:26 +080026 pdr_utils::PdrEntry e;
Deepak Kodihallic682fe22020-03-04 00:42:54 -060027 auto record1 = pdr::getRecordByHandle(outRepo, 1, e);
George Liue53193f2020-02-24 09:23:26 +080028 ASSERT_NE(record1, nullptr);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050029 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +080030 reinterpret_cast<pldm_state_effecter_pdr*>(e.data);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050031
32 ASSERT_EQ(pdr->hdr.record_handle, 1);
33 ASSERT_EQ(pdr->hdr.version, 1);
34 ASSERT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR);
35 ASSERT_EQ(pdr->hdr.record_change_num, 0);
Sampa Misraa2fa0702019-05-31 01:28:55 -050036 ASSERT_EQ(pdr->hdr.length, 23);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050037
38 ASSERT_EQ(pdr->terminus_handle, 0);
39 ASSERT_EQ(pdr->effecter_id, 1);
40 ASSERT_EQ(pdr->entity_type, 33);
41 ASSERT_EQ(pdr->entity_instance, 0);
42 ASSERT_EQ(pdr->container_id, 0);
43 ASSERT_EQ(pdr->effecter_semantic_id, 0);
44 ASSERT_EQ(pdr->effecter_init, PLDM_NO_INIT);
45 ASSERT_EQ(pdr->has_description_pdr, false);
Sampa Misraa2fa0702019-05-31 01:28:55 -050046 ASSERT_EQ(pdr->composite_effecter_count, 2);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050047 state_effecter_possible_states* states =
48 reinterpret_cast<state_effecter_possible_states*>(pdr->possible_states);
49 ASSERT_EQ(states->state_set_id, 196);
50 ASSERT_EQ(states->possible_states_size, 1);
51 bitfield8_t bf1{};
52 bf1.byte = 2;
53 ASSERT_EQ(states->states[0].byte, bf1.byte);
54
Deepak Kodihallic682fe22020-03-04 00:42:54 -060055 const auto& paths = handler.getEffecterObjs(pdr->effecter_id);
Deepak Kodihallid0782962019-05-07 16:36:47 +053056 ASSERT_EQ(paths[0], "/foo/bar");
57
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050058 // Check second PDR
Deepak Kodihallic682fe22020-03-04 00:42:54 -060059 auto record2 = pdr::getRecordByHandle(outRepo, 2, e);
George Liue53193f2020-02-24 09:23:26 +080060 ASSERT_NE(record2, nullptr);
61 pdr = reinterpret_cast<pldm_state_effecter_pdr*>(e.data);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050062
63 ASSERT_EQ(pdr->hdr.record_handle, 2);
64 ASSERT_EQ(pdr->hdr.version, 1);
65 ASSERT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR);
66 ASSERT_EQ(pdr->hdr.record_change_num, 0);
67 ASSERT_EQ(pdr->hdr.length, 24);
68
69 ASSERT_EQ(pdr->terminus_handle, 0);
70 ASSERT_EQ(pdr->effecter_id, 2);
71 ASSERT_EQ(pdr->entity_type, 100);
72 ASSERT_EQ(pdr->entity_instance, 0);
73 ASSERT_EQ(pdr->container_id, 0);
74 ASSERT_EQ(pdr->effecter_semantic_id, 0);
75 ASSERT_EQ(pdr->effecter_init, PLDM_NO_INIT);
76 ASSERT_EQ(pdr->has_description_pdr, false);
77 ASSERT_EQ(pdr->composite_effecter_count, 2);
78 states =
79 reinterpret_cast<state_effecter_possible_states*>(pdr->possible_states);
80 ASSERT_EQ(states->state_set_id, 197);
81 ASSERT_EQ(states->possible_states_size, 1);
82 bf1.byte = 2;
83 ASSERT_EQ(states->states[0].byte, bf1.byte);
84 states = reinterpret_cast<state_effecter_possible_states*>(
85 pdr->possible_states + sizeof(state_effecter_possible_states));
86 ASSERT_EQ(states->state_set_id, 198);
87 ASSERT_EQ(states->possible_states_size, 2);
88 bitfield8_t bf2[2];
89 bf2[0].byte = 38;
90 bf2[1].byte = 128;
91 ASSERT_EQ(states->states[0].byte, bf2[0].byte);
92 ASSERT_EQ(states->states[1].byte, bf2[1].byte);
Deepak Kodihallid0782962019-05-07 16:36:47 +053093
Deepak Kodihallic682fe22020-03-04 00:42:54 -060094 const auto& paths1 = handler.getEffecterObjs(pdr->effecter_id);
95 ASSERT_EQ(paths1[0], "/foo/bar");
96 ASSERT_EQ(paths1[1], "/foo/bar/baz");
Deepak Kodihallid0782962019-05-07 16:36:47 +053097
Deepak Kodihallic682fe22020-03-04 00:42:54 -060098 ASSERT_THROW(handler.getEffecterObjs(0xDEAD), std::exception);
99
100 pldm_pdr_destroy(inPDRRepo);
101 pldm_pdr_destroy(outPDRRepo);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -0500102}
103
104TEST(GeneratePDR, testNoJson)
105{
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600106 auto pdrRepo = pldm_pdr_init();
107
108 ASSERT_THROW(Handler("./pdr_jsons/not_there", pdrRepo), std::exception);
109
110 pldm_pdr_destroy(pdrRepo);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -0500111}
112
113TEST(GeneratePDR, testMalformedJson)
114{
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600115 auto inPDRRepo = pldm_pdr_init();
116 auto outPDRRepo = pldm_pdr_init();
117 Repo outRepo(outPDRRepo);
118 Handler handler("./pdr_jsons/state_effecter/good", inPDRRepo);
119 Repo inRepo(inPDRRepo);
120 getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
121
122 ASSERT_EQ(outRepo.getRecordCount(), 2);
George Liue53193f2020-02-24 09:23:26 +0800123 ASSERT_THROW(pdr_utils::readJson("./pdr_jsons/state_effecter/malformed"),
Sampa Misraaa8ae722019-12-12 03:20:40 -0600124 std::exception);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600125
126 pldm_pdr_destroy(inPDRRepo);
127 pldm_pdr_destroy(outPDRRepo);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -0500128}