| // Copyright 2026 Google LLC |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // https://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| // |
| |
| #include "utils/byte_writers.h" |
| |
| #include <cstdint> |
| #include <vector> |
| |
| #include "gmock/gmock.h" |
| #include "gtest/gtest.h" |
| |
| namespace credentio { |
| |
| namespace { |
| |
| using ::testing::ElementsAre; |
| |
| TEST(ByteWritersTest, Write64Bit) { |
| std::vector<uint8_t> bytes; |
| WriteUint64NetworkOrder(0x1122334455667788, &bytes); |
| EXPECT_THAT(bytes, |
| ElementsAre(0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88)); |
| } |
| |
| TEST(ByteWritersTest, Write32Bit) { |
| std::vector<uint8_t> bytes; |
| WriteUint32NetworkOrder(0x11223344, &bytes); |
| EXPECT_THAT(bytes, ElementsAre(0x11, 0x22, 0x33, 0x44)); |
| } |
| |
| TEST(ByteWritersTest, Write16Bit) { |
| std::vector<uint8_t> bytes; |
| WriteUint16NetworkOrder(0x1234, &bytes); |
| EXPECT_THAT(bytes, ElementsAre(0x12, 0x34)); |
| } |
| |
| TEST(ByteWritersTest, Write64BitToNonEmptyVector) { |
| std::vector<uint8_t> bytes = {0xaa, 0xbb}; |
| WriteUint64NetworkOrder(0x1122334455667788, &bytes); |
| EXPECT_THAT(bytes, ElementsAre(0xaa, 0xbb, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, |
| 0x77, 0x88)); |
| } |
| |
| TEST(ByteWritersTest, Write32BitToNonEmptyVector) { |
| std::vector<uint8_t> bytes = {0xaa, 0xbb}; |
| WriteUint32NetworkOrder(0x11223344, &bytes); |
| EXPECT_THAT(bytes, ElementsAre(0xaa, 0xbb, 0x11, 0x22, 0x33, 0x44)); |
| } |
| |
| TEST(ByteWritersTest, Write16BitToNonEmptyVector) { |
| std::vector<uint8_t> bytes = {0xaa, 0xbb}; |
| WriteUint16NetworkOrder(0x1234, &bytes); |
| EXPECT_THAT(bytes, ElementsAre(0xaa, 0xbb, 0x12, 0x34)); |
| } |
| |
| } // namespace |
| } // namespace credentio |