nss: upgrade to release 3.73
[LibreOffice.git] / package / qa / cppunit / test_package.cxx
blob29a8d9281cb5600249da2b243bf7399f2f96b2fc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <comphelper/processfactory.hxx>
11 #include <unotest/filters-test.hxx>
12 #include <unotest/bootstrapfixturebase.hxx>
13 #include <comphelper/threadpool.hxx>
14 #include <com/sun/star/packages/zip/ZipFileAccess.hpp>
15 #include <com/sun/star/beans/NamedValue.hpp>
17 #include <iterator>
19 using namespace ::com::sun::star;
21 namespace
23 class PackageTest
24 : public test::FiltersTest
25 , public test::BootstrapFixtureBase
27 public:
28 PackageTest() {}
30 virtual void setUp() override;
32 virtual bool load(const OUString &,
33 const OUString &rURL, const OUString &,
34 SfxFilterFlags, SotClipboardFormatId, unsigned int) override;
36 void test();
37 void testThreadedStreams();
38 void testBufferedThreadedStreams();
40 CPPUNIT_TEST_SUITE(PackageTest);
41 CPPUNIT_TEST(test);
42 CPPUNIT_TEST(testThreadedStreams);
43 CPPUNIT_TEST(testBufferedThreadedStreams);
44 CPPUNIT_TEST_SUITE_END();
46 private:
47 uno::Reference<container::XNameAccess> mxNA;
48 void verifyStreams( std::vector<std::vector<char>> &aBuffers );
51 void PackageTest::setUp()
53 BootstrapFixtureBase::setUp();
54 OUString aURL = m_directories.getURLFromSrc("/package/qa/cppunit/data/a2z.zip");
56 uno::Sequence<beans::NamedValue> aNVs(1);
57 aNVs[0].Name = "URL";
58 aNVs[0].Value <<= aURL;
60 uno::Sequence<uno::Any> aArgs(1);
61 aArgs[0] <<= aNVs;
63 uno::Reference<uno::XComponentContext> xCxt = comphelper::getProcessComponentContext();
64 uno::Reference<lang::XMultiComponentFactory> xSvcMgr = xCxt->getServiceManager();
66 uno::Reference<packages::zip::XZipFileAccess2> xZip(
67 xSvcMgr->createInstanceWithArgumentsAndContext(
68 "com.sun.star.packages.zip.ZipFileAccess", aArgs, xCxt),
69 uno::UNO_QUERY);
71 CPPUNIT_ASSERT(xZip.is());
73 mxNA = xZip;
74 CPPUNIT_ASSERT(mxNA.is());
77 bool PackageTest::load(const OUString &,
78 const OUString &rURL, const OUString &,
79 SfxFilterFlags, SotClipboardFormatId, unsigned int)
81 try
83 uno::Reference<css::packages::zip::XZipFileAccess2> xZip(
84 css::packages::zip::ZipFileAccess::createWithURL(comphelper::getProcessComponentContext(), rURL));
85 return xZip.is();
87 catch(...)
89 return false;
93 void PackageTest::test()
95 testDir(OUString(),
96 m_directories.getURLFromSrc("/package/qa/cppunit/data/"));
99 void PackageTest::verifyStreams( std::vector<std::vector<char>> &aBuffers )
101 CPPUNIT_ASSERT_EQUAL(size_t(26), aBuffers.size());
102 auto itBuf = aBuffers.begin();
104 for (char c = 'a'; c <= 'z'; ++c, ++itBuf)
106 const std::vector<char>& rBuf = *itBuf;
107 CPPUNIT_ASSERT_EQUAL(size_t(1048576), rBuf.size()); // 1 MB each.
108 for (char check : rBuf)
109 CPPUNIT_ASSERT_EQUAL(c, check);
113 // TODO : This test currently doesn't fail even when you set
114 // UseBufferedStream to false. Look into this and replace it with a better
115 // test that actually fails when the aforementioned flag is set to false.
116 void PackageTest::testThreadedStreams()
118 class Worker : public comphelper::ThreadTask
120 uno::Reference<io::XInputStream> mxStrm;
121 std::vector<char>& mrBuf;
123 public:
124 Worker(
125 const std::shared_ptr<comphelper::ThreadTaskTag>& pTag,
126 const uno::Reference<io::XInputStream>& xStrm,
127 std::vector<char>& rBuf ) :
128 comphelper::ThreadTask(pTag), mxStrm(xStrm), mrBuf(rBuf) {}
130 virtual void doWork() override
132 sal_Int32 nSize = mxStrm->available();
134 uno::Sequence<sal_Int8> aBytes;
135 while (nSize > 0)
137 sal_Int32 nBytesRead = mxStrm->readBytes(aBytes, 4096);
138 const sal_Int8* p = aBytes.getArray();
139 const sal_Int8* pEnd = p + nBytesRead;
140 std::copy(p, pEnd, std::back_inserter(mrBuf));
141 nSize -= nBytesRead;
147 comphelper::ThreadPool aPool(4);
148 std::shared_ptr<comphelper::ThreadTaskTag> pTag = comphelper::ThreadPool::createThreadTaskTag();
150 std::vector<std::vector<char>> aTestBuffers(26);
151 auto itBuf = aTestBuffers.begin();
153 for (char c = 'a'; c <= 'z'; ++c, ++itBuf)
155 OUString aName = OUStringChar(c) + ".txt";
157 uno::Reference<io::XInputStream> xStrm;
158 mxNA->getByName(aName) >>= xStrm;
160 CPPUNIT_ASSERT(xStrm.is());
161 aPool.pushTask(std::make_unique<Worker>(pTag, xStrm, *itBuf));
164 aPool.waitUntilDone(pTag);
165 verifyStreams( aTestBuffers );
169 void PackageTest::testBufferedThreadedStreams()
171 std::vector<std::vector<char>> aTestBuffers(26);
172 auto itBuf = aTestBuffers.begin();
173 sal_Int32 nReadSize = 0;
175 for (char c = 'a'; c <= 'z'; ++c, ++itBuf)
177 OUString aName = OUStringChar(c) + ".txt";
179 uno::Reference<io::XInputStream> xStrm;
180 //Size of each stream is 1mb (>10000) => XBufferedThreadedStream
181 mxNA->getByName(aName) >>= xStrm;
183 CPPUNIT_ASSERT(xStrm.is());
184 sal_Int32 nSize = xStrm->available();
186 uno::Sequence<sal_Int8> aBytes;
187 //Read chunks of increasing size
188 nReadSize += 1024;
190 while (nSize > 0)
192 sal_Int32 nBytesRead = xStrm->readBytes(aBytes, nReadSize);
193 const sal_Int8* p = aBytes.getArray();
194 const sal_Int8* pEnd = p + nBytesRead;
195 std::copy(p, pEnd, std::back_inserter(*itBuf));
196 nSize -= nBytesRead;
200 verifyStreams( aTestBuffers );
203 CPPUNIT_TEST_SUITE_REGISTRATION(PackageTest);
206 CPPUNIT_PLUGIN_IMPLEMENT();
208 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */