tizen beta release
[framework/web/wrt-commons.git] / tests / core / test_zip_input.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        test_zip_input.cpp
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of zip input tests
21  */
22 #include <dpl/test/test_runner.h>
23 #include <dpl/zip_input.h>
24 #include <dpl/foreach.h>
25 #include <dpl/abstract_waitable_input_adapter.h>
26 #include <dpl/abstract_waitable_output_adapter.h>
27 #include <dpl/binary_queue.h>
28 #include <dpl/scoped_array.h>
29 #include <dpl/copy.h>
30 #include <dpl/log/log.h>
31
32 namespace {
33 const char* PATH_NO_FILE = "/opt/apps/wrt/wrt-commons/tests/core/no_such_file";
34 const char* PATH_ARCHIVE = "/opt/apps/wrt/wrt-commons/tests/core/sample.zip";
35 const char* ARCHIVED_FILE = "sample.txt";
36 }
37
38 RUNNER_TEST_GROUP_INIT(DPL)
39
40 RUNNER_TEST(ZipInput_OpenFailed)
41 {
42     bool opened = true;
43
44     Try
45     {
46         DPL::ZipInput zip(PATH_NO_FILE);
47         (void)zip;
48     }
49     Catch(DPL::ZipInput::Exception::OpenFailed)
50     {
51         opened = false;
52     }
53
54     RUNNER_ASSERT(opened == false);
55 }
56
57 RUNNER_TEST(ZipInput_OpenFile)
58 {
59     DPL::ZipInput zip(PATH_ARCHIVE);
60
61     FOREACH(iter, zip)
62     {
63         LogDebug("---------");
64         LogDebug("FileInfo: ");
65 #define FIELD(X) LogDebug(#X ": " << iter->X)
66         FIELD(name);
67         FIELD(comment);
68         FIELD(version);
69         FIELD(versionNeeded);
70         FIELD(flag);
71         FIELD(compressionMethod);
72         FIELD(dosDate);
73         FIELD(crc);
74         FIELD(compressedSize);
75         FIELD(uncompressedSize);
76         FIELD(diskNumberStart);
77         FIELD(internalFileAttributes);
78         FIELD(externalFileAttributes);
79 #undef  FIELD
80     }
81 }
82
83 RUNNER_TEST(ZipInput_UnzipSingleFile)
84 {
85     DPL::ZipInput zip(PATH_ARCHIVE);
86     DPL::ZipInput::File *file = zip.OpenFile(ARCHIVED_FILE);
87     DPL::AbstractWaitableInputAdapter fileAdapter(file);
88     DPL::BinaryQueue buffer;
89     DPL::AbstractWaitableOutputAdapter bufferAdapter(&buffer);
90
91     DPL::Copy(&fileAdapter, &bufferAdapter);
92
93     DPL::ScopedArray<char> data(new char[buffer.Size() + 1]);
94     buffer.Flatten(data.Get(), buffer.Size());
95     data[buffer.Size()] = '\0';
96
97     RUNNER_ASSERT(std::string(data.Get()) == "test");
98 }