Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions tests/compat/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ find_package(Qt6 COMPONENTS Core Concurrent QUIET)

# ---- zrpc_tests: TcpBuffer / NetAddress / ZRpcCodeC / ZRpcController ----
if(TARGET zrpc)
add_executable(zrpc_tests zrpc_test.cpp)
add_executable(zrpc_tests zrpc_test.cpp zrpc_extra_test.cpp)
target_link_libraries(zrpc_tests PRIVATE
GTest::GTest
GTest::Main
Expand All @@ -26,12 +26,15 @@ endif()

# ---- framework_tests: Event / EventChannel / EventSequence / EventDispatcher ----
if(TARGET dde-cooperation-framework)
add_executable(framework_tests event_test.cpp lifecycle_test.cpp)
add_executable(framework_tests event_test.cpp lifecycle_test.cpp lifecycle_private_test.cpp)
target_link_libraries(framework_tests PRIVATE
GTest::GTest
GTest::Main
dde-cooperation-framework
)
target_include_directories(framework_tests PRIVATE
${CMAKE_SOURCE_DIR}/src/compat/framework/lifecycle/private
)
if(TARGET Qt6::Core)
target_link_libraries(framework_tests PRIVATE Qt6::Core)
endif()
Expand All @@ -40,3 +43,18 @@ if(TARGET dde-cooperation-framework)
endif()
add_test(NAME framework_tests COMMAND framework_tests)
endif()

# ---- commonstruct_tests: commonstruct.h JSON serialization round-trips ----
if(TARGET co)
add_executable(commonstruct_tests commonstruct_test.cpp)
target_link_libraries(commonstruct_tests PRIVATE
GTest::GTest
GTest::Main
co
)
target_include_directories(commonstruct_tests PRIVATE
${CMAKE_SOURCE_DIR}/src/compat/common
${CMAKE_SOURCE_DIR}/3rdparty/coost/include
)
add_test(NAME commonstruct_tests COMMAND commonstruct_tests)
endif()
203 changes: 203 additions & 0 deletions tests/compat/commonstruct_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later

#include <gtest/gtest.h>

Check warning on line 4 in tests/compat/commonstruct_test.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <gtest/gtest.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include "co/json.h"

Check warning on line 6 in tests/compat/commonstruct_test.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: "co/json.h" not found.
#include "commonstruct.h"

Check warning on line 7 in tests/compat/commonstruct_test.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: "commonstruct.h" not found.

#include <string>

Check warning on line 9 in tests/compat/commonstruct_test.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <string> not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace {

template <typename T>
T roundtrip(const T& src)
{
co::Json j = src.as_json();
fastring dumped = j.str();
co::Json parsed;
parsed.parse_from(dumped);
T dst;
dst.from_json(parsed);
return dst;
}

}

TEST(CommonStructTest, SendResultRoundtrip)
{
SendResult src;
src.protocolType = 7u;
src.errorType = -3;
src.data = "payload";
auto dst = roundtrip(src);
EXPECT_EQ(dst.protocolType, 7u);
EXPECT_EQ(dst.errorType, -3);
EXPECT_STREQ(dst.data.c_str(), "payload");
}

TEST(CommonStructTest, UserLoginInfoRoundtrip)
{
UserLoginInfo src;
src.name = "alice";
src.auth = "tok";
src.my_uid = "1001";
src.my_name = "me";
src.session_id = "sess";
src.selfappName = "coop";
src.appName = "dde";
src.version = "1.0";
src.ip = "10.0.0.1";
auto dst = roundtrip(src);
EXPECT_STREQ(dst.name.c_str(), "alice");
EXPECT_STREQ(dst.auth.c_str(), "tok");
EXPECT_STREQ(dst.ip.c_str(), "10.0.0.1");
EXPECT_STREQ(dst.version.c_str(), "1.0");
}

TEST(CommonStructTest, PeerInfoRoundtrip)
{
PeerInfo src;
src.username = "bob";
src.hostname = "host";
src.platform = "linux";
src.version = "2.0";
src.privacy_mode = true;
auto dst = roundtrip(src);
EXPECT_STREQ(dst.username.c_str(), "bob");
EXPECT_TRUE(dst.privacy_mode);
}

TEST(CommonStructTest, UserLoginResultInfoNestedRoundtrip)
{
UserLoginResultInfo src;
src.peer.username = "carol";
src.peer.privacy_mode = false;
src.token = "tkn";
src.appName = "app";
src.result = true;
auto dst = roundtrip(src);
EXPECT_STREQ(dst.peer.username.c_str(), "carol");
EXPECT_FALSE(dst.peer.privacy_mode);
EXPECT_STREQ(dst.token.c_str(), "tkn");
EXPECT_TRUE(dst.result);
}

TEST(CommonStructTest, FileEntryRoundtrip)
{
FileEntry src;
src.filetype = 1;
src.name = "a.txt";
src.hidden = true;
src.size = 9999;
src.modified_time = 123456;
src.appName = "x";
src.rcvappName = "y";
auto dst = roundtrip(src);
EXPECT_EQ(src.filetype, dst.filetype);
EXPECT_EQ(src.size, dst.size);
EXPECT_TRUE(dst.hidden);
EXPECT_STREQ(dst.name.c_str(), "a.txt");
}

TEST(CommonStructTest, FileTransBlockRoundtrip)
{
FileTransBlock src;
src.job_id = 5;
src.file_id = 6;
src.rootdir = "/r";
src.filename = "f";
src.blk_id = 11u;
src.flags = 2;
src.data_size = 4096;
auto dst = roundtrip(src);
EXPECT_EQ(dst.job_id, 5);
EXPECT_EQ(dst.blk_id, 11u);
EXPECT_EQ(dst.data_size, 4096);
EXPECT_STREQ(dst.rootdir.c_str(), "/r");
}

TEST(CommonStructTest, ShareEventsRoundtrip)
{
ShareEvents src;
src.eventType = 42u;
src.data = "blob";
auto dst = roundtrip(src);
EXPECT_EQ(dst.eventType, 42u);
EXPECT_STREQ(dst.data.c_str(), "blob");
}

TEST(CommonStructTest, PingPongRoundtrip)
{
PingPong src;
src.appName = "a";
src.tarAppname = "b";
src.ip = "1.2.3.4";
auto dst = roundtrip(src);
EXPECT_STREQ(dst.appName.c_str(), "a");
EXPECT_STREQ(dst.tarAppname.c_str(), "b");
EXPECT_STREQ(dst.ip.c_str(), "1.2.3.4");
}

TEST(CommonStructTest, DiscoverInfoRoundtrip)
{
DiscoverInfo src;
src.ip = "9.9.9.9";
src.msg = "hi";
auto dst = roundtrip(src);
EXPECT_STREQ(dst.ip.c_str(), "9.9.9.9");
EXPECT_STREQ(dst.msg.c_str(), "hi");
}

TEST(CommonStructTest, MiscInfoRoundtrip)
{
MiscInfo src;
src.appName = "dde-cooperation";
src.json = "{\"k\":1}";
auto dst = roundtrip(src);
EXPECT_STREQ(dst.appName.c_str(), "dde-cooperation");
EXPECT_STREQ(dst.json.c_str(), "{\"k\":1}");
}

TEST(CommonStructTest, FileTransResponseDefaultsAndRoundtrip)
{
FileTransResponse src;
EXPECT_EQ(src.id, -1);
EXPECT_EQ(src.result, -1);
src.id = 99;
src.name = "resp";
src.result = 0;
auto dst = roundtrip(src);
EXPECT_EQ(dst.id, 99);
EXPECT_EQ(dst.result, 0);
EXPECT_STREQ(dst.name.c_str(), "resp");
}

TEST(CommonStructTest, FileTransCreateNestedEntryRoundtrip)
{
FileTransCreate src;
src.job_id = 3;
src.file_id = 4;
src.sub_dir = "sub";
src.entry.name = "inner.txt";
src.entry.size = 555;
auto dst = roundtrip(src);
EXPECT_EQ(dst.job_id, 3);
EXPECT_EQ(dst.file_id, 4);
EXPECT_STREQ(dst.sub_dir.c_str(), "sub");
EXPECT_STREQ(dst.entry.name.c_str(), "inner.txt");
EXPECT_EQ(dst.entry.size, 555);
}

TEST(CommonStructTest, AsJsonProducesValidJsonString)
{
ShareConnectApply src;
src.appName = "a";
src.tarAppname = "b";
src.ip = "1.1.1.1";
src.tarIp = "2.2.2.2";
src.data = "d";
fastring s = src.as_json().str();
EXPECT_NE(s.find("appName"), std::string::npos);
EXPECT_NE(s.find("tarIp"), std::string::npos);
}
Loading
Loading