File: subst_translate-test.c

package info (click to toggle)
subversion 1.8.10-6%2Bdeb8u2~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 62,048 kB
  • sloc: ansic: 795,684; python: 115,859; java: 17,742; sh: 13,597; ruby: 12,397; cpp: 11,206; lisp: 7,540; perl: 5,649; sql: 1,466; makefile: 1,109; xml: 577
file content (521 lines) | stat: -rw-r--r-- 20,065 bytes parent folder | download | duplicates (4)
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
 * subst_translate-test.c -- test the svn_subst_translate* functions
 *
 * ====================================================================
 *    Licensed to the Apache Software Foundation (ASF) under one
 *    or more contributor license agreements.  See the NOTICE file
 *    distributed with this work for additional information
 *    regarding copyright ownership.  The ASF licenses this file
 *    to you under the Apache License, Version 2.0 (the
 *    "License"); you may not use this file except in compliance
 *    with the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing,
 *    software distributed under the License is distributed on an
 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *    KIND, either express or implied.  See the License for the
 *    specific language governing permissions and limitations
 *    under the License.
 * ====================================================================
 */

#include <locale.h>
#include <string.h>
#include <apr_time.h>

#include "../svn_test.h"

#include "svn_types.h"
#include "svn_string.h"
#include "svn_subst.h"
#include "svn_hash.h"

#define ARRAY_LEN(ary) ((sizeof (ary)) / (sizeof ((ary)[0])))

/* Test inputs and expected output for svn_subst_translate_string2(). */
struct translate_string2_data_t
{
  const char *source;
  const char *expected_str;
  svn_boolean_t translated_to_utf8;
  svn_boolean_t translated_line_endings;
};

static svn_error_t *
test_svn_subst_translate_string2(apr_pool_t *pool)
{
  static const struct translate_string2_data_t tests[] =
    {
      /* No reencoding, no translation of line endings */
      { "abcdefz",
        "abcdefz", FALSE, FALSE },
      /* No reencoding, translation of line endings */
      { "     \r\n\r\n      \r\n        \r\n",
        "     \n\n      \n        \n", FALSE, TRUE },
      /* Reencoding, no translation of line endings */
      { "\xc7\xa9\xf4\xdf",
        "\xc3\x87\xc2\xa9\xc3\xb4\xc3\x9f", TRUE, FALSE },
      /* Reencoding, translation of line endings */
      { "\xc7\xa9\xf4\xdf\r\n",
        "\xc3\x87\xc2\xa9\xc3\xb4\xc3\x9f\n", TRUE, TRUE },
      { NULL, NULL, FALSE, FALSE }
    };
  const struct translate_string2_data_t *t;

  for (t = tests; t->source != NULL; t++)
  {
    svn_string_t *source_string = svn_string_create(t->source, pool);
    svn_string_t *new_value = NULL;
    svn_boolean_t translated_line_endings = ! t->translated_line_endings;
    svn_boolean_t translated_to_utf8;

    SVN_ERR(svn_subst_translate_string2(&new_value,
                                        NULL, &translated_line_endings,
                                        source_string, "ISO-8859-1", FALSE,
                                        pool, pool));
    SVN_TEST_STRING_ASSERT(new_value->data, t->expected_str);
    SVN_TEST_ASSERT(translated_line_endings == t->translated_line_endings);

    new_value = NULL;
    translated_to_utf8 = ! t->translated_to_utf8;
    translated_line_endings = ! t->translated_line_endings;
    SVN_ERR(svn_subst_translate_string2(&new_value, &translated_to_utf8,
                                        &translated_line_endings,
                                        source_string, "ISO-8859-1", FALSE,
                                        pool, pool));
    SVN_TEST_STRING_ASSERT(new_value->data, t->expected_str);
    SVN_TEST_ASSERT(translated_to_utf8 == t->translated_to_utf8);
    SVN_TEST_ASSERT(translated_line_endings == t->translated_line_endings);
  }

  /* Test that when REPAIR is FALSE, SVN_ERR_IO_INCONSISTENT_EOL is returned. */
    {
      svn_string_t *source_string = svn_string_create("  \r   \r\n  \n ", pool);
      svn_string_t *new_value = NULL;
      svn_error_t *err = svn_subst_translate_string2(&new_value, NULL, NULL,
                                                     source_string,
                                                     "ISO-8859-1", FALSE, pool,
                                                     pool);
      SVN_TEST_ASSERT_ERROR(err, SVN_ERR_IO_INCONSISTENT_EOL);
    }

  return SVN_NO_ERROR;
}

