~ubuntu-branches/ubuntu/raring/gnupg2/raring-proposed

« back to all changes in this revision

Viewing changes to jnlib/t-stringhelp.c

  • Committer: Bazaar Package Importer
  • Author(s): Eric Dorland
  • Date: 2009-09-07 20:38:23 UTC
  • mfrom: (1.1.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20090907203823-d7hsk7lnfqoc4yom
Tags: 2.0.13-1
* New upstream release.
* debian/control: Depend instead of Recommend gnupg-agent. (Closes:
  #538947)

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#include <stdio.h>
22
22
#include <stdlib.h>
23
23
#include <string.h>
 
24
#include <errno.h>
 
25
#ifdef HAVE_PWD_H
 
26
# include <pwd.h>
 
27
#endif
 
28
#include <unistd.h>
 
29
#include <sys/types.h>
24
30
 
25
31
#include "stringhelp.h"
26
32
 
27
33
#include "t-support.h"
28
34
 
29
35
 
 
36
static char *home_buffer;
 
37
 
 
38
 
 
39
const char *
 
40
gethome (void)
 
41
{
 
42
  if (!home_buffer)
 
43
    {
 
44
      char *home = getenv("HOME");
 
45
      
 
46
#if defined(HAVE_GETPWUID) && defined(HAVE_PWD_H)
 
47
      if(home)
 
48
        home_buffer = xstrdup (home);
 
49
      else
 
50
        {
 
51
          struct passwd *pwd;
 
52
          
 
53
          pwd = getpwuid (getuid());
 
54
          if (pwd)
 
55
            home_buffer = xstrdup (pwd->pw_dir);
 
56
        }
 
57
#endif
 
58
    }
 
59
  return home_buffer;
 
60
}
 
61
 
30
62
 
31
63
static void
32
64
test_percent_escape (void)
118
150
}
119
151
 
120
152
 
 
153
static void
 
154
test_strconcat (void)
 
155
{
 
156
  char *out;
 
157
 
 
158
  out = strconcat ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
 
159
                   "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
 
160
                   "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
 
161
                   "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
 
162
                   "1", "2", "3", "4", "5", "6", "7", NULL);
 
163
  if (!out)
 
164
    fail (0);
 
165
  else
 
166
    xfree (out);
 
167
  out = strconcat ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
 
168
                   "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
 
169
                   "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
 
170
                   "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
 
171
                   "1", "2", "3", "4", "5", "6", "7", "8", NULL);
 
172
  if (out)
 
173
    fail (0);
 
174
  else if (errno != EINVAL)
 
175
    fail (0);
 
176
 
 
177
  out = strconcat ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
 
178
                   "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
 
179
                   "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
 
180
                   "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
 
181
                   "1", "2", "3", "4", "5", "6", "7", "8", "9", NULL);
 
182
  if (out)
 
183
    fail (0);
 
184
  else if (errno != EINVAL)
 
185
    fail (0);
 
186
  
 
187
#if __GNUC__ < 4 /* gcc 4.0 has a sentinel attribute.  */
 
188
  out = strconcat (NULL);
 
189
  if (!out || *out)
 
190
    fail (1);
 
191
#endif
 
192
  out = strconcat (NULL, NULL);
 
193
  if (!out || *out)
 
194
    fail (1);
 
195
  out = strconcat ("", NULL);
 
196
  if (!out || *out)
 
197
    fail (1);
 
198
  xfree (out);
 
199
 
 
200
  out = strconcat ("", "", NULL);
 
201
  if (!out || *out)
 
202
    fail (2);
 
203
  xfree (out);
 
204
 
 
205
  out = strconcat ("a", "b", NULL);
 
206
  if (!out || strcmp (out, "ab"))
 
207
    fail (3);
 
208
  xfree (out);
 
209
  out = strconcat ("a", "b", "c", NULL);
 
210
  if (!out || strcmp (out, "abc"))
 
211
    fail (3);
 
212
  xfree (out);
 
213
 
 
214
  out = strconcat ("a", "b", "cc", NULL);
 
215
  if (!out || strcmp (out, "abcc"))
 
216
    fail (4);
 
217
  xfree (out);
 
218
  out = strconcat ("a1", "b1", "c1", NULL);
 
219
  if (!out || strcmp (out, "a1b1c1"))
 
220
    fail (4);
 
221
  xfree (out);
 
222
 
 
223
  out = strconcat ("", " long b ", "", "--even-longer--", NULL);
 
224
  if (!out || strcmp (out, " long b --even-longer--"))
 
225
    fail (5);
 
226
  xfree (out);
 
227
 
 
228
  out = strconcat ("", " long b ", "", "--even-longer--", NULL);
 
229
  if (!out || strcmp (out, " long b --even-longer--"))
 
230
    fail (5);
 
231
  xfree (out);
 
232
}
 
233
 
 
234
static void
 
235
test_xstrconcat (void)
 
236
{
 
237
  char *out;
 
238
 
 
239
  out = xstrconcat ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
 
240
                   "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
 
241
                   "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
 
242
                   "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
 
243
                   "1", "2", "3", "4", "5", "6", "7", NULL);
 
244
  if (!out)
 
245
    fail (0);
 
246
 
 
247
#if __GNUC__ < 4 /* gcc 4.0 has a sentinel attribute.  */
 
248
  out = xstrconcat (NULL);
 
249
  if (!out)
 
250
    fail (1);
 
251
#endif
 
252
  out = xstrconcat (NULL, NULL);
 
253
  if (!out)
 
254
    fail (1);
 
255
  out = xstrconcat ("", NULL);
 
256
  if (!out || *out)
 
257
    fail (1);
 
258
  xfree (out);
 
