Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / oox / qa / unit / vba_compression.cxx
blob9299a3de9eaaa52d4308911a30a4653ada166cfc
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 <cppunit/plugin/TestPlugIn.h>
11 #include <cppunit/extensions/HelperMacros.h>
12 #include <cppunit/TestFixture.h>
13 #include <osl/file.hxx>
14 #include <oox/ole/vbaexport.hxx>
15 #include <rtl/bootstrap.hxx>
16 #include <tools/stream.hxx>
17 #include <unotest/directories.hxx>
18 #include <algorithm>
20 class TestVbaCompression : public CppUnit::TestFixture
22 public:
23 // just a sequence of bytes that should not be compressed at all
24 void testSimple1();
26 // a sequence containing one subsequence that can be compressed
27 void testSimple2();
29 void testSimple3();
31 // real stream from a document
32 void testComplex1();
34 // tests taken from the VBA specification
35 // section 3.2
37 // section 3.2.1
38 void testSpec321();
39 // section 3.2.2
40 void testSpec322();
41 // section 3.2.3
42 void testSpec323();
44 CPPUNIT_TEST_SUITE(TestVbaCompression);
45 CPPUNIT_TEST(testSimple1);
46 CPPUNIT_TEST(testSimple2);
47 CPPUNIT_TEST(testSimple3);
48 CPPUNIT_TEST(testComplex1);
49 CPPUNIT_TEST(testSpec321);
50 CPPUNIT_TEST(testSpec322);
51 CPPUNIT_TEST(testSpec323);
52 CPPUNIT_TEST_SUITE_END();
54 private:
55 static OUString const & getDebugDirUrl() {
56 struct DebugDirUrl {
57 DebugDirUrl() {
58 url = "$UserInstallation/debug/";
59 rtl::Bootstrap::expandMacros(url);
60 //TODO: provide an OUString -> OUString expansion function, and which throws on
61 // failure
62 auto e = osl::Directory::create(url);
63 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, e);
65 OUString url;
67 static DebugDirUrl url;
68 return url.url;
71 test::Directories const m_directories;
74 namespace {
76 void ReadFiles(const OUString& rTestFile, const OUString& rReference,
77 SvMemoryStream& rOutputMemoryStream, SvMemoryStream& rReferenceMemoryStream,
78 const OUString& rDebugPath)
80 SvFileStream aInputStream(rTestFile, StreamMode::READ);
81 SvMemoryStream aInputMemoryStream(4096, 4096);
82 aInputMemoryStream.WriteStream(aInputStream);
84 VBACompression aCompression(rOutputMemoryStream, aInputMemoryStream);
85 aCompression.write();
87 SvFileStream aReferenceStream(rReference, StreamMode::READ);
88 rReferenceMemoryStream.WriteStream(aReferenceStream);
90 rOutputMemoryStream.Seek(0);
91 SvFileStream aDebugStream(rDebugPath, StreamMode::WRITE);
92 aDebugStream.WriteStream(rOutputMemoryStream);
97 void TestVbaCompression::testSimple1()
99 OUString aTestFile = m_directories.getPathFromSrc("/oox/qa/unit/data/vba/simple1.bin");
100 OUString aReference = m_directories.getPathFromSrc("/oox/qa/unit/data/vba/reference/simple1.bin");
102 SvMemoryStream aOutputMemoryStream(4096, 4096);
103 SvMemoryStream aReferenceMemoryStream(4096, 4096);
104 ReadFiles(aTestFile, aReference, aOutputMemoryStream,
105 aReferenceMemoryStream, getDebugDirUrl() + "vba_debug.bin");
107 CPPUNIT_ASSERT_EQUAL(aReferenceMemoryStream.GetSize(), aOutputMemoryStream.GetSize());
109 const sal_uInt8* pReferenceData = static_cast<const sal_uInt8*>( aReferenceMemoryStream.GetData() );
110 const sal_uInt8* pData = static_cast<const sal_uInt8*>( aOutputMemoryStream.GetData() );
112 const sal_uInt64 nSize = std::min(aReferenceMemoryStream.GetSize(),
113 aOutputMemoryStream.GetSize());
114 for (sal_uInt64 i = 0; i < nSize; ++i)
116 CPPUNIT_ASSERT_EQUAL(static_cast<int>(pReferenceData[i]), static_cast<int>(pData[i]));
120 void TestVbaCompression::testSimple2()
122 OUString aTestFile = m_directories.getPathFromSrc("/oox/qa/unit/data/vba/simple2.bin");
123 OUString aReference = m_directories.getPathFromSrc("/oox/qa/unit/data/vba/reference/simple2.bin");
125 SvMemoryStream aOutputMemoryStream(4096, 4096);
126 SvMemoryStream aReferenceMemoryStream(4096, 4096);
127 ReadFiles(aTestFile, aReference, aOutputMemoryStream, aReferenceMemoryStream, getDebugDirUrl() + "vba_debug2.bin");
129 CPPUNIT_ASSERT_EQUAL(aReferenceMemoryStream.GetSize(), aOutputMemoryStream.GetSize());
131 const sal_uInt8* pReferenceData = static_cast<const sal_uInt8*>( aReferenceMemoryStream.GetData() );
132 const sal_uInt8* pData = static_cast<const sal_uInt8*>( aOutputMemoryStream.GetData() );
134 const sal_uInt64 nSize = std::min(aReferenceMemoryStream.GetSize(),
135 aOutputMemoryStream.GetSize());
136 for (sal_uInt64 i = 0; i < nSize; ++i)
138 CPPUNIT_ASSERT_EQUAL(static_cast<int>(pReferenceData[i]), static_cast<int>(pData[i]));
142 void TestVbaCompression::testSimple3()
144 OUString aTestFile = m_directories.getPathFromSrc("/oox/qa/unit/data/vba/simple3.bin");
145 OUString aReference = m_directories.getPathFromSrc("/oox/qa/unit/data/vba/reference/simple3.bin");
147 SvMemoryStream aOutputMemoryStream(4096, 4096);
148 SvMemoryStream aReferenceMemoryStream(4096, 4096);
149 ReadFiles(aTestFile, aReference, aOutputMemoryStream, aReferenceMemoryStream, getDebugDirUrl() + "vba_debug3.bin");
151 CPPUNIT_ASSERT_EQUAL(aReferenceMemoryStream.GetSize(), aOutputMemoryStream.GetSize());
153 const sal_uInt8* pReferenceData = static_cast<const sal_uInt8*>( aReferenceMemoryStream.GetData() );
154 const sal_uInt8* pData = static_cast<const sal_uInt8*>( aOutputMemoryStream.GetData() );
156 const sal_uInt64 nSize = std::min(aReferenceMemoryStream.GetSize(),
157 aOutputMemoryStream.GetSize());
158 for (sal_uInt64 i = 0; i < nSize; ++i)
160 CPPUNIT_ASSERT_EQUAL(static_cast<int>(pReferenceData[i]), static_cast<int>(pData[i]));
164 void TestVbaCompression::testComplex1()
166 OUString aTestFile = m_directories.getPathFromSrc("/oox/qa/unit/data/vba/complex1.bin");
167 OUString aReference = m_directories.getPathFromSrc("/oox/qa/unit/data/vba/reference/complex1.bin");
169 SvMemoryStream aOutputMemoryStream(4096, 4096);
170 SvMemoryStream aReferenceMemoryStream(4096, 4096);
171 ReadFiles(aTestFile, aReference, aOutputMemoryStream, aReferenceMemoryStream, getDebugDirUrl() + "vba_debug_complex1.bin");
173 CPPUNIT_ASSERT_EQUAL(aReferenceMemoryStream.GetSize(), aOutputMemoryStream.GetSize());
175 const sal_uInt8* pReferenceData = static_cast<const sal_uInt8*>( aReferenceMemoryStream.GetData() );
176 const sal_uInt8* pData = static_cast<const sal_uInt8*>( aOutputMemoryStream.GetData() );
178 const sal_uInt64 nSize = std::min(aReferenceMemoryStream.GetSize(),
179 aOutputMemoryStream.GetSize());
180 for (sal_uInt64 i = 0; i < nSize; ++i)
182 CPPUNIT_ASSERT_EQUAL(static_cast<int>(pReferenceData[i]), static_cast<int>(pData[i]));
186 void TestVbaCompression::testSpec321()
188 OUString aTestFile = m_directories.getPathFromSrc("/oox/qa/unit/data/vba/spec321.bin");
189 OUString aReference = m_directories.getPathFromSrc("/oox/qa/unit/data/vba/reference/spec321.bin");
191 SvMemoryStream aOutputMemoryStream(4096, 4096);
192 SvMemoryStream aReferenceMemoryStream(4096, 4096);
193 ReadFiles(aTestFile, aReference, aOutputMemoryStream, aReferenceMemoryStream, getDebugDirUrl() + "vba_debug_spec321.bin");
195 CPPUNIT_ASSERT_EQUAL(aReferenceMemoryStream.GetSize(), aOutputMemoryStream.GetSize());
197 const sal_uInt8* pReferenceData = static_cast<const sal_uInt8*>( aReferenceMemoryStream.GetData() );
198 const sal_uInt8* pData = static_cast<const sal_uInt8*>( aOutputMemoryStream.GetData() );
200 const sal_uInt64 nSize = std::min(aReferenceMemoryStream.GetSize(),
201 aOutputMemoryStream.GetSize());
202 for (sal_uInt64 i = 0; i < nSize; ++i)
204 CPPUNIT_ASSERT_EQUAL(static_cast<int>(pReferenceData[i]), static_cast<int>(pData[i]));
208 void TestVbaCompression::testSpec322()
210 OUString aTestFile = m_directories.getPathFromSrc("/oox/qa/unit/data/vba/spec322.bin");
211 OUString aReference = m_directories.getPathFromSrc("/oox/qa/unit/data/vba/reference/spec322.bin");
213 SvMemoryStream aOutputMemoryStream(4096, 4096);
214 SvMemoryStream aReferenceMemoryStream(4096, 4096);
215 ReadFiles(aTestFile, aReference, aOutputMemoryStream, aReferenceMemoryStream, getDebugDirUrl() + "vba_debug_spec322.bin");
217 CPPUNIT_ASSERT_EQUAL(aReferenceMemoryStream.GetSize(), aOutputMemoryStream.GetSize());
219 const sal_uInt8* pReferenceData = static_cast<const sal_uInt8*>( aReferenceMemoryStream.GetData() );
220 const sal_uInt8* pData = static_cast<const sal_uInt8*>( aOutputMemoryStream.GetData() );
222 const sal_uInt64 nSize = std::min(aReferenceMemoryStream.GetSize(),
223 aOutputMemoryStream.GetSize());
224 for (sal_uInt64 i = 0; i < nSize; ++i)
226 CPPUNIT_ASSERT_EQUAL(static_cast<int>(pReferenceData[i]), static_cast<int>(pData[i]));
230 void TestVbaCompression::testSpec323()
232 OUString aTestFile = m_directories.getPathFromSrc("/oox/qa/unit/data/vba/spec323.bin");
233 OUString aReference = m_directories.getPathFromSrc("/oox/qa/unit/data/vba/reference/spec323.bin");
235 SvMemoryStream aOutputMemoryStream(4096, 4096);
236 SvMemoryStream aReferenceMemoryStream(4096, 4096);
237 ReadFiles(aTestFile, aReference, aOutputMemoryStream, aReferenceMemoryStream, getDebugDirUrl() + "vba_debug_spec323.bin");
239 CPPUNIT_ASSERT_EQUAL(aReferenceMemoryStream.GetSize(), aOutputMemoryStream.GetSize());
241 const sal_uInt8* pReferenceData = static_cast<const sal_uInt8*>( aReferenceMemoryStream.GetData() );
242 const sal_uInt8* pData = static_cast<const sal_uInt8*>( aOutputMemoryStream.GetData() );
244 const sal_uInt64 nSize = std::min(aReferenceMemoryStream.GetSize(),
245 aOutputMemoryStream.GetSize());
246 for (sal_uInt64 i = 0; i < nSize; ++i)
248 CPPUNIT_ASSERT_EQUAL(static_cast<int>(pReferenceData[i]), static_cast<int>(pData[i]));
252 CPPUNIT_TEST_SUITE_REGISTRATION(TestVbaCompression);
254 CPPUNIT_PLUGIN_IMPLEMENT();
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */