1 | #include <gtest/gtest.h> |
2 | |
3 | #include <engine/server/name_ban.h> |
4 | |
5 | TEST(NameBan, Empty) |
6 | { |
7 | CNameBans Bans; |
8 | EXPECT_FALSE(Bans.IsBanned("" )); |
9 | EXPECT_FALSE(Bans.IsBanned("abc" )); |
10 | } |
11 | |
12 | TEST(NameBan, BanInfo) |
13 | { |
14 | CNameBans Bans; |
15 | |
16 | Bans.Ban(pName: "abc" , pReason: "old reason" , Distance: 1, IsSubstring: false); |
17 | { |
18 | const CNameBan *pOld = Bans.IsBanned(pName: "abc" ); |
19 | ASSERT_TRUE(pOld); |
20 | EXPECT_STREQ(pOld->m_aName, "abc" ); |
21 | EXPECT_STREQ(pOld->m_aReason, "old reason" ); |
22 | EXPECT_EQ(pOld->m_Distance, 1); |
23 | EXPECT_EQ(pOld->m_IsSubstring, false); |
24 | } |
25 | |
26 | // Update existing name ban |
27 | Bans.Ban(pName: "abc" , pReason: "new reason" , Distance: 2, IsSubstring: true); |
28 | { |
29 | const CNameBan *pNew = Bans.IsBanned(pName: "abc" ); |
30 | ASSERT_TRUE(pNew); |
31 | EXPECT_STREQ(pNew->m_aName, "abc" ); |
32 | EXPECT_STREQ(pNew->m_aReason, "new reason" ); |
33 | EXPECT_EQ(pNew->m_Distance, 2); |
34 | EXPECT_EQ(pNew->m_IsSubstring, true); |
35 | } |
36 | } |
37 | |
38 | TEST(NameBan, Equality) |
39 | { |
40 | CNameBans Bans; |
41 | Bans.Ban(pName: "abc" , pReason: "" , Distance: 0, IsSubstring: false); |
42 | EXPECT_TRUE(Bans.IsBanned("abc" )); |
43 | EXPECT_TRUE(Bans.IsBanned(" abc" )); |
44 | EXPECT_TRUE(Bans.IsBanned("abc " )); |
45 | EXPECT_TRUE(Bans.IsBanned("abc foo" )); // Maximum name length. |
46 | EXPECT_TRUE(Bans.IsBanned("äbc" )); // Confusables |
47 | EXPECT_FALSE(Bans.IsBanned("def" )); |
48 | EXPECT_FALSE(Bans.IsBanned("abcdef" )); |
49 | } |
50 | |
51 | TEST(NameBan, Substring) |
52 | { |
53 | CNameBans Bans; |
54 | Bans.Ban(pName: "xyz" , pReason: "" , Distance: 0, IsSubstring: true); |
55 | EXPECT_TRUE(Bans.IsBanned("abcxyz" )); |
56 | EXPECT_TRUE(Bans.IsBanned("abcxyzdef" )); |
57 | EXPECT_FALSE(Bans.IsBanned("abcdef" )); |
58 | } |
59 | |
60 | TEST(NameBan, Unban) |
61 | { |
62 | CNameBans Bans; |
63 | Bans.Ban(pName: "abc" , pReason: "" , Distance: 0, IsSubstring: false); |
64 | Bans.Ban(pName: "xyz" , pReason: "" , Distance: 0, IsSubstring: false); |
65 | EXPECT_TRUE(Bans.IsBanned("abc" )); |
66 | EXPECT_TRUE(Bans.IsBanned("xyz" )); |
67 | Bans.Unban(pName: "abc" ); |
68 | EXPECT_FALSE(Bans.IsBanned("abc" )); |
69 | EXPECT_TRUE(Bans.IsBanned("xyz" )); |
70 | Bans.Unban(pName: "xyz" ); |
71 | EXPECT_FALSE(Bans.IsBanned("abc" )); |
72 | EXPECT_FALSE(Bans.IsBanned("xyz" )); |
73 | } |
74 | |
75 | TEST(NameBan, UnbanNotFound) |
76 | { |
77 | // Try to remove a name ban that does not exist |
78 | CNameBans Bans; |
79 | Bans.Unban(pName: "abc" ); |
80 | } |
81 | |