Files
dshash/tests/test_lib.cpp
2025-09-02 16:05:30 +12:00

125 lines
4.0 KiB
C++

#include "../src/dshash.hpp"
#include <iostream>
#include <cassert>
#include <fstream>
#include <filesystem>
#include <cstring>
void test_string_hash() {
auto hash = DSHash::hashString("abc");
std::string hashStr = DSHash::toString(hash);
assert(hashStr == "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
std::cout << "✓ String hash test passed" << std::endl;
}
void test_empty_string() {
auto hash = DSHash::hashString("");
std::string hashStr = DSHash::toString(hash);
assert(hashStr == "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
std::cout << "✓ Empty string hash test passed" << std::endl;
}
void test_long_string() {
auto hash = DSHash::hashString("The quick brown fox jumps over the lazy dog");
std::string hashStr = DSHash::toString(hash);
assert(hashStr == "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592");
std::cout << "✓ Long string hash test passed" << std::endl;
}
void test_incremental_update() {
DSHash hasher;
hasher.update("a");
hasher.update("b");
hasher.update("c");
auto hash = hasher.finalize();
std::string hashStr = DSHash::toString(hash);
assert(hashStr == "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
std::cout << "✓ Incremental update test passed" << std::endl;
}
void test_file_hash() {
std::string tempFile = "/tmp/test_hash_file.txt";
std::ofstream out(tempFile);
out << "Test content for hashing";
out.close();
auto hash = DSHash::hashFile(tempFile);
std::string hashStr = DSHash::toString(hash);
DSHash hasher;
hasher.update("Test content for hashing");
auto expectedHash = hasher.finalize();
std::string expectedHashStr = DSHash::toString(expectedHash);
assert(hashStr == expectedHashStr);
std::filesystem::remove(tempFile);
std::cout << "✓ File hash test passed" << std::endl;
}
void test_large_data() {
DSHash hasher;
std::string chunk(1000, 'a');
for (int i = 0; i < 100; i++) {
hasher.update(chunk);
}
auto hash = hasher.finalize();
std::string hashStr = DSHash::toString(hash);
assert(hashStr.length() == 64);
for (char c : hashStr) {
assert((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'));
}
std::cout << "✓ Large data hash test passed" << std::endl;
}
void test_known_vectors() {
struct TestVector {
std::string input;
std::string expected;
};
TestVector vectors[] = {
{"a", "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"},
{"message digest", "f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650"},
{"abcdefghijklmnopqrstuvwxyz", "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73"},
{"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
"db4bfcbd4da0cd85a60c3c37d3fbd8805c77f15fc6b1fdfe614ee0a7c8fdb4c0"}
};
for (const auto& vec : vectors) {
auto hash = DSHash::hashString(vec.input);
std::string hashStr = DSHash::toString(hash);
assert(hashStr == vec.expected);
}
std::cout << "✓ Known test vectors passed" << std::endl;
}
void test_binary_data() {
uint8_t binaryData[] = {0x00, 0x01, 0x02, 0x03, 0xFF, 0xFE, 0xFD};
DSHash hasher;
hasher.update(binaryData, sizeof(binaryData));
auto hash = hasher.finalize();
std::string hashStr = DSHash::toString(hash);
assert(hashStr.length() == 64);
std::cout << "✓ Binary data hash test passed" << std::endl;
}
int main() {
try {
test_string_hash();
test_empty_string();
test_long_string();
test_incremental_update();
test_file_hash();
test_large_data();
test_known_vectors();
test_binary_data();
std::cout << "\nAll library tests passed!" << std::endl;
return 0;
} catch (const std::exception& e) {
std::cerr << "Test failed with exception: " << e.what() << std::endl;
return 1;
}
}