1//
2// PartialStreamTest.cpp
3//
4// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "PartialStreamTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/Zip/PartialStream.h"
15#include "Poco/Zip/AutoDetectStream.h"
16#include "Poco/Zip/ZipUtil.h"
17#include "Poco/MemoryStream.h"
18#include "Poco/StreamCopier.h"
19#include <sstream>
20
21
22using namespace Poco::Zip;
23
24
25PartialStreamTest::PartialStreamTest(const std::string& name): CppUnit::TestCase(name)
26{
27}
28
29
30PartialStreamTest::~PartialStreamTest()
31{
32}
33
34
35void PartialStreamTest::testReading()
36{
37 std::string message("some dummy message !");
38 std::string prefix("pre ");
39 std::string postfix(" post");
40 std::string result(prefix+message+postfix);
41 std::istringstream istr(message);
42 PartialInputStream in(istr, 0, static_cast<std::streamoff>(message.length()), true, prefix, postfix);
43 char buf[124];
44 in.read(buf, 124);
45 std::string res(buf, static_cast<std::string::size_type>(in.gcount()));
46 assertTrue (res == result);
47}
48
49
50void PartialStreamTest::testWriting()
51{
52 std::string prefix("X");
53 std::string message("some test message");
54 std::string postfix("YYY");
55 std::string result(prefix+message+postfix);
56 std::ostringstream ostr;
57 PartialOutputStream out(ostr, prefix.size(), postfix.size());
58 out.write(result.c_str(), static_cast<std::streamsize>(result.length()));
59 assertTrue (out.good());
60 out.close();
61 std::string res (ostr.str());
62 assertTrue (out.bytesWritten() == message.size());
63 assertTrue (message == res);
64}
65
66
67void PartialStreamTest::testWritingZero()
68{
69 std::string prefix("X");
70 std::string message;
71 std::string postfix("YYY");
72 std::string result(prefix+message+postfix);
73 std::ostringstream ostr;
74 PartialOutputStream out(ostr, prefix.size(), postfix.size());
75 out.write(result.c_str(), static_cast<std::streamsize>(result.length()));
76 assertTrue (out.good());
77 out.close();
78 std::string res (ostr.str());
79 assertTrue (out.bytesWritten() == message.size());
80 assertTrue (message == res);
81}
82
83
84void PartialStreamTest::testWritingOne()
85{
86 std::string prefix("X");
87 std::string message("a");
88 std::string postfix("YYY");
89 std::string result(prefix+message+postfix);
90 std::ostringstream ostr;
91 PartialOutputStream out(ostr, prefix.size(), postfix.size());
92 out.write(result.c_str(), static_cast<std::streamsize>(result.length()));
93 assertTrue (out.good());
94 out.close();
95 std::string res (ostr.str());
96 assertTrue (out.bytesWritten() == message.size());
97 assertTrue (message == res);
98}
99
100
101void PartialStreamTest::testAutoDetect()
102{
103 std::string header = ZipUtil::fakeZLibInitString(ZipCommon::CL_NORMAL);
104 std::string crc("\01\02\03\04");
105 const char data[] =
106 {
107 '\x01', '\x02', '\x03', '\x04',
108 '\x05', '\x06', '\x07', '\x08', // fake data
109 '\x50', '\x4b', '\x07', '\x08', // data signature in compressed data
110 '\x01', '\x02', '\x03', '\x04',
111 '\x50',
112 '\x50', '\x4b', '\x07', '\x08', // real data signature
113 '\x00', '\x00', '\x00', '\x00', // CRC (ignored)
114 '\x11', '\x00', '\x00', '\x00', // compressed size
115 '\x00', '\x00', '\x00', '\x00' // uncompressed size (ignored)
116 };
117
118 Poco::MemoryInputStream istr(data, sizeof(data));
119 AutoDetectInputStream adi(istr, header, crc, false, 0);
120 std::string result;
121 Poco::StreamCopier::copyToString(adi, result);
122 assertTrue (result.size() == 23);
123}
124
125
126void PartialStreamTest::setUp()
127{
128}
129
130
131void PartialStreamTest::tearDown()
132{
133}
134
135
136CppUnit::Test* PartialStreamTest::suite()
137{
138 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("PartialStreamTest");
139
140 CppUnit_addTest(pSuite, PartialStreamTest, testReading);
141 CppUnit_addTest(pSuite, PartialStreamTest, testWriting);
142 CppUnit_addTest(pSuite, PartialStreamTest, testWritingZero);
143 CppUnit_addTest(pSuite, PartialStreamTest, testWritingOne);
144 CppUnit_addTest(pSuite, PartialStreamTest, testAutoDetect);
145
146 return pSuite;
147}
148