Debian Codegraph @

src/test/RVNGDirectoryStreamTest.cpp

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* librevenge
3 * Version: MPL 2.0 / LGPLv2.1+
4 *
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 *
9 * Alternatively, the contents of this file may be used under the terms
10 * of the GNU Lesser General Public License Version 2.1 or later
11 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
12 * applicable instead of those above.
13 */
14
15 #include <sys/stat.h>
16 #include <cassert>
17 #include <memory>
18 #include <string>
19 #include <vector>
20
21 #include <cppunit/TestFixture.h>
22 #include <cppunit/extensions/HelperMacros.h>
23
24 #include <librevenge-stream/librevenge-stream.h>
25
26 #if !defined RVNG_DIRECTORY_STREAM_TEST_DIR
27 #error RVNG_DIRECTORY_STREAM_TEST_DIR not defined, cannot test
28 #endif
29
30 using librevenge::RVNGDirectoryStream;
31 using librevenge::RVNGInputStream;
32
33 namespace test
34 {
35
36 namespace
37 {
38
39 static const char TEST_DIR[] = RVNG_DIRECTORY_STREAM_TEST_DIR;
40 static const char TEST_FILENAME[] = "RVNGDirectoryStream.h";
41 static const char TEST_NONEXISTENT[] = "foobar";
42
43 #ifndef NDEBUG
44
45 static bool isReg(const char *const path)
46 {
47 struct stat statBuf;
48 if (stat(path, &statBuf))
49 return false;
50 if (S_ISREG(statBuf.st_mode))
51 return true;
52 #ifndef _WIN32
53 else if (S_ISLNK(statBuf.st_mode))
54 {
55 if (lstat(path, &statBuf))
56 return false;
57 if (S_ISREG(statBuf.st_mode))
58 return true;
59 }
60 #endif
61 return false;
62 }
63
64 static bool isDir(const char *const path)
65 {
66 struct stat statBuf;
67 if (stat(path, &statBuf))
68 return false;
69 if (S_ISDIR(statBuf.st_mode))
70 return true;
71 #ifndef _WIN32
72 else if (S_ISLNK(statBuf.st_mode))
73 {
74 if (lstat(path, &statBuf))
75 return false;
76 if (S_ISDIR(statBuf.st_mode))
77 return true;
78 }
79 #endif
80 return false;
81 }
82
83 static bool exists(const char *const path)
84 {
85 struct stat statBuf;
86 if (stat(path, &statBuf))
87 return false;
88 return true;
89 }
90
91 #endif
92
93 }
94
95 class RVNGDirectoryStreamTest : public CPPUNIT_NS::TestFixture
96 {
97 public:
98 RVNGDirectoryStreamTest();
99
100 virtual void setUp();
101 virtual void tearDown();
102
103 private:
104 CPPUNIT_TEST_SUITE(RVNGDirectoryStreamTest);
105 CPPUNIT_TEST(testConstruction);
106 CPPUNIT_TEST(testDetection);
107 CPPUNIT_TEST(testDataOperations);
108 CPPUNIT_TEST(testStructuredOperations);
109 CPPUNIT_TEST_SUITE_END();
110
111 private:
112 void testConstruction();
113 void testDetection();
114 void testDataOperations();
115 void testStructuredOperations();
116
117 private:
118 std::string m_dir;
119 std::string m_file;
120 std::string m_nonexistent;
121 };
122
123 RVNGDirectoryStreamTest::RVNGDirectoryStreamTest()
124 : m_dir(TEST_DIR)
125 , m_file(TEST_DIR)
126 , m_nonexistent(TEST_DIR)
127 {
128 m_file.append("/");
129 m_file.append(TEST_FILENAME);
130 m_nonexistent.append("/");
131 m_nonexistent.append(TEST_NONEXISTENT); // dir
132 m_nonexistent.append("/");
133 m_nonexistent.append(TEST_NONEXISTENT); // file
134 // sanity check
135 assert(isDir(m_dir.c_str()));
136 assert(isReg(m_file.c_str()));
137 assert(!exists(m_nonexistent.c_str()));
138 }
139
140 void RVNGDirectoryStreamTest::setUp()
141 {
142 }
143
144 void RVNGDirectoryStreamTest::tearDown()
145 {
146 }
147
148 void RVNGDirectoryStreamTest::testConstruction()
149 {
150 const std::unique_ptr<RVNGDirectoryStream> dir(RVNGDirectoryStream::createForParent(m_file.c_str()));
151 CPPUNIT_ASSERT(bool(dir));
152 CPPUNIT_ASSERT(dir->isStructured());
153
154 // this should work for dirs too
155 const std::unique_ptr<RVNGDirectoryStream> dir2(RVNGDirectoryStream::createForParent(m_dir.c_str()));
156 CPPUNIT_ASSERT(bool(dir2));
157 CPPUNIT_ASSERT(dir2->isStructured());
158
159 // for nonexistent dirs nothing is created
160 const std::unique_ptr<RVNGDirectoryStream> nondir(RVNGDirectoryStream::createForParent(m_nonexistent.c_str()));
161 CPPUNIT_ASSERT(!nondir);
162
163 // even if try harder, just an empty shell is created
164 RVNGDirectoryStream nondir2(m_nonexistent.c_str());
165 CPPUNIT_ASSERT(!nondir2.isStructured());
166 }
167
168 void RVNGDirectoryStreamTest::testDetection()
169 {
170 CPPUNIT_ASSERT(RVNGDirectoryStream::isDirectory(m_dir.c_str()));
171 CPPUNIT_ASSERT(!RVNGDirectoryStream::isDirectory(m_file.c_str()));
172 CPPUNIT_ASSERT(!RVNGDirectoryStream::isDirectory(m_nonexistent.c_str()));
173 }
174
175 void RVNGDirectoryStreamTest::testDataOperations()
176 {
177 // data operations do not make sense on a directory -> just dummy
178 // impls.
179 RVNGDirectoryStream dir(m_dir.c_str());
180
181 CPPUNIT_ASSERT(dir.isEnd());
182 CPPUNIT_ASSERT_EQUAL(0L, dir.tell());
183 CPPUNIT_ASSERT_EQUAL(-1, dir.seek(0, librevenge::RVNG_SEEK_CUR));
184
185 unsigned long numBytesRead = 0;
186 CPPUNIT_ASSERT(0 == dir.read(1, numBytesRead));
187 CPPUNIT_ASSERT_EQUAL(0UL, numBytesRead);
188 }
189
190 void RVNGDirectoryStreamTest::testStructuredOperations()
191 {
192 {
193 RVNGDirectoryStream dir(m_dir.c_str());
194
195 CPPUNIT_ASSERT(dir.isStructured());
196 std::unique_ptr<RVNGInputStream> substream(dir.getSubStreamByName(TEST_FILENAME));
197 CPPUNIT_ASSERT(substream.get());
198
199 // TODO: test for other operations when they are implemented =)
200 }
201
202 {
203 std::unique_ptr<RVNGDirectoryStream> dir(RVNGDirectoryStream::createForParent(m_file.c_str()));
204
205 CPPUNIT_ASSERT(dir->isStructured());
206 std::unique_ptr<RVNGInputStream> substream(dir->getSubStreamByName(TEST_FILENAME));
207 CPPUNIT_ASSERT(substream.get());
208
209 // TODO: test for other operations when they are implemented =)
210 }
211 }
212
213 CPPUNIT_TEST_SUITE_REGISTRATION(RVNGDirectoryStreamTest);
214
215 }
216
217 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */