refactor: port net/FingerprintDatabaseTest to QtTests
This commit is contained in:
parent
38dc845c72
commit
8a80c52208
5 changed files with 122 additions and 84 deletions
|
|
@ -1,84 +0,0 @@
|
|||
/*
|
||||
* Deskflow -- mouse and keyboard sharing utility
|
||||
* SPDX-FileCopyrightText: (C) 2025 Deskflow Developers
|
||||
* SPDX-FileCopyrightText: (C) 2021 Barrier Contributors
|
||||
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
||||
*/
|
||||
|
||||
#include "net/FingerprintDatabase.h"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
namespace deskflow {
|
||||
|
||||
TEST(FingerprintDatabase, parseDbLine)
|
||||
{
|
||||
ASSERT_FALSE(FingerprintDatabase::parseDbLine("").valid());
|
||||
ASSERT_FALSE(FingerprintDatabase::parseDbLine("abcd").valid());
|
||||
ASSERT_FALSE(FingerprintDatabase::parseDbLine("v1:algo:something").valid());
|
||||
ASSERT_FALSE(FingerprintDatabase::parseDbLine("v2:algo:something").valid());
|
||||
ASSERT_FALSE(FingerprintDatabase::parseDbLine("v2:algo:01020304abc").valid());
|
||||
ASSERT_FALSE(FingerprintDatabase::parseDbLine("v2:algo:01020304ZZ").valid());
|
||||
ASSERT_EQ(FingerprintDatabase::parseDbLine("v2:algo:01020304ab"), (FingerprintData{"algo", {1, 2, 3, 4, 0xab}}));
|
||||
}
|
||||
|
||||
TEST(FingerprintDatabase, read)
|
||||
{
|
||||
std::istringstream stream;
|
||||
stream.str(R"(
|
||||
v2:algo1:01020304ab
|
||||
v2:algo2:03040506ab
|
||||
AB:CD:EF:00:01:02:03:04:05:06:07:08:09:10:11:12:13:14:15:16
|
||||
)");
|
||||
FingerprintDatabase db;
|
||||
db.readStream(stream);
|
||||
|
||||
std::vector<FingerprintData> expected = {
|
||||
{"algo1", {1, 2, 3, 4, 0xab}},
|
||||
{"algo2", {3, 4, 5, 6, 0xab}},
|
||||
{"sha1", {0xab, 0xcd, 0xef, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}},
|
||||
};
|
||||
ASSERT_EQ(db.fingerprints(), expected);
|
||||
}
|
||||
|
||||
TEST(FingerprintDatabase, write)
|
||||
{
|
||||
std::ostringstream stream;
|
||||
|
||||
FingerprintDatabase db;
|
||||
db.addTrusted({"algo1", {1, 2, 3, 4, 0xab}});
|
||||
db.addTrusted({"algo2", {3, 4, 5, 6, 0xab}});
|
||||
db.writeStream(stream);
|
||||
|
||||
ASSERT_EQ(stream.str(), R"(v2:algo1:01020304ab
|
||||
v2:algo2:03040506ab
|
||||
)");
|
||||
}
|
||||
|
||||
TEST(FingerprintDatabase, clear)
|
||||
{
|
||||
FingerprintDatabase db;
|
||||
db.addTrusted({"algo1", {1, 2, 3, 4, 0xab}});
|
||||
db.clear();
|
||||
ASSERT_TRUE(db.fingerprints().empty());
|
||||
}
|
||||
|
||||
TEST(FingerprintDatabase, addTrusted_noDuplicates)
|
||||
{
|
||||
FingerprintDatabase db;
|
||||
db.addTrusted({"algo1", {1, 2, 3, 4, 0xab}});
|
||||
db.addTrusted({"algo2", {3, 4, 5, 6, 0xab}});
|
||||
db.addTrusted({"algo1", {1, 2, 3, 4, 0xab}});
|
||||
ASSERT_EQ(db.fingerprints().size(), 2);
|
||||
}
|
||||
|
||||
TEST(FingerprintDatabase, isTrusted)
|
||||
{
|
||||
FingerprintDatabase db;
|
||||
db.addTrusted({"algo1", {1, 2, 3, 4, 0xab}});
|
||||
ASSERT_TRUE(db.isTrusted({"algo1", {1, 2, 3, 4, 0xab}}));
|
||||
ASSERT_FALSE(db.isTrusted({"algo2", {1, 2, 3, 4, 0xab}}));
|
||||
ASSERT_FALSE(db.isTrusted({"algo1", {1, 2, 3, 4, 0xac}}));
|
||||
}
|
||||
|
||||
} // namespace deskflow
|
||||
|
|
@ -46,6 +46,7 @@ add_subdirectory(base)
|
|||
add_subdirectory(common)
|
||||
add_subdirectory(deskflow)
|
||||
add_subdirectory(gui)
|
||||
add_subdirectory(net)
|
||||
add_subdirectory(platform)
|
||||
add_subdirectory(server)
|
||||
|
||||
|
|
|
|||
16
src/unittests/net/CMakeLists.txt
Normal file
16
src/unittests/net/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
# SPDX-FileCopyrightText: 2025 Deskflow Developers
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
if(WIN32)
|
||||
set(extra_libs version)
|
||||
endif()
|
||||
|
||||
create_test(
|
||||
NAME FingerprintDatabaseTests
|
||||
DEPENDS net
|
||||
LIBS base arch mt io ${extra_libs}
|
||||
SOURCE FingerprintDatabaseTests.cpp
|
||||
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/src/lib/net"
|
||||
)
|
||||
|
||||
|
||||
87
src/unittests/net/FingerprintDatabaseTests.cpp
Normal file
87
src/unittests/net/FingerprintDatabaseTests.cpp
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
/*
|
||||
* Deskflow -- mouse and keyboard sharing utility
|
||||
* SPDX-FileCopyrightText: (C) 2025 Deskflow Developers
|
||||
* SPDX-FileCopyrightText: (C) 2021 Barrier Contributors
|
||||
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
||||
*/
|
||||
|
||||
#include "FingerprintDatabaseTests.h"
|
||||
|
||||
#include "../../lib/net/FingerprintData.h"
|
||||
#include "../../lib/net/FingerprintDatabase.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
using namespace deskflow;
|
||||
|
||||
void FingerprintDatabaseTests::parseDBLine()
|
||||
{
|
||||
QVERIFY(!FingerprintDatabase::parseDbLine("").valid());
|
||||
QVERIFY(!FingerprintDatabase::parseDbLine("abcd").valid());
|
||||
QVERIFY(!FingerprintDatabase::parseDbLine("v1:algo:something").valid());
|
||||
QVERIFY(!FingerprintDatabase::parseDbLine("v2:algo:something").valid());
|
||||
QVERIFY(!FingerprintDatabase::parseDbLine("v2:algo:01020304abc").valid());
|
||||
QVERIFY(!FingerprintDatabase::parseDbLine("v2:algo:01020304ZZ").valid());
|
||||
QCOMPARE(FingerprintDatabase::parseDbLine("v2:algo:01020304ab"), (FingerprintData{"algo", {1, 2, 3, 4, 0xab}}));
|
||||
}
|
||||
|
||||
void FingerprintDatabaseTests::readFile()
|
||||
{
|
||||
std::istringstream stream;
|
||||
stream.str(R"(
|
||||
v2:algo1:01020304ab
|
||||
v2:algo2:03040506ab
|
||||
AB:CD:EF:00:01:02:03:04:05:06:07:08:09:10:11:12:13:14:15:16
|
||||
)");
|
||||
FingerprintDatabase db;
|
||||
db.readStream(stream);
|
||||
|
||||
std::vector<FingerprintData> expected = {
|
||||
{"algo1", {1, 2, 3, 4, 0xab}},
|
||||
{"algo2", {3, 4, 5, 6, 0xab}},
|
||||
{"sha1", {0xab, 0xcd, 0xef, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}},
|
||||
};
|
||||
|
||||
QCOMPARE(db.fingerprints(), expected);
|
||||
}
|
||||
|
||||
void FingerprintDatabaseTests::writeFile()
|
||||
{
|
||||
std::ostringstream stream;
|
||||
|
||||
FingerprintDatabase db;
|
||||
db.addTrusted({"algo1", {1, 2, 3, 4, 0xab}});
|
||||
db.addTrusted({"algo2", {3, 4, 5, 6, 0xab}});
|
||||
db.writeStream(stream);
|
||||
|
||||
QCOMPARE(stream.str(), R"(v2:algo1:01020304ab
|
||||
v2:algo2:03040506ab
|
||||
)");
|
||||
}
|
||||
|
||||
void FingerprintDatabaseTests::clear()
|
||||
{
|
||||
FingerprintDatabase db;
|
||||
db.addTrusted({"algo1", {1, 2, 3, 4, 0xab}});
|
||||
db.clear();
|
||||
|
||||
QVERIFY(db.fingerprints().empty());
|
||||
}
|
||||
|
||||
void FingerprintDatabaseTests::trusted()
|
||||
{
|
||||
FingerprintDatabase db;
|
||||
|
||||
db.addTrusted({"algo1", {1, 2, 3, 4, 0xab}});
|
||||
QVERIFY(db.isTrusted({"algo1", {1, 2, 3, 4, 0xab}}));
|
||||
QVERIFY(!db.isTrusted({"algo2", {1, 2, 3, 4, 0xab}}));
|
||||
QVERIFY(!db.isTrusted({"algo1", {1, 2, 3, 4, 0xac}}));
|
||||
|
||||
db.addTrusted({"algo2", {3, 4, 5, 6, 0xab}});
|
||||
QVERIFY(db.isTrusted({"algo2", {3, 4, 5, 6, 0xab}}));
|
||||
|
||||
db.addTrusted({"algo1", {1, 2, 3, 4, 0xab}});
|
||||
QCOMPARE(db.fingerprints().size(), 2);
|
||||
}
|
||||
|
||||
QTEST_MAIN(FingerprintDatabaseTests)
|
||||
18
src/unittests/net/FingerprintDatabaseTests.h
Normal file
18
src/unittests/net/FingerprintDatabaseTests.h
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
/*
|
||||
* Deskflow -- mouse and keyboard sharing utility
|
||||
* SPDX-FileCopyrightText: (C) 2025 Chris Rizzitello <sithlord48@gmail.com>
|
||||
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
||||
*/
|
||||
|
||||
#include <QTest>
|
||||
|
||||
class FingerprintDatabaseTests : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
private slots:
|
||||
void parseDBLine();
|
||||
void readFile();
|
||||
void writeFile();
|
||||
void clear();
|
||||
void trusted();
|
||||
};
|
||||
Loading…
Reference in a new issue