File indexing completed on 2025-01-05 04:37:09
0001 /* 0002 SPDX-FileCopyrightText: 2012 Joris Guisson <joris.guisson@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include <QString> 0008 #include <QtTest> 0009 #include <algorithm> 0010 #include <dht/key.h> 0011 #include <util/error.h> 0012 #include <util/log.h> 0013 0014 using namespace bt; 0015 0016 static bt::Uint8 HexCharToUint8(char c) 0017 { 0018 if (c >= 'a' && c <= 'f') 0019 return 10 + (c - 'a'); 0020 else if (c >= 'A' && c <= 'F') 0021 return 10 + (c - 'A'); 0022 else if (c >= '0' && c <= '9') 0023 return c - '0'; 0024 else 0025 return 0; 0026 } 0027 0028 static dht::Key KeyFromHexString(const QString &str) 0029 { 0030 bt::Uint8 result[20]; 0031 std::fill(result, result + 20, 0); 0032 0033 QString s = str.toLower(); 0034 if (s.size() % 2 != 0) 0035 s.prepend('0'); 0036 0037 int j = 19; 0038 0039 for (int i = s.size() - 1; i >= 0; i -= 2) { 0040 char left = s[i - 1].toLatin1(); 0041 char right = s[i].toLatin1(); 0042 result[j--] = (HexCharToUint8(left) << 4) | HexCharToUint8(right); 0043 } 0044 0045 return dht::Key(result); 0046 } 0047 0048 class KeyTest : public QObject 0049 { 0050 Q_OBJECT 0051 private Q_SLOTS: 0052 void initTestCase() 0053 { 0054 bt::InitLog("keytest.log", false, true); 0055 } 0056 0057 void cleanupTestCase() 0058 { 0059 } 0060 0061 void testAddition() 0062 { 0063 dht::Key a = KeyFromHexString("14455"); 0064 dht::Key b = KeyFromHexString("FFEEDD"); 0065 dht::Key c = a + b; 0066 QVERIFY(c == KeyFromHexString("1013332")); 0067 0068 dht::Key d = a + 1; 0069 QVERIFY(d == KeyFromHexString("14456")); 0070 0071 dht::Key e = KeyFromHexString("FFFF") + 1; 0072 QVERIFY(e == KeyFromHexString("10000")); 0073 0074 dht::Key f = KeyFromHexString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF") + 1; 0075 QVERIFY(f == KeyFromHexString("0000000000000000000000000000000000000000")); 0076 0077 dht::Key h = KeyFromHexString("FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF"); 0078 dht::Key j = KeyFromHexString("0000000100000000000000010000000000000001"); 0079 dht::Key g = j + h; 0080 QVERIFY(g == KeyFromHexString("0000000000000001000000000000000100000000")); 0081 } 0082 0083 void testSubtraction() 0084 { 0085 dht::Key a = KeyFromHexString("556677"); 0086 dht::Key b = KeyFromHexString("3384E6"); 0087 dht::Key c = a - b; 0088 QVERIFY(c == KeyFromHexString("21E191")); 0089 0090 dht::Key d = KeyFromHexString("550077"); 0091 dht::Key e = KeyFromHexString("3384E6"); 0092 dht::Key f = d - e; 0093 QVERIFY(f == KeyFromHexString("217B91")); 0094 0095 f = e - d; 0096 QVERIFY(f == KeyFromHexString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFDE846F")); 0097 0098 dht::Key g = KeyFromHexString("0000000000000001000000000000000100000000"); 0099 dht::Key j = KeyFromHexString("0000000100000000000000010000000000000001"); 0100 dht::Key h = g - j; 0101 QVERIFY(h == KeyFromHexString("FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF")); 0102 } 0103 0104 void testDivision() 0105 { 0106 dht::Key a = KeyFromHexString("550078"); 0107 dht::Key b = a / 2; 0108 QVERIFY(b == KeyFromHexString("2A803C")); 0109 0110 dht::Key c = KeyFromHexString("0000000100000000000000010000000000000001"); 0111 dht::Key d = c / 32; 0112 QVERIFY(d == KeyFromHexString("0000000008000000000000000800000000000000")); 0113 } 0114 0115 void testConversion() 0116 { 0117 dht::Key d = KeyFromHexString("0102030405060708090AFFFEFDFCFBFAF9F8F7F6"); 0118 QVERIFY(d.toString() == QLatin1String("0102030405060708090afffefdfcfbfaf9f8f7f6")); 0119 } 0120 }; 0121 0122 QTEST_MAIN(KeyTest) 0123 0124 #include "keytest.moc"