Tizen 2.0 Release
[framework/web/wrt-commons.git] / tests / dpl / 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/share/wrt/wrt-commons/tests/core/no_such_file";
34 const char* PATH_ARCHIVE = "/opt/share/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(compressedSize);
69         FIELD(uncompressedSize);
70 #undef  FIELD
71     }
72 }
73
74 RUNNER_TEST(ZipInput_UnzipSingleFile)
75 {
76     DPL::ZipInput zip(PATH_ARCHIVE);
77     DPL::ZipInput::File *file = zip.OpenFile(ARCHIVED_FILE);
78     DPL::AbstractWaitableInputAdapter fileAdapter(file);
79     DPL::BinaryQueue buffer;
80     DPL::AbstractWaitableOutputAdapter bufferAdapter(&buffer);
81
82     DPL::Copy(&fileAdapter, &bufferAdapter);
83
84     DPL::ScopedArray<char> data(new char[buffer.Size() + 1]);
85     buffer.Flatten(data.Get(), buffer.Size());
86     data[buffer.Size()] = '\0';
87
88     RUNNER_ASSERT(std::string(data.Get()) == "test");
89 }