xref: /openbmc/phosphor-ipmi-blobs/test/ipmi_unittest.cpp (revision ef3aeadc9be37c47d0627e576e81a74a5bb9e94f)
1*ef3aeadcSPatrick Venture #include "ipmi.hpp"
2*ef3aeadcSPatrick Venture 
3*ef3aeadcSPatrick Venture #include <cstring>
4*ef3aeadcSPatrick Venture 
5*ef3aeadcSPatrick Venture #include <gtest/gtest.h>
6*ef3aeadcSPatrick Venture 
7*ef3aeadcSPatrick Venture namespace blobs
8*ef3aeadcSPatrick Venture {
9*ef3aeadcSPatrick Venture 
10*ef3aeadcSPatrick Venture // ipmid.hpp isn't installed where we can grab it and this value is per BMC
11*ef3aeadcSPatrick Venture // SoC.
12*ef3aeadcSPatrick Venture #define MAX_IPMI_BUFFER 64
13*ef3aeadcSPatrick Venture 
14*ef3aeadcSPatrick Venture TEST(StringInputTest, NullPointerInput)
15*ef3aeadcSPatrick Venture {
16*ef3aeadcSPatrick Venture     // The method should verify it did receive a non-null input pointer.
17*ef3aeadcSPatrick Venture 
18*ef3aeadcSPatrick Venture     EXPECT_STREQ("", stringFromBuffer(NULL, 5).c_str());
19*ef3aeadcSPatrick Venture }
20*ef3aeadcSPatrick Venture 
21*ef3aeadcSPatrick Venture TEST(StringInputTest, ZeroBytesInput)
22*ef3aeadcSPatrick Venture {
23*ef3aeadcSPatrick Venture     // Verify that if the input length is 0 that it'll return the empty string.
24*ef3aeadcSPatrick Venture 
25*ef3aeadcSPatrick Venture     const char* request = "asdf";
26*ef3aeadcSPatrick Venture     EXPECT_STREQ("", stringFromBuffer(request, 0).c_str());
27*ef3aeadcSPatrick Venture }
28*ef3aeadcSPatrick Venture 
29*ef3aeadcSPatrick Venture TEST(StringInputTest, NulTerminatorNotFound)
30*ef3aeadcSPatrick Venture {
31*ef3aeadcSPatrick Venture     // Verify that if there isn't a nul-terminator found in an otherwise valid
32*ef3aeadcSPatrick Venture     // string, it'll return the emptry string.
33*ef3aeadcSPatrick Venture 
34*ef3aeadcSPatrick Venture     char request[MAX_IPMI_BUFFER];
35*ef3aeadcSPatrick Venture     std::memset(request, 'a', sizeof(request));
36*ef3aeadcSPatrick Venture     EXPECT_STREQ("", stringFromBuffer(request, sizeof(request)).c_str());
37*ef3aeadcSPatrick Venture }
38*ef3aeadcSPatrick Venture 
39*ef3aeadcSPatrick Venture TEST(StringInputTest, TwoNulsFound)
40*ef3aeadcSPatrick Venture {
41*ef3aeadcSPatrick Venture     // Verify it makes you use the entire data region for the string.
42*ef3aeadcSPatrick Venture     char request[MAX_IPMI_BUFFER];
43*ef3aeadcSPatrick Venture     request[0] = 'a';
44*ef3aeadcSPatrick Venture     request[1] = 0;
45*ef3aeadcSPatrick Venture     std::memset(&request[2], 'b', sizeof(request) - 2);
46*ef3aeadcSPatrick Venture     request[MAX_IPMI_BUFFER - 1] = 0;
47*ef3aeadcSPatrick Venture 
48*ef3aeadcSPatrick Venture     // This case has two strings, and the last character is a nul-terminator.
49*ef3aeadcSPatrick Venture     EXPECT_STREQ("", stringFromBuffer(request, sizeof(request)).c_str());
50*ef3aeadcSPatrick Venture }
51*ef3aeadcSPatrick Venture 
52*ef3aeadcSPatrick Venture TEST(StringInputTest, NulTerminatorFound)
53*ef3aeadcSPatrick Venture {
54*ef3aeadcSPatrick Venture     // Verify that if it's provided a valid nul-terminated string, it'll
55*ef3aeadcSPatrick Venture     // return it.
56*ef3aeadcSPatrick Venture 
57*ef3aeadcSPatrick Venture     const char* request = "asdf";
58*ef3aeadcSPatrick Venture     EXPECT_STREQ("asdf", stringFromBuffer(request, 5).c_str());
59*ef3aeadcSPatrick Venture }
60*ef3aeadcSPatrick Venture } // namespace blobs
61