blob: 73bcfbbde95b22b884fd1c043e2708ea0ddc2743 [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
George Liu1ec85d42020-02-12 16:05:32 +080055 const auto& [dbusMappings1, dbusValMaps1] =
56 handler.getDbusObjMaps(pdr->effecter_id);
57 ASSERT_EQ(dbusMappings1[0].objectPath, "/foo/bar");
Deepak Kodihallid0782962019-05-07 16:36:47 +053058
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050059 // Check second PDR
Deepak Kodihallic682fe22020-03-04 00:42:54 -060060 auto record2 = pdr::getRecordByHandle(outRepo, 2, e);
George Liue53193f2020-02-24 09:23:26 +080061 ASSERT_NE(record2, nullptr);
62 pdr = reinterpret_cast<pldm_state_effecter_pdr*>(e.data);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050063
64 ASSERT_EQ(pdr->hdr.record_handle, 2);
65 ASSERT_EQ(pdr->hdr.version, 1);
66 ASSERT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR);
67 ASSERT_EQ(pdr->hdr.record_change_num, 0);
68 ASSERT_EQ(pdr->hdr.length, 24);
69
70 ASSERT_EQ(pdr->terminus_handle, 0);
71 ASSERT_EQ(pdr->effecter_id, 2);
72 ASSERT_EQ(pdr->entity_type, 100);
73 ASSERT_EQ(pdr->entity_instance, 0);
74 ASSERT_EQ(pdr->container_id, 0);
75 ASSERT_EQ(pdr->effecter_semantic_id, 0);
76 ASSERT_EQ(pdr->effecter_init, PLDM_NO_INIT);
77 ASSERT_EQ(pdr->has_description_pdr, false);
78 ASSERT_EQ(pdr->composite_effecter_count, 2);
79 states =
80 reinterpret_cast<state_effecter_possible_states*>(pdr->possible_states);
81 ASSERT_EQ(states->state_set_id, 197);
82 ASSERT_EQ(states->possible_states_size, 1);
83 bf1.byte = 2;
84 ASSERT_EQ(states->states[0].byte, bf1.byte);
85 states = reinterpret_cast<state_effecter_possible_states*>(
86 pdr->possible_states + sizeof(state_effecter_possible_states));
87 ASSERT_EQ(states->state_set_id, 198);
88 ASSERT_EQ(states->possible_states_size, 2);
89 bitfield8_t bf2[2];
90 bf2[0].byte = 38;
91 bf2[1].byte = 128;
92 ASSERT_EQ(states->states[0].byte, bf2[0].byte);
93 ASSERT_EQ(states->states[1].byte, bf2[1].byte);
Deepak Kodihallid0782962019-05-07 16:36:47 +053094
George Liu1ec85d42020-02-12 16:05:32 +080095 const auto& [dbusMappings2, dbusValMaps2] =
96 handler.getDbusObjMaps(pdr->effecter_id);
97 ASSERT_EQ(dbusMappings2[0].objectPath, "/foo/bar");
98 ASSERT_EQ(dbusMappings2[1].objectPath, "/foo/bar/baz");
Deepak Kodihallid0782962019-05-07 16:36:47 +053099
George Liu1ec85d42020-02-12 16:05:32 +0800100 ASSERT_THROW(handler.getDbusObjMaps(0xDEAD), std::exception);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600101
102 pldm_pdr_destroy(inPDRRepo);
103 pldm_pdr_destroy(outPDRRepo);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -0500104}
105
106TEST(GeneratePDR, testNoJson)
107{
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600108 auto pdrRepo = pldm_pdr_init();
109
110 ASSERT_THROW(Handler("./pdr_jsons/not_there", pdrRepo), std::exception);
111
112 pldm_pdr_destroy(pdrRepo);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -0500113}
114
115TEST(GeneratePDR, testMalformedJson)
116{
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600117 auto inPDRRepo = pldm_pdr_init();
118 auto outPDRRepo = pldm_pdr_init();
119 Repo outRepo(outPDRRepo);
120 Handler handler("./pdr_jsons/state_effecter/good", inPDRRepo);
121 Repo inRepo(inPDRRepo);
122 getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
123
124 ASSERT_EQ(outRepo.getRecordCount(), 2);
George Liue53193f2020-02-24 09:23:26 +0800125 ASSERT_THROW(pdr_utils::readJson("./pdr_jsons/state_effecter/malformed"),
Sampa Misraaa8ae722019-12-12 03:20:40 -0600126 std::exception);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600127
128 pldm_pdr_destroy(inPDRRepo);
129 pldm_pdr_destroy(outPDRRepo);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -0500130}