Debian Codegraph @

src/test/unittests/net/FingerprintDatabaseTests.cpp

raw
1 /*
2 barrier -- mouse and keyboard sharing utility
3 Copyright (C) Barrier contributors
4
5 This package is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 found in the file LICENSE that should have accompanied this file.
8
9 This package is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "net/FingerprintDatabase.h"
19 #include "test/global/gtest.h"
20
21 namespace barrier {
22
23 TEST(FingerprintDatabase, parse_db_line)
24 {
25 ASSERT_FALSE(FingerprintDatabase::parse_db_line("").valid());
26 ASSERT_FALSE(FingerprintDatabase::parse_db_line("abcd").valid());
27 ASSERT_FALSE(FingerprintDatabase::parse_db_line("v1:algo:something").valid());
28 ASSERT_FALSE(FingerprintDatabase::parse_db_line("v2:algo:something").valid());
29 ASSERT_FALSE(FingerprintDatabase::parse_db_line("v2:algo:01020304abc").valid());
30 ASSERT_FALSE(FingerprintDatabase::parse_db_line("v2:algo:01020304ZZ").valid());
31 ASSERT_EQ(FingerprintDatabase::parse_db_line("v2:algo:01020304ab"),
32 (FingerprintData{"algo", {1, 2, 3, 4, 0xab}}));
33 }
34
35 TEST(FingerprintDatabase, read)
36 {
37 std::istringstream stream;
38 stream.str(R"(
39 v2:algo1:01020304ab
40 v2:algo2:03040506ab
41 AB:CD:EF:00:01:02:03:04:05:06:07:08:09:10:11:12:13:14:15:16
42 )");
43 FingerprintDatabase db;
44 db.read_stream(stream);
45
46 std::vector<FingerprintData> expected = {
47 { "algo1", { 1, 2, 3, 4, 0xab } },
48 { "algo2", { 3, 4, 5, 6, 0xab } },
49 { "sha1", { 0xab, 0xcd, 0xef, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
50 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 } },
51 };
52 ASSERT_EQ(db.fingerprints(), expected);
53 }
54
55 TEST(FingerprintDatabase, write)
56 {
57 std::ostringstream stream;
58
59 FingerprintDatabase db;
60 db.add_trusted({ "algo1", { 1, 2, 3, 4, 0xab } });
61 db.add_trusted({ "algo2", { 3, 4, 5, 6, 0xab } });
62 db.write_stream(stream);
63
64 ASSERT_EQ(stream.str(), R"(v2:algo1:01020304ab
65 v2:algo2:03040506ab
66 )");
67 }
68
69 TEST(FingerprintDatabase, clear)
70 {
71 FingerprintDatabase db;
72 db.add_trusted({ "algo1", { 1, 2, 3, 4, 0xab } });
73 db.clear();
74 ASSERT_TRUE(db.fingerprints().empty());
75 }
76
77 TEST(FingerprintDatabase, add_trusted_no_duplicates)
78 {
79 FingerprintDatabase db;
80 db.add_trusted({ "algo1", { 1, 2, 3, 4, 0xab } });
81 db.add_trusted({ "algo2", { 3, 4, 5, 6, 0xab } });
82 db.add_trusted({ "algo1", { 1, 2, 3, 4, 0xab } });
83 ASSERT_EQ(db.fingerprints().size(), 2);
84 }
85
86 TEST(FingerprintDatabase, is_trusted)
87 {
88 FingerprintDatabase db;
89 db.add_trusted({ "algo1", { 1, 2, 3, 4, 0xab } });
90 ASSERT_TRUE(db.is_trusted({ "algo1", { 1, 2, 3, 4, 0xab } }));
91 ASSERT_FALSE(db.is_trusted({ "algo2", { 1, 2, 3, 4, 0xab } }));
92 ASSERT_FALSE(db.is_trusted({ "algo1", { 1, 2, 3, 4, 0xac } }));
93 }
94
95 } // namespace barrier