| // Copyright 2024 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 |
| // |
| // http://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 "message_intf.hpp" |
| #include "message_reinit.hpp" |
| |
| #include <memory> |
| #include <stdexcept> |
| |
| #include <gmock/gmock.h> |
| #include <gtest/gtest.h> |
| |
| namespace google |
| { |
| namespace hoth |
| { |
| |
| static size_t init_count = 0; |
| static uint8_t valid_data = 0; |
| |
| class MessageCounter : public MessageIntf |
| { |
| public: |
| MessageCounter() |
| { |
| init_count++; |
| } |
| |
| void send(const uint8_t* data, size_t /*size*/, size_t /*seek*/) override |
| { |
| if (data == nullptr) |
| { |
| throw std::runtime_error("Send Failed"); |
| } |
| } |
| |
| void recv(uint8_t *data, size_t /*size*/, size_t /*seek*/) override |
| { |
| if (data == nullptr) { |
| throw std::runtime_error("Recv Failed"); |
| } |
| } |
| }; |
| |
| class MessageReinitTest : public ::testing::Test |
| { |
| protected: |
| MessageReinitTest() |
| { |
| init_count = 0; |
| mc = std::make_unique<MessageReinit<MessageCounter>>(); |
| } |
| std::unique_ptr<MessageReinit<MessageCounter>> mc; |
| }; |
| |
| TEST_F(MessageReinitTest, ImmediateInit) |
| { |
| EXPECT_EQ(1, init_count); |
| mc.reset(); |
| EXPECT_EQ(1, init_count); |
| } |
| |
| TEST_F(MessageReinitTest, SuccessInitOnce) |
| { |
| mc->send(&valid_data, 0, 0); |
| EXPECT_EQ(1, init_count); |
| mc->send(&valid_data, 0, 0); |
| EXPECT_EQ(1, init_count); |
| } |
| |
| TEST_F(MessageReinitTest, FailReinitOnce) |
| { |
| EXPECT_THROW(mc->send(nullptr, 0, 0), std::runtime_error); |
| EXPECT_EQ(1, init_count); |
| mc->send(&valid_data, 0, 0); |
| EXPECT_EQ(2, init_count); |
| mc->send(&valid_data, 0, 0); |
| EXPECT_EQ(2, init_count); |
| EXPECT_THROW(mc->send(nullptr, 0, 0), std::runtime_error); |
| EXPECT_EQ(2, init_count); |
| } |
| |
| } // namespace hoth |
| } // namespace google |