Avoid potential negative array index access to cached text.
[LibreOffice.git] / sc / qa / unit / filters-test.cxx
blob671f9760d197663ca617b52d88ee83632a865395
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 <sal/config.h>
12 #include "helper/scfiltertestbase.hxx"
14 #include <docsh.hxx>
15 #include <document.hxx>
16 #include <testlotus.hxx>
18 #include <osl/file.hxx>
19 #include <sfx2/docfilt.hxx>
20 #include <sfx2/docfile.hxx>
21 #include <tools/stream.hxx>
22 #include <tools/urlobj.hxx>
23 #include <unotools/tempfile.hxx>
25 using namespace ::com::sun::star;
26 using namespace ::com::sun::star::uno;
28 /* Implementation of Filters test */
30 class ScFiltersTest
31 : public ScFilterTestBase
33 public:
34 ScFiltersTest();
36 virtual bool load( const OUString &rFilter, const OUString &rURL,
37 const OUString &rUserData, SfxFilterFlags nFilterFlags,
38 SotClipboardFormatId nClipboardID, unsigned int nFilterVersion) override;
39 /**
40 * Ensure CVEs remain unbroken
42 void testCVEs();
44 void testContentofz9704();
45 void testTdf90299();
47 CPPUNIT_TEST_SUITE(ScFiltersTest);
48 CPPUNIT_TEST(testCVEs);
49 CPPUNIT_TEST(testContentofz9704);
50 CPPUNIT_TEST(testTdf90299);
52 CPPUNIT_TEST_SUITE_END();
54 private:
55 void createFileURL(std::u16string_view aFileBase, std::u16string_view aFileExtension, OUString& rFilePath);
58 void ScFiltersTest::createFileURL(
59 std::u16string_view aFileBase, std::u16string_view aFileExtension, OUString& rFilePath)
61 // aFileBase may contain multiple segments, so use
62 // GetNewAbsURL instead of insertName for them:
63 INetURLObject url(m_directories.getSrcRootURL());
64 url.setFinalSlash();
65 url.GetNewAbsURL("sc/qa/unit/data", &url);
66 url.insertName(aFileExtension, true);
67 url.GetNewAbsURL(OUString::Concat(aFileBase) + aFileExtension, &url);
68 rFilePath = url.GetMainURL(INetURLObject::DecodeMechanism::NONE);
72 bool ScFiltersTest::load(const OUString &rFilter, const OUString &rURL,
73 const OUString &rUserData, SfxFilterFlags nFilterFlags,
74 SotClipboardFormatId nClipboardID, unsigned int nFilterVersion)
76 ScDocShellRef xDocShRef = loadDoc(rURL, rFilter, rUserData,
77 OUString(), nFilterFlags, nClipboardID, nFilterVersion );
78 bool bLoaded = xDocShRef.is();
79 //reference counting of ScDocShellRef is very confused.
80 if (bLoaded)
81 xDocShRef->DoClose();
82 return bLoaded;
85 void ScFiltersTest::testCVEs()
87 #ifndef DISABLE_CVE_TESTS
88 testDir("Quattro Pro 6.0",
89 m_directories.getURLFromSrc(u"/sc/qa/unit/data/qpro/"));
91 //warning, the current "sylk filter" in sc (docsh.cxx) automatically
92 //chains on failure on trying as csv, rtf, etc. so "success" may
93 //not indicate that it imported as .slk.
94 testDir("SYLK",
95 m_directories.getURLFromSrc(u"/sc/qa/unit/data/slk/"));
97 testDir("MS Excel 97",
98 m_directories.getURLFromSrc(u"/sc/qa/unit/data/xls/"));
100 testDir("Calc Office Open XML",
101 m_directories.getURLFromSrc(u"/sc/qa/unit/data/xlsx/"), OUString(), XLSX_FORMAT_TYPE);
103 testDir("Calc Office Open XML",
104 m_directories.getURLFromSrc(u"/sc/qa/unit/data/xlsm/"), OUString(), XLSX_FORMAT_TYPE);
106 testDir("dBase",
107 m_directories.getURLFromSrc(u"/sc/qa/unit/data/dbf/"));
109 testDir("Lotus",
110 m_directories.getURLFromSrc(u"/sc/qa/unit/data/wks/"));
112 #endif
115 void ScFiltersTest::testContentofz9704()
117 OUString aFileName;
118 createFileURL(u"ofz9704.", u"123", aFileName);
119 SvFileStream aFileStream(aFileName, StreamMode::READ);
120 TestImportWKS(aFileStream);
123 void ScFiltersTest::testTdf90299()
125 const OUString aTmpDirectory1URL = utl::CreateTempURL(nullptr, true);
126 const OUString aTmpDirectory2URL = utl::CreateTempURL(nullptr, true);
127 const OUString aSavedFileURL = utl::CreateTempURL(&aTmpDirectory1URL);
129 OUString aReferencedFileURL;
130 OUString aReferencingFileURL;
131 createFileURL(u"tdf90299.", u"xls", aReferencingFileURL);
133 auto eError = osl::File::copy(aReferencingFileURL, aTmpDirectory1URL + "/tdf90299.xls");
134 CPPUNIT_ASSERT_EQUAL(osl::File::E_None, eError);
136 aReferencingFileURL = aTmpDirectory1URL + "/tdf90299.xls";
137 aReferencedFileURL = aTmpDirectory1URL + "/dummy.xls";
139 ScDocShellRef xShell = loadDoc(aReferencingFileURL, "MS Excel 97", OUString(), OUString(),
140 XLS_FORMAT_TYPE, SotClipboardFormatId::STARCALC_8);
142 ScDocument& rDoc = xShell->GetDocument();
143 CPPUNIT_ASSERT_EQUAL(OUString("='" + aReferencedFileURL + "'#$Sheet1.A1"), rDoc.GetFormula(0, 0, 0));
145 aReferencingFileURL = aSavedFileURL;
147 SfxMedium aStoreMedium(aReferencingFileURL, StreamMode::STD_WRITE);
149 auto pExportFilter = std::make_shared<SfxFilter>(
150 "MS Excel 97", OUString(), XLS_FORMAT_TYPE, SotClipboardFormatId::NONE, OUString(),
151 OUString(), OUString(), "private:factory/scalc*");
152 pExportFilter->SetVersion(SOFFICE_FILEFORMAT_CURRENT);
154 aStoreMedium.SetFilter(pExportFilter);
156 xShell->DoSaveAs(aStoreMedium);
157 xShell->DoClose();
159 eError = osl::File::copy(aReferencingFileURL, aTmpDirectory2URL + "/tdf90299.xls");
160 CPPUNIT_ASSERT_EQUAL(osl::File::E_None, eError);
162 aReferencingFileURL = aTmpDirectory2URL + "/tdf90299.xls";
163 aReferencedFileURL = aTmpDirectory2URL + "/dummy.xls";
165 xShell = loadDoc(aReferencingFileURL, "MS Excel 97", OUString(), OUString(),
166 XLS_FORMAT_TYPE, SotClipboardFormatId::STARCALC_8);
167 ScDocument& rDoc2 = xShell->GetDocument();
168 CPPUNIT_ASSERT_EQUAL(OUString("='" + aReferencedFileURL + "'#$Sheet1.A1"), rDoc2.GetFormula(0, 0, 0));
170 xShell->DoClose();
173 ScFiltersTest::ScFiltersTest()
174 : ScFilterTestBase()
178 CPPUNIT_TEST_SUITE_REGISTRATION(ScFiltersTest);
180 CPPUNIT_PLUGIN_IMPLEMENT();
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */