xref: /openbmc/phosphor-ipmi-blobs/test/ipmi_unittest.cpp (revision 97e69ca106fc2415f89370eea36fb674435b5bdb)
1ef3aeadcSPatrick Venture #include "ipmi.hpp"
2ef3aeadcSPatrick Venture 
3ef3aeadcSPatrick Venture #include <cstring>
4ef3aeadcSPatrick Venture 
5ef3aeadcSPatrick Venture #include <gtest/gtest.h>
6ef3aeadcSPatrick Venture 
7ef3aeadcSPatrick Venture namespace blobs
8ef3aeadcSPatrick Venture {
9ef3aeadcSPatrick Venture 
10ef3aeadcSPatrick Venture // ipmid.hpp isn't installed where we can grab it and this value is per BMC
11ef3aeadcSPatrick Venture // SoC.
12ef3aeadcSPatrick Venture #define MAX_IPMI_BUFFER 64
13ef3aeadcSPatrick Venture 
TEST(StringInputTest,NullPointerInput)14ef3aeadcSPatrick Venture TEST(StringInputTest, NullPointerInput)
15ef3aeadcSPatrick Venture {
16ef3aeadcSPatrick Venture     // The method should verify it did receive a non-null input pointer.
17067ece15SWilly Tu     EXPECT_STREQ("", stringFromBuffer({}).c_str());
18ef3aeadcSPatrick Venture }
19ef3aeadcSPatrick Venture 
TEST(StringInputTest,ZeroBytesInput)20ef3aeadcSPatrick Venture TEST(StringInputTest, ZeroBytesInput)
21ef3aeadcSPatrick Venture {
22ef3aeadcSPatrick Venture     // Verify that if the input length is 0 that it'll return the empty string.
23067ece15SWilly Tu     const std::string request = "asdf";
24067ece15SWilly Tu     EXPECT_STREQ("", stringFromBuffer(
25067ece15SWilly Tu                          std::vector<uint8_t>(request.begin(), request.end()))
26067ece15SWilly Tu                          .c_str());
27ef3aeadcSPatrick Venture }
28ef3aeadcSPatrick Venture 
TEST(StringInputTest,NulTerminatorNotFound)29ef3aeadcSPatrick Venture TEST(StringInputTest, NulTerminatorNotFound)
30ef3aeadcSPatrick Venture {
31ef3aeadcSPatrick Venture     // Verify that if there isn't a nul-terminator found in an otherwise valid
32ef3aeadcSPatrick Venture     // string, it'll return the emptry string.
33067ece15SWilly Tu     std::array<char, MAX_IPMI_BUFFER> request;
34067ece15SWilly Tu     std::memset(request.data(), 'a', sizeof(request));
35067ece15SWilly Tu     EXPECT_STREQ("", stringFromBuffer(
36067ece15SWilly Tu                          std::vector<uint8_t>(request.begin(), request.end()))
37067ece15SWilly Tu                          .c_str());
38ef3aeadcSPatrick Venture }
39ef3aeadcSPatrick Venture 
TEST(StringInputTest,NulTerminatorFound)40ef3aeadcSPatrick Venture TEST(StringInputTest, NulTerminatorFound)
41ef3aeadcSPatrick Venture {
42ef3aeadcSPatrick Venture     // Verify that if it's provided a valid nul-terminated string, it'll
43ef3aeadcSPatrick Venture     // return it.
44067ece15SWilly Tu     std::string request = "asdf";
45067ece15SWilly Tu     request.push_back('\0');
46*97e69ca1SPatrick Williams     EXPECT_STREQ(
47*97e69ca1SPatrick Williams         "asdf",
48*97e69ca1SPatrick Williams         stringFromBuffer(std::vector<uint8_t>(request.begin(), request.end()))
49067ece15SWilly Tu             .c_str());
50ef3aeadcSPatrick Venture }
51ef3aeadcSPatrick Venture } // namespace blobs
52