PageRenderTime 21ms CodeModel.GetById 9ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 1ms

/libnpk/tests/libnpk_xxtea.cpp

http://npk.googlecode.com/
C++ | 48 lines | 34 code | 12 blank | 2 comment | 11 complexity | 43a76cd617e1f549436a308d096a1c09 MD5 | raw file
 1#include "testutil.h"
 2#include <npk.h>
 3#include <npk_dev.h>
 4
 5int libnpk_xxtea( int argc, char * argv [] )
 6{
 7    int teakey[4] = {rand(), rand(), rand(), rand()};
 8
 9    NPK_PACKAGE pack;
10    NPK_ENTITY entity;
11
12    // create a pack
13    CHECK( NPK_SUCCESS == npk_package_alloc( &pack, teakey ) );
14    CHECK( NPK_SUCCESS == npk_package_add_file( pack, "sample.txt", "sample.txt", &entity ) );
15    CHECK( NPK_SUCCESS == npk_package_add_file( pack, "sample.txt", "zip.txt", &entity ) );
16    CHECK( NPK_SUCCESS == npk_entity_set_flag( entity, NPK_ENTITY_COMPRESS_ZLIB ) );
17    CHECK( NPK_SUCCESS == npk_package_add_file( pack, "sample.txt", "xxtea.txt", &entity ) );
18    CHECK( NPK_SUCCESS == npk_entity_set_flag( entity, NPK_ENTITY_ENCRYPT_XXTEA ) );
19    CHECK( NPK_SUCCESS == npk_package_add_file( pack, "sample.txt", "zipnxxtea.txt", &entity ) );
20    CHECK( NPK_SUCCESS == npk_entity_set_flag( entity, NPK_ENTITY_COMPRESS_ZLIB | NPK_ENTITY_ENCRYPT_XXTEA | NPK_ENTITY_REVERSE ) );
21    CHECK( NPK_SUCCESS == npk_package_save( pack, "foo.npk", true ) );
22
23    npk_package_close( pack );
24
25    // validation
26    pack = npk_package_open( "foo.npk", teakey );
27
28    std::string entityNames[4] = { "sample.txt", "zip.txt", "xxtea.txt", "zipnxxtea.txt" };
29
30    for( int i = 0; i < 4; ++i )
31    {
32        NPK_ENTITY entity = npk_package_get_entity( pack, entityNames[i].c_str() );
33        CHECK( entity != NULL );
34
35        NPK_SIZE size = npk_entity_get_size( entity );
36        void* buf = malloc( size );
37
38        CHECK( npk_entity_read( entity, buf ) );
39        printf( "%d : %s\n", i, (char*)buf );
40        CHECK_EQUAL_STR_WITH_FILE( (const char*)buf, "sample.txt" );
41
42        free( buf );
43    }
44
45    npk_package_close( pack );
46
47    return 0;
48}