/* The body of the svn_subst_translate_string2_null_encoding test. It should
   only be called by test_svn_subst_translate_string2_null_encoding(), as this
   code assumes that the process locale has been changed to a locale that uses
   either CP-1252 or ISO-8859-1 for the default narrow string encoding. */
static svn_error_t *
test_svn_subst_translate_string2_null_encoding_helper(apr_pool_t *pool)
{
  {
    svn_string_t *new_value = NULL;
    svn_boolean_t translated_to_utf8 = FALSE;
    svn_boolean_t translated_line_endings = TRUE;
    /* '', which is 0xc6 in both ISO-8859-1 and Windows-1252 */
    svn_string_t *source_string = svn_string_create("\xc6", pool);

    SVN_ERR(svn_subst_translate_string2(&new_value, &translated_to_utf8,
                                        &translated_line_endings,
                                        source_string, NULL, FALSE,
                                        pool, pool));
    SVN_TEST_STRING_ASSERT(new_value->data, "\xc3\x86");
    SVN_TEST_ASSERT(translated_to_utf8);
    SVN_TEST_ASSERT(!translated_line_endings);
  }

  return SVN_NO_ERROR;
}

/* Test that when ENCODING is NULL, the system-default language encoding is used.

   This is a wrapper of test_svn_subst_translate_string2_null_encoding_helper()
   that ensures that the system-default character encoding is set to either
   CP-1252 or ISO-8859-1 before test_svn_subst_translate_string2_null_encoding_helper()
   is called, later restoring the original system-default character encoding. */
static svn_error_t *
test_svn_subst_translate_string2_null_encoding(apr_pool_t *pool)
{
  char orig_lc_all[256] = { '\0' };
  svn_error_t *test_result;

  const char *other_locales[] =
    {
      /* For Windows' msvcrt */
      "English.1252", "German.1252", "French.1252",

      /* For glibc */
      "en_US.ISO-8859-1", "en_GB.ISO-8859-1", "de_DE.ISO-8859-1",

      /* For OpenBSD's libc */
      "en_US.ISO8859-1",  "en_GB.ISO8859-1",  "de_DE.ISO8859-1",

      /* Must be last */
      NULL
    };
  const char **other_locale;

  strncpy(orig_lc_all, setlocale(LC_ALL, NULL), sizeof (orig_lc_all));

  for (other_locale = other_locales; *other_locale != NULL; ++other_locale)
  {
    if (setlocale(LC_ALL, *other_locale))
      break;
  }

  /* If the end of the list of other locales to try has been reached, then none
     of the tested locales are installed, so the test must be skipped. */
  if (*other_locale == NULL)
    return svn_error_createf(SVN_ERR_TEST_SKIPPED, NULL,
                             "Tried %d locales, but none are installed.",
                             (int) (ARRAY_LEN(other_locales) - 1));

  test_result = test_svn_subst_translate_string2_null_encoding_helper(pool);

  /* Restore the original locale for category LC_ALL. */
  SVN_TEST_ASSERT(setlocale(LC_ALL, orig_lc_all) != NULL);

  return test_result;
}

