blob: a5a79ddd90fe1e0c86d2709b59ca21b8d9d9adf5 [file] [log] [blame]
Peter Hanson4a589852017-06-07 17:40:45 -07001#include "host-ipmid/ipmid-api.h"
2#include "host-ipmid/oemrouter.hpp"
3#include "sample.h"
4
5#include <cstring>
6#include <gtest/gtest.h>
7
8// Watch for correct singleton behavior.
9static oem::Router* singletonUnderTest;
10
11static ipmid_callback_t wildHandler;
12
13static ipmi_netfn_t lastNetFunction;
14
15// Fake ipmi_register_callback() for this test.
16void ipmi_register_callback(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
17 ipmi_context_t context, ipmid_callback_t cb,
18 ipmi_cmd_privilege_t priv)
19{
20 EXPECT_EQ(NETFUN_OEM_GROUP, netfn);
21 EXPECT_EQ(IPMI_CMD_WILDCARD, cmd);
22 EXPECT_EQ(reinterpret_cast<void*>(singletonUnderTest), context);
23 EXPECT_EQ(PRIVILEGE_OEM, priv);
24 lastNetFunction = netfn;
25 wildHandler = cb;
26}
27
28namespace oem
29{
30
31namespace
32{
33void MakeRouter()
34{
35 if (!singletonUnderTest)
36 {
37 singletonUnderTest = mutableRouter();
38 }
39 ASSERT_EQ(singletonUnderTest, mutableRouter());
40}
41
42void ActivateRouter()
43{
44 MakeRouter();
45 singletonUnderTest->activate();
46 ASSERT_EQ(NETFUN_OEM_GROUP, lastNetFunction);
47}
48
49void RegisterWithRouter(Number oen, ipmi_cmd_t cmd, Handler cb)
50{
51 ActivateRouter();
52 singletonUnderTest->registerHandler(oen, cmd, cb);
53}
54
55uint8_t msgPlain[] = { 0x56, 0x34, 0x12 };
56uint8_t replyPlain[] = { 0x56, 0x34, 0x12, 0x31, 0x41 };
57uint8_t msgPlus2[] = { 0x67, 0x45, 0x23, 0x10, 0x20 };
58uint8_t msgBadOen[] = { 0x57, 0x34, 0x12 };
59
60void RegisterTwoWays(ipmi_cmd_t *nextCmd)
61{
62 Handler f = [](ipmi_cmd_t cmd, const uint8_t* reqBuf,
63 uint8_t* replyBuf, size_t* dataLen)
64 {
65 // Check inputs
66 EXPECT_EQ(0x78, cmd);
67 EXPECT_EQ(0, *dataLen); // Excludes OEN
68
69 // Generate reply.
70 *dataLen = 2;
71 std::memcpy(replyBuf, replyPlain + 3, *dataLen);
72 return 0;
73 };
74 RegisterWithRouter(0x123456, 0x78, f);
75
76 *nextCmd = IPMI_CMD_WILDCARD;
77 Handler g = [nextCmd](ipmi_cmd_t cmd, const uint8_t* reqBuf,
78 uint8_t* replyBuf, size_t* dataLen)
79 {
80 // Check inputs
81 EXPECT_EQ(*nextCmd, cmd);
82 EXPECT_EQ(2, *dataLen); // Excludes OEN
83 if (2 != *dataLen)
84 {
85 return 0xE0;
86 }
87 EXPECT_EQ(msgPlus2[3], reqBuf[0]);
88 EXPECT_EQ(msgPlus2[4], reqBuf[1]);
89
90 // Generate reply.
91 *dataLen = 0;
92 return 0;
93 };
94 RegisterWithRouter(0x234567, IPMI_CMD_WILDCARD, g);
95}
96} // namespace
97
98TEST(OemRouterTest, MakeRouterProducesConsistentSingleton) {
99 MakeRouter();
100}
101
102TEST(OemRouterTest, ActivateRouterSetsLastNetToOEMGROUP) {
103 lastNetFunction = 0;
104 ActivateRouter();
105}
106
107TEST(OemRouterTest, VerifiesSpecificCommandMatches) {
108 ipmi_cmd_t cmd;
109 uint8_t reply[256];
110 size_t dataLen;
111
112 RegisterTwoWays(&cmd);
113
114 dataLen = 3;
115 EXPECT_EQ(0,
116 wildHandler(NETFUN_OEM_GROUP, 0x78, msgPlain, reply,
117 &dataLen, nullptr));
118 EXPECT_EQ(5, dataLen);
119 EXPECT_EQ(replyPlain[0], reply[0]);
120 EXPECT_EQ(replyPlain[1], reply[1]);
121 EXPECT_EQ(replyPlain[2], reply[2]);
122 EXPECT_EQ(replyPlain[3], reply[3]);
123 EXPECT_EQ(replyPlain[4], reply[4]);
124}
125
126TEST(OemRouterTest, WildCardMatchesTwoRandomCodes) {
127 ipmi_cmd_t cmd;
128 uint8_t reply[256];
129 size_t dataLen;
130
131 RegisterTwoWays(&cmd);
132
133 // Check two random command codes.
134 dataLen = 5;
135 cmd = 0x89;
136 EXPECT_EQ(0,
137 wildHandler(NETFUN_OEM_GROUP, cmd, msgPlus2, reply,
138 &dataLen, nullptr));
139 EXPECT_EQ(3, dataLen);
140
141 dataLen = 5;
142 cmd = 0x67;
143 EXPECT_EQ(0,
144 wildHandler(NETFUN_OEM_GROUP, cmd, msgPlus2, reply,
145 &dataLen, nullptr));
146 EXPECT_EQ(3, dataLen);
147}
148
149TEST(OemRouterTest, CommandsAreRejectedIfInvalid) {
150 ipmi_cmd_t cmd;
151 uint8_t reply[256];
152 size_t dataLen;
153
154 RegisterTwoWays(&cmd);
155
156 // Message too short to include whole OEN?
157 dataLen = 2;
158 EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
159 wildHandler(NETFUN_OEM_GROUP, 0x78, msgPlain, reply,
160 &dataLen, nullptr));
161
162 // Wrong specific command?
163 dataLen = 3;
164 EXPECT_EQ(IPMI_CC_INVALID,
165 wildHandler(NETFUN_OEM_GROUP, 0x89, msgPlain, reply,
166 &dataLen, nullptr));
167
168 // Wrong OEN?
169 dataLen = 3;
170 EXPECT_EQ(IPMI_CC_INVALID,
171 wildHandler(NETFUN_OEM_GROUP, 0x78, msgBadOen, reply,
172 &dataLen, nullptr));
173}
174
175} // namespace oem