PageRenderTime 17ms CodeModel.GetById 12ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/libnpk/tests/libnpk_read_entity_partial.cpp

http://npk.googlecode.com/
C++ | 44 lines | 32 code | 10 blank | 2 comment | 4 complexity | b38233ce71d2ca6a464d00ab5940cf64 MD5 | raw file
 1#include "testutil.h"
 2#include <string>
 3#include <npk.h>
 4#include <npk_dev.h>
 5
 6int libnpk_read_entity_partial( int argc, char * argv [] )
 7{
 8    int teakey[4] = {98521,16322,7163,992};
 9
10    NPK_PACKAGE pack = npk_package_open( "sample.npk", teakey );
11    CHECK( pack != NULL );
12
13    std::string entityNames[2] = { "sample.txt", "tea.txt" };
14
15    for( int i = 0; i < 2; ++i )
16    {
17        NPK_ENTITY entity = npk_package_get_entity( pack, entityNames[i].c_str() );
18        CHECK( entity != NULL );
19
20        NPK_SIZE size = npk_entity_get_size( entity );
21        void* buf = malloc( size );
22
23        NPK_FLAG flag;
24        npk_entity_get_current_flag( entity, &flag );
25
26        if( flag & ( NPK_ENTITY_ENCRYPT_TEA | NPK_ENTITY_ENCRYPT_XXTEA ) )
27        {
28            // offset must be aligned by 8 bytes
29            CHECK( (!npk_entity_read_partial( entity, buf, 9, 32 )) );
30
31            // Size can be not aligned by 8 bytes when read end of file
32            NPK_SIZE offset = size - (size % 8) - 32;
33            CHECK( npk_entity_read_partial( entity, buf, offset, size - offset ) );
34            CHECK_EQUAL_STR_WITH_FILE_PARTIAL( (const char*)buf, "sample.txt", offset, size - offset );
35        }
36        CHECK( npk_entity_read_partial( entity, buf, 8, 32 ) );
37        CHECK_EQUAL_STR_WITH_FILE_PARTIAL( (const char*)buf, "sample.txt", 8, 32 );
38
39        free( buf );
40    }
41
42    npk_package_close( pack );
43    return 0;
44}