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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
#include "config.h"
#include "test_hash_check_queue.h"
#include "helpers/test_thread.h"
#include "helpers/test_utils.h"
#include <functional>
#include <signal.h>
#include "data/chunk_handle.h"
#include "data/thread_disk.h"
#include "utils/sha1.h"
#include "torrent/chunk_manager.h"
#include "torrent/exceptions.h"
#include "test_chunk_list.h"
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(test_hash_check_queue, "data");
pthread_mutex_t done_chunks_lock = PTHREAD_MUTEX_INITIALIZER;
static void
chunk_done(done_chunks_type* done_chunks, torrent::HashChunk* hash_chunk, const torrent::HashString& hash_value) {
pthread_mutex_lock(&done_chunks_lock);
(*done_chunks)[hash_chunk->handle().index()] = hash_value;
pthread_mutex_unlock(&done_chunks_lock);
}
torrent::HashString
hash_for_index(uint32_t index) {
char buffer[10];
std::memset(buffer, index, 10);
torrent::Sha1 sha1;
torrent::HashString hash;
sha1.init();
sha1.update(buffer, 10);
sha1.final_c(hash.data());
return hash;
}
bool
verify_hash(const done_chunks_type* done_chunks, int index, const torrent::HashString& hash) {
pthread_mutex_lock(&done_chunks_lock);
done_chunks_type::const_iterator itr = done_chunks->find(index);
if (itr == done_chunks->end()) {
pthread_mutex_unlock(&done_chunks_lock);
return false;
}
bool matches = itr->second == hash;
pthread_mutex_unlock(&done_chunks_lock);
if (!matches) {
// std::cout << "chunk compare: " << index << " "
// << torrent::hash_string_to_hex_str(itr->second) << ' ' << torrent::hash_string_to_hex_str(hash) << ' '
// << (itr != done_chunks->end() && itr->second == hash)
// << std::endl;
throw torrent::internal_error("Could not verify hash...");
}
return true;
}
void
test_hash_check_queue::test_single() {
SETUP_CHUNK_LIST();
torrent::HashCheckQueue hash_queue;
done_chunks_type done_chunks;
hash_queue.slot_chunk_done() = std::bind(&chunk_done, &done_chunks, std::placeholders::_1, std::placeholders::_2);
torrent::ChunkHandle handle_0 = chunk_list->get(0, torrent::ChunkList::get_not_hashing | torrent::ChunkList::get_blocking);
hash_queue.push_back(new torrent::HashChunk(handle_0));
CPPUNIT_ASSERT(hash_queue.size() == 1);
CPPUNIT_ASSERT(hash_queue.front()->handle().is_blocking());
CPPUNIT_ASSERT(hash_queue.front()->handle().object() == &((*chunk_list)[0]));
hash_queue.perform();
CPPUNIT_ASSERT(done_chunks.find(0) != done_chunks.end());
CPPUNIT_ASSERT(done_chunks[0] == hash_for_index(0));
// Should not be needed... Also verify that HashChunk gets deleted.
chunk_list->release(&handle_0, torrent::ChunkList::release_default);
CLEANUP_CHUNK_LIST();
}
void
test_hash_check_queue::test_multiple() {
SETUP_CHUNK_LIST();
torrent::HashCheckQueue hash_queue;
done_chunks_type done_chunks;
hash_queue.slot_chunk_done() = std::bind(&chunk_done, &done_chunks, std::placeholders::_1, std::placeholders::_2);
handle_list handles;
for (unsigned int i = 0; i < 20; i++) {
handles.push_back(chunk_list->get(i, torrent::ChunkList::get_not_hashing | torrent::ChunkList::get_blocking));
hash_queue.push_back(new torrent::HashChunk(handles.back()));
CPPUNIT_ASSERT(hash_queue.size() == i + 1);
CPPUNIT_ASSERT(hash_queue.back()->handle().is_blocking());
CPPUNIT_ASSERT(hash_queue.back()->handle().object() == &((*chunk_list)[i]));
}
hash_queue.perform();
for (unsigned int i = 0; i < 20; i++) {
CPPUNIT_ASSERT(done_chunks.find(i) != done_chunks.end());
CPPUNIT_ASSERT(done_chunks[i] == hash_for_index(i));
// Should not be needed...
chunk_list->release(&handles[i], torrent::ChunkList::release_default);
}
CLEANUP_CHUNK_LIST();
}
void
test_hash_check_queue::test_erase() {
// SETUP_CHUNK_LIST();
// torrent::HashCheckQueue hash_queue;
// done_chunks_type done_chunks;
// hash_queue.slot_chunk_done() = std::bind(&chunk_done, &done_chunks, std::placeholders::_1, std::placeholders::_2);
// handle_list handles;
// for (unsigned int i = 0; i < 20; i++) {
// handles.push_back(chunk_list->get(i, torrent::ChunkList::get_not_hashing | torrent::ChunkList::get_blocking));
// hash_queue.push_back(new torrent::HashChunk(handles.back()));
// CPPUNIT_ASSERT(hash_queue.size() == i + 1);
// CPPUNIT_ASSERT(hash_queue.back()->handle().is_blocking());
// CPPUNIT_ASSERT(hash_queue.back()->handle().object() == &((*chunk_list)[i]));
// }
// hash_queue.perform();
// for (unsigned int i = 0; i < 20; i++) {
// CPPUNIT_ASSERT(done_chunks.find(i) != done_chunks.end());
// CPPUNIT_ASSERT(done_chunks[i] == hash_for_index(i));
// // Should not be needed...
// chunk_list->release(&handles[i]);
// }
// CLEANUP_CHUNK_LIST();
}
void
test_hash_check_queue::test_thread_interrupt() {
SETUP_CHUNK_LIST();
torrent::HashCheckQueue* hash_queue = torrent::thread_disk()->hash_check_queue();
done_chunks_type done_chunks;
hash_queue->slot_chunk_done() = std::bind(&chunk_done, &done_chunks, std::placeholders::_1, std::placeholders::_2);
for (int i = 0; i < 1000; i++) {
pthread_mutex_lock(&done_chunks_lock);
done_chunks.erase(0);
pthread_mutex_unlock(&done_chunks_lock);
torrent::ChunkHandle handle_0 = chunk_list->get(0, torrent::ChunkList::get_not_hashing | torrent::ChunkList::get_blocking);
hash_queue->push_back(new torrent::HashChunk(handle_0));
torrent::thread_disk()->interrupt();
CPPUNIT_ASSERT(wait_for_true(std::bind(&verify_hash, &done_chunks, 0, hash_for_index(0))));
chunk_list->release(&handle_0, torrent::ChunkList::release_default);
}
CLEANUP_CHUNK_LIST();
}
|