add test for bundle.cc because I'm going to modify it

master
benny 2013-10-03 02:42:35 +02:00
parent 61fe7528e1
commit 747496eddd
3 changed files with 129 additions and 0 deletions

1
.gitignore vendored
View File

@ -13,6 +13,7 @@ install_manifest.txt
/tests/*/*.o
/tests/encrypted_file/encrypted_file
/tests/rolling_hash/rolling_hash
/tests/bundle/bundle
# build results
/zbackup

44
tests/bundle/bundle.pro Normal file
View File

@ -0,0 +1,44 @@
######################################################################
# Automatically generated by qmake (2.01a) Sun Jul 14 20:54:52 2013
######################################################################
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
LIBS += -lcrypto -lprotobuf -lz -lprotobuf -llzma
DEFINES += __STDC_FORMAT_MACROS
# Input
SOURCES += test_bundle.cc \
../../unbuffered_file.cc \
../../tmp_mgr.cc \
../../page_size.cc \
../../random.cc \
../../encryption_key.cc \
../../encryption.cc \
../../encrypted_file.cc \
../../file.cc \
../../dir.cc \
../../bundle.cc \
../../message.cc \
../../hex.cc \
../../zbackup.pb.cc
HEADERS += \
../../unbuffered_file.hh \
../../tmp_mgr.hh \
../../adler32.hh \
../../page_size.hh \
../../random.hh \
../../encryption_key.hh \
../../encrypted_file.hh \
../../encryption.hh \
../../ex.hh \
../../file.hh \
../../dir.hh \
../../bundle.hh \
../../message.hh \
../../hex.hh \
../../zbackup.pb.h

View File

@ -0,0 +1,84 @@
// Copyright (c) 2012-2013 Konstantin Isakov <ikm@zbackup.org>
// Part of ZBackup. Licensed under GNU GPLv2 or later
#include <stdlib.h>
//TODO don't use printf and sprintf...
#include <stdio.h>
#include "../../encrypted_file.hh"
#include "../../encryption_key.hh"
#include "../../random.hh"
#include "../../tmp_mgr.hh"
#include "../../check.hh"
#include "../../adler32.hh"
#include "../../bundle.hh"
char tmpbuf[100];
void readAndWrite( EncryptionKey const & key )
{
// temporary file for the bundle
TmpMgr tmpMgr( "/dev/shm" );
sptr< TemporaryFile > tempFile = tmpMgr.makeTemporaryFile();
// some chunk data
int chunk_count = rand() % 30;
size_t chunk_size = 64*1024;
char** chunks = new char*[chunk_count];
string* chunkIds = new string[chunk_count];
// write bundle
{
Bundle::Creator bundle;
for (int i=0;i<chunk_count;i++) {
chunks[i] = new char[chunk_size];
Random::genaratePseudo( chunks[i], chunk_size );
//TODO make it look like a real Id (or even let it match the data)
//TODO make sure we don't have any duplicate Ids
sprintf(tmpbuf, "0x%08x", rand());
chunkIds[i] = string(tmpbuf);
bundle.addChunk( chunkIds[i], chunks[i], chunk_size );
}
bundle.write( tempFile->getFileName().c_str(), key );
}
// read it and compare
{
Bundle::Reader bundle( tempFile->getFileName().c_str(), key );
for (int i=0;i<chunk_count;i++) {
string data;
size_t size;
bool ret = bundle.get( chunkIds[i], data, size );
CHECK( ret, "bundle.get returned false for chunk %d (%s)", i, chunkIds[i].c_str() );
CHECK( size == chunk_size, "wrong chunk size for chunk %d (%s)", i, chunkIds[i].c_str() );
CHECK( memcmp(data.c_str(), chunks[i], chunk_size) == 0, "wrong chunk data for chunk %d (%s)", i, chunkIds[i].c_str() );
}
}
// clean up
for (int i=0;i<chunk_count;i++)
delete[] chunks[i];
delete[] chunks;
//TODO does that call the destructors?
delete[] chunkIds;
printf(".");
fflush(stdout);
}
int main()
{
EncryptionKeyInfo keyInfo;
EncryptionKey::generate( "blah", keyInfo );
EncryptionKey key( "blah", &keyInfo );
EncryptionKey noKey( std::string(), NULL );
for ( size_t iteration = 100; iteration--; )
readAndWrite( ( rand() & 1 ) ? key : noKey );
printf("\n");
}