| // 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_readers.h" |
| |
| #include <cstdint> |
| |
| #include "absl/status/status.h" |
| #include "absl/status/status_matchers.h" |
| #include "absl/strings/string_view.h" |
| #include "gmock/gmock.h" |
| #include "gtest/gtest.h" |
| |
| namespace credentio { |
| |
| namespace { |
| |
| using ::absl_testing::IsOk; |
| using ::absl_testing::IsOkAndHolds; |
| using ::absl_testing::StatusIs; |
| |
| TEST(ByteReadersTest, ReadTooSmall) { |
| EXPECT_THAT(ReadUint<uint16_t>("\x12"), |
| StatusIs(absl::StatusCode::kInvalidArgument)); |
| } |
| |
| TEST(ByteReadersTest, ReadUint) { |
| EXPECT_THAT(ReadUint<uint16_t>("\x12\x34"), IsOkAndHolds(0x1234)); |
| } |
| |
| TEST(ByteReadersTest, ReadUintWithRemainder) { |
| EXPECT_THAT(ReadUint<uint16_t>("\x12\x34\x56\x78"), IsOkAndHolds(0x1234)); |
| } |
| |
| TEST(ByteReadersTest, ConsumeNull) { |
| EXPECT_THAT(ConsumeUint<uint16_t>(nullptr), |
| StatusIs(absl::StatusCode::kInvalidArgument)); |
| } |
| |
| TEST(ByteReadersTest, ConsumeTooSmall) { |
| absl::string_view input = "\x12"; |
| EXPECT_THAT(ConsumeUint<uint16_t>(&input), |
| StatusIs(absl::StatusCode::kInvalidArgument)); |
| EXPECT_EQ(input, "\x12"); |
| } |
| |
| TEST(ByteReadersTest, ConsumeUint) { |
| absl::string_view input = "\x12\x34"; |
| EXPECT_THAT(ConsumeUint<uint16_t>(&input), IsOkAndHolds(0x1234)); |
| EXPECT_EQ(input, ""); |
| } |
| |
| TEST(ByteReadersTest, ConsumeUintWithRemainder) { |
| absl::string_view input = "\x12\x34\x56\x78"; |
| EXPECT_THAT(ConsumeUint<uint16_t>(&input), IsOkAndHolds(0x1234)); |
| EXPECT_EQ(input, "\x56\x78"); |
| } |
| |
| TEST(ByteReadersTest, SkipBytesNull) { |
| EXPECT_THAT(SkipBytes<uint16_t>(nullptr), |
| StatusIs(absl::StatusCode::kInvalidArgument)); |
| } |
| |
| TEST(ByteReadersTest, SkipBytes) { |
| absl::string_view input = "\x12\x34"; |
| EXPECT_THAT(SkipBytes<uint8_t>(&input), IsOk()); |
| EXPECT_EQ(input, "\x34"); |
| } |
| |
| TEST(ByteReadersTest, SkipBytesAll) { |
| absl::string_view input = "\x12\x34"; |
| EXPECT_THAT(SkipBytes<uint16_t>(&input), IsOk()); |
| EXPECT_EQ(input, ""); |
| } |
| |
| TEST(ByteReadersTest, SkipBytesNotEnough) { |
| absl::string_view input = "\x12\x34"; |
| EXPECT_THAT(SkipBytes<uint32_t>(&input), |
| StatusIs(absl::StatusCode::kInvalidArgument)); |
| EXPECT_EQ(input, "\x12\x34"); |
| } |
| |
| } // namespace |
| } // namespace credentio |