1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6  */
7 
8 #include "test_global.hpp"
9 #include <algorithm>
10 #include <cstdlib>
11 #include <vector>
12 
13 #include "orcus/zip_archive_stream.hpp"
14 
15 #define ASSERT_THROW(expr) \
16 try \
17 { \
18     expr; \
19     assert(0); \
20 } \
21 catch (...) \
22 { \
23 }
24 
25 using namespace std;
26 using namespace orcus;
27 
test_zip_archive_stream(zip_archive_stream * const strm,const unsigned char * const data,size_t const length)28 void test_zip_archive_stream(zip_archive_stream* const strm, const unsigned char* const data, size_t const length)
29 {
30     assert(strm->size() == length);
31     assert(strm->tell() == 0);
32 
33     std::vector<unsigned char> buffer(length, 0);
34     unsigned char* buf = buffer.data();
35 
36     strm->read(buf, 2);
37     assert(equal(data, data + 2, buf));
38     assert(strm->tell() == 0);
39     strm->read(buf, length);
40     assert(equal(data, data + length, buf));
41     ASSERT_THROW(strm->read(buf, length + 1));
42     strm->read(buf, 0);
43 
44     strm->seek(2);
45     assert(strm->tell() == 2);
46     strm->read(buf, 2);
47     assert(equal(data + 2, data + 4, buf));
48     strm->seek(length);
49     assert(strm->tell() == length);
50     ASSERT_THROW(strm->seek(length + 1));
51     assert(strm->tell() == length);
52 }
53 
test_zip_archive_stream_blob()54 void test_zip_archive_stream_blob()
55 {
56     const unsigned char data[] = "My hovercraft is full of eels.";
57     zip_archive_stream_blob strm(data, sizeof(data));
58     test_zip_archive_stream(&strm, data, sizeof(data));
59 }
60 
main()61 int main()
62 {
63     test_zip_archive_stream_blob();
64 
65     return EXIT_SUCCESS;
66 }
67 
68 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
69