blob: 7016c6e58238a2287d9f74ae1ad834ef0e00344e [file] [log] [blame]
William A. Kennington III194375f2018-12-14 02:14:33 -08001#include <ipmid/api.h>
Peter Hanson4a589852017-06-07 17:40:45 -07002
William A. Kennington III194375f2018-12-14 02:14:33 -08003#include <ipmid/oemrouter.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -07004
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05005#include <cstring>
Patrick Venture0b02be92018-08-31 11:55:55 -07006
Peter Hanson4a589852017-06-07 17:40:45 -07007#include <gtest/gtest.h>
8
9// Watch for correct singleton behavior.
10static oem::Router* singletonUnderTest;
11
12static ipmid_callback_t wildHandler;
13
14static ipmi_netfn_t lastNetFunction;
15
16// Fake ipmi_register_callback() for this test.
17void ipmi_register_callback(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
18 ipmi_context_t context, ipmid_callback_t cb,
19 ipmi_cmd_privilege_t priv)
20{
21 EXPECT_EQ(NETFUN_OEM_GROUP, netfn);
George Liuc6837ed2024-12-05 17:15:18 +080022 EXPECT_EQ(cmdWildcard, cmd);
Peter Hanson4a589852017-06-07 17:40:45 -070023 EXPECT_EQ(reinterpret_cast<void*>(singletonUnderTest), context);
24 EXPECT_EQ(PRIVILEGE_OEM, priv);
25 lastNetFunction = netfn;
26 wildHandler = cb;
27}
28
29namespace oem
30{
31
32namespace
33{
34void MakeRouter()
35{
36 if (!singletonUnderTest)
37 {
38 singletonUnderTest = mutableRouter();
39 }
40 ASSERT_EQ(singletonUnderTest, mutableRouter());
41}
42
43void ActivateRouter()
44{
45 MakeRouter();
46 singletonUnderTest->activate();
47 ASSERT_EQ(NETFUN_OEM_GROUP, lastNetFunction);
48}
49
50void RegisterWithRouter(Number oen, ipmi_cmd_t cmd, Handler cb)
51{
52 ActivateRouter();
53 singletonUnderTest->registerHandler(oen, cmd, cb);
54}
55
Patrick Venture0b02be92018-08-31 11:55:55 -070056uint8_t msgPlain[] = {0x56, 0x34, 0x12};
57uint8_t replyPlain[] = {0x56, 0x34, 0x12, 0x31, 0x41};
58uint8_t msgPlus2[] = {0x67, 0x45, 0x23, 0x10, 0x20};
59uint8_t msgBadOen[] = {0x57, 0x34, 0x12};
Peter Hanson4a589852017-06-07 17:40:45 -070060
Patrick Venture0b02be92018-08-31 11:55:55 -070061void RegisterTwoWays(ipmi_cmd_t* nextCmd)
Peter Hanson4a589852017-06-07 17:40:45 -070062{
Patrick Venture0b02be92018-08-31 11:55:55 -070063 Handler f = [](ipmi_cmd_t cmd, const uint8_t* reqBuf, uint8_t* replyBuf,
64 size_t* dataLen) {
Peter Hanson4a589852017-06-07 17:40:45 -070065 // Check inputs
66 EXPECT_EQ(0x78, cmd);
Patrick Venture0b02be92018-08-31 11:55:55 -070067 EXPECT_EQ(0, *dataLen); // Excludes OEN
Peter Hanson4a589852017-06-07 17:40:45 -070068
69 // Generate reply.
70 *dataLen = 2;
71 std::memcpy(replyBuf, replyPlain + 3, *dataLen);
72 return 0;
73 };
74 RegisterWithRouter(0x123456, 0x78, f);
75
George Liuc6837ed2024-12-05 17:15:18 +080076 *nextCmd = cmdWildcard;
Peter Hanson4a589852017-06-07 17:40:45 -070077 Handler g = [nextCmd](ipmi_cmd_t cmd, const uint8_t* reqBuf,
Patrick Venture0b02be92018-08-31 11:55:55 -070078 uint8_t* replyBuf, size_t* dataLen) {
Peter Hanson4a589852017-06-07 17:40:45 -070079 // Check inputs
80 EXPECT_EQ(*nextCmd, cmd);
Patrick Venture0b02be92018-08-31 11:55:55 -070081 EXPECT_EQ(2, *dataLen); // Excludes OEN
Peter Hanson4a589852017-06-07 17:40:45 -070082 if (2 != *dataLen)
83 {
84 return 0xE0;
85 }
86 EXPECT_EQ(msgPlus2[3], reqBuf[0]);
87 EXPECT_EQ(msgPlus2[4], reqBuf[1]);
88
89 // Generate reply.
90 *dataLen = 0;
91 return 0;
92 };
George Liuc6837ed2024-12-05 17:15:18 +080093 RegisterWithRouter(0x234567, cmdWildcard, g);
Peter Hanson4a589852017-06-07 17:40:45 -070094}
Patrick Venture0b02be92018-08-31 11:55:55 -070095} // namespace
Peter Hanson4a589852017-06-07 17:40:45 -070096
Patrick Venture0b02be92018-08-31 11:55:55 -070097TEST(OemRouterTest, MakeRouterProducesConsistentSingleton)
98{
Peter Hanson4a589852017-06-07 17:40:45 -070099 MakeRouter();
100}
101
Patrick Venture0b02be92018-08-31 11:55:55 -0700102TEST(OemRouterTest, ActivateRouterSetsLastNetToOEMGROUP)
103{
Peter Hanson4a589852017-06-07 17:40:45 -0700104 lastNetFunction = 0;
105 ActivateRouter();
106}
107
Patrick Venture0b02be92018-08-31 11:55:55 -0700108TEST(OemRouterTest, VerifiesSpecificCommandMatches)
109{
Peter Hanson4a589852017-06-07 17:40:45 -0700110 ipmi_cmd_t cmd;
111 uint8_t reply[256];
112 size_t dataLen;
113
114 RegisterTwoWays(&cmd);
115
116 dataLen = 3;
Patrick Venture0b02be92018-08-31 11:55:55 -0700117 EXPECT_EQ(0, wildHandler(NETFUN_OEM_GROUP, 0x78, msgPlain, reply, &dataLen,
118 nullptr));
Peter Hanson4a589852017-06-07 17:40:45 -0700119 EXPECT_EQ(5, dataLen);
120 EXPECT_EQ(replyPlain[0], reply[0]);
121 EXPECT_EQ(replyPlain[1], reply[1]);
122 EXPECT_EQ(replyPlain[2], reply[2]);
123 EXPECT_EQ(replyPlain[3], reply[3]);
124 EXPECT_EQ(replyPlain[4], reply[4]);
125}
126
Patrick Venture0b02be92018-08-31 11:55:55 -0700127TEST(OemRouterTest, WildCardMatchesTwoRandomCodes)
128{
Peter Hanson4a589852017-06-07 17:40:45 -0700129 ipmi_cmd_t cmd;
130 uint8_t reply[256];
131 size_t dataLen;
132
133 RegisterTwoWays(&cmd);
134
135 // Check two random command codes.
136 dataLen = 5;
137 cmd = 0x89;
Patrick Venture0b02be92018-08-31 11:55:55 -0700138 EXPECT_EQ(0, wildHandler(NETFUN_OEM_GROUP, cmd, msgPlus2, reply, &dataLen,
139 nullptr));
Peter Hanson4a589852017-06-07 17:40:45 -0700140 EXPECT_EQ(3, dataLen);
141
142 dataLen = 5;
143 cmd = 0x67;
Patrick Venture0b02be92018-08-31 11:55:55 -0700144 EXPECT_EQ(0, wildHandler(NETFUN_OEM_GROUP, cmd, msgPlus2, reply, &dataLen,
145 nullptr));
Peter Hanson4a589852017-06-07 17:40:45 -0700146 EXPECT_EQ(3, dataLen);
147}
148
Patrick Venture0b02be92018-08-31 11:55:55 -0700149TEST(OemRouterTest, CommandsAreRejectedIfInvalid)
150{
Peter Hanson4a589852017-06-07 17:40:45 -0700151 ipmi_cmd_t cmd;
152 uint8_t reply[256];
153 size_t dataLen;
154
155 RegisterTwoWays(&cmd);
156
157 // Message too short to include whole OEN?
158 dataLen = 2;
159 EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
Patrick Venture0b02be92018-08-31 11:55:55 -0700160 wildHandler(NETFUN_OEM_GROUP, 0x78, msgPlain, reply, &dataLen,
161 nullptr));
Peter Hanson4a589852017-06-07 17:40:45 -0700162
163 // Wrong specific command?
164 dataLen = 3;
Patrick Venture0b02be92018-08-31 11:55:55 -0700165 EXPECT_EQ(IPMI_CC_INVALID, wildHandler(NETFUN_OEM_GROUP, 0x89, msgPlain,
166 reply, &dataLen, nullptr));
Peter Hanson4a589852017-06-07 17:40:45 -0700167
168 // Wrong OEN?
169 dataLen = 3;
Patrick Venture0b02be92018-08-31 11:55:55 -0700170 EXPECT_EQ(IPMI_CC_INVALID, wildHandler(NETFUN_OEM_GROUP, 0x78, msgBadOen,
171 reply, &dataLen, nullptr));
Peter Hanson4a589852017-06-07 17:40:45 -0700172}
173
Patrick Venture0b02be92018-08-31 11:55:55 -0700174} // namespace oem