Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9613a87c authored by Chris Manton's avatar Chris Manton Committed by Gerrit Code Review
Browse files

Merge "test: Allow ability to print test logging information"

parents 7a58427b af61bf47
Loading
Loading
Loading
Loading
+34 −0
Original line number Diff line number Diff line
@@ -507,3 +507,37 @@ cc_defaults {
        },
    },
}

cc_test {
    name: "net_test_common",
    host_supported: true,
    local_include_dirs: [
        "common",
    ],
    include_dirs: [
        "packages/modules/Bluetooth/system",
        "packages/modules/Bluetooth/system/include",
        "packages/modules/Bluetooth/system/gd",
    ],
    srcs: [
        "common/log_msg_test.cc",
        "common/log_msg.cc",
    ],
    shared_libs: [
        "libchrome",
    ],
    cflags: [
        "-Wall",
        "-Wextra",
        "-Werror",
        // there are too many unused parameters in all the code.
        "-Wno-unused-parameter",
        // http://b/264549607
        "-Wno-deprecated-builtins",
    ],
    sanitize: {
        address: true,
        cfi: true,
        misc_undefined: ["bounds"],
    },
}
+26 −1
Original line number Diff line number Diff line
@@ -14,6 +14,31 @@
 * limitations under the License.
 */

#include "test/common/log_msg.h"

#include <cstdint>
#include <cstdio>
#include <functional>

namespace {
constexpr size_t kTestBufferLogSize = 512;
}  // namespace

size_t bluetooth::testing::common::get_common_log_msg_size() {
  return kTestBufferLogSize;
}

std::function<void(uint32_t, const char*)> bluetooth::testing::common::log_msg =
    []([[maybe_unused]] uint32_t trace_set_mask,
       [[maybe_unused]] const char* buffer) {};

extern "C" void LogMsg(uint32_t trace_set_mask, const char* fmt_str, ...) {
  char buffer[kTestBufferLogSize];

  va_list ap;
  va_start(ap, fmt_str);
  vsnprintf(buffer, kTestBufferLogSize, fmt_str, ap);
  va_end(ap);

extern "C" void LogMsg(uint32_t trace_set_mask, const char* fmt_str, ...) {}
  bluetooth::testing::common::log_msg(trace_set_mask, buffer);
}
+32 −0
Original line number Diff line number Diff line
/*
 * Copyright 2023 The Android Open Source Project
 *
 * 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.
 */

#pragma once

#include <cstdint>
#include <functional>
#include <string>

namespace bluetooth {
namespace testing {
namespace common {

size_t get_common_log_msg_size();
extern std::function<void(uint32_t, const char*)> log_msg;

}  // namespace common
}  // namespace testing
}  // namespace bluetooth
+92 −0
Original line number Diff line number Diff line
/*
 * Copyright 2023 The Android Open Source Project
 *
 * 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 "test/common/log_msg.h"

#include <gtest/gtest.h>

#include <cstdint>
#include <string>

#include "internal_include/bt_trace.h"

namespace {
uint32_t kDefaultTraceSetMask = 0x5a5a5a5a;
}

class CommonLogMsgTest : public ::testing::Test {
 protected:
  void SetUp() override {}
  void TearDown() override {}
};

class CommonLogMsgOutputTest : public CommonLogMsgTest {
 protected:
  void SetUp() override {
    last_ = {};
    bluetooth::testing::common::log_msg = [this](uint32_t mask,
                                                 const char* data) {
      if (mask) {
        printf("This is printed :%s", data);
        last_.mask = mask;
        last_.data_len = strlen(data);
      }
    };
  }
  void TearDown() override { bluetooth::testing::common::log_msg = {}; }

  const size_t max_buffer_size_ =
      bluetooth::testing::common::get_common_log_msg_size();

  struct {
    uint32_t mask;
    size_t data_len;
  } last_;
};

TEST_F(CommonLogMsgTest, default_no_output) {
  LogMsg(kDefaultTraceSetMask, "This is a test");
}

TEST_F(CommonLogMsgOutputTest, simple_with_output) {
  LogMsg(kDefaultTraceSetMask, "This will be printed\n");
  ASSERT_EQ(kDefaultTraceSetMask, last_.mask);
  ASSERT_EQ(strlen("This will be printed\n"), last_.data_len);
}

TEST_F(CommonLogMsgOutputTest, simple_with_no_output) {
  LogMsg(0U, "This will not be printed\n");
  ASSERT_EQ(0U, last_.mask);
  ASSERT_EQ(0UL, last_.data_len);
}

TEST_F(CommonLogMsgOutputTest, max_string) {
  auto long_string = std::string(max_buffer_size_ - sizeof('\0'), 'x');
  LogMsg(kDefaultTraceSetMask, long_string.c_str());
  ASSERT_EQ(max_buffer_size_ - sizeof('\0'), last_.data_len);
}

TEST_F(CommonLogMsgOutputTest, max_string_plus_string_terminator) {
  auto long_string = std::string(max_buffer_size_, 'x');
  LogMsg(kDefaultTraceSetMask, long_string.c_str());
  ASSERT_EQ(max_buffer_size_ - sizeof('\0'), last_.data_len);
}

TEST_F(CommonLogMsgOutputTest, too_large_string) {
  auto long_string = std::string(4096UL, 'x');
  LogMsg(kDefaultTraceSetMask, long_string.c_str());
  ASSERT_EQ(max_buffer_size_ - sizeof('\0'), last_.data_len);
}