File: infohashtester.cpp

package info (click to toggle)
opendht 3.0.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,284 kB
  • sloc: cpp: 23,342; python: 2,189; ansic: 2,041; makefile: 207; sh: 72
file content (151 lines) | stat: -rw-r--r-- 5,907 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 *  Copyright (C) 2014-2023 Savoir-faire Linux Inc.
 *
 *  Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

#include "infohashtester.h"

// std
#include <iostream>
#include <string>

// opendht
#include "opendht/infohash.h"

namespace test {
CPPUNIT_TEST_SUITE_REGISTRATION(InfoHashTester);

void
InfoHashTester::setUp() {

}

void
InfoHashTester::testConstructors() {
    // Default constructor creates a null infohash
    auto nullHash = dht::InfoHash();
    CPPUNIT_ASSERT_EQUAL((size_t)20u, nullHash.size());
    CPPUNIT_ASSERT(!nullHash);
    // Build from a uint8_t. if length to short, should get a null infohash
    uint8_t to_short[] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8};
    auto infohash = dht::InfoHash(to_short, 8);
    CPPUNIT_ASSERT_EQUAL((size_t)20u, infohash.size());
    CPPUNIT_ASSERT_EQUAL(std::string("0000000000000000000000000000000000000000"), infohash.toString());
    // Build from a uint8_t. if length is enough, data should contains the uint8_t
    uint8_t enough[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa,
                        0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa};
    infohash = dht::InfoHash(enough, 20);
    CPPUNIT_ASSERT(infohash.size() == 20);
    const auto* data = infohash.data();
    for (auto i = 0; i < 20; ++i) {
        CPPUNIT_ASSERT_EQUAL(enough[i], data[i]);
    }
    // if too long, should be cutted to 20
    uint8_t tooLong[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa,
                        0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb0};
    infohash = dht::InfoHash(tooLong, 21);
    CPPUNIT_ASSERT(infohash.size() == 20);
    const auto* data2 = infohash.data();
    for (auto i = 0; i < 20; ++i) {
        CPPUNIT_ASSERT_EQUAL(enough[i], data2[i]);
    }
    // Build from string
    auto infohashFromStr = dht::InfoHash("0102030405060708090A0102030405060708090A");
    CPPUNIT_ASSERT_EQUAL((size_t)20u, infohashFromStr.size());
    const auto* dataStr = infohashFromStr.data();
    for (auto i = 0; i < 20; ++i) {
        CPPUNIT_ASSERT_EQUAL((int)dataStr[i], (int)data[i]);
    }
}

void
InfoHashTester::testComparators() {
    auto nullHash = dht::InfoHash();
    auto minHash = dht::InfoHash("0000000000000000000000000000000000111110");
    auto maxHash = dht::InfoHash("0111110000000000000000000000000000000000");
    // operator ==
    CPPUNIT_ASSERT_EQUAL(minHash, minHash);
    CPPUNIT_ASSERT_EQUAL(minHash, dht::InfoHash("0000000000000000000000000000000000111110"));
	CPPUNIT_ASSERT(!(minHash == maxHash));
    // operator !=
    CPPUNIT_ASSERT(!(minHash != minHash));
    CPPUNIT_ASSERT(!(minHash != dht::InfoHash("0000000000000000000000000000000000111110")));
	CPPUNIT_ASSERT(minHash != maxHash);
    // operator<
    CPPUNIT_ASSERT(nullHash < minHash);
    CPPUNIT_ASSERT(nullHash < maxHash);
    CPPUNIT_ASSERT(minHash < maxHash);
    CPPUNIT_ASSERT(!(minHash < nullHash));
    CPPUNIT_ASSERT(!(maxHash < nullHash));
    CPPUNIT_ASSERT(!(maxHash < minHash));
    CPPUNIT_ASSERT(!(minHash < minHash));
    // bool()
    CPPUNIT_ASSERT(maxHash);
    CPPUNIT_ASSERT(!nullHash);

}

void
InfoHashTester::testLowBit() {
    auto nullHash = dht::InfoHash();
    auto minHash = dht::InfoHash("0000000000000000000000000000000000000010");
    auto maxHash = dht::InfoHash("0100000000000000000000000000000000000000");
    CPPUNIT_ASSERT_EQUAL(nullHash.lowbit(), -1);
    CPPUNIT_ASSERT_EQUAL(minHash.lowbit(), 155);
    CPPUNIT_ASSERT_EQUAL(maxHash.lowbit(), 7);
}

void
InfoHashTester::testCommonBits() {
    auto nullHash = dht::InfoHash();
    auto minHash = dht::InfoHash("0000000000000000000000000000000000000010");
    auto maxHash = dht::InfoHash("0100000000000000000000000000000000000000");
    CPPUNIT_ASSERT_EQUAL(dht::InfoHash::commonBits(nullHash, nullHash), (unsigned)160);
    CPPUNIT_ASSERT_EQUAL(dht::InfoHash::commonBits(nullHash, minHash), (unsigned)155);
    CPPUNIT_ASSERT_EQUAL(dht::InfoHash::commonBits(nullHash, maxHash), (unsigned)7);
    CPPUNIT_ASSERT_EQUAL(dht::InfoHash::commonBits(minHash, maxHash), (unsigned)7);
}

void
InfoHashTester::testXorCmp() {
    auto nullHash = dht::InfoHash();
    auto minHash = dht::InfoHash("0000000000000000000000000000000000000010");
    auto maxHash = dht::InfoHash("0100000000000000000000000000000000000000");
    CPPUNIT_ASSERT_EQUAL(minHash.xorCmp(nullHash, maxHash), -1);
    CPPUNIT_ASSERT_EQUAL(minHash.xorCmp(maxHash, nullHash), 1);
    CPPUNIT_ASSERT_EQUAL(minHash.xorCmp(minHash, maxHash), -1);
    CPPUNIT_ASSERT_EQUAL(minHash.xorCmp(maxHash, minHash), 1);
    CPPUNIT_ASSERT_EQUAL(nullHash.xorCmp(minHash, maxHash), -1);
    CPPUNIT_ASSERT_EQUAL(nullHash.xorCmp(maxHash, minHash), 1);
    // Because hashes are circular in distance.
    CPPUNIT_ASSERT_EQUAL(maxHash.xorCmp(nullHash, minHash), -1);
    CPPUNIT_ASSERT_EQUAL(maxHash.xorCmp(minHash, nullHash), 1);
}

void
InfoHashTester::testHex() {
    const std::string TEST_HASH_STR("01b20304d5060708090a010203e05060708090ae");
    dht::InfoHash TEST_HASH(TEST_HASH_STR);
    CPPUNIT_ASSERT_EQUAL(TEST_HASH_STR, TEST_HASH.toString());
    CPPUNIT_ASSERT_EQUAL(TEST_HASH_STR, dht::toHex(TEST_HASH.data(), TEST_HASH.size()));
}

void
InfoHashTester::tearDown() {

}
}  // namespace test