PageRenderTime 15ms CodeModel.GetById 1ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/libnpk/tests/libnpk_streamable.cpp

http://npk.googlecode.com/
C++ | 82 lines | 64 code | 15 blank | 3 comment | 16 complexity | 7688768ee32d45ea92f754832486a5e6 MD5 | raw file
 1#include "testutil.h"
 2#include <sys/stat.h>
 3#include <npk.h>
 4#include <npk_dev.h>
 5
 6int libnpk_streamable( int argc, char * argv [] )
 7{
 8    int teakey[4] = {1,2,3,4};
 9
10    NPK_PACKAGE pack;
11    NPK_ENTITY entity;
12
13    // create a pack
14    CHECK( NPK_SUCCESS == npk_package_alloc( &pack, teakey ) );
15    CHECK( NPK_SUCCESS == npk_package_add_file( pack, "sample.txt", "sample.txt", &entity ) );
16    CHECK( NPK_SUCCESS == npk_package_add_file( pack, "sample.txt", "tea.txt", &entity ) );
17    CHECK( NPK_SUCCESS == npk_entity_set_flag( entity, NPK_ENTITY_ENCRYPT_TEA ) );
18    CHECK( NPK_SUCCESS == npk_package_add_file( pack, "sample.txt", "xxtea.txt", &entity ) );
19    CHECK( NPK_SUCCESS == npk_entity_set_flag( entity, NPK_ENTITY_ENCRYPT_XXTEA ) );
20    CHECK( NPK_SUCCESS == npk_package_add_file( pack, "sample.txt", "zip.txt", &entity ) );
21    CHECK( NPK_SUCCESS == npk_entity_set_flag( entity, NPK_ENTITY_COMPRESS_ZLIB ) );
22    CHECK( NPK_SUCCESS == npk_package_save( pack, "foo.npk", true ) );
23
24    npk_package_close( pack );
25
26    // simulate download
27    int rh = open( "foo.npk", O_RDONLY | O_BINARY );
28    size_t filesize = npk_seek( rh, 0, SEEK_END );
29    npk_seek( rh, 0, SEEK_SET );
30
31    int wh = open( "foo_2.npk", O_CREAT | O_RDWR | O_TRUNC | O_BINARY, S_IREAD | S_IWRITE );
32
33    // validation
34    std::string entityNames[4] = { "sample.txt", "tea.txt", "xxtea.txt", "zip.txt" };
35    pack = 0;
36    int i = 0;
37    size_t offset = 0;
38    char buf[255];
39
40    while( offset < filesize )
41    {
42        size_t r = rand()%16;
43        if( r + offset > filesize )
44            r = filesize - offset;
45
46        read( rh, &buf, sizeof(char)*r );
47        write( wh, buf, sizeof(char)*r );
48        printf( "offset %ld, reading %ld byte(s).\n", offset, r );
49        offset += r;
50
51        if( pack == 0 )
52        {
53            pack = npk_package_open( "foo_2.npk", teakey );
54            if( pack != 0 )
55                printf( "   package loaded.\n" );
56        }
57        else
58        {
59            NPK_ENTITY entity = npk_package_get_entity( pack, entityNames[i].c_str() );
60            CHECK( entity != NULL );
61
62            NPK_SIZE size = npk_entity_get_size( entity );
63            if( npk_entity_is_ready( entity ) )
64            {
65                printf( "   entity %s ready.\n", entityNames[i].c_str() );
66                void* buf = malloc( size );
67
68                CHECK( npk_entity_read( entity, buf ) );
69                CHECK_EQUAL_STR_WITH_FILE( (const char*)buf, "sample.txt" );
70
71                free( buf );
72                ++i;
73            }
74        }
75    }
76
77    npk_package_close( pack );
78    close( wh );
79    close( rh );
80
81    return 0;
82}