File: check_encoding.c

package info (click to toggle)
owncloud-client 2.2.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 23,144 kB
  • ctags: 5,433
  • sloc: cpp: 35,894; ansic: 7,917; perl: 1,474; python: 217; ruby: 174; makefile: 38; sh: 23
file content (223 lines) | stat: -rw-r--r-- 6,490 bytes parent folder | download | duplicates (2)
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
/*
 * 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
 */
#include "torture.h"
#include <stdio.h>
#include "c_string.h"
#include "c_path.h"

#ifdef _WIN32
#include <string.h>
#endif


static void setup(void **state)
{
    int rc = 0;

    (void) state; /* unused */

#ifdef HAVE_ICONV
#ifdef __APPLE__
    /* this test only works on apple because linux does not know the
     * UTF-8-MAC encoding that we use here. */
    rc = c_setup_iconv("UTF-8-MAC");
#endif
#endif
    assert_int_equal(rc, 0);
}

static void teardown(void **state)
{
    int rc = 0;

    (void) state; /* unused */
#ifdef HAVE_ICONV
    // this must never crash
    rc = c_close_iconv();
#endif
    assert_int_equal(rc, 0);
}

static void check_iconv_to_native_normalization(void **state)
{
    mbchar_t *out = NULL;
    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

    out = c_utf8_path_to_locale(in);
    assert_string_equal(out, exp_out);

    c_free_locale_string(out);
    assert_null(out);

    (void) state; /* unused */
}

static void check_iconv_from_native_normalization(void **state)
{
    char *out = NULL;
#ifdef _WIN32
    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
#endif
#endif
    const char *exp_out = "\x48\xc3\xa4"; // UTF-8

    out = c_utf8_from_locale(in);
    assert_string_equal(out, exp_out);

    c_free_locale_string(out);
    assert_null(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
    char *out = NULL;
    const char *exp_out = "abc/ABC\\123";

    out = c_utf8_from_locale(in);
    assert_string_equal(out, exp_out);

    c_free_locale_string(out);
    assert_null(out);

    (void) state; /* unused */
}

#define TESTSTRING "#cA\\#fß§4"
#define LTESTSTRING L"#cA\\#fß§4"

static void check_to_multibyte(void **state)
{
    int rc = -1;

    mbchar_t *mb_string = c_utf8_path_to_locale( TESTSTRING );
    mbchar_t *mb_null   = c_utf8_path_to_locale( NULL );

    (void) state;

#ifdef _WIN32
    assert_int_equal( wcscmp( LTESTSTRING, mb_string), 0 );
#else
    assert_string_equal(mb_string, TESTSTRING);
#endif
    assert_true( mb_null == NULL );
    assert_int_equal(rc, -1);

    c_free_locale_string(mb_string);
    c_free_locale_string(mb_null);
}

static void check_long_win_path(void **state)
{
    (void) state; /* unused */

    {
        const char *path = "C://DATA/FILES/MUSIC/MY_MUSIC.mp3"; // check a short path
        const char *exp_path = "\\\\?\\C:\\\\DATA\\FILES\\MUSIC\\MY_MUSIC.mp3";
        const char *new_short = c_path_to_UNC(path);
        assert_string_equal(new_short, exp_path);
        SAFE_FREE(new_short);
    }

    {
        const char *path = "\\\\foo\\bar/MY_MUSIC.mp3";
        const char *exp_path = "\\\\foo\\bar\\MY_MUSIC.mp3";
        const char *new_short = c_path_to_UNC(path);
        assert_string_equal(new_short, exp_path);
        SAFE_FREE(new_short);
    }

    {
        const char *path = "//foo\\bar/MY_MUSIC.mp3";
        const char *exp_path = "\\\\foo\\bar\\MY_MUSIC.mp3";
        const char *new_short = c_path_to_UNC(path);
        assert_string_equal(new_short, exp_path);
        SAFE_FREE(new_short);
    }

    {
        const char *path = "\\foo\\bar";
        const char *exp_path = "\\\\?\\foo\\bar";
        const char *new_short = c_path_to_UNC(path);
        assert_string_equal(new_short, exp_path);
        SAFE_FREE(new_short);
    }

    {
        const char *path = "/foo/bar";
        const char *exp_path = "\\\\?\\foo\\bar";
        const char *new_short = c_path_to_UNC(path);
        assert_string_equal(new_short, exp_path);
        SAFE_FREE(new_short);
    }

    const char *longPath = "D://alonglonglonglong/blonglonglonglong/clonglonglonglong/dlonglonglonglong/"
            "elonglonglonglong/flonglonglonglong/glonglonglonglong/hlonglonglonglong/ilonglonglonglong/"
            "jlonglonglonglong/klonglonglonglong/llonglonglonglong/mlonglonglonglong/nlonglonglonglong/"
            "olonglonglonglong/file.txt";
    const char *longPathConv = "\\\\?\\D:\\\\alonglonglonglong\\blonglonglonglong\\clonglonglonglong\\dlonglonglonglong\\"
            "elonglonglonglong\\flonglonglonglong\\glonglonglonglong\\hlonglonglonglong\\ilonglonglonglong\\"
            "jlonglonglonglong\\klonglonglonglong\\llonglonglonglong\\mlonglonglonglong\\nlonglonglonglong\\"
            "olonglonglonglong\\file.txt";

    const char *new_long = c_path_to_UNC(longPath);
    // printf( "XXXXXXXXXXXX %s %d\n", new_long, mem_reserved);

    assert_string_equal(new_long, longPathConv);

    // printf( "YYYYYYYYYYYY %ld\n", strlen(new_long));
    assert_int_equal( strlen(new_long), 286);
    SAFE_FREE(new_long);
}

int torture_run_tests(void)
{
    const UnitTest tests[] = {
        unit_test_setup_teardown(check_long_win_path,                    setup, teardown),
        unit_test_setup_teardown(check_to_multibyte,                    setup, teardown),
        unit_test_setup_teardown(check_iconv_ascii,                     setup, teardown),
        unit_test_setup_teardown(check_iconv_to_native_normalization,   setup, teardown),
        unit_test_setup_teardown(check_iconv_from_native_normalization, setup, teardown),

    };

    return run_tests(tests);
}