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