120 lines
3.8 KiB
C++
120 lines
3.8 KiB
C++
#include "../src/dshash.hpp"
|
|
#include <iostream>
|
|
#include <cassert>
|
|
#include <fstream>
|
|
#include <filesystem>
|
|
#include <cstring>
|
|
|
|
void test_string_hash() {
|
|
DSHash hasher(std::string("abc"));
|
|
std::string hashStr = hasher.toString();
|
|
assert(hashStr == "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
|
|
std::cout << "✓ String hash test passed" << std::endl;
|
|
}
|
|
|
|
void test_empty_string() {
|
|
DSHash hasher(std::string(""));
|
|
std::string hashStr = hasher.toString();
|
|
assert(hashStr == "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
|
|
std::cout << "✓ Empty string hash test passed" << std::endl;
|
|
}
|
|
|
|
void test_long_string() {
|
|
DSHash hasher(std::string("The quick brown fox jumps over the lazy dog"));
|
|
std::string hashStr = hasher.toString();
|
|
assert(hashStr == "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592");
|
|
std::cout << "✓ Long string hash 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();
|
|
|
|
DSHash fileHasher{std::filesystem::path(tempFile)};
|
|
std::string hashStr = fileHasher.toString();
|
|
|
|
DSHash stringHasher(std::string("Test content for hashing"));
|
|
std::string expectedHashStr = stringHasher.toString();
|
|
|
|
assert(hashStr == expectedHashStr);
|
|
std::filesystem::remove(tempFile);
|
|
std::cout << "✓ File hash test passed" << std::endl;
|
|
}
|
|
|
|
void test_get_method() {
|
|
DSHash hasher(std::string("test"));
|
|
tHash hash = hasher.get();
|
|
|
|
assert(hash.size() == 32);
|
|
|
|
std::string hashStr = hasher.toString();
|
|
assert(hashStr.length() == 64);
|
|
std::cout << "✓ get() method 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) {
|
|
DSHash hasher(std::string(vec.input));
|
|
std::string hashStr = hasher.toString();
|
|
assert(hashStr == vec.expected);
|
|
}
|
|
std::cout << "✓ Known test vectors passed" << std::endl;
|
|
}
|
|
|
|
void test_directory_hash() {
|
|
std::filesystem::path tempDir = "/tmp/test_hash_dir";
|
|
std::filesystem::create_directories(tempDir);
|
|
std::filesystem::create_directories(tempDir / "subdir");
|
|
|
|
std::ofstream file1(tempDir / "file1.txt");
|
|
file1 << "content1";
|
|
file1.close();
|
|
|
|
std::ofstream file2(tempDir / "subdir" / "file2.txt");
|
|
file2 << "content2";
|
|
file2.close();
|
|
|
|
DSHash dirHasher(tempDir);
|
|
std::string hashStr = dirHasher.toString();
|
|
|
|
assert(hashStr.length() == 64);
|
|
for (char c : hashStr) {
|
|
assert((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'));
|
|
}
|
|
|
|
std::filesystem::remove_all(tempDir);
|
|
std::cout << "✓ Directory hash test passed" << std::endl;
|
|
}
|
|
|
|
int main() {
|
|
try {
|
|
test_string_hash();
|
|
test_empty_string();
|
|
test_long_string();
|
|
test_file_hash();
|
|
test_get_method();
|
|
test_known_vectors();
|
|
test_directory_hash();
|
|
|
|
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;
|
|
}
|
|
} |