static svn_error_t *
test_repairing_svn_subst_translate_string2(apr_pool_t *pool)
{
    {
      svn_string_t *source_string = svn_string_create("  \r   \r\n  \n ", pool);
      svn_string_t *new_value = NULL;
      SVN_ERR(svn_subst_translate_string2(&new_value, NULL, NULL, source_string,
                                          "ISO-8859-1", TRUE, pool, pool));
      SVN_TEST_ASSERT(new_value != NULL);
      SVN_TEST_ASSERT(new_value->data != NULL);
      SVN_TEST_STRING_ASSERT(new_value->data, "  \n   \n  \n ");
    }

  return SVN_NO_ERROR;
}

/*static svn_error_t *
test_svn_subst_copy_and_translate4(apr_pool_t *pool)
{
  return SVN_NO_ERROR;
}

static svn_error_t *
test_svn_subst_stream_translated(apr_pool_t *pool)
{
  return SVN_NO_ERROR;
}*/

/* Test inputs and expected output for svn_subst_translate_cstring2(). */
struct translate_cstring2_data_t
{
  const char *source;
  const char *eol_str;
  svn_boolean_t repair;
  const char *expected_str;
};

static svn_error_t *
test_svn_subst_translate_cstring2(apr_pool_t *pool)
{
  static const struct translate_cstring2_data_t tests[] =
    {
      /* Test the unusual case where EOL_STR is an empty string. */
      { "   \r   \n\r\n     \n\n\n", "", TRUE,
        "           " },
      /* Test the unusual case where EOL_STR is not a standard EOL string. */
      { "   \r   \n\r\n     \n\n\n", "z", TRUE,
        "   z   zz     zzz" },
      { "    \n    \n ", "buzz", FALSE,
        "    buzz    buzz " },
      { "    \r\n    \n", "buzz", TRUE ,
        "    buzz    buzz"},
      { NULL, NULL, FALSE, NULL }
    };
  const struct translate_cstring2_data_t *t;

  for (t = tests; t->source != NULL; t++)
    {
      const char *result = NULL;
      SVN_ERR(svn_subst_translate_cstring2(t->source, &result, t->eol_str,
                                           t->repair, NULL, FALSE, pool));
      SVN_TEST_STRING_ASSERT(result, t->expected_str);
    }

  return SVN_NO_ERROR;
}

static svn_error_t *
test_svn_subst_build_keywords3(apr_pool_t *pool)
{
  /* Test expansion of custom keywords. */
  struct keywords_tests_data
    {
      const char *keyword_name;
      const char *keywords_string;
      const char *expanded_keyword;
      const char *rev;
      const char *url;
      const char *repos_root_url;
      /* Can't test date since expanded value depends on local clock. */
      const char *author;
    }
  tests[] =
    {
      {"FOO", "FOO=%P%_%a%_%b%_%%",
       "trunk/foo.txt stsp foo.txt %",
       "1234", "http://svn.example.com/repos/trunk/foo.txt",
       "http://svn.example.com/repos", "stsp"},
      {"FOO", "FOO=author%_=%_%a",
       "author = stsp",
       "1234", "http://svn.example.com/repos/trunk/foo.txt",
       "http://svn.example.com/repos", "stsp"},
      {"MyKeyword", "MyKeyword=%r%_%u%_%_%a",
       "4567 http://svn.example.com/svn/branches/myfile  jrandom",
       "4567", "http://svn.example.com/svn/branches/myfile",
       "http://svn.example.com/svn", "jrandom"},
      {"FreeBSD", "FreeBSD=%H",
       "head/README 222812  joel", /* date is not expanded in this test */
       "222812", "http://svn.freebsd.org/base/head/README",
       "http://svn.freebsd.org/base", "joel"},
      {"FreeBSD", "FreeBSD=%I",
       "README 222812  joel", /* date is not expanded in this test */
       "222812", "http://svn.freebsd.org/base/head/README",
       "http://svn.freebsd.org/base", "joel"},
      { NULL, NULL, NULL, NULL, NULL, NULL, NULL}
  };

  const struct keywords_tests_data *t;

  for (t = tests; t->keyword_name != NULL; t++)
    {
      apr_hash_t *kw;
      svn_string_t *expanded_keyword;

      SVN_ERR(svn_subst_build_keywords3(&kw, t->keywords_string,
                                        t->rev, t->url, t->repos_root_url,
                                        0 /* date */, t->author, pool));
      expanded_keyword = svn_hash_gets(kw, t->keyword_name);
      SVN_TEST_ASSERT(expanded_keyword != NULL);
      SVN_TEST_STRING_ASSERT(expanded_keyword->data, t->expanded_keyword);
    }

  return SVN_NO_ERROR;
}

static svn_error_t *
test_svn_subst_truncated_keywords(apr_pool_t *pool)
{
  svn_string_t *src_string
    = svn_string_create("$Qq: "
                        "01234567890123456789012345678901234567890123456789"
                        "01234567890123456789012345678901234567890123456789"
                        "01234567890123456789012345678901234567890123456789"
                        "01234567890123456789012345678901234567890123456789"
                        "012345678901234567890123456789012345678901234567"
                        " $", pool);
  svn_stream_t *src_stream = svn_stream_from_string(src_string, pool);
  svn_stringbuf_t *dst_stringbuf = svn_stringbuf_create_empty(pool);
  svn_stream_t *dst_stream = svn_stream_from_stringbuf(dst_stringbuf, pool);
  apr_hash_t *keywords = apr_hash_make(pool);
  svn_string_t *expanded
    = svn_string_create("01234567890123456789012345678901234567890123456789"
                        "01234567890123456789012345678901234567890123456789"
                        "01234567890123456789012345678901234567890123456789"
                        "01234567890123456789012345678901234567890123456789"
                        "012345678901234567890123456789012345678901234567"
                        "xxxxxxxxxx",
                        pool);

  /* The source is already at the maximum length. */
  SVN_TEST_ASSERT(src_string->len == SVN_KEYWORD_MAX_LEN);

  svn_hash_sets(keywords, "Qq", expanded);
  dst_stream = svn_subst_stream_translated(dst_stream, NULL, FALSE, keywords,
                                           TRUE, pool);
  SVN_ERR(svn_stream_copy3(src_stream, dst_stream, NULL, NULL, pool));

  /* The expanded value would make the keyword longer than the maximum
     allowed so it must be truncated; the remaining part of the
     expanded value is the same as the source. */
  SVN_TEST_STRING_ASSERT(dst_stringbuf->data, src_string->data);

  return SVN_NO_ERROR;
}

static svn_error_t *
test_one_long_keyword(const char *keyword,
                      const char *expected,
                      apr_pool_t *pool)
{
  svn_string_t *src_string;
  svn_stream_t *src_stream, *dst_stream;
  svn_stringbuf_t *dst_stringbuf, *src_stringbuf;
  apr_hash_t *keywords = apr_hash_make(pool);
  svn_string_t *expanded = svn_string_create("abcdefg", pool);

  svn_hash_sets(keywords, keyword, expanded);

  /* Expand */
  src_string = svn_string_createf(pool, "$%s$", keyword);
  src_stream = svn_stream_from_string(src_string, pool);
  dst_stringbuf = svn_stringbuf_create_empty(pool);
  dst_stream = svn_stream_from_stringbuf(dst_stringbuf, pool);
  dst_stream = svn_subst_stream_translated(dst_stream, NULL, FALSE, keywords,
                                           TRUE, pool);
  SVN_ERR(svn_stream_copy3(src_stream, dst_stream, NULL, NULL, pool));

  SVN_TEST_STRING_ASSERT(dst_stringbuf->data, expected);

  /* Unexpand */
  src_stringbuf = dst_stringbuf;
  src_stream = svn_stream_from_stringbuf(src_stringbuf, pool);
  dst_stringbuf = svn_stringbuf_create_empty(pool);
  dst_stream = svn_stream_from_stringbuf(dst_stringbuf, pool);
  dst_stream = svn_subst_stream_translated(dst_stream, NULL, FALSE, keywords,
                                           FALSE, pool);
  SVN_ERR(svn_stream_copy3(src_stream, dst_stream, NULL, NULL, pool));

  SVN_TEST_STRING_ASSERT(dst_stringbuf->data, src_string->data);

  return SVN_NO_ERROR;
}

