1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
/*
* Copyright (C) 2021 Matthieu Gautier <mgautier@kymeria.fr>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
* NON-INFRINGEMENT. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <zim/zim.h>
#include <zim/writer/creator.h>
#include <zim/writer/item.h>
#include <zim/writer/contentProvider.h>
#include <zim/archive.h>
#include "tools.h"
#include "../src/file_compound.h"
#include "../src/file_reader.h"
#include "../src/direntreader.h"
#include "../src/dirent_accessor.h"
#include "../src/_dirent.h"
#include "../src/fileheader.h"
#include "../src/cluster.h"
#include "../src/rawstreamreader.h"
#include "gtest/gtest.h"
namespace
{
using namespace zim;
struct NoneType {};
const NoneType None;
template<typename T>
struct Optional{
Optional(NoneType none) : active(false) {};
Optional(T value) : active(true), value(value) {};
void check(const T& value) { if (active) { ASSERT_EQ(this->value, value); } }
bool active;
T value;
};
template<>
struct Optional<const std::string> {
Optional(NoneType none) : active(false) {};
Optional(std::string value) : active(true), value(value) {};
Optional(const char* value) : active(true), value(value) {};
void check(const std::string& value) { if (active) { ASSERT_EQ(this->value, value); } }
bool active;
std::string value;
};
void test_article_dirent(
std::shared_ptr<const Dirent> dirent,
Optional<char> ns,
Optional<const std::string> url,
Optional<const std::string> title,
Optional<uint16_t> mimetype,
Optional<cluster_index_t> clusterNumber,
Optional<blob_index_t> blobNumber)
{
ASSERT_TRUE(dirent->isArticle());
ns.check(dirent->getNamespace());
url.check(dirent->getUrl());
title.check(dirent->getTitle());
mimetype.check(dirent->getMimeType());
clusterNumber.check(dirent->getClusterNumber());
blobNumber.check(dirent->getBlobNumber());
}
void test_redirect_dirent(
std::shared_ptr<const Dirent> dirent,
Optional<char> ns,
Optional<const std::string> url,
Optional<const std::string> title,
Optional<entry_index_t> target)
{
ASSERT_TRUE(dirent->isRedirect());
ns.check(dirent->getNamespace());
url.check(dirent->getUrl());
title.check(dirent->getTitle());
target.check(dirent->getRedirectIndex());
}
TEST(ZimCreator, DoNothing)
{
// Creating a creator instance and do nothing on it should not crash.
writer::Creator creator;
}
TEST(ZimCreator, createEmptyZim)
{
unittests::TempFile temp("emptyzimfile");
auto tempPath = temp.path();
zim::Uuid uuid;
// Force special char in the uuid to be sure they are not handled particularly.
uuid.data[5] = '\n';
uuid.data[10] = '\0';
writer::Creator creator;
creator.setUuid(uuid);
creator.startZimCreation(tempPath);
creator.finishZimCreation();
// Do not use the high level Archive to test that zim file is correctly created but lower structure.
auto fileCompound = std::make_shared<FileCompound>(tempPath);
auto reader = std::make_shared<MultiPartFileReader>(fileCompound);
Fileheader header;
header.read(*reader);
ASSERT_FALSE(header.hasMainPage());
ASSERT_EQ(header.getArticleCount(), 2); // counter + titleListIndexesv0
//Read the only one item existing.
auto urlPtrReader = reader->sub_reader(offset_t(header.getUrlPtrPos()), zsize_t(sizeof(offset_t)*header.getArticleCount()));
DirectDirentAccessor direntAccessor(std::make_shared<DirentReader>(reader), std::move(urlPtrReader), entry_index_t(header.getArticleCount()));
std::shared_ptr<const Dirent> dirent;
dirent = direntAccessor.getDirent(entry_index_t(0));
test_article_dirent(dirent, 'M', "Counter", None, 1, cluster_index_t(0), None);
dirent = direntAccessor.getDirent(entry_index_t(1));
test_article_dirent(dirent, 'X', "listing/titleOrdered/v0", None, 0, cluster_index_t(1), None);
auto v0BlobIndex = dirent->getBlobNumber();
auto clusterPtrPos = header.getClusterPtrPos();
auto clusterOffset = offset_t(reader->read_uint<offset_type>(offset_t(clusterPtrPos+8)));
auto cluster = Cluster::read(*reader, clusterOffset);
ASSERT_EQ(cluster->getCompression(), Cluster::Compression::None);
ASSERT_EQ(cluster->count(), blob_index_t(1)); // Only titleListIndexesv0
auto blob = cluster->getBlob(v0BlobIndex);
ASSERT_EQ(blob.size(), 2*sizeof(title_index_t));
}
class TestItem : public writer::Item
{
public:
TestItem(const std::string& path, const std::string& title, const std::string& content):
path(path), title(title), content(content) { }
virtual ~TestItem() = default;
virtual std::string getPath() const { return path; };
virtual std::string getTitle() const { return title; };
virtual std::string getMimeType() const { return "text/html"; };
virtual writer::Hints getHints() const { return { { writer::FRONT_ARTICLE, 1 } }; }
virtual std::unique_ptr<writer::ContentProvider> getContentProvider() const {
return std::unique_ptr<writer::ContentProvider>(new writer::StringProvider(content));
}
std::string path;
std::string title;
std::string content;
};
TEST(ZimCreator, createZim)
{
unittests::TempFile temp("zimfile");
auto tempPath = temp.path();
zim::Uuid uuid;
// Force special char in the uuid to be sure they are not handled particularly.
uuid.data[5] = '\n';
uuid.data[10] = '\0';
writer::Creator creator;
creator.setUuid(uuid);
creator.configIndexing(true, "eng");
creator.startZimCreation(tempPath);
creator.addRedirection("foo", "WrongRedirection", "foobar", {{zim::writer::FRONT_ARTICLE, true}}); // Will be replaced by item
auto item = std::make_shared<TestItem>("foo", "Foo", "FooContent");
EXPECT_NO_THROW(creator.addItem(item));
EXPECT_THROW(creator.addItem(item), std::runtime_error);
// Be sure that title order is not the same that url order
item = std::make_shared<TestItem>("foo2", "AFoo", "Foo2Content");
creator.addItem(item);
creator.addMetadata("Title", "This is a title");
creator.addIllustration(48, "PNGBinaryContent48");
creator.addIllustration(96, "PNGBinaryContent96");
creator.setMainPath("foo");
creator.addRedirection("foo3", "FooRedirection", "foo"); // No a front article.
creator.addRedirection("foo4", "FooRedirection", "NoExistant", {{zim::writer::FRONT_ARTICLE, true}}); // Invalid redirection, must be removed by creator
creator.finishZimCreation();
// Do not use the high level Archive to test that zim file is correctly created but lower structure.
auto fileCompound = std::make_shared<FileCompound>(tempPath);
auto reader = std::make_shared<MultiPartFileReader>(fileCompound);
Fileheader header;
header.read(*reader);
ASSERT_TRUE(header.hasMainPage());
#if defined(ENABLE_XAPIAN)
entry_index_type nb_entry = 12; // counter + 2*illustration + xapiantitleIndex + xapianfulltextIndex + foo + foo2 + foo3 + Title + mainPage + titleListIndexes*2
int xapian_mimetype = 0;
int listing_mimetype = 1;
int png_mimetype = 2;
int html_mimetype = 3;
int plain_mimetype = 4;
int plainutf8_mimetype = 5;
#else
entry_index_type nb_entry = 10; // counter + 2*illustration + foo + foo2 + foo3 + Title + mainPage + titleListIndexes*2
int listing_mimetype = 0;
int png_mimetype = 1;
int html_mimetype = 2;
int plain_mimetype = 3;
int plainutf8_mimetype = 4;
#endif
ASSERT_EQ(header.getArticleCount(), nb_entry);
// Read dirent
auto urlPtrReader = reader->sub_reader(offset_t(header.getUrlPtrPos()), zsize_t(sizeof(offset_t)*header.getArticleCount()));
DirectDirentAccessor direntAccessor(std::make_shared<DirentReader>(reader), std::move(urlPtrReader), entry_index_t(header.getArticleCount()));
std::shared_ptr<const Dirent> dirent;
entry_index_type direntIdx = 0;
dirent = direntAccessor.getDirent(entry_index_t(direntIdx++));
test_article_dirent(dirent, 'C', "foo", "Foo", html_mimetype, cluster_index_t(0), None);
auto fooBlobIndex = dirent->getBlobNumber();
dirent = direntAccessor.getDirent(entry_index_t(direntIdx++));
test_article_dirent(dirent, 'C', "foo2", "AFoo", html_mimetype, cluster_index_t(0), None);
auto foo2BlobIndex = dirent->getBlobNumber();
dirent = direntAccessor.getDirent(entry_index_t(direntIdx++));
test_redirect_dirent(dirent, 'C', "foo3", "FooRedirection", entry_index_t(0));
dirent = direntAccessor.getDirent(entry_index_t(direntIdx++));
test_article_dirent(dirent, 'M', "Counter", None, plain_mimetype, cluster_index_t(0), None);
auto counterBlobIndex = dirent->getBlobNumber();
dirent = direntAccessor.getDirent(entry_index_t(direntIdx++));
test_article_dirent(dirent, 'M', "Illustration_48x48@1", None, png_mimetype, cluster_index_t(1), None);
auto illustration48BlobIndex = dirent->getBlobNumber();
dirent = direntAccessor.getDirent(entry_index_t(direntIdx++));
test_article_dirent(dirent, 'M', "Illustration_96x96@1", None, png_mimetype, cluster_index_t(1), None);
auto illustration96BlobIndex = dirent->getBlobNumber();
dirent = direntAccessor.getDirent(entry_index_t(direntIdx++));
test_article_dirent(dirent, 'M', "Title", "Title", plainutf8_mimetype, cluster_index_t(0), None);
auto titleBlobIndex = dirent->getBlobNumber();
dirent = direntAccessor.getDirent(entry_index_t(direntIdx++));
test_redirect_dirent(dirent, 'W', "mainPage", "mainPage", entry_index_t(0));
#if defined(ENABLE_XAPIAN)
dirent = direntAccessor.getDirent(entry_index_t(direntIdx++));
test_article_dirent(dirent, 'X', "fulltext/xapian", "fulltext/xapian", xapian_mimetype, cluster_index_t(1), None);
#endif
dirent = direntAccessor.getDirent(entry_index_t(direntIdx++));
test_article_dirent(dirent, 'X', "listing/titleOrdered/v0", None, listing_mimetype, cluster_index_t(1), None);
auto v0BlobIndex = dirent->getBlobNumber();
dirent = direntAccessor.getDirent(entry_index_t(direntIdx++));
test_article_dirent(dirent, 'X', "listing/titleOrdered/v1", None, listing_mimetype, cluster_index_t(1), None);
auto v1BlobIndex = dirent->getBlobNumber();
#if defined(ENABLE_XAPIAN)
dirent = direntAccessor.getDirent(entry_index_t(direntIdx++));
test_article_dirent(dirent, 'X', "title/xapian", "title/xapian", xapian_mimetype, cluster_index_t(1), None);
#endif
auto clusterPtrPos = header.getClusterPtrPos();
// Test main content
auto clusterOffset = offset_t(reader->read_uint<offset_type>(offset_t(clusterPtrPos)));
auto cluster = Cluster::read(*reader, clusterOffset);
ASSERT_EQ(cluster->getCompression(), Cluster::Compression::Zstd);
ASSERT_EQ(cluster->count(), blob_index_t(4)); // 4 entries are compressed content
auto blob = cluster->getBlob(fooBlobIndex);
ASSERT_EQ(std::string(blob), "FooContent");
blob = cluster->getBlob(foo2BlobIndex);
ASSERT_EQ(std::string(blob), "Foo2Content");
blob = cluster->getBlob(titleBlobIndex);
ASSERT_EQ(std::string(blob), "This is a title");
blob = cluster->getBlob(counterBlobIndex);
ASSERT_EQ(std::string(blob), "text/html=2");
// Test listing content
clusterOffset = offset_t(reader->read_uint<offset_type>(offset_t(clusterPtrPos + 8)));
cluster = Cluster::read(*reader, clusterOffset);
ASSERT_EQ(cluster->getCompression(), Cluster::Compression::None);
ASSERT_EQ(cluster->count(), blob_index_t(nb_entry-6)); // 6 entries are either compressed or redirections
ASSERT_EQ(header.getTitleIdxPos(), (clusterOffset+cluster->getBlobOffset(v0BlobIndex)).v);
blob = cluster->getBlob(v0BlobIndex);
ASSERT_EQ(blob.size(), nb_entry*sizeof(title_index_t));
std::vector<char> blob0Data(blob.data(), blob.end());
std::vector<char> expectedBlob0Data = {
1, 0, 0, 0,
0, 0, 0, 0,
2, 0, 0, 0,
3, 0, 0, 0,
4, 0, 0, 0,
5, 0, 0, 0,
6, 0, 0, 0,
7, 0, 0, 0,
8, 0, 0, 0,
9, 0, 0, 0
#if defined(ENABLE_XAPIAN)
,10, 0, 0, 0
,11, 0, 0, 0
#endif
};
ASSERT_EQ(blob0Data, expectedBlob0Data);
blob = cluster->getBlob(v1BlobIndex);
ASSERT_EQ(blob.size(), 2*sizeof(title_index_t));
std::vector<char> blob1Data(blob.data(), blob.end());
std::vector<char> expectedBlob1Data = {
1, 0, 0, 0,
0, 0, 0, 0
};
ASSERT_EQ(blob1Data, expectedBlob1Data);
blob = cluster->getBlob(illustration48BlobIndex);
ASSERT_EQ(std::string(blob), "PNGBinaryContent48");
blob = cluster->getBlob(illustration96BlobIndex);
ASSERT_EQ(std::string(blob), "PNGBinaryContent96");
}
TEST(ZimCreator, interruptedZimCreation)
{
unittests::TempFile tmpFile("zimfile");
{
writer::Creator creator;
creator.configClusterSize(16*1024);
creator.startZimCreation(tmpFile.path());
std::ostringstream oss;
for ( size_t i = 0; i < 12345; ++i ) {
oss << i;
}
const std::string content(oss.str());
for ( char c = 'a'; c <= 'z'; ++c ) {
const std::string path(1, c);
creator.addItem(std::make_shared<TestItem>(path, path, content));
}
// creator.finishZimCreation() is not called
}
EXPECT_THROW(
{
const zim::Archive archive(tmpFile.path());
},
zim::ZimFileFormatError
);
}
} // unnamed namespace
|