diff options
Diffstat (limited to 'test/message')
-rw-r--r-- | test/message/pack.cpp | 71 | ||||
-rw-r--r-- | test/message/payload.cpp | 134 | ||||
-rw-r--r-- | test/message/unpack.cpp | 51 |
3 files changed, 253 insertions, 3 deletions
diff --git a/test/message/pack.cpp b/test/message/pack.cpp index d0a738a..9e88f2b 100644 --- a/test/message/pack.cpp +++ b/test/message/pack.cpp @@ -235,6 +235,27 @@ TEST(PackBasics, VectorUint8) ASSERT_EQ(p.raw, k); } +TEST(PackBasics, VectorUnaligned) +{ + ipmi::message::Payload p; + EXPECT_EQ(p.pack(true, std::vector<uint8_t>{1}), 1); + EXPECT_EQ(p.raw, std::vector<uint8_t>{0b1}); +} + +TEST(PackBasics, StringView) +{ + ipmi::message::Payload p; + EXPECT_EQ(p.pack(std::string_view{"\x24\x30\x11"}), 0); + EXPECT_EQ(p.raw, std::vector<uint8_t>({0x24, 0x30, 0x11})); +} + +TEST(PackBasics, StringViewUnaligned) +{ + ipmi::message::Payload p; + EXPECT_EQ(p.pack(true, std::string_view{"abc"}), 1); + EXPECT_EQ(p.raw, std::vector<uint8_t>({0b1})); +} + TEST(PackBasics, OptionalEmpty) { // an optional will only pack if the value is present @@ -261,6 +282,56 @@ TEST(PackBasics, OptionalContainsValue) ASSERT_EQ(p.raw, k); } +TEST(PackBasics, Payload) +{ + ipmi::message::Payload p; + EXPECT_EQ(p.pack(true), 0); + EXPECT_EQ(p.pack(ipmi::message::Payload({0x24, 0x30})), 0); + EXPECT_EQ(p.raw, std::vector<uint8_t>({0b1, 0x24, 0x30})); +} + +TEST(PackBasics, PayloadUnaligned) +{ + ipmi::message::Payload p; + EXPECT_EQ(p.pack(true, ipmi::message::Payload({0x24})), 1); + EXPECT_EQ(p.raw, std::vector<uint8_t>({0b1})); +} + +TEST(PackBasics, PayloadOtherUnaligned) +{ + ipmi::message::Payload p, q; + q.appendBits(1, 1); + EXPECT_EQ(p.pack(true), 0); + EXPECT_EQ(p.pack(q), 1); + EXPECT_EQ(p.raw, std::vector<uint8_t>({0b1})); +} + +TEST(PackBasics, PrependPayload) +{ + ipmi::message::Payload p; + EXPECT_EQ(p.pack(true), 0); + EXPECT_EQ(p.prepend(ipmi::message::Payload({0x24, 0x30})), 0); + EXPECT_EQ(p.raw, std::vector<uint8_t>({0x24, 0x30, 0b1})); +} + +TEST(PackBasics, PrependPayloadUnaligned) +{ + ipmi::message::Payload p; + p.appendBits(1, 1); + EXPECT_EQ(p.prepend(ipmi::message::Payload({0x24})), 1); + p.drain(); + EXPECT_EQ(p.raw, std::vector<uint8_t>({0b1})); +} + +TEST(PackBasics, PrependPayloadOtherUnaligned) +{ + ipmi::message::Payload p, q; + q.appendBits(1, 1); + EXPECT_EQ(p.pack(true), 0); + EXPECT_EQ(p.prepend(q), 1); + EXPECT_EQ(p.raw, std::vector<uint8_t>({0b1})); +} + TEST(PackAdvanced, Uints) { // all elements will be processed in order, with each multi-byte diff --git a/test/message/payload.cpp b/test/message/payload.cpp index 9d20ff0..56d8d41 100644 --- a/test/message/payload.cpp +++ b/test/message/payload.cpp @@ -13,8 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#define SD_JOURNAL_SUPPRESS_LOCATION + +#include <systemd/sd-journal.h> + #include <ipmid/api.hpp> #include <ipmid/message.hpp> +#include <stdexcept> #include <gtest/gtest.h> @@ -306,3 +311,132 @@ TEST(PayloadRequest, PartialPayload) uint32_t k2 = 0x02008604; ASSERT_EQ(v2, k2); } + +std::vector<std::string> logs; + +extern "C" { +int sd_journal_send(const char* format, ...) +{ + logs.push_back(format); + return 0; +} + +int sd_journal_send_with_location(const char* file, const char* line, + const char* func, const char* format, ...) +{ + logs.push_back(format); + return 0; +} +} + +class PayloadLogging : public testing::Test +{ + public: + void SetUp() + { + logs.clear(); + } +}; + +TEST_F(PayloadLogging, TrailingOk) +{ + { + ipmi::message::Payload p({1, 2}); + } + EXPECT_EQ(logs.size(), 0); +} + +TEST_F(PayloadLogging, EnforcingUnchecked) +{ + { + ipmi::message::Payload p({1, 2}); + p.trailingOk = false; + } + EXPECT_EQ(logs.size(), 1); +} + +TEST_F(PayloadLogging, EnforcingUncheckedUnpacked) +{ + { + ipmi::message::Payload p({1, 2}); + p.trailingOk = false; + uint8_t out; + p.unpack(out, out); + } + EXPECT_EQ(logs.size(), 1); +} + +TEST_F(PayloadLogging, EnforcingUncheckedError) +{ + { + ipmi::message::Payload p({1, 2}); + p.trailingOk = false; + uint32_t out; + p.unpack(out); + } + EXPECT_EQ(logs.size(), 0); +} + +TEST_F(PayloadLogging, EnforcingChecked) +{ + { + ipmi::message::Payload p({1, 2}); + p.trailingOk = false; + EXPECT_FALSE(p.fullyUnpacked()); + } + EXPECT_EQ(logs.size(), 0); +} + +TEST_F(PayloadLogging, EnforcingCheckedUnpacked) +{ + { + ipmi::message::Payload p({1, 2}); + p.trailingOk = false; + uint8_t out; + p.unpack(out, out); + EXPECT_TRUE(p.fullyUnpacked()); + } + EXPECT_EQ(logs.size(), 0); +} + +TEST_F(PayloadLogging, EnforcingUnpackPayload) +{ + { + ipmi::message::Payload p; + { + ipmi::message::Payload q({1, 2}); + q.trailingOk = false; + q.unpack(p); + } + EXPECT_EQ(logs.size(), 0); + } + EXPECT_EQ(logs.size(), 1); +} + +TEST_F(PayloadLogging, EnforcingMove) +{ + { + ipmi::message::Payload p; + { + ipmi::message::Payload q({1, 2}); + q.trailingOk = false; + p = std::move(q); + } + EXPECT_EQ(logs.size(), 0); + } + EXPECT_EQ(logs.size(), 1); +} + +TEST_F(PayloadLogging, EnforcingException) +{ + try + { + ipmi::message::Payload p({1, 2}); + p.trailingOk = false; + throw std::runtime_error("test"); + } + catch (...) + { + } + EXPECT_EQ(logs.size(), 0); +} diff --git a/test/message/unpack.cpp b/test/message/unpack.cpp index 611a5fe..7d69218 100644 --- a/test/message/unpack.cpp +++ b/test/message/unpack.cpp @@ -660,8 +660,8 @@ TEST(Vectors, VectorUint32NonIntegralBytes) 0x99, 0x77, 0x55, 0x33, 0x78, 0x56, 0x34}; ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i)); std::vector<uint32_t> v; - // check that the number of bytes matches - ASSERT_NE(p.unpack(v), 0); + // check that the vector unpacks successfully + ASSERT_EQ(p.unpack(v), 0); // check that the payload was not fully unpacked ASSERT_FALSE(p.fullyUnpacked()); // arrays of uint32_t will be unpacked one at a time, so the @@ -686,6 +686,51 @@ TEST(Vectors, VectorUint8) ASSERT_EQ(v, k); } +TEST(Vectors, VectorEmptyOk) +{ + // an empty input vector to show that unpacking elements is okay + std::vector<uint8_t> i{}; + ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i)); + std::vector<uint32_t> v; + // check that the number of bytes matches + ASSERT_EQ(p.unpack(v), 0); + // check that the payload was fully unpacked + ASSERT_TRUE(p.fullyUnpacked()); + std::vector<uint32_t> k{}; + // check that the unpacked vector is empty as expected + ASSERT_EQ(v, k); +} + +TEST(Vectors, VectorOfTuplesOk) +{ + // a vector of bytes will be unpacked verbatim, low-order element first + std::vector<uint8_t> i = {0x02, 0x00, 0x86, 0x04}; + ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i)); + std::vector<std::tuple<uint8_t, uint8_t>> v; + // check that the number of bytes matches + ASSERT_EQ(p.unpack(v), 0); + // check that the payload was fully unpacked + ASSERT_TRUE(p.fullyUnpacked()); + std::vector<std::tuple<uint8_t, uint8_t>> k = {{0x02, 0x00}, {0x86, 0x04}}; + // check that the bytes were correctly unpacked (in byte order) + ASSERT_EQ(v, k); +} + +TEST(Vectors, VectorOfTuplesInsufficientBytes) +{ + // a vector of bytes will be unpacked verbatim, low-order element first + std::vector<uint8_t> i = {0x02, 0x00, 0x86, 0x04, 0xb4}; + ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i)); + std::vector<std::tuple<uint8_t, uint8_t>> v; + // check that the number of bytes matches + ASSERT_EQ(p.unpack(v), 0); + // check that the payload was not fully unpacked + ASSERT_FALSE(p.fullyUnpacked()); + std::vector<std::tuple<uint8_t, uint8_t>> k = {{0x02, 0x00}, {0x86, 0x04}}; + // check that the bytes were correctly unpacked (in byte order) + ASSERT_EQ(v, k); +} + // Cannot test TooManyBytes or InsufficientBytes for vector<uint8_t> // because it will always unpack whatever bytes are remaining // TEST(Vectors, VectorUint8TooManyBytes) {} @@ -716,7 +761,7 @@ TEST(UnpackAdvanced, OptionalInsufficientBytes) ASSERT_EQ(p.unpack(v), 0); // check that the payload was fully unpacked ASSERT_FALSE(p.fullyUnpacked()); - std::optional<std::tuple<uint8_t, uint32_t>> k = {{0, 0}}; + std::optional<std::tuple<uint8_t, uint32_t>> k; // check that the bytes were correctly unpacked (in byte order) ASSERT_EQ(v, k); } |