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
|
// Copyright (c) 2012-2014 Konstantin Isakov <ikm@zbackup.org> and ZBackup contributors, see CONTRIBUTORS
// Part of ZBackup. Licensed under GNU GPLv2 or later + OpenSSL, see LICENSE
#include <stdlib.h>
#include "../../encrypted_file.hh"
#include "../../encryption_key.hh"
#include "../../random.hh"
#include "../../tmp_mgr.hh"
#include "../../check.hh"
#include "../../adler32.hh"
char rnd[ 16384 ];
Adler32::Value adler( int sz )
{
Adler32 a;
a.add( rnd, sz );
return a.result();
}
void readAndWrite( EncryptionKey const & key, bool writeBackups,
bool readBackups, bool readSkips )
{
TmpMgr tmpMgr( "/dev/shm" );
sptr< TemporaryFile > tempFile = tmpMgr.makeTemporaryFile();
int fileSize = rand() % ( sizeof( rnd ) + 1 );
fprintf( stderr, "Run with %d bytes, %s%s%s%sfile %s...\n", fileSize,
key.hasKey() ? "" : "no encryption, ",
writeBackups ? "write backups, " : "",
readBackups ? "read backups, " : "",
readSkips ? "read skips, " : "",
tempFile->getFileName().c_str() );
char iv[ Encryption::IvSize ];
Random::genaratePseudo( iv, sizeof( iv ) );
// Write
{
EncryptedFile::OutputStream out( tempFile->getFileName().c_str(), key, iv );
char const * next = rnd;
int avail = 0;
for ( int left = fileSize; left; )
{
CHECK( out.ByteCount() == fileSize - left, "Incorrect bytecount in the "
"middle of writing" );
void * data;
CHECK( out.Next( &data, &avail ), "out.Next() returned false" );
CHECK( avail > 0, "out.Next() returned zero size" );
bool doBackup = writeBackups && ( rand() & 1 );
int backup;
if ( doBackup )
{
backup = rand() % ( avail + 1 );
// Make sure we don't back up and then need to back up again to finish
// the write
if ( avail > left )
backup = avail - left;
avail -= backup;
}
int toWrite = avail > left ? left : avail;
memcpy( data, next, toWrite );
if ( doBackup )
out.BackUp( backup );
next += toWrite;
left -= toWrite;
avail -= toWrite;
if ( !avail && ( rand() & 1 ) )
{
CHECK( adler( next - rnd ) == out.getAdler32(),
"bad adler32 in the middle of writing" );
}
}
if ( avail || ( rand() & 1 ) )
out.BackUp( avail );
CHECK( out.ByteCount() == fileSize, "Incorrect bytecount after writing" );
if ( rand() & 1 )
{
CHECK( adler( fileSize ) == out.getAdler32(),
"bad adler32 of the written file" );
}
}
// Read back
{
EncryptedFile::InputStream in( tempFile->getFileName().c_str(), key, iv );
char const * next = rnd;
void const * data;
int avail = 0;
for ( int left = fileSize; left; )
{
if ( readSkips && ( rand() & 1 ) )
{
int toSkip = rand() % ( left + 1 );
in.Skip( toSkip );
next += toSkip;
left -= toSkip;
avail = 0;
continue;
}
CHECK( in.ByteCount() == fileSize - left, "Incorrect bytecount in the "
"middle of reading" );
CHECK( in.Next( &data, &avail ), "file ended while %d were still left",
left );
CHECK( avail > 0, "in.Next() returned zero size" );
bool doBackup = readBackups && ( rand() & 1 );
int backup;
if ( doBackup )
{
backup = rand() % ( avail + 1 );
avail -= backup;
}
int toRead = avail > left ? left : avail;
CHECK( memcmp( next, data, toRead ) == 0, "Different bytes read than "
"expected at offset %d", int( next - rnd ) );
if ( doBackup )
in.BackUp( backup );
next += toRead;
left -= toRead;
avail -= toRead;
if ( !avail && ( rand() & 1 ) )
{
CHECK( adler( next - rnd ) == in.getAdler32(),
"bad adler32 in the middle of the reading" );
}
}
CHECK( in.ByteCount() == fileSize, "Incorrect bytecount after reading" );
CHECK( !avail, "at least %d bytes still available", avail );
CHECK( !in.Next( &data, &avail ), "file should have ended but resulted in "
"%d more bytes", avail );
if ( rand() & 1 )
{
CHECK( adler( fileSize ) == in.getAdler32(),
"bad adler32 of the read file" );
}
}
}
int main()
{
Random::genaratePseudo( rnd, sizeof( rnd ) );
EncryptionKeyInfo keyInfo;
EncryptionKey::generate( "blah", keyInfo );
EncryptionKey key( "blah", &keyInfo );
EncryptionKey noKey( std::string(), NULL );
for ( size_t iteration = 100000; iteration--; )
readAndWrite( ( rand() & 1 ) ? key : noKey, rand() & 1, rand() & 1,
rand() & 1 );
}
|