2014-02-24 14:08:58 +04:00
|
|
|
/*
|
|
|
|
* libcsync -- a library to sync a directory with another
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013 by Klaas Freitag <freitag@owncloud.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2020-09-01 18:49:17 +03:00
|
|
|
#include <cstdio>
|
2013-01-28 23:35:35 +04:00
|
|
|
#include "c_string.h"
|
2017-09-05 17:12:32 +03:00
|
|
|
#include "c_utf8.h"
|
2017-08-30 20:34:41 +03:00
|
|
|
#include "common/filesystembase.h"
|
|
|
|
#include "torture.h"
|
2013-01-28 23:35:35 +04:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
|
2017-09-07 12:02:05 +03:00
|
|
|
#include "torture.h"
|
2015-06-19 13:59:56 +03:00
|
|
|
|
2013-01-30 19:00:49 +04:00
|
|
|
static void check_iconv_to_native_normalization(void **state)
|
|
|
|
{
|
2020-06-05 01:51:32 +03:00
|
|
|
mbchar_t *out = nullptr;
|
2013-01-30 19:00:49 +04:00
|
|
|
const char *in= "\x48\xc3\xa4"; // UTF8
|
|
|
|
#ifdef __APPLE__
|
|
|
|
const char *exp_out = "\x48\x61\xcc\x88"; // UTF-8-MAC
|
|
|
|
#else
|
|
|
|
const char *exp_out = "\x48\xc3\xa4"; // UTF8
|
|
|
|
#endif
|
|
|
|
|
2015-07-03 12:45:58 +03:00
|
|
|
out = c_utf8_path_to_locale(in);
|
2013-01-30 19:00:49 +04:00
|
|
|
assert_string_equal(out, exp_out);
|
|
|
|
|
2013-02-07 14:41:46 +04:00
|
|
|
c_free_locale_string(out);
|
2013-01-30 19:00:49 +04:00
|
|
|
assert_null(out);
|
|
|
|
|
|
|
|
(void) state; /* unused */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void check_iconv_from_native_normalization(void **state)
|
|
|
|
{
|
2013-01-28 23:35:35 +04:00
|
|
|
#ifdef _WIN32
|
2013-01-30 19:00:49 +04:00
|
|
|
const mbchar_t *in = L"\x48\xc3\xa4"; // UTF-8
|
|
|
|
#else
|
|
|
|
#ifdef __APPLE__
|
|
|
|
const mbchar_t *in = "\x48\x61\xcc\x88"; // UTF-8-MAC
|
|
|
|
#else
|
|
|
|
const mbchar_t *in = "\x48\xc3\xa4"; // UTF-8
|
2013-01-28 23:35:35 +04:00
|
|
|
#endif
|
2013-01-30 19:00:49 +04:00
|
|
|
#endif
|
|
|
|
const char *exp_out = "\x48\xc3\xa4"; // UTF-8
|
|
|
|
|
2017-09-05 17:12:32 +03:00
|
|
|
QByteArray out = c_utf8_from_locale(in);
|
2013-01-30 19:00:49 +04:00
|
|
|
assert_string_equal(out, exp_out);
|
|
|
|
|
|
|
|
(void) state; /* unused */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void check_iconv_ascii(void **state)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
const mbchar_t *in = L"abc/ABC\\123"; // UTF-8
|
|
|
|
#else
|
|
|
|
#ifdef __APPLE__
|
|
|
|
const mbchar_t *in = "abc/ABC\\123"; // UTF-8-MAC
|
|
|
|
#else
|
|
|
|
const mbchar_t *in = "abc/ABC\\123"; // UTF-8
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
const char *exp_out = "abc/ABC\\123";
|
|
|
|
|
2017-09-05 17:12:32 +03:00
|
|
|
QByteArray out = c_utf8_from_locale(in);
|
2013-01-30 19:00:49 +04:00
|
|
|
assert_string_equal(out, exp_out);
|
|
|
|
|
|
|
|
(void) state; /* unused */
|
|
|
|
}
|
|
|
|
|
2013-10-02 14:33:10 +04:00
|
|
|
#define TESTSTRING "#cA\\#fߧ4"
|
|
|
|
#define LTESTSTRING L"#cA\\#fߧ4"
|
2013-01-28 23:35:35 +04:00
|
|
|
|
|
|
|
static void check_to_multibyte(void **state)
|
|
|
|
{
|
|
|
|
int rc = -1;
|
|
|
|
|
2020-06-10 04:47:49 +03:00
|
|
|
mbchar_t *mb_string = c_utf8_path_to_locale(TESTSTRING);
|
|
|
|
mbchar_t *mb_null = c_utf8_path_to_locale(nullptr);
|
2013-01-30 19:00:49 +04:00
|
|
|
|
|
|
|
(void) state;
|
|
|
|
|
2013-01-28 23:35:35 +04:00
|
|
|
#ifdef _WIN32
|
2020-06-10 04:47:49 +03:00
|
|
|
assert_int_equal(wcscmp(LTESTSTRING, mb_string), 0);
|
2013-01-28 23:35:35 +04:00
|
|
|
#else
|
|
|
|
assert_string_equal(mb_string, TESTSTRING);
|
|
|
|
#endif
|
2020-06-10 04:47:49 +03:00
|
|
|
assert_false(mb_null);
|
2013-01-28 23:35:35 +04:00
|
|
|
assert_int_equal(rc, -1);
|
2013-03-08 21:49:50 +04:00
|
|
|
|
|
|
|
c_free_locale_string(mb_string);
|
|
|
|
c_free_locale_string(mb_null);
|
2013-01-28 23:35:35 +04:00
|
|
|
}
|
|
|
|
|
2015-06-19 13:59:56 +03:00
|
|
|
static void check_long_win_path(void **state)
|
|
|
|
{
|
2015-06-23 12:40:26 +03:00
|
|
|
(void) state; /* unused */
|
|
|
|
|
2015-09-04 15:00:02 +03:00
|
|
|
{
|
|
|
|
const char *path = "C://DATA/FILES/MUSIC/MY_MUSIC.mp3"; // check a short path
|
2020-06-04 15:09:06 +03:00
|
|
|
const char *exp_path = R"(\\?\C:\\DATA\FILES\MUSIC\MY_MUSIC.mp3)";
|
2017-08-30 20:34:41 +03:00
|
|
|
QByteArray new_short = OCC::FileSystem::pathtoUNC(QByteArray::fromRawData(path, strlen(path)));
|
2015-09-04 15:00:02 +03:00
|
|
|
assert_string_equal(new_short, exp_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2020-06-04 15:09:06 +03:00
|
|
|
const char *path = R"(\\foo\bar/MY_MUSIC.mp3)";
|
|
|
|
const char *exp_path = R"(\\foo\bar\MY_MUSIC.mp3)";
|
2017-08-30 20:34:41 +03:00
|
|
|
QByteArray new_short = OCC::FileSystem::pathtoUNC(QByteArray::fromRawData(path, strlen(path)));
|
2015-09-04 15:00:02 +03:00
|
|
|
assert_string_equal(new_short, exp_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2020-06-04 15:09:06 +03:00
|
|
|
const char *path = R"(//foo\bar/MY_MUSIC.mp3)";
|
|
|
|
const char *exp_path = R"(\\foo\bar\MY_MUSIC.mp3)";
|
2017-08-30 20:34:41 +03:00
|
|
|
QByteArray new_short = OCC::FileSystem::pathtoUNC(QByteArray::fromRawData(path, strlen(path)));
|
2015-09-04 15:00:02 +03:00
|
|
|
assert_string_equal(new_short, exp_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const char *path = "\\foo\\bar";
|
2020-06-04 15:09:06 +03:00
|
|
|
const char *exp_path = R"(\\?\foo\bar)";
|
2017-08-30 20:34:41 +03:00
|
|
|
QByteArray new_short = OCC::FileSystem::pathtoUNC(QByteArray::fromRawData(path, strlen(path)));
|
2015-09-04 15:00:02 +03:00
|
|
|
assert_string_equal(new_short, exp_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const char *path = "/foo/bar";
|
2020-06-04 15:09:06 +03:00
|
|
|
const char *exp_path = R"(\\?\foo\bar)";
|
2017-08-30 20:34:41 +03:00
|
|
|
QByteArray new_short = OCC::FileSystem::pathtoUNC(QByteArray::fromRawData(path, strlen(path)));
|
2015-09-04 15:00:02 +03:00
|
|
|
assert_string_equal(new_short, exp_path);
|
|
|
|
}
|
2015-06-23 12:40:26 +03:00
|
|
|
|
|
|
|
const char *longPath = "D://alonglonglonglong/blonglonglonglong/clonglonglonglong/dlonglonglonglong/"
|
|
|
|
"elonglonglonglong/flonglonglonglong/glonglonglonglong/hlonglonglonglong/ilonglonglonglong/"
|
|
|
|
"jlonglonglonglong/klonglonglonglong/llonglonglonglong/mlonglonglonglong/nlonglonglonglong/"
|
|
|
|
"olonglonglonglong/file.txt";
|
2020-06-04 15:09:06 +03:00
|
|
|
const char *longPathConv = R"(\\?\D:\\alonglonglonglong\blonglonglonglong\clonglonglonglong\dlonglonglonglong\)"
|
|
|
|
R"(elonglonglonglong\flonglonglonglong\glonglonglonglong\hlonglonglonglong\ilonglonglonglong\)"
|
|
|
|
R"(jlonglonglonglong\klonglonglonglong\llonglonglonglong\mlonglonglonglong\nlonglonglonglong\)"
|
|
|
|
R"(olonglonglonglong\file.txt)";
|
2015-06-23 12:40:26 +03:00
|
|
|
|
2017-08-30 20:34:41 +03:00
|
|
|
QByteArray new_long = OCC::FileSystem::pathtoUNC(QByteArray::fromRawData(longPath, strlen(longPath)));
|
2020-06-10 04:47:49 +03:00
|
|
|
// printf("XXXXXXXXXXXX %s %d\n", new_long, mem_reserved);
|
2015-06-23 12:40:26 +03:00
|
|
|
|
|
|
|
assert_string_equal(new_long, longPathConv);
|
|
|
|
|
2020-06-10 04:47:49 +03:00
|
|
|
// printf("YYYYYYYYYYYY %ld\n", strlen(new_long));
|
|
|
|
assert_int_equal(strlen(new_long), 286);
|
2015-06-19 13:59:56 +03:00
|
|
|
}
|
|
|
|
|
2013-01-28 23:35:35 +04:00
|
|
|
int torture_run_tests(void)
|
|
|
|
{
|
2017-08-21 21:05:00 +03:00
|
|
|
const struct CMUnitTest tests[] = {
|
|
|
|
cmocka_unit_test(check_long_win_path),
|
|
|
|
cmocka_unit_test(check_to_multibyte),
|
|
|
|
cmocka_unit_test(check_iconv_ascii),
|
|
|
|
cmocka_unit_test(check_iconv_to_native_normalization),
|
|
|
|
cmocka_unit_test(check_iconv_from_native_normalization),
|
2015-06-19 13:59:56 +03:00
|
|
|
|
2013-01-28 23:35:35 +04:00
|
|
|
};
|
|
|
|
|
2020-06-05 01:51:32 +03:00
|
|
|
return cmocka_run_group_tests(tests, nullptr, nullptr);
|
2013-01-28 23:35:35 +04:00
|
|
|
}
|
|
|
|
|