1 //
2 // Copyright (C) 2024 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #include "debug_lmrt.h"
17 #include <gtest/gtest.h>
18 #include <gmock/gmock.h>
19
20
21 extern lmrt_payload_t lmrt_payloads;
22 class MockLogger {
23 public:
24 MOCK_METHOD(void, logVerbose, (const std::string& message), ());
25 };
26
27 // Global instance of the mock logger
28 MockLogger mock_logger_instance;
29 // Redefine the logging function to use the mock
logVerbose(const std::string & message)30 void logVerbose(const std::string& message) {
31 mock_logger_instance.logVerbose(message);
32 }
33
34 class LmrtLogTest : public ::testing::Test {
35 protected:
SetUp()36 void SetUp() override {
37 // Reset mock expectations before each test
38 EXPECT_CALL(mock_logger_instance, logVerbose).Times(0);
39 }
TearDown()40 void TearDown() override {
41 testing::Mock::AllowLeak(&mock_logger_instance);
42 }
43 };
44
45 // Test: Logging when payloads are empty
TEST_F(LmrtLogTest,LogEmptyLmrtPayloads)46 TEST_F(LmrtLogTest, LogEmptyLmrtPayloads) {
47 // Set up test data for empty payloads
48 lmrt_payloads.more.clear();
49 lmrt_payloads.entry_count.clear();
50 lmrt_payloads.tlvs.clear();
51 EXPECT_CALL(mock_logger_instance, logVerbose("lmrt_log: No payloads to log"))
52 .Times(1);
53 lmrt_log();
54 }
55
56 // Test: Logging a single LMRT payload
TEST_F(LmrtLogTest,LogSingleLmrtPayload)57 TEST_F(LmrtLogTest, LogSingleLmrtPayload) {
58 lmrt_payloads.more = {1};
59 lmrt_payloads.entry_count = {3};
60 lmrt_payloads.tlvs = {{0x01, 0x02, 0x03}};
61 EXPECT_CALL(mock_logger_instance, logVerbose("lmrt_log: Packet 1/1"))
62 .Times(1);
63 EXPECT_CALL(mock_logger_instance, logVerbose("lmrt_log: 3 entries in this packet"))
64 .Times(1);
65 EXPECT_CALL(mock_logger_instance, logVerbose("lmrt_log: tlv: 010203"))
66 .Times(1);
67 lmrt_log();
68 }
69
70 // Test: Logging multiple LMRT payloads
TEST_F(LmrtLogTest,LogMultipleLmrtPayloads)71 TEST_F(LmrtLogTest, LogMultipleLmrtPayloads) {
72 lmrt_payloads.more = {1, 0};
73 lmrt_payloads.entry_count = {3, 2};
74 lmrt_payloads.tlvs = {
75 {0x01, 0x02, 0x03},
76 {0x04, 0x05}
77 };
78 EXPECT_CALL(mock_logger_instance, logVerbose("lmrt_log: Packet 1/2"))
79 .Times(1);
80 EXPECT_CALL(mock_logger_instance, logVerbose("lmrt_log: 3 entries in this packet"))
81 .Times(1);
82 EXPECT_CALL(mock_logger_instance, logVerbose("lmrt_log: tlv: 010203"))
83 .Times(1);
84 EXPECT_CALL(mock_logger_instance, logVerbose("lmrt_log: Packet 2/2"))
85 .Times(1);
86 EXPECT_CALL(mock_logger_instance, logVerbose("lmrt_log: 2 entries in this packet"))
87 .Times(1);
88 EXPECT_CALL(mock_logger_instance, logVerbose("lmrt_log: tlv: 0405"))
89 .Times(1);
90 lmrt_log();
91 }
92