From 32061bfe63fce77accf1657a6631700c859fa541 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Thu, 2 Mar 2017 00:27:17 +0300 Subject: Fix some issues in virtfs and update tests. --- src/fs/virtfs/virtfsdir_unittest.cc | 14 ++ src/fs/virtfs/virtfszip.cpp | 43 +++++-- src/fs/virtfs/virtfszip.h | 2 + src/fs/virtfs/virtfszip_unittest.cc | 4 + src/fs/virtfs_unittest.cc | 250 +++++++++++++++++++++++++++++++++++- 5 files changed, 304 insertions(+), 9 deletions(-) (limited to 'src/fs') diff --git a/src/fs/virtfs/virtfsdir_unittest.cc b/src/fs/virtfs/virtfsdir_unittest.cc index ad55acf2e..0a39dbdb4 100644 --- a/src/fs/virtfs/virtfsdir_unittest.cc +++ b/src/fs/virtfs/virtfsdir_unittest.cc @@ -247,6 +247,9 @@ TEST_CASE("VirtFsDir exists") Append_false, SkipError_false); + REQUIRE(VirtFsDir::exists("test") == true); + REQUIRE(VirtFsDir::exists("test/dir1")); + REQUIRE(VirtFsDir::exists("test/dir") == false); REQUIRE(VirtFsDir::exists("test//units.xml") == true); REQUIRE(VirtFsDir::exists("test/\\units123.xml") == false); REQUIRE(VirtFsDir::exists("tesQ/units.xml") == false); @@ -259,6 +262,9 @@ TEST_CASE("VirtFsDir exists") Append_false, SkipError_false); + REQUIRE(VirtFsDir::exists("test") == true); + REQUIRE(VirtFsDir::exists("test/dir1")); + REQUIRE(VirtFsDir::exists("test/dir") == false); REQUIRE(VirtFsDir::exists("test\\units.xml") == true); REQUIRE(VirtFsDir::exists("test/units123.xml") == false); REQUIRE(VirtFsDir::exists("tesQ/units.xml") == false); @@ -267,6 +273,9 @@ TEST_CASE("VirtFsDir exists") VirtFsDir::removeFromSearchPathSilent("data/test"); VirtFsDir::removeFromSearchPathSilent("../data/test"); + REQUIRE(VirtFsDir::exists("test") == true); + REQUIRE(VirtFsDir::exists("test/dir1")); + REQUIRE(VirtFsDir::exists("test/dir") == false); REQUIRE(VirtFsDir::exists("test\\units.xml") == true); REQUIRE(VirtFsDir::exists("test/units123.xml") == false); REQUIRE(VirtFsDir::exists("tesQ/units.xml") == false); @@ -430,6 +439,11 @@ TEST_CASE("VirtFsDir enumerateFiles1") REQUIRE(list->names.size() == cnt2); VirtFsDir::freeList(list); + VirtFsDir::permitLinks(true); + list = VirtFsDir::enumerateFiles("test/units.xml"); + REQUIRE(list->names.size() == 0); + VirtFsDir::freeList(list); + VirtFsDir::permitLinks(false); list = VirtFsDir::enumerateFiles("test\\"); removeTemp(list->names); diff --git a/src/fs/virtfs/virtfszip.cpp b/src/fs/virtfs/virtfszip.cpp index c4ce6f9e8..425883f6d 100644 --- a/src/fs/virtfs/virtfszip.cpp +++ b/src/fs/virtfs/virtfszip.cpp @@ -67,7 +67,34 @@ namespace VirtFsZip entry->mHeaders) { if ((*it2)->fileName == filename) - return *it2;; + return *it2; + } + } + return nullptr; + } + + VirtZipEntry *searchZipEntryByNameWithDir(const std::string &restrict + filename) + { + std::string dirName = filename; + if (findLast(dirName, std::string(dirSeparator)) == false) + dirName += dirSeparator; + FOR_EACH (std::vector::const_iterator, it, mEntries) + { + VirtZipEntry *const entry = *it; + FOR_EACH (std::vector::const_iterator, + it2, + entry->mHeaders) + { + if ((*it2)->fileName == filename) + return entry; + } + FOR_EACH (std::vector::const_iterator, + it2, + entry->mDirs) + { + if (*it2 == dirName) + return entry; } } return nullptr; @@ -246,9 +273,10 @@ namespace VirtFsZip std::string getRealDirInternal(const std::string &filename) { - ZipLocalHeader *restrict const header = searchHeaderByName(filename); - if (header != nullptr) - return header->zipEntry->mArchiveName; + VirtZipEntry *restrict const entry = searchZipEntryByNameWithDir( + filename); + if (entry != nullptr) + return entry->mArchiveName; return std::string(); } @@ -261,10 +289,9 @@ namespace VirtFsZip name.c_str()); return false; } - ZipLocalHeader *restrict const header = searchHeaderByName(name); - if (header != nullptr) - return true; - return false; + VirtZipEntry *restrict const entry = searchZipEntryByNameWithDir( + name); + return entry != nullptr; } VirtList *enumerateFiles(std::string dirName) diff --git a/src/fs/virtfs/virtfszip.h b/src/fs/virtfs/virtfszip.h index c1ed7b05c..3048e9d0d 100644 --- a/src/fs/virtfs/virtfszip.h +++ b/src/fs/virtfs/virtfszip.h @@ -40,6 +40,8 @@ namespace VirtFsZip VirtZipEntry *searchEntryByArchive(const std::string &restrict archiveName); ZipLocalHeader *searchHeaderByName(const std::string &restrict filename); + VirtZipEntry *searchZipEntryByNameWithDir(const std::string &restrict + filename); bool addToSearchPath(std::string newDir, const Append append); bool addToSearchPathSilent(std::string newDir, diff --git a/src/fs/virtfs/virtfszip_unittest.cc b/src/fs/virtfs/virtfszip_unittest.cc index 7ac442c78..0cde2c80f 100644 --- a/src/fs/virtfs/virtfszip_unittest.cc +++ b/src/fs/virtfs/virtfszip_unittest.cc @@ -256,6 +256,7 @@ TEST_CASE("VirtFsZip exists") REQUIRE(VirtFsZip::exists("units1.xml") == false); REQUIRE(VirtFsZip::exists("dir/hide.png") == true); REQUIRE(VirtFsZip::exists("dir/brimmedhat.png") == false); + REQUIRE(VirtFsZip::exists("dir\\1")); VirtFsZip::addToSearchPathSilent("data/test/test.zip", Append_false); @@ -268,6 +269,7 @@ TEST_CASE("VirtFsZip exists") REQUIRE(VirtFsZip::exists("units1.xml") == false); REQUIRE(VirtFsZip::exists("dir/hide.png") == true); REQUIRE(VirtFsZip::exists("dir/brimmedhat.png") == true); + REQUIRE(VirtFsZip::exists("dir\\1")); VirtFsZip::removeFromSearchPathSilent("data/test/test2.zip"); VirtFsZip::removeFromSearchPathSilent("../data/test/test2.zip"); @@ -278,6 +280,7 @@ TEST_CASE("VirtFsZip exists") REQUIRE(VirtFsZip::exists("units1.xml") == false); REQUIRE(VirtFsZip::exists("dir/\\/hide.png") == true); REQUIRE(VirtFsZip::exists("dir/brimmedhat.png") == true); + REQUIRE(VirtFsZip::exists("dir\\1") == false); REQUIRE_THROWS(VirtFsZip::exists("test/../units.xml")); @@ -299,6 +302,7 @@ TEST_CASE("VirtFsZip getRealDir") REQUIRE(VirtFsZip::getRealDir(".") == ""); REQUIRE(VirtFsZip::getRealDir("..") == ""); REQUIRE(VirtFsZip::getRealDir("test.txt") == prefix + "test2.zip"); + REQUIRE(VirtFsZip::getRealDir("dir/1") == prefix + "test2.zip"); REQUIRE(VirtFsZip::getRealDir("dir\\dye.png") == prefix + "test2.zip"); REQUIRE(VirtFsZip::getRealDir("zzz") == ""); diff --git a/src/fs/virtfs_unittest.cc b/src/fs/virtfs_unittest.cc index 5cbc9d237..2e9c1c31d 100644 --- a/src/fs/virtfs_unittest.cc +++ b/src/fs/virtfs_unittest.cc @@ -54,6 +54,9 @@ TEST_CASE("VirtFs exists1") VirtFs::addDirToSearchPath("data", Append_false); VirtFs::addDirToSearchPath("../data", Append_false); + REQUIRE(VirtFs::exists("test") == true); + REQUIRE(VirtFs::exists("test/dir1") == true); + REQUIRE(VirtFs::exists("test/dir") == false); REQUIRE(VirtFs::exists("test/units.xml") == true); REQUIRE(VirtFs::exists("test/units123.xml") == false); REQUIRE(VirtFs::exists("tesQ/units.xml") == false); @@ -62,6 +65,9 @@ TEST_CASE("VirtFs exists1") VirtFs::addDirToSearchPath("data/test", Append_false); VirtFs::addDirToSearchPath("../data/test", Append_false); + REQUIRE(VirtFs::exists("test") == true); + REQUIRE(VirtFs::exists("test/dir1") == true); + REQUIRE(VirtFs::exists("test/dir") == false); REQUIRE(VirtFs::exists("test/units.xml") == true); REQUIRE(VirtFs::exists("test/units123.xml") == false); REQUIRE(VirtFs::exists("tesQ/units.xml") == false); @@ -70,6 +76,9 @@ TEST_CASE("VirtFs exists1") VirtFs::removeDirFromSearchPath("data/test"); VirtFs::removeDirFromSearchPath("../data/test"); + REQUIRE(VirtFs::exists("test") == true); + REQUIRE(VirtFs::exists("test/dir1") == true); + REQUIRE(VirtFs::exists("test/dir") == false); REQUIRE(VirtFs::exists("test/units.xml") == true); REQUIRE(VirtFs::exists("test/units123.xml") == false); REQUIRE(VirtFs::exists("tesQ/units.xml") == false); @@ -86,9 +95,12 @@ TEST_CASE("VirtFs exists2") VirtFs::addZipToSearchPath("data/test/test2.zip", Append_false); VirtFs::addZipToSearchPath("../data/test/test2.zip", Append_false); + REQUIRE(VirtFs::exists("test") == false); REQUIRE(VirtFs::exists("test/units.xml") == false); REQUIRE(VirtFs::exists("test.txt") == true); REQUIRE(VirtFs::exists("dir/hide.png") == true); + REQUIRE(VirtFs::exists("dir/gpl") == true); + REQUIRE(VirtFs::exists("dir/gpl/zzz") == false); REQUIRE(VirtFs::exists("units.xml") == true); REQUIRE(VirtFs::exists("units.xml.") == false); REQUIRE(VirtFs::exists("units.xml2") == false); @@ -98,6 +110,64 @@ TEST_CASE("VirtFs exists2") delete2(logger); } +TEST_CASE("VirtFs exists3") +{ + logger = new Logger(); + VirtFs::addZipToSearchPath("data/test/test.zip", Append_false); + VirtFs::addZipToSearchPath("../data/test/test.zip", Append_false); + VirtFs::addZipToSearchPath("data/test/test2.zip", Append_false); + VirtFs::addZipToSearchPath("../data/test/test2.zip", Append_false); + + REQUIRE(VirtFs::exists("test") == false); + REQUIRE(VirtFs::exists("test/units.xml") == false); + REQUIRE(VirtFs::exists("dir/brimmedhat.png")); + REQUIRE(VirtFs::exists("dir//brimmedhat.png")); + REQUIRE(VirtFs::exists("dir//hide.png")); + REQUIRE(VirtFs::exists("dir/1")); + REQUIRE(VirtFs::exists("dir/gpl")); + REQUIRE(VirtFs::exists("dir/dye.png")); + REQUIRE(VirtFs::exists("dir/2") == false); + REQUIRE(VirtFs::exists("dir2/2") == false); + REQUIRE(VirtFs::exists("dir2/paths.xml")); + + VirtFs::removeZipFromSearchPath("data/test/test.zip"); + VirtFs::removeZipFromSearchPath("../data/test/test.zip"); + VirtFs::removeZipFromSearchPath("data/test/test2.zip"); + VirtFs::removeZipFromSearchPath("../data/test/test2.zip"); + delete2(logger); +} + +TEST_CASE("VirtFs exists4") +{ + logger = new Logger(); + VirtFs::addZipToSearchPath("data/test/test.zip", Append_false); + VirtFs::addZipToSearchPath("../data/test/test.zip", Append_false); + VirtFs::addDirToSearchPath("data/test", Append_false); + VirtFs::addDirToSearchPath("../data/test", Append_false); + + REQUIRE(VirtFs::exists("test") == false); + REQUIRE(VirtFs::exists("test/units.xml") == false); + REQUIRE(VirtFs::exists("dir/brimmedhat.png")); + REQUIRE(VirtFs::exists("dir//brimmedhat.png")); + REQUIRE(VirtFs::exists("dir//hide.png")); + REQUIRE(VirtFs::exists("dir/1") == false); + REQUIRE(VirtFs::exists("dir/gpl") == false); + REQUIRE(VirtFs::exists("dir/dye.png") == false); + REQUIRE(VirtFs::exists("dir/2") == false); + REQUIRE(VirtFs::exists("dir2/2") == false); + REQUIRE(VirtFs::exists("dir2/paths.xml") == false); + REQUIRE(VirtFs::exists("units.xml")); + REQUIRE(VirtFs::exists("dir1/file1.txt")); + REQUIRE(VirtFs::exists("dir2/file2.txt")); + REQUIRE(VirtFs::exists("dir2/file3.txt") == false); + + VirtFs::removeZipFromSearchPath("data/test/test.zip"); + VirtFs::removeZipFromSearchPath("../data/test/test.zip"); + VirtFs::removeDirFromSearchPath("data/test"); + VirtFs::removeDirFromSearchPath("../data/test"); + delete2(logger); +} + static void removeTemp(StringVect &restrict list) { int cnt = 0; @@ -166,6 +236,11 @@ TEST_CASE("VirtFs enumerateFiles1") REQUIRE(list->names.size() == cnt1); VirtFs::freeList(list); + list = VirtFs::enumerateFiles("test/units.xml"); + removeTemp(list->names); + REQUIRE(list->names.size() == 0); + VirtFs::freeList(list); + VirtFs::removeDirFromSearchPath("data"); VirtFs::removeDirFromSearchPath("../data"); delete2(logger); @@ -237,6 +312,35 @@ TEST_CASE("VirtFs enumerateFiles4") delete2(logger); } +TEST_CASE("VirtFs enumerateFiles5") +{ + logger = new Logger; + + VirtFs::addZipToSearchPath("data/test/test2.zip", + Append_false); + VirtFs::addZipToSearchPath("../data/test/test2.zip", + Append_false); + VirtFs::addDirToSearchPath("data/test", Append_false); + VirtFs::addDirToSearchPath("../data/test", Append_false); + + VirtList *list = nullptr; + + list = VirtFs::enumerateFiles("dir2"); + REQUIRE(inList(list, "file1.txt")); + REQUIRE(inList(list, "file2.txt")); + REQUIRE(inList(list, "hide.png")); + REQUIRE(inList(list, "paths.xml")); + REQUIRE(inList(list, "test.txt")); + REQUIRE(inList(list, "units.xml")); + VirtFs::freeList(list); + + VirtFs::removeZipFromSearchPath("data/test/test2.zip"); + VirtFs::removeZipFromSearchPath("../data/test/test2.zip"); + VirtFs::removeDirFromSearchPath("data/test"); + VirtFs::removeDirFromSearchPath("../data/test"); + delete2(logger); +} + TEST_CASE("VirtFs isDirectory1") { logger = new Logger(); @@ -311,6 +415,33 @@ TEST_CASE("VirtFs isDirectory2") VirtFs::removeZipFromSearchPath("data/test/test2.zip"); VirtFs::removeZipFromSearchPath("../data/test/test2.zip"); + delete2(logger); +} + +TEST_CASE("VirtFs isDirectory3") +{ + logger = new Logger(); + VirtFs::addDirToSearchPath("data", Append_false); + VirtFs::addDirToSearchPath("../data", Append_false); + VirtFs::addZipToSearchPath("data/test/test2.zip", Append_false); + VirtFs::addZipToSearchPath("../data/test/test2.zip", Append_false); + + REQUIRE(VirtFs::isDirectory("test/units.xml") == false); + REQUIRE(VirtFs::isDirectory("test")); + REQUIRE(VirtFs::isDirectory("test//dye.png") == false); + REQUIRE(VirtFs::isDirectory("dir")); + REQUIRE(VirtFs::isDirectory("dir/")); + REQUIRE(VirtFs::isDirectory("dir//")); + REQUIRE(VirtFs::isDirectory("dir2")); + REQUIRE(VirtFs::isDirectory("dir3") == false); + REQUIRE(VirtFs::isDirectory("test.txt") == false); + REQUIRE(VirtFs::isDirectory("dir/hide.png") == false); + + VirtFs::removeZipFromSearchPath("data/test/test2.zip"); + VirtFs::removeZipFromSearchPath("../data/test/test2.zip"); + VirtFs::removeDirFromSearchPath("data"); + VirtFs::removeDirFromSearchPath("../data"); + delete2(logger); } TEST_CASE("VirtFs openRead1") @@ -400,6 +531,40 @@ TEST_CASE("VirtFs openRead2") delete2(logger); } +TEST_CASE("VirtFs openRead3") +{ + logger = new Logger(); + VirtFs::addZipToSearchPath("data/test/test2.zip", Append_false); + VirtFs::addZipToSearchPath("../data/test/test2.zip", Append_false); + VirtFs::addDirToSearchPath("data/test", Append_false); + VirtFs::addDirToSearchPath("../data/test", Append_false); + + VirtFile *file = nullptr; + + file = VirtFs::openRead("test/units.xml"); + REQUIRE(file == nullptr); + file = VirtFs::openRead("units.xml"); + REQUIRE(file != nullptr); + VirtFs::close(file); + file = VirtFs::openRead("dir/hide.png"); + REQUIRE(file != nullptr); + VirtFs::close(file); + file = VirtFs::openRead("dir//hide.png"); + REQUIRE(file != nullptr); + VirtFs::close(file); + file = VirtFs::openRead("dir/dye.png"); + REQUIRE(file != nullptr); + VirtFs::close(file); + file = VirtFs::openRead("dir/dye.pn_"); + REQUIRE(file == nullptr); + + VirtFs::removeZipFromSearchPath("data/test/test2.zip"); + VirtFs::removeZipFromSearchPath("../data/test/test2.zip"); + VirtFs::removeDirFromSearchPath("data/test"); + VirtFs::removeDirFromSearchPath("../data/test"); + delete2(logger); +} + TEST_CASE("VirtFs addZipToSearchPath") { // +++ need implement @@ -410,7 +575,7 @@ TEST_CASE("VirtFs removeZipFromSearchPath") // +++ need implement } -TEST_CASE("VirtFs getRealDir") +TEST_CASE("VirtFs getRealDir1") { logger = new Logger(); REQUIRE(VirtFs::getRealDir(".") == ""); @@ -494,6 +659,49 @@ TEST_CASE("VirtFs getRealDir") delete2(logger); } +TEST_CASE("VirtFs getrealDir2") +{ + logger = new Logger(); + VirtFs::addZipToSearchPath("data/test/test2.zip", Append_false); + VirtFs::addZipToSearchPath("../data/test/test2.zip", Append_false); + VirtFs::addDirToSearchPath("data/test", Append_false); + VirtFs::addDirToSearchPath("../data/test", Append_false); + const bool dir1 = VirtFs::addDirToSearchPath("data", Append_false); + REQUIRE((dir1 || VirtFs::addDirToSearchPath("../data", Append_false)) == + true); + + REQUIRE(VirtFs::getRealDir("zzz") == ""); + + if (dir1 == true) + { + REQUIRE(VirtFs::getRealDir("dir1/file1.txt") == + "data/test"); + REQUIRE(VirtFs::getRealDir("hide.png") == "data/test"); + REQUIRE(VirtFs::getRealDir("dir//hide.png") == + "data/test/test2.zip"); + REQUIRE(VirtFs::getRealDir("dir/1//test.txt") == + "data/test/test2.zip"); + } + else + { + REQUIRE(VirtFs::getRealDir("dir1/file1.txt") == + "../data/test"); + REQUIRE(VirtFs::getRealDir("hide.png") == "../data/test"); + REQUIRE(VirtFs::getRealDir("dir//hide.png") == + "../data/test/test2.zip"); + REQUIRE(VirtFs::getRealDir("dir/1//test.txt") == + "../data/test/test2.zip"); + } + + VirtFs::removeZipFromSearchPath("data/test/test2.zip"); + VirtFs::removeZipFromSearchPath("../data/test/test2.zip"); + VirtFs::removeDirFromSearchPath("data/test"); + VirtFs::removeDirFromSearchPath("../data/test"); + VirtFs::removeDirFromSearchPath("data"); + VirtFs::removeDirFromSearchPath("../data"); + delete2(logger); +} + TEST_CASE("VirtFs permitLinks") { logger = new Logger(); @@ -598,3 +806,43 @@ TEST_CASE("VirtFs read2") VirtFs::removeZipFromSearchPath("../data/test/test2.zip"); delete2(logger); } + +TEST_CASE("VirtFs read3") +{ + logger = new Logger(); + VirtFs::addZipToSearchPath("data/test/test2.zip", Append_false); + VirtFs::addZipToSearchPath("../data/test/test2.zip", Append_false); + VirtFs::addDirToSearchPath("data", Append_false); + VirtFs::addDirToSearchPath("../data", Append_false); + + VirtFile *file = VirtFs::openRead("dir2/test.txt"); + REQUIRE(file != nullptr); + REQUIRE(VirtFs::fileLength(file) == 23); + const int fileSize = VirtFs::fileLength(file); + + void *restrict buffer = calloc(fileSize + 1, 1); + REQUIRE(VirtFs::read(file, buffer, 1, fileSize) == fileSize); + REQUIRE(strcmp(static_cast(buffer), + "test line 1\ntest line 2") == 0); + REQUIRE(VirtFs::tell(file) == fileSize); + REQUIRE(VirtFs::eof(file) == true); + + free(buffer); + buffer = calloc(fileSize + 1, 1); + REQUIRE(VirtFs::seek(file, 12) != 0); + REQUIRE(VirtFs::eof(file) == false); + REQUIRE(VirtFs::tell(file) == 12); + REQUIRE(VirtFs::read(file, buffer, 1, 11) == 11); + REQUIRE(strcmp(static_cast(buffer), + "test line 2") == 0); + REQUIRE(VirtFs::eof(file) == true); + + VirtFs::close(file); + free(buffer); + + VirtFs::removeZipFromSearchPath("data/test/test2.zip"); + VirtFs::removeZipFromSearchPath("../data/test/test2.zip"); + VirtFs::removeDirFromSearchPath("data"); + VirtFs::removeDirFromSearchPath("../data"); + delete2(logger); +} -- cgit v1.2.3-70-g09d2