| 1 | /* test_path.cc - Test path functionality |
| 2 | part of the minizip-ng project |
| 3 | |
| 4 | Copyright (C) Nathan Moinvaziri |
| 5 | https://github.com/zlib-ng/minizip-ng |
| 6 | |
| 7 | This program is distributed under the terms of the same license as zlib. |
| 8 | See the accompanying LICENSE file for the full text of the license. |
| 9 | */ |
| 10 | |
| 11 | #include "mz.h" |
| 12 | #include "mz_os.h" |
| 13 | |
| 14 | #include <algorithm> |
| 15 | #include <gtest/gtest.h> |
| 16 | |
| 17 | struct resolve_path_param { |
| 18 | const char *path; |
| 19 | const char *expected_path; |
| 20 | |
| 21 | friend std::ostream &operator<<(std::ostream &os, const resolve_path_param ¶m) { |
| 22 | return os << "path: " << param.path; |
| 23 | } |
| 24 | }; |
| 25 | |
| 26 | constexpr resolve_path_param resolve_path_tests[] = { |
| 27 | { "c:\\test\\.", "c:\\test\\"}, |
| 28 | { "c:\\test\\.\\", "c:\\test\\"}, |
| 29 | { "c:\\test\\.\\.", "c:\\test\\"}, |
| 30 | { "c:\\test\\..", "c:\\"}, |
| 31 | { "c:\\test\\..\\", "c:\\"}, |
| 32 | { "c:\\test\\.\\..", "c:\\"}, |
| 33 | { "c:\\test\\.\\\\..", "c:\\"}, |
| 34 | { ".", "."}, |
| 35 | { ".\\", ""}, |
| 36 | { "..", ""}, |
| 37 | { "..\\", ""}, |
| 38 | { ".\\test\\123", "test\\123"}, |
| 39 | { ".\\..\\test\\123", "test\\123"}, |
| 40 | { "..\\..\\test\\123", "test\\123"}, |
| 41 | { "test\\.abc.txt", "test\\.abc.txt"}, |
| 42 | { "c:\\test\\123\\.\\abc.txt", "c:\\test\\123\\abc.txt"}, |
| 43 | { "c:\\test\\123\\..\\abc.txt", "c:\\test\\abc.txt"}, |
| 44 | { "c:\\test\\123\\..\\..\\abc.txt", "c:\\abc.txt"}, |
| 45 | {"c:\\test\\123\\..\\..\\..\\abc.txt", "abc.txt"}, |
| 46 | { "c:\\test\\123\\..\\.\\..\\abc.txt", "c:\\abc.txt"}, |
| 47 | }; |
| 48 | |
| 49 | class path_resolve : public ::testing::TestWithParam<resolve_path_param> {}; |
| 50 | |
| 51 | INSTANTIATE_TEST_SUITE_P(os, path_resolve, testing::ValuesIn(resolve_path_tests)); |
| 52 | |
| 53 | TEST_P(path_resolve, os) { |
| 54 | const auto ¶m = GetParam(); |
| 55 | std::string path = param.path; |
| 56 | std::string expected_path = param.expected_path; |
| 57 | char output[256]; |
| 58 | |
| 59 | memset(output, 'z', sizeof(output)); |
| 60 | // archiving and unarchiving data on a system should preserve its structure |
| 61 | if (!mz_os_is_dir_separator('\\')) { |
| 62 | std::replace(path.begin(), path.end(), '\\', '/'); |
| 63 | std::replace(expected_path.begin(), expected_path.end(), '\\', '/'); |
| 64 | } |
| 65 | mz_path_resolve(path.c_str(), output, sizeof(output)); |
| 66 | EXPECT_STREQ(output, expected_path.c_str()); |
| 67 | } |