static svn_error_t *
test_svn_subst_long_keywords(apr_pool_t *pool)
{
  /* The longest keyword that can be expanded to a value: there is
     space for one character in the expanded value. */
  const char keyword_p1[]
    = "Q"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "012345678901234567890123456789012345678901234567";

  /* The longest keyword that can be expanded: the value is empty. */ 
  const char keyword_z[]
    = "Q"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "0123456789012345678901234567890123456789012345678";

  /* One more than the longest keyword that can be expanded. */
  const char keyword_m1[]
    = "Q"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789";

  /* Two more than the longest keyword that can be expanded. */
  const char keyword_m2[]
    = "Q"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "0";

  /* Three more than the longest keyword that can be expanded. */
  const char keyword_m3[]
    = "Q"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "01";

  /* Four more than the longest keyword that can be expanded. */
  const char keyword_m4[]
    = "Q"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "012";

  /* Five more than the longest keyword that can be expanded. */
  const char keyword_m5[]
    = "Q"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "0123";

  /* Six more than the longest keyword that can be expanded. */
  const char keyword_m6[]
    = "Q"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      "01234";

  SVN_ERR(test_one_long_keyword(keyword_p1,
                                apr_psprintf(pool, "$%s: a $", keyword_p1),
                                pool));

  SVN_ERR(test_one_long_keyword(keyword_z,
                                apr_psprintf(pool, "$%s:  $", keyword_z),
                                pool));

  SVN_ERR(test_one_long_keyword(keyword_m1,
                                apr_psprintf(pool, "$%s$", keyword_m1),
                                pool));

  SVN_ERR(test_one_long_keyword(keyword_m2,
                                apr_psprintf(pool, "$%s$", keyword_m2),
                                pool));

  SVN_ERR(test_one_long_keyword(keyword_m3,
                                apr_psprintf(pool, "$%s$", keyword_m3),
                                pool));

  SVN_ERR(test_one_long_keyword(keyword_m4,
                                apr_psprintf(pool, "$%s$", keyword_m4),
                                pool));

  SVN_ERR(test_one_long_keyword(keyword_m5,
                                apr_psprintf(pool, "$%s$", keyword_m5),
                                pool));

  SVN_ERR(test_one_long_keyword(keyword_m6,
                                apr_psprintf(pool, "$%s$", keyword_m6),
                                pool));

  return SVN_NO_ERROR;
}

struct svn_test_descriptor_t test_funcs[] =
  {
    SVN_TEST_NULL,
    SVN_TEST_PASS2(test_svn_subst_translate_string2,
                   "test svn_subst_translate_string2()"),
    SVN_TEST_PASS2(test_svn_subst_translate_string2_null_encoding,
                   "test svn_subst_translate_string2(encoding = NULL)"),
    SVN_TEST_PASS2(test_repairing_svn_subst_translate_string2,
                   "test repairing svn_subst_translate_string2()"),
    SVN_TEST_PASS2(test_svn_subst_translate_cstring2,
                   "test svn_subst_translate_cstring2()"),
    SVN_TEST_PASS2(test_svn_subst_build_keywords3,
                   "test svn_subst_build_keywords3()"),
    SVN_TEST_PASS2(test_svn_subst_truncated_keywords,
                   "test truncated keywords (issue 4349)"),
    SVN_TEST_PASS2(test_svn_subst_long_keywords,
                   "test long keywords (issue 4350)"),
    SVN_TEST_NULL
  };