259
 
 
260
  out = xstrconcat ("", "", NULL);
 
261
  if (!out || *out)
 
262
    fail (2);
 
263
  xfree (out);
 
264
 
 
265
  out = xstrconcat ("a", "b", NULL);
 
266
  if (!out || strcmp (out, "ab"))
 
267
    fail (3);
 
268
  xfree (out);
 
269
  out = xstrconcat ("a", "b", "c", NULL);
 
270
  if (!out || strcmp (out, "abc"))
 
271
    fail (3);
 
272
  xfree (out);
 
273
 
 
274
  out = xstrconcat ("a", "b", "cc", NULL);
 
275
  if (!out || strcmp (out, "abcc"))
 
276
    fail (4);
 
277
  xfree (out);
 
278
  out = xstrconcat ("a1", "b1", "c1", NULL);
 
279
  if (!out || strcmp (out, "a1b1c1"))
 
280
    fail (4);
 
281
  xfree (out);
 
282
 
 
283
  out = xstrconcat ("", " long b ", "", "--even-longer--", NULL);
 
284
  if (!out || strcmp (out, " long b --even-longer--"))
 
285
    fail (5);
 
286
  xfree (out);
 
287
 
 
288
  out = xstrconcat ("", " long b ", "", "--even-longer--", NULL);
 
289
  if (!out || strcmp (out, " long b --even-longer--"))
 
290
    fail (5);
 
291
  xfree (out);
 
292
}
 
293
 
 
294
 
 
295
static void
 
296
test_make_filename_try (void)
 
297
{
 
298
  char *out;
 
299
  const char *home = gethome ();
 
300
  size_t homelen = home? strlen (home):0;
 
301
 
 
302
  out = make_filename_try ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
 
303
                           "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
 
304
                           "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
 
305
                           "1", "2", "3", NULL);
 
306
  if (out)
 
307
    fail (0);
 
308
  else if (errno != EINVAL)
 
309
    fail (0);
 
310
  xfree (out);
 
311
  out = make_filename_try ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
 
312
                           "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
 
313
                           "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
 
314
                           "1", "2", "3", "4", NULL);
 
315
  if (out)
 
316
    fail (0);
 
317
  else if (errno != EINVAL)
 
318
    fail (0);
 
319
  xfree (out);
 
320
 
 
321
  out = make_filename_try ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
 
322
                           "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
 
323
                           "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
 
324
                           "1", "2", NULL);
 
325
  if (!out || strcmp (out, 
 
326
                      "1/2/3/4/5/6/7/8/9/10/" 
 
327
                      "1/2/3/4/5/6/7/8/9/10/" 
 
328
                      "1/2/3/4/5/6/7/8/9/10/" 
 
329
                      "1/2"))
 
330
    fail (0);
 
331
  xfree (out);
 
332
 
 
333
  out = make_filename_try ("foo", "~/bar", "baz/cde", NULL);
 
334
  if (!out || strcmp (out, "foo/~/bar/baz/cde"))
 
335
    fail (1);
 
336
  xfree (out);
 
337
 
 
338
  out = make_filename_try ("foo", "~/bar", "baz/cde/", NULL);
 
339
  if (!out || strcmp (out, "foo/~/bar/baz/cde/"))
 
340
    fail (1);
 
341
  xfree (out);
 
342
 
 
343
  out = make_filename_try ("/foo", "~/bar", "baz/cde/", NULL);
 
344
  if (!out || strcmp (out, "/foo/~/bar/baz/cde/"))
 
345
    fail (1);
 
346
  xfree (out);
 
347
 
 
348
  out = make_filename_try ("//foo", "~/bar", "baz/cde/", NULL);
 
349
  if (!out || strcmp (out, "//foo/~/bar/baz/cde/"))
 
350
    fail (1);
 
351
  xfree (out);
 
352
 
 
353
  out = make_filename_try ("", "~/bar", "baz/cde", NULL);
 
354
  if (!out || strcmp (out, "/~/bar/baz/cde"))
 
355
    fail (1);
 
356
  xfree (out);
 
357
 
 
358
 
 
359
  out = make_filename_try ("~/foo", "bar", NULL);
 
360
  if (!out)
 
361
    fail (2);
 
362
  if (home)
 
363
    {
 
364
      if (strlen (out) < homelen + 7)
 
365
        fail (2);
 
366
      if (strncmp (out, home, homelen))
 
367
        fail (2);
 
368
      if (strcmp (out+homelen, "/foo/bar"))
 
369
        fail (2);
 
370
    }
 
371
  else
 
372
    {
 
373
      if (strcmp (out, "~/foo/bar"))
 
374
        fail (2);
 
375
    }
 
376
  xfree (out);
 
377
 
 
378
  out = make_filename_try ("~", "bar", NULL);
 
379
  if (!out)
 
380
    fail (2);
 
381
  if (home)
 
382
    {
 
383
      if (strlen (out) < homelen + 3)
 
384
        fail (2);
 
385
      if (strncmp (out, home, homelen))
 
386
        fail (2);
 
387
      if (strcmp (out+homelen, "/bar"))
 
388
        fail (2);
 
389
    }
 
390
  else
 
391
    {
 
392
      if (strcmp (out, "~/bar"))
 
393
        fail (2);
 
394
    }
 
395
  xfree (out);
 
396
}
 
397
 
 
398
 
121
399
int
122
400
main (int argc, char **argv)
123
401
{
126
404
 
127
405
  test_percent_escape ();
128
406
  test_compare_filenames ();
 
407
  test_strconcat ();
 
408
  test_xstrconcat ();
 
409
  test_make_filename_try ();
129
410
 
 
411
  xfree (home_buffer);
130
412
  return 0;
131
413
}
132
414