243 lines
8.6 KiB
C++
243 lines
8.6 KiB
C++
// ================================================================================================
|
|
// Audio Backend - 沙盒工厂测试
|
|
// ================================================================================================
|
|
|
|
#include <gtest/gtest.h>
|
|
#include <gmock/gmock.h>
|
|
#include "plugin_host/sandbox/sandbox_interface.h"
|
|
#include "tests/common/test_fixtures.h"
|
|
|
|
using namespace audio_backend;
|
|
using namespace audio_backend::plugin_host;
|
|
|
|
// 沙盒工厂测试固定装置
|
|
class SandboxFactoryTest : public test::PluginSandboxTest {
|
|
protected:
|
|
void SetUp() override {
|
|
test::PluginSandboxTest::SetUp();
|
|
}
|
|
|
|
void TearDown() override {
|
|
test::PluginSandboxTest::TearDown();
|
|
}
|
|
|
|
// 创建基础沙盒配置
|
|
SandboxConfig create_basic_config() {
|
|
SandboxConfig config;
|
|
config.sandbox_type = SandboxType::ProcessLevelIsolation;
|
|
config.process_creation_timeout = 5000;
|
|
config.enable_heartbeat = true;
|
|
config.heartbeat_interval_ms = 1000;
|
|
config.heartbeat_timeout_ms = 5000;
|
|
config.enable_resource_monitoring = true;
|
|
config.resource_monitoring_interval_ms = 1000;
|
|
return config;
|
|
}
|
|
};
|
|
|
|
// 测试获取支持的沙盒类型
|
|
TEST_F(SandboxFactoryTest, GetSupportedSandboxTypes) {
|
|
// 获取支持的沙盒类型
|
|
auto types = SandboxFactory::get_supported_sandbox_types();
|
|
|
|
// 验证至少支持一种类型
|
|
EXPECT_FALSE(types.empty());
|
|
|
|
// 验证支持的类型是有效的
|
|
for (auto type : types) {
|
|
EXPECT_NE(type, SandboxType::Unknown);
|
|
}
|
|
|
|
// 验证ProcessLevelIsolation类型应该被大多数平台支持
|
|
EXPECT_TRUE(std::find(types.begin(), types.end(), SandboxType::ProcessLevelIsolation) != types.end());
|
|
}
|
|
|
|
// 测试检查特定沙盒类型是否被支持
|
|
TEST_F(SandboxFactoryTest, IsSandboxTypeSupported) {
|
|
// 检查ProcessLevelIsolation类型应该被支持
|
|
EXPECT_TRUE(SandboxFactory::is_sandbox_type_supported(SandboxType::ProcessLevelIsolation));
|
|
|
|
// 检查Unknown类型应该不被支持
|
|
EXPECT_FALSE(SandboxFactory::is_sandbox_type_supported(SandboxType::Unknown));
|
|
|
|
// 检查所有支持的类型
|
|
auto supported_types = SandboxFactory::get_supported_sandbox_types();
|
|
for (auto type : supported_types) {
|
|
EXPECT_TRUE(SandboxFactory::is_sandbox_type_supported(type));
|
|
}
|
|
}
|
|
|
|
// 测试获取推荐的沙盒类型
|
|
TEST_F(SandboxFactoryTest, GetRecommendedSandboxType) {
|
|
// 获取推荐的沙盒类型
|
|
auto recommended_type = SandboxFactory::get_recommended_sandbox_type();
|
|
|
|
// 验证推荐的类型是有效的
|
|
EXPECT_NE(recommended_type, SandboxType::Unknown);
|
|
|
|
// 验证推荐的类型是被支持的
|
|
EXPECT_TRUE(SandboxFactory::is_sandbox_type_supported(recommended_type));
|
|
}
|
|
|
|
// 测试创建平台特定的沙盒
|
|
TEST_F(SandboxFactoryTest, CreatePlatformSandbox) {
|
|
// 创建基础配置
|
|
SandboxConfig config = create_basic_config();
|
|
|
|
// 创建平台特定的沙盒
|
|
auto sandbox = SandboxFactory::create_platform_sandbox(config);
|
|
|
|
// 验证沙盒创建成功
|
|
EXPECT_NE(sandbox, nullptr);
|
|
|
|
// 验证沙盒类型
|
|
EXPECT_NE(sandbox->get_sandbox_type(), SandboxType::Unknown);
|
|
|
|
// 验证沙盒配置
|
|
EXPECT_EQ(sandbox->get_config().sandbox_type, sandbox->get_sandbox_type());
|
|
EXPECT_EQ(sandbox->get_config().process_creation_timeout, config.process_creation_timeout);
|
|
EXPECT_EQ(sandbox->get_config().enable_heartbeat, config.enable_heartbeat);
|
|
}
|
|
|
|
// 测试创建指定类型的沙盒
|
|
TEST_F(SandboxFactoryTest, CreateSpecificTypeSandbox) {
|
|
// 创建基础配置
|
|
SandboxConfig config = create_basic_config();
|
|
|
|
// 获取支持的沙盒类型
|
|
auto supported_types = SandboxFactory::get_supported_sandbox_types();
|
|
if (supported_types.empty()) {
|
|
GTEST_SKIP() << "No supported sandbox types available on this platform";
|
|
}
|
|
|
|
// 选择第一个支持的类型
|
|
SandboxType type = supported_types[0];
|
|
config.sandbox_type = type;
|
|
|
|
// 创建指定类型的沙盒
|
|
auto sandbox = SandboxFactory::create_sandbox(type, config);
|
|
|
|
// 验证沙盒创建成功
|
|
EXPECT_NE(sandbox, nullptr);
|
|
|
|
// 验证沙盒类型
|
|
EXPECT_EQ(sandbox->get_sandbox_type(), type);
|
|
|
|
// 验证沙盒配置
|
|
EXPECT_EQ(sandbox->get_config().sandbox_type, type);
|
|
EXPECT_EQ(sandbox->get_config().process_creation_timeout, config.process_creation_timeout);
|
|
EXPECT_EQ(sandbox->get_config().enable_heartbeat, config.enable_heartbeat);
|
|
}
|
|
|
|
// 测试创建不支持的沙盒类型
|
|
TEST_F(SandboxFactoryTest, CreateUnsupportedSandboxType) {
|
|
// 创建基础配置
|
|
SandboxConfig config = create_basic_config();
|
|
|
|
// 设置不支持的类型
|
|
config.sandbox_type = SandboxType::Unknown;
|
|
|
|
// 尝试创建不支持的类型
|
|
auto sandbox = SandboxFactory::create_sandbox(SandboxType::Unknown, config);
|
|
|
|
// 应该返回nullptr
|
|
EXPECT_EQ(sandbox, nullptr);
|
|
}
|
|
|
|
// 测试创建沙盒并验证初始化
|
|
TEST_F(SandboxFactoryTest, CreateAndInitializeSandbox) {
|
|
// 创建基础配置
|
|
SandboxConfig config = create_basic_config();
|
|
|
|
// 创建平台特定的沙盒
|
|
auto sandbox = SandboxFactory::create_platform_sandbox(config);
|
|
ASSERT_NE(sandbox, nullptr);
|
|
|
|
// 初始化沙盒
|
|
auto result = sandbox->initialize(config);
|
|
EXPECT_EQ(result, common::ErrorCode::Success);
|
|
EXPECT_TRUE(sandbox->is_initialized());
|
|
|
|
// 关闭沙盒
|
|
result = sandbox->shutdown();
|
|
EXPECT_EQ(result, common::ErrorCode::Success);
|
|
EXPECT_FALSE(sandbox->is_initialized());
|
|
}
|
|
|
|
// 测试创建多个不同类型的沙盒
|
|
TEST_F(SandboxFactoryTest, CreateMultipleSandboxTypes) {
|
|
// 获取支持的沙盒类型
|
|
auto supported_types = SandboxFactory::get_supported_sandbox_types();
|
|
if (supported_types.size() < 2) {
|
|
GTEST_SKIP() << "Fewer than 2 supported sandbox types available on this platform";
|
|
}
|
|
|
|
// 创建基础配置
|
|
SandboxConfig config = create_basic_config();
|
|
|
|
// 创建不同类型的沙盒
|
|
std::vector<std::unique_ptr<ISandbox>> sandboxes;
|
|
for (auto type : supported_types) {
|
|
config.sandbox_type = type;
|
|
auto sandbox = SandboxFactory::create_sandbox(type, config);
|
|
|
|
if (sandbox) {
|
|
EXPECT_EQ(sandbox->get_sandbox_type(), type);
|
|
sandboxes.push_back(std::move(sandbox));
|
|
}
|
|
}
|
|
|
|
// 验证创建的沙盒类型
|
|
EXPECT_GE(sandboxes.size(), 1);
|
|
|
|
// 验证创建的沙盒类型各不相同
|
|
std::set<SandboxType> unique_types;
|
|
for (const auto& sandbox : sandboxes) {
|
|
unique_types.insert(sandbox->get_sandbox_type());
|
|
}
|
|
EXPECT_EQ(unique_types.size(), sandboxes.size());
|
|
}
|
|
|
|
// 测试沙盒类型的字符串转换
|
|
TEST_F(SandboxFactoryTest, SandboxTypeStringConversion) {
|
|
// 定义沙盒类型和对应的字符串
|
|
const std::vector<std::pair<SandboxType, std::string>> type_strings = {
|
|
{SandboxType::Unknown, "Unknown"},
|
|
{SandboxType::ProcessLevelIsolation, "ProcessLevelIsolation"},
|
|
{SandboxType::ContainerBased, "ContainerBased"},
|
|
{SandboxType::VirtualMachine, "VirtualMachine"},
|
|
{SandboxType::WindowsAppContainer, "WindowsAppContainer"},
|
|
{SandboxType::LinuxNamespace, "LinuxNamespace"},
|
|
{SandboxType::MacOSSandbox, "MacOSSandbox"},
|
|
{SandboxType::BrowserIsolation, "BrowserIsolation"},
|
|
{SandboxType::Emulation, "Emulation"}
|
|
};
|
|
|
|
// 为沙盒类型到字符串的函数声明一个函数指针类型
|
|
using SandboxTypeToStringFunc = std::string (*)(SandboxType);
|
|
|
|
// 获取所有支持的沙盒类型
|
|
auto supported_types = SandboxFactory::get_supported_sandbox_types();
|
|
|
|
// 遍历所有类型
|
|
for (const auto& [type, expected_str] : type_strings) {
|
|
// 检查类型是否被支持
|
|
bool is_supported = std::find(supported_types.begin(), supported_types.end(), type) != supported_types.end();
|
|
|
|
// 如果有实现获取类型字符串的函数,可以使用它
|
|
// (这里假设有这样的函数,实际上可能没有,但这是单元测试,所以我们可以测试预期的行为)
|
|
|
|
// 创建一个简单的检查,验证类型和字符串的一致性
|
|
if (is_supported) {
|
|
// 这是一种方法来检查类型的字符串表示,取决于实际实现
|
|
std::stringstream ss;
|
|
ss << "检查沙盒类型: " << static_cast<int>(type);
|
|
EXPECT_FALSE(expected_str.empty()) << ss.str();
|
|
}
|
|
}
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
return RUN_ALL_TESTS();
|
|
} |