Avoid potential negative array index access to cached text.
[LibreOffice.git] / writerperfect / qa / unit / DirectoryStreamTest.cxx
blobfd75805f589936012106aa437aec3dcc49390715
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 <memory>
12 #include <cppunit/extensions/HelperMacros.h>
14 #include <comphelper/processfactory.hxx>
16 #include <ucbhelper/content.hxx>
18 #include <test/bootstrapfixture.hxx>
20 #include <DirectoryStream.hxx>
21 #include <com/sun/star/ucb/XContent.hpp>
23 namespace ucb = com::sun::star::ucb;
24 namespace uno = com::sun::star::uno;
26 using std::unique_ptr;
28 using librevenge::RVNGInputStream;
30 using writerperfect::DirectoryStream;
32 namespace
34 class DirectoryStreamTest : public test::BootstrapFixture
36 public:
37 DirectoryStreamTest();
39 public:
40 CPPUNIT_TEST_SUITE(DirectoryStreamTest);
41 CPPUNIT_TEST(testConstruction);
42 CPPUNIT_TEST(testDetection);
43 CPPUNIT_TEST(testDataOperations);
44 CPPUNIT_TEST(testStructuredOperations);
45 CPPUNIT_TEST_SUITE_END();
47 private:
48 void testConstruction();
49 void testDetection();
50 void testDataOperations();
51 void testStructuredOperations();
53 private:
54 uno::Reference<ucb::XContent> m_xDir;
55 uno::Reference<ucb::XContent> m_xFile;
56 uno::Reference<ucb::XContent> m_xNonexistent;
59 constexpr OUStringLiteral g_aDirPath = u"/writerperfect/qa/unit/data/stream/test.dir";
60 constexpr OUStringLiteral g_aNondirPath = u"/writerperfect/qa/unit/data/stream/test.dir/mimetype";
61 constexpr OUStringLiteral g_aNonexistentPath = u"/writerperfect/qa/unit/data/stream/foo/bar";
63 DirectoryStreamTest::DirectoryStreamTest()
65 const uno::Reference<ucb::XCommandEnvironment> xCmdEnv;
66 const uno::Reference<uno::XComponentContext> xContext(comphelper::getProcessComponentContext());
68 using ucbhelper::Content;
70 m_xDir = Content(m_directories.getURLFromSrc(g_aDirPath), xCmdEnv, xContext).get();
71 m_xFile = Content(m_directories.getURLFromSrc(g_aNondirPath), xCmdEnv, xContext).get();
72 m_xNonexistent
73 = Content(m_directories.getURLFromSrc(g_aNonexistentPath), xCmdEnv, xContext).get();
76 void DirectoryStreamTest::testConstruction()
78 const unique_ptr<DirectoryStream> pDir(DirectoryStream::createForParent(m_xFile));
79 CPPUNIT_ASSERT(bool(pDir));
80 CPPUNIT_ASSERT(pDir->isStructured());
82 // this should work for dirs too
83 const unique_ptr<DirectoryStream> pDir2(DirectoryStream::createForParent(m_xDir));
84 CPPUNIT_ASSERT(bool(pDir2));
85 CPPUNIT_ASSERT(pDir2->isStructured());
87 // for nonexistent dirs nothing is created
88 const unique_ptr<DirectoryStream> pNondir(DirectoryStream::createForParent(m_xNonexistent));
89 CPPUNIT_ASSERT(!pNondir);
91 // even if we try harder, just an empty shell is created
92 DirectoryStream aNondir2(m_xNonexistent);
93 CPPUNIT_ASSERT(!aNondir2.isStructured());
96 void DirectoryStreamTest::testDetection()
98 CPPUNIT_ASSERT(DirectoryStream::isDirectory(m_xDir));
99 CPPUNIT_ASSERT(!DirectoryStream::isDirectory(m_xFile));
100 CPPUNIT_ASSERT(!DirectoryStream::isDirectory(m_xNonexistent));
103 void lcl_testDataOperations(RVNGInputStream& rStream)
105 CPPUNIT_ASSERT(rStream.isEnd());
106 CPPUNIT_ASSERT_EQUAL(0L, rStream.tell());
107 CPPUNIT_ASSERT_EQUAL(-1, rStream.seek(0, librevenge::RVNG_SEEK_CUR));
109 unsigned long numBytesRead = 0;
110 CPPUNIT_ASSERT(!rStream.read(1, numBytesRead));
111 CPPUNIT_ASSERT_EQUAL(0UL, numBytesRead);
114 void DirectoryStreamTest::testDataOperations()
116 // data operations do not make sense on a directory -> just dummy
117 // impls.
118 DirectoryStream aDir(m_xDir);
119 lcl_testDataOperations(aDir);
121 // ... and they are equally empty if we try to pass a file
122 DirectoryStream aFile(m_xFile);
123 lcl_testDataOperations(aFile);
126 void lcl_testStructuredOperations(RVNGInputStream& rStream)
128 CPPUNIT_ASSERT(rStream.isStructured());
129 unique_ptr<RVNGInputStream> pSubstream(rStream.getSubStreamByName("mimetype"));
130 CPPUNIT_ASSERT(bool(pSubstream));
132 // TODO: test for other operations when they are implemented =)
135 void DirectoryStreamTest::testStructuredOperations()
137 DirectoryStream aDir(m_xDir);
138 lcl_testStructuredOperations(aDir);
140 unique_ptr<DirectoryStream> pDir(DirectoryStream::createForParent(m_xFile));
141 CPPUNIT_ASSERT(bool(pDir));
142 lcl_testStructuredOperations(*pDir);
145 CPPUNIT_TEST_SUITE_REGISTRATION(DirectoryStreamTest);
148 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */