tizen 2.4 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 <memory>
23 #include <dpl/test/test_runner.h>
24 #include <dpl/zip_input.h>
25 #include <dpl/foreach.h>
26 #include <dpl/abstract_waitable_input_adapter.h>
27 #include <dpl/abstract_waitable_output_adapter.h>
28 #include <dpl/binary_queue.h>
29 #include <dpl/copy.h>
30 #include <dpl/log/wrt_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 /*
41 Name: ZipInput_OpenFailed
42 Description: tests opening non existing file
43 Expected: exception throw
44 */
45 RUNNER_TEST(ZipInput_OpenFailed)
46 {
47     bool opened = true;
48
49     Try
50     {
51         DPL::ZipInput zip(PATH_NO_FILE);
52         (void)zip;
53     }
54     Catch(DPL::ZipInput::Exception::OpenFailed)
55     {
56         opened = false;
57     }
58
59     RUNNER_ASSERT(opened == false);
60 }
61
62 /*
63 Name: ZipInput_OpenFile
64 Description: tests opening existing file
65 Expected: zip stats should mkatch expected
66 */
67 RUNNER_TEST(ZipInput_OpenFile)
68 {
69     DPL::ZipInput zip(PATH_ARCHIVE);
70
71     FOREACH(iter, zip)
72     {
73         WrtLogD("---------");
74         WrtLogD("FileInfo: ");
75         WrtLogD("name: %s", iter->name.c_str());
76         WrtLogD("comment: %s", iter->comment.c_str());
77         WrtLogD("compressedSize: %lli", iter->compressedSize);
78         WrtLogD("uncompressedSize: %lli", iter->uncompressedSize);
79     }
80 }
81
82 /*
83 Name: ZipInput_UnzipSingleFile
84 Description: tests opening existing file and unzipping single file
85 Expected: right content
86 */
87 RUNNER_TEST(ZipInput_UnzipSingleFile)
88 {
89     DPL::ZipInput zip(PATH_ARCHIVE);
90     DPL::ZipInput::File *file = zip.OpenFile(ARCHIVED_FILE);
91     DPL::AbstractWaitableInputAdapter fileAdapter(file);
92     DPL::BinaryQueue buffer;
93     DPL::AbstractWaitableOutputAdapter bufferAdapter(&buffer);
94
95     DPL::Copy(&fileAdapter, &bufferAdapter);
96
97     std::unique_ptr<char[]> data(new char[buffer.Size() + 1]);
98     buffer.Flatten(data.get(), buffer.Size());
99     data[buffer.Size()] = '\0';
100
101     RUNNER_ASSERT(std::string(data.get()) == "test");
102 }