Page MenuHome GnuPG

No OneTemporary

diff --git a/common/t-convert.c b/common/t-convert.c
index a03c680ad..ad33dff9b 100644
--- a/common/t-convert.c
+++ b/common/t-convert.c
@@ -1,461 +1,461 @@
/* t-convert.c - Module test for convert.c
* Copyright (C) 2006, 2008 Free Software Foundation, Inc.
*
* This file is part of GnuPG.
*
* GnuPG is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* GnuPG 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "util.h"
#define pass() do { ; } while(0)
#define fail(a) do { fprintf (stderr, "%s:%d: test %d failed\n",\
__FILE__,__LINE__, (a)); \
/*exit (1)*/; \
} while(0)
static void
test_hex2bin (void)
{
static const char *valid[] = {
"00112233445566778899aabbccddeeff11223344",
"00112233445566778899AABBCCDDEEFF11223344",
"00112233445566778899AABBCCDDEEFF11223344 blah",
"00112233445566778899AABBCCDDEEFF11223344\tblah",
"00112233445566778899AABBCCDDEEFF11223344\nblah",
NULL
};
static const char *invalid[] = {
"00112233445566778899aabbccddeeff1122334",
"00112233445566778899AABBCCDDEEFF1122334",
"00112233445566778899AABBCCDDEEFG11223344",
"00 112233445566778899aabbccddeeff11223344",
"00:112233445566778899aabbccddeeff11223344",
":00112233445566778899aabbccddeeff11223344",
"0:0112233445566778899aabbccddeeff11223344",
"00112233445566778899aabbccddeeff11223344:",
"00112233445566778899aabbccddeeff112233445",
"00112233445566778899aabbccddeeff1122334455",
"00112233445566778899aabbccddeeff11223344blah",
NULL
};
static const char *valid2[] = {
"00",
"00 x",
NULL
};
static const char *invalid2[] = {
"",
"0",
"00:",
"00x",
" 00",
NULL
};
unsigned char buffer[20];
int len;
int i;
for (i=0; valid[i]; i++)
{
len = hex2bin (valid[i], buffer, sizeof buffer);
if (len < 0)
fail (i);
if (memcmp (buffer, ("\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa"
"\xbb\xcc\xdd\xee\xff\x11\x22\x33\x44"), 20))
fail (i);
}
if (hex2bin (valid[0], buffer, sizeof buffer) != 40)
fail (0);
if (hex2bin (valid[2], buffer, sizeof buffer) != 41)
fail (0);
for (i=0; invalid[i]; i++)
{
len = hex2bin (invalid[i], buffer, sizeof buffer);
if (!(len < 0))
fail (i);
}
for (i=0; valid2[i]; i++)
{
len = hex2bin (valid2[i], buffer, 1);
if (len < 0)
fail (i);
if (memcmp (buffer, "\x00", 1))
fail (i);
}
if (hex2bin (valid2[0], buffer, 1) != 2)
fail (0);
if (hex2bin (valid2[1], buffer, 1) != 3)
fail (0);
for (i=0; invalid2[i]; i++)
{
len = hex2bin (invalid2[i], buffer, 1);
if (!(len < 0))
fail (i);
}
}
static void
test_hexcolon2bin (void)
{
static const char *valid[] = {
"00112233445566778899aabbccddeeff11223344",
"00112233445566778899AABBCCDDEEFF11223344",
"00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:11:22:33:44",
"00112233445566778899AABBCCDDEEFF11223344 blah",
"00112233445566778899AABBCCDDEEFF11223344\tblah",
"00112233445566778899AABBCCDDEEFF11223344\nblah",
NULL
};
static const char *invalid[] = {
"00112233445566778899aabbccddeeff1122334",
"00112233445566778899AABBCCDDEEFF1122334",
"00112233445566778899AABBCCDDEEFG11223344",
":00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:11:22:33:44",
"00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:11:22:33:44:",
"00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:11:22:3344",
"00:1122:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:11:22:33:44",
"0011:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:11:22:33:44",
"00 11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:11:22:33:44",
"00:11 22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:11:22:33:44",
"00112233445566778899aabbccddeeff112233445",
"00112233445566778899aabbccddeeff1122334455",
"00112233445566778899aabbccddeeff11223344blah",
NULL
};
static const char *valid2[] = {
"00",
"00 x",
NULL
};
static const char *invalid2[] = {
"",
"0",
"00:",
":00",
"0:0",
"00x",
" 00",
NULL
};
unsigned char buffer[20];
int len;
int i;
for (i=0; valid[i]; i++)
{
len = hexcolon2bin (valid[i], buffer, sizeof buffer);
if (len < 0)
fail (i);
if (memcmp (buffer, ("\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa"
"\xbb\xcc\xdd\xee\xff\x11\x22\x33\x44"), 20))
fail (i);
}
if (hexcolon2bin (valid[0], buffer, sizeof buffer) != 40)
fail (0);
if (hexcolon2bin (valid[3], buffer, sizeof buffer) != 41)
fail (0);
for (i=0; invalid[i]; i++)
{
len = hexcolon2bin (invalid[i], buffer, sizeof buffer);
if (!(len < 0))
fail (i);
}
for (i=0; valid2[i]; i++)
{
len = hexcolon2bin (valid2[i], buffer, 1);
if (len < 0)
fail (i);
if (memcmp (buffer, "\x00", 1))
fail (i);
}
if (hexcolon2bin (valid2[0], buffer, 1) != 2)
fail (0);
if (hexcolon2bin (valid2[1], buffer, 1) != 3)
fail (0);
for (i=0; invalid2[i]; i++)
{
len = hexcolon2bin (invalid2[i], buffer, 1);
if (!(len < 0))
fail (i);
}
}
static void
test_bin2hex (void)
{
char stuff[20+1] = ("\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa"
"\xbb\xcc\xdd\xee\xff\x01\x10\x02\xa3");
char hexstuff[] = "00112233445566778899AABBCCDDEEFF011002A3";
char buffer[2*20+1];
char *p;
p = bin2hex (stuff, 20, buffer);
if (!p)
fail (0);
if (p != buffer)
fail (0);
if (strcmp (buffer, hexstuff))
fail (0);
p = bin2hex (stuff, 20, NULL);
if (!p)
fail (0);
- if (strcmp (p, hexstuff))
+ else if (strcmp (p, hexstuff))
fail (0);
p = bin2hex (stuff, (size_t)(-1), NULL);
if (p)
fail (0);
- if (errno != ENOMEM)
+ else if (errno != ENOMEM)
fail (1);
}
static void
test_bin2hexcolon (void)
{
char stuff[20+1] = ("\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa"
"\xbb\xcc\xdd\xee\xff\x01\x10\x02\xa3");
char hexstuff[] = ("00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD:EE:FF"
":01:10:02:A3");
char buffer[3*20+1];
char *p;
p = bin2hexcolon (stuff, 20, buffer);
if (!p)
fail (0);
if (p != buffer)
fail (0);
if (strcmp (buffer, hexstuff))
fail (0);
p = bin2hexcolon (stuff, 20, NULL);
if (!p)
fail (0);
- if (strcmp (p, hexstuff))
+ else if (strcmp (p, hexstuff))
fail (0);
p = bin2hexcolon (stuff, (size_t)(-1), NULL);
if (p)
fail (0);
- if (errno != ENOMEM)
+ else if (errno != ENOMEM)
fail (1);
}
static void
test_hex2str (void)
{
static struct {
const char *hex;
const char *str;
int len; /* Length of STR. This may included embedded nuls. */
int off;
int no_alloc_test;
} tests[] = {
/* Simple tests. */
{ "112233445566778899aabbccddeeff1122",
"\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
17, 34 },
{ "112233445566778899aabbccddeeff1122 blah",
"\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
17, 34 },
{ "112233445566778899aabbccddeeff1122\tblah",
"\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
17, 34 },
{ "112233445566778899aabbccddeeff1122\nblah",
"\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
17, 34 },
/* Valid tests yielding an empty string. */
{ "00",
"",
1, 2 },
{ "00 x",
"",
1, 2 },
{ "",
"",
0, 0 },
{ " ",
"",
0, 0 },
/* Test trailing Nul feature. */
{ "112233445566778899aabbccddeeff1100",
"\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x00",
17, 34 },
{ "112233445566778899aabbccddeeff1100 ",
"\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x00",
17, 34 },
/* Test buffer size. (buffer is of length 20) */
{ "6162636465666768696A6b6c6D6e6f70717273",
"abcdefghijklmnopqrs",
19, 38 },
{ "6162636465666768696A6b6c6D6e6f7071727300",
"abcdefghijklmnopqrs",
20, 40 },
{ "6162636465666768696A6b6c6D6e6f7071727374",
NULL,
0, 0, 1 },
{ "6162636465666768696A6b6c6D6e6f707172737400",
NULL,
0, 0, 1 },
{ "6162636465666768696A6b6c6D6e6f707172737475",
NULL,
0, 0, 1 },
/* Invalid tests. */
{ "112233445566778899aabbccddeeff1122334", NULL, 0, 0 },
{ "112233445566778899AABBCCDDEEFF1122334", NULL, 0, 0 },
{ "112233445566778899AABBCCDDEEFG11223344", NULL, 0, 0 },
{ "0:0112233445566778899aabbccddeeff11223344", NULL, 0, 0 },
{ "112233445566778899aabbccddeeff11223344:", NULL, 0, 0 },
{ "112233445566778899aabbccddeeff112233445", NULL, 0, 0 },
{ "112233445566778899aabbccddeeff1122334455", NULL, 0, 0, 1 },
{ "112233445566778899aabbccddeeff11223344blah", NULL, 0, 0 },
{ "0", NULL, 0, 0 },
{ "00:", NULL, 0, 0 },
{ "00x", NULL, 0, 0 },
{ NULL, NULL, 0, 0 }
};
int idx;
char buffer[20];
const char *tail;
size_t count;
char *result;
for (idx=0; tests[idx].hex; idx++)
{
tail = hex2str (tests[idx].hex, buffer, sizeof buffer, &count);
if (tests[idx].str)
{
/* Good case test. */
if (!tail)
fail (idx);
else if (strcmp (tests[idx].str, buffer))
fail (idx);
else if (tail - tests[idx].hex != tests[idx].off)
fail (idx);
else if (tests[idx].len != count)
fail (idx);
}
else
{
/* Bad case test. */
if (tail)
fail (idx);
}
}
/* Same tests again using in-place conversion. */
for (idx=0; tests[idx].hex; idx++)
{
char tmpbuf[100];
assert (strlen (tests[idx].hex)+1 < sizeof tmpbuf);
strcpy (tmpbuf, tests[idx].hex);
/* Note: we still need to use 20 as buffer length because our
tests assume that. */
tail = hex2str (tmpbuf, tmpbuf, 20, &count);
if (tests[idx].str)
{
/* Good case test. */
if (!tail)
fail (idx);
else if (strcmp (tests[idx].str, tmpbuf))
fail (idx);
else if (tail - tmpbuf != tests[idx].off)
fail (idx);
else if (tests[idx].len != count)
fail (idx);
}
else
{
/* Bad case test. */
if (tail)
fail (idx);
if (strcmp (tmpbuf, tests[idx].hex))
fail (idx); /* Buffer was modified. */
}
}
/* Test the allocation variant. */
for (idx=0; tests[idx].hex; idx++)
{
if (tests[idx].no_alloc_test)
continue;
result = hex2str_alloc (tests[idx].hex, &count);
if (tests[idx].str)
{
/* Good case test. */
if (!result)
fail (idx);
else if (strcmp (tests[idx].str, result))
fail (idx);
else if (count != tests[idx].off)
fail (idx);
}
else
{
/* Bad case test. */
if (result)
fail (idx);
}
xfree (result);
}
}
int
main (int argc, char **argv)
{
(void)argc;
(void)argv;
test_hex2bin ();
test_hexcolon2bin ();
test_bin2hex ();
test_bin2hexcolon ();
test_hex2str ();
return 0;
}
diff --git a/common/t-mapstrings.c b/common/t-mapstrings.c
index 88c6674bf..8f4c6507b 100644
--- a/common/t-mapstrings.c
+++ b/common/t-mapstrings.c
@@ -1,100 +1,100 @@
/* t-mapstrings.c - Regression tests for mapstrings.c
* Copyright (C) 2014 Werner Koch
*
* This file is part of GnuPG.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either
*
* - the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* or
*
* - the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* or both in parallel, as here.
*
* This file 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "t-support.h"
#include "stringhelp.h"
static void
test_map_static_macro_string (void)
{
static struct {
const char *string;
const char *expected;
const char *lastresult;
} tests[] = {
{ "@GPG@ (@GNUPG@)",
GPG_NAME " (" GNUPG_NAME ")" },
{ "@GPG@(@GNUPG@)",
GPG_NAME "(" GNUPG_NAME ")" },
{ "@GPG@@GNUPG@",
GPG_NAME GNUPG_NAME },
{ " @GPG@@GNUPG@",
" " GPG_NAME GNUPG_NAME },
{ " @GPG@@GNUPG@ ",
" " GPG_NAME GNUPG_NAME " " },
{ " @GPG@GNUPG@ ",
" " GPG_NAME "GNUPG@ " },
{ " @ GPG@GNUPG@ ",
" @ GPG" GNUPG_NAME " " },
{ "--@GPGTAR@",
"--" GPGTAR_NAME }
};
int testno;
const char *result;
for (testno=0; testno < DIM(tests); testno++)
{
result = map_static_macro_string (tests[testno].string);
if (!result)
fail (testno);
- if (strcmp (result, tests[testno].expected))
+ else if (strcmp (result, tests[testno].expected))
fail (testno);
if (!tests[testno].lastresult)
tests[testno].lastresult = result;
}
/* A second time to check that the same string is been returned. */
for (testno=0; testno < DIM(tests); testno++)
{
result = map_static_macro_string (tests[testno].string);
if (!result)
fail (testno);
- if (strcmp (result, tests[testno].expected))
+ else if (strcmp (result, tests[testno].expected))
fail (testno);
if (result != tests[testno].lastresult)
fail (testno);
}
}
int
main (int argc, char **argv)
{
(void)argc;
(void)argv;
test_map_static_macro_string ();
return 0;
}
diff --git a/common/t-stringhelp.c b/common/t-stringhelp.c
index e97b64a6e..af79cb5cd 100644
--- a/common/t-stringhelp.c
+++ b/common/t-stringhelp.c
@@ -1,834 +1,834 @@
/* t-stringhelp.c - Regression tests for stringhelp.c
* Copyright (C) 2007 Free Software Foundation, Inc.
* 2015 g10 Code GmbH
*
* This file is part of GnuPG.
*
* GnuPG is free software; you can redistribute it and/or modify it
* under the terms of either
*
* - the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* or
*
* - the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* or both in parallel, as here.
*
* GnuPG 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
* General Public License for more details.
*
* You should have received a copies of the GNU General Public License
* and the GNU Lesser General Public License along with this program;
* if not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#ifdef HAVE_PWD_H
# include <pwd.h>
#endif
#include <unistd.h>
#include <sys/types.h>
#include "t-support.h"
#include "stringhelp.h"
static char *home_buffer;
const char *
gethome (void)
{
if (!home_buffer)
{
char *home = getenv("HOME");
if(home)
home_buffer = xstrdup (home);
#if defined(HAVE_GETPWUID) && defined(HAVE_PWD_H)
else
{
struct passwd *pwd;
pwd = getpwuid (getuid());
if (pwd)
home_buffer = xstrdup (pwd->pw_dir);
}
#endif
}
return home_buffer;
}
static char *
mygetcwd (void)
{
char *buffer;
size_t size = 100;
for (;;)
{
buffer = xmalloc (size+1);
#ifdef HAVE_W32CE_SYSTEM
strcpy (buffer, "/"); /* Always "/". */
return buffer;
#else
if (getcwd (buffer, size) == buffer)
return buffer;
xfree (buffer);
if (errno != ERANGE)
{
fprintf (stderr,"error getting current cwd: %s\n",
strerror (errno));
exit (2);
}
size *= 2;
#endif
}
}
static void
test_percent_escape (void)
{
char *result;
static struct {
const char *extra;
const char *value;
const char *expected;
} tests[] =
{
{ NULL, "", "" },
{ NULL, "%", "%25" },
{ NULL, "%%", "%25%25" },
{ NULL, " %", " %25" },
{ NULL, ":", "%3a" },
{ NULL, " :", " %3a" },
{ NULL, ": ", "%3a " },
{ NULL, " : ", " %3a " },
{ NULL, "::", "%3a%3a" },
{ NULL, ": :", "%3a %3a" },
{ NULL, "%:", "%25%3a" },
{ NULL, ":%", "%3a%25" },
{ "\\\n:", ":%", "%3a%25" },
{ "\\\n:", "\\:%", "%5c%3a%25" },
{ "\\\n:", "\n:%", "%0a%3a%25" },
{ "\\\n:", "\xff:%", "\xff%3a%25" },
{ "\\\n:", "\xfe:%", "\xfe%3a%25" },
{ "\\\n:", "\x01:%", "\x01%3a%25" },
{ "\x01", "\x01:%", "%01%3a%25" },
{ "\xfe", "\xfe:%", "%fe%3a%25" },
{ "\xfe", "\xff:%", "\xff%3a%25" },
{ NULL, NULL, NULL }
};
int testno;
result = percent_escape (NULL, NULL);
if (result)
fail (0);
for (testno=0; tests[testno].value; testno++)
{
result = percent_escape (tests[testno].value, tests[testno].extra);
if (!result)
fail (testno);
- if (strcmp (result, tests[testno].expected))
+ else if (strcmp (result, tests[testno].expected))
fail (testno);
xfree (result);
}
}
static void
test_compare_filenames (void)
{
struct {
const char *a;
const char *b;
int result;
} tests[] = {
{ "", "", 0 },
{ "", "a", -1 },
{ "a", "", 1 },
{ "a", "a", 0 },
{ "a", "aa", -1 },
{ "aa", "a", 1 },
{ "a", "b", -1 },
#ifdef HAVE_W32_SYSTEM
{ "a", "A", 0 },
{ "A", "a", 0 },
{ "foo/bar", "foo\\bar", 0 },
{ "foo\\bar", "foo/bar", 0 },
{ "foo\\", "foo/", 0 },
{ "foo/", "foo\\", 0 },
#endif /*HAVE_W32_SYSTEM*/
{ NULL, NULL, 0}
};
int testno, result;
for (testno=0; tests[testno].a; testno++)
{
result = compare_filenames (tests[testno].a, tests[testno].b);
result = result < 0? -1 : result > 0? 1 : 0;
if (result != tests[testno].result)
fail (testno);
}
}
static void
test_strconcat (void)
{
char *out;
out = strconcat ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"1", "2", "3", "4", "5", "6", "7", NULL);
if (!out)
fail (0);
else
xfree (out);
out = strconcat ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"1", "2", "3", "4", "5", "6", "7", "8", NULL);
if (out)
fail (0);
else if (errno != EINVAL)
fail (0);
out = strconcat ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"1", "2", "3", "4", "5", "6", "7", "8", "9", NULL);
if (out)
fail (0);
else if (errno != EINVAL)
fail (0);
#if __GNUC__ < 4 /* gcc 4.0 has a sentinel attribute. */
out = strconcat (NULL);
if (!out || *out)
fail (1);
#endif
out = strconcat (NULL, NULL);
if (!out || *out)
fail (1);
out = strconcat ("", NULL);
if (!out || *out)
fail (1);
xfree (out);
out = strconcat ("", "", NULL);
if (!out || *out)
fail (2);
xfree (out);
out = strconcat ("a", "b", NULL);
if (!out || strcmp (out, "ab"))
fail (3);
xfree (out);
out = strconcat ("a", "b", "c", NULL);
if (!out || strcmp (out, "abc"))
fail (3);
xfree (out);
out = strconcat ("a", "b", "cc", NULL);
if (!out || strcmp (out, "abcc"))
fail (4);
xfree (out);
out = strconcat ("a1", "b1", "c1", NULL);
if (!out || strcmp (out, "a1b1c1"))
fail (4);
xfree (out);
out = strconcat ("", " long b ", "", "--even-longer--", NULL);
if (!out || strcmp (out, " long b --even-longer--"))
fail (5);
xfree (out);
out = strconcat ("", " long b ", "", "--even-longer--", NULL);
if (!out || strcmp (out, " long b --even-longer--"))
fail (5);
xfree (out);
}
static void
test_xstrconcat (void)
{
char *out;
out = xstrconcat ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"1", "2", "3", "4", "5", "6", "7", NULL);
if (!out)
fail (0);
#if __GNUC__ < 4 /* gcc 4.0 has a sentinel attribute. */
out = xstrconcat (NULL);
if (!out)
fail (1);
#endif
out = xstrconcat (NULL, NULL);
if (!out)
fail (1);
out = xstrconcat ("", NULL);
if (!out || *out)
fail (1);
xfree (out);
out = xstrconcat ("", "", NULL);
if (!out || *out)
fail (2);
xfree (out);
out = xstrconcat ("a", "b", NULL);
if (!out || strcmp (out, "ab"))
fail (3);
xfree (out);
out = xstrconcat ("a", "b", "c", NULL);
if (!out || strcmp (out, "abc"))
fail (3);
xfree (out);
out = xstrconcat ("a", "b", "cc", NULL);
if (!out || strcmp (out, "abcc"))
fail (4);
xfree (out);
out = xstrconcat ("a1", "b1", "c1", NULL);
if (!out || strcmp (out, "a1b1c1"))
fail (4);
xfree (out);
out = xstrconcat ("", " long b ", "", "--even-longer--", NULL);
if (!out || strcmp (out, " long b --even-longer--"))
fail (5);
xfree (out);
out = xstrconcat ("", " long b ", "", "--even-longer--", NULL);
if (!out || strcmp (out, " long b --even-longer--"))
fail (5);
xfree (out);
}
static void
test_make_filename_try (void)
{
char *out;
const char *home = gethome ();
size_t homelen = home? strlen (home):0;
out = make_filename_try ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"1", "2", "3", NULL);
if (out)
fail (0);
else if (errno != EINVAL)
fail (0);
xfree (out);
out = make_filename_try ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"1", "2", "3", "4", NULL);
if (out)
fail (0);
else if (errno != EINVAL)
fail (0);
xfree (out);
out = make_filename_try ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"1", "2", NULL);
if (!out || strcmp (out,
"1/2/3/4/5/6/7/8/9/10/"
"1/2/3/4/5/6/7/8/9/10/"
"1/2/3/4/5/6/7/8/9/10/"
"1/2"))
fail (0);
xfree (out);
out = make_filename_try ("foo", "~/bar", "baz/cde", NULL);
if (!out || strcmp (out, "foo/~/bar/baz/cde"))
fail (1);
xfree (out);
out = make_filename_try ("foo", "~/bar", "baz/cde/", NULL);
if (!out || strcmp (out, "foo/~/bar/baz/cde/"))
fail (1);
xfree (out);
out = make_filename_try ("/foo", "~/bar", "baz/cde/", NULL);
if (!out || strcmp (out, "/foo/~/bar/baz/cde/"))
fail (1);
xfree (out);
out = make_filename_try ("//foo", "~/bar", "baz/cde/", NULL);
if (!out || strcmp (out, "//foo/~/bar/baz/cde/"))
fail (1);
xfree (out);
out = make_filename_try ("", "~/bar", "baz/cde", NULL);
if (!out || strcmp (out, "/~/bar/baz/cde"))
fail (1);
xfree (out);
out = make_filename_try ("~/foo", "bar", NULL);
if (!out)
fail (2);
- if (home)
+ else if (home)
{
if (strlen (out) < homelen + 7)
fail (2);
- if (strncmp (out, home, homelen))
+ else if (strncmp (out, home, homelen))
fail (2);
- if (strcmp (out+homelen, "/foo/bar"))
+ else if (strcmp (out+homelen, "/foo/bar"))
fail (2);
}
else
{
if (strcmp (out, "~/foo/bar"))
fail (2);
}
xfree (out);
out = make_filename_try ("~", "bar", NULL);
if (!out)
fail (2);
- if (home)
+ else if (home)
{
if (strlen (out) < homelen + 3)
fail (2);
- if (strncmp (out, home, homelen))
+ else if (strncmp (out, home, homelen))
fail (2);
- if (strcmp (out+homelen, "/bar"))
+ else if (strcmp (out+homelen, "/bar"))
fail (2);
}
else
{
if (strcmp (out, "~/bar"))
fail (2);
}
xfree (out);
}
static void
test_make_absfilename_try (void)
{
char *out;
char *cwd = mygetcwd ();
size_t cwdlen = strlen (cwd);
out = make_absfilename_try ("foo", "bar", NULL);
if (!out)
fail (0);
- if (strlen (out) < cwdlen + 7)
+ else if (strlen (out) < cwdlen + 7)
fail (0);
- if (strncmp (out, cwd, cwdlen))
+ else if (strncmp (out, cwd, cwdlen))
fail (0);
- if (strcmp (out+cwdlen, "/foo/bar"))
+ else if (strcmp (out+cwdlen, "/foo/bar"))
fail (0);
xfree (out);
out = make_absfilename_try ("./foo", NULL);
if (!out)
fail (1);
- if (strlen (out) < cwdlen + 5)
+ else if (strlen (out) < cwdlen + 5)
fail (1);
- if (strncmp (out, cwd, cwdlen))
+ else if (strncmp (out, cwd, cwdlen))
fail (1);
- if (strcmp (out+cwdlen, "/./foo"))
+ else if (strcmp (out+cwdlen, "/./foo"))
fail (1);
xfree (out);
out = make_absfilename_try (".", NULL);
if (!out)
fail (2);
- if (strlen (out) < cwdlen)
+ else if (strlen (out) < cwdlen)
fail (2);
- if (strncmp (out, cwd, cwdlen))
+ else if (strncmp (out, cwd, cwdlen))
fail (2);
- if (strcmp (out+cwdlen, ""))
+ else if (strcmp (out+cwdlen, ""))
fail (2);
xfree (out);
xfree (cwd);
}
static void
test_strsplit (void)
{
struct {
const char *s;
char delim;
char replacement;
const char *fields_expected[10];
} tv[] = {
{
"a:bc:cde:fghi:jklmn::foo:", ':', '\0',
{ "a", "bc", "cde", "fghi", "jklmn", "", "foo", "", NULL }
},
{
",a,bc,,def,", ',', '!',
{ "!a!bc!!def!", "a!bc!!def!", "bc!!def!", "!def!", "def!", "", NULL }
},
{
"", ':', ',',
{ "", NULL }
}
};
int tidx;
for (tidx = 0; tidx < DIM(tv); tidx++)
{
char *s2;
int field_count;
char **fields;
int field_count_expected;
int i;
/* Count the fields. */
for (field_count_expected = 0;
tv[tidx].fields_expected[field_count_expected];
field_count_expected ++)
;
/* We need to copy s since strsplit modifies it in place. */
s2 = xstrdup (tv[tidx].s);
fields = strsplit (s2, tv[tidx].delim, tv[tidx].replacement,
&field_count);
if (field_count != field_count_expected)
fail (tidx * 1000);
for (i = 0; i < field_count_expected; i ++)
if (strcmp (tv[tidx].fields_expected[i], fields[i]) != 0)
{
printf ("For field %d, expected '%s', but got '%s'\n",
i, tv[tidx].fields_expected[i], fields[i]);
fail (tidx * 1000 + i + 1);
}
xfree (s2);
}
}
static void
test_strtokenize (void)
{
struct {
const char *s;
const char *delim;
const char *fields_expected[10];
} tv[] = {
{
"", ":",
{ "", NULL }
},
{
"a", ":",
{ "a", NULL }
},
{
":", ":",
{ "", "", NULL }
},
{
"::", ":",
{ "", "", "", NULL }
},
{
"a:b:c", ":",
{ "a", "b", "c", NULL }
},
{
"a:b:", ":",
{ "a", "b", "", NULL }
},
{
"a:b", ":",
{ "a", "b", NULL }
},
{
"aa:b:cd", ":",
{ "aa", "b", "cd", NULL }
},
{
"aa::b:cd", ":",
{ "aa", "", "b", "cd", NULL }
},
{
"::b:cd", ":",
{ "", "", "b", "cd", NULL }
},
{
"aa: : b:cd ", ":",
{ "aa", "", "b", "cd", NULL }
},
{
" aa: : b: cd ", ":",
{ "aa", "", "b", "cd", NULL }
},
{
" ", ":",
{ "", NULL }
},
{
" :", ":",
{ "", "", NULL }
},
{
" : ", ":",
{ "", "", NULL }
},
{
": ", ":",
{ "", "", NULL }
},
{
": x ", ":",
{ "", "x", NULL }
},
{
"a:bc:cde:fghi:jklmn::foo:", ":",
{ "a", "bc", "cde", "fghi", "jklmn", "", "foo", "", NULL }
},
{
",a,bc,,def,", ",",
{ "", "a", "bc", "", "def", "", NULL }
},
{
" a ", " ",
{ "", "a", "", NULL }
},
{
" ", " ",
{ "", "", NULL }
},
{
"", " ",
{ "", NULL }
}
};
int tidx;
for (tidx = 0; tidx < DIM(tv); tidx++)
{
char **fields;
int field_count;
int field_count_expected;
int i;
for (field_count_expected = 0;
tv[tidx].fields_expected[field_count_expected];
field_count_expected ++)
;
fields = strtokenize (tv[tidx].s, tv[tidx].delim);
if (!fields)
fail (tidx * 1000);
else
{
for (field_count = 0; fields[field_count]; field_count++)
;
if (field_count != field_count_expected)
fail (tidx * 1000);
else
{
for (i = 0; i < field_count_expected; i++)
if (strcmp (tv[tidx].fields_expected[i], fields[i]))
{
printf ("For field %d, expected '%s', but got '%s'\n",
i, tv[tidx].fields_expected[i], fields[i]);
fail (tidx * 1000 + i + 1);
}
}
}
xfree (fields);
}
}
static char *
stresc (char *s)
{
char *p;
int l = strlen (s) + 1;
for (p = s; *p; p ++)
if (*p == '\n')
l ++;
p = xmalloc (l);
for (l = 0; *s; s ++, l ++)
{
if (*s == ' ')
p[l] = '_';
else if (*p == '\n')
{
p[l ++] = '\\';
p[l ++] = 'n';
p[l] = '\n';
}
else
p[l] = *s;
}
p[l] = *s;
return p;
}
static void
test_format_text (void)
{
struct test
{
int target_cols, max_cols;
char *input;
char *expected;
};
struct test tests[] = {
{
10, 12,
"",
"",
},
{
10, 12,
" ",
"",
},
{
10, 12,
" ",
"",
},
{
10, 12,
" \n ",
" \n",
},
{
10, 12,
" \n \n ",
" \n \n",
},
{
10, 12,
"0123456789 0123456789 0",
"0123456789\n0123456789\n0",
},
{
10, 12,
" 0123456789 0123456789 0 ",
" 0123456789\n0123456789\n0",
},
{
10, 12,
"01 34 67 90 23 56 89 12 45 67 89 1",
"01 34 67\n90 23 56\n89 12 45\n67 89 1"
},
{
10, 12,
"01 34 67 90 23 56 89 12 45 67 89 1",
"01 34 67\n90 23 56\n89 12 45\n67 89 1"
},
{
72, 80,
"Warning: if you think you've seen more than 10 messages "
"signed by this key, then this key might be a forgery! "
"Carefully examine the email address for small variations "
"(e.g., additional white space). If the key is suspect, "
"then use 'gpg --tofu-policy bad \"FINGERPRINT\"' to mark it as being bad.\n",
"Warning: if you think you've seen more than 10 messages signed by this\n"
"key, then this key might be a forgery! Carefully examine the email\n"
"address for small variations (e.g., additional white space). If the key\n"
"is suspect, then use 'gpg --tofu-policy bad \"FINGERPRINT\"' to mark it as\n"
"being bad.\n"
},
{
72, 80,
"Normally, there is only a single key associated with an email "
"address. However, people sometimes generate a new key if "
"their key is too old or they think it might be compromised. "
"Alternatively, a new key may indicate a man-in-the-middle "
"attack! Before accepting this key, you should talk to or "
"call the person to make sure this new key is legitimate.",
"Normally, there is only a single key associated with an email "
"address.\nHowever, people sometimes generate a new key if "
"their key is too old or\nthey think it might be compromised. "
"Alternatively, a new key may indicate\na man-in-the-middle "
"attack! Before accepting this key, you should talk\nto or "
"call the person to make sure this new key is legitimate.",
}
};
int i;
int failed = 0;
for (i = 0; i < sizeof (tests) / sizeof (tests[0]); i ++)
{
struct test *test = &tests[i];
char *result =
format_text (test->input, 0, test->target_cols, test->max_cols);
if (strcmp (result, test->expected) != 0)
{
printf ("%s: Test #%d failed.\nExpected: '%s'\nResult: '%s'\n",
__func__, i + 1, stresc (test->expected), stresc (result));
failed ++;
}
xfree (result);
}
if (failed)
fail(0);
}
int
main (int argc, char **argv)
{
(void)argc;
(void)argv;
test_percent_escape ();
test_compare_filenames ();
test_strconcat ();
test_xstrconcat ();
test_make_filename_try ();
test_make_absfilename_try ();
test_strsplit ();
test_strtokenize ();
test_format_text ();
xfree (home_buffer);
return 0;
}
diff --git a/common/t-timestuff.c b/common/t-timestuff.c
index cb7cd392a..a80aaff59 100644
--- a/common/t-timestuff.c
+++ b/common/t-timestuff.c
@@ -1,172 +1,175 @@
/* t-timestuff.c - Regression tests for time functions
* Copyright (C) 2007 Free Software Foundation, Inc.
*
* This file is part of GnuPG.
*
* GnuPG is free software; you can redistribute it and/or modify it
* under the terms of either
*
* - the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* or
*
* - the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* or both in parallel, as here.
*
* GnuPG 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
* General Public License for more details.
*
* You should have received a copies of the GNU General Public License
* and the GNU Lesser General Public License along with this program;
* if not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include "mischelp.h"
#include "t-support.h"
static int
cmp_time_s (struct tm *a, struct tm *b)
{
if (a->tm_year != b->tm_year
|| a->tm_mon != b->tm_mon
|| a->tm_mday != b->tm_mday
|| a->tm_hour != b->tm_hour
|| a->tm_min != b->tm_min
|| a->tm_sec != b->tm_sec
|| a->tm_wday != b->tm_wday
|| a->tm_yday != b->tm_yday
|| !a->tm_isdst != !b->tm_isdst)
return -1;
return 0;
}
static void
test_timegm (void)
{
static struct {
int year, mon, mday, hour, min, sec;
} tvalues[] = {
{ -1 },
{ -2, 1 },
{ -2, 2 },
{ -2, 86399 },
{ -2, 86400 },
{ -2, 0x7ffffffe },
{ -2, 0x7fffffff },
/* Note: Because we use mktime below we can only start with the
day after Epoch. */
{ 1970, 0, 2, 0, 0 , 1},
{ 1970, 0, 2, 0, 0 , 2},
{ 1970, 0, 2, 12, 0 , 0},
{ 1970, 0, 2, 23, 59 , 59},
{ 1999, 11, 31, 23, 59 , 59},
{ 2000, 0, 1, 0, 0, 0},
{ 2000, 0, 1, 0, 0, 1},
{ 2010, 11, 31, 23, 59 , 59},
{ 2010, 0, 1, 0, 0, 0},
{ 2010, 0, 1, 0, 0, 1},
/* On GNU based 32 bit systems the end of all ticks will be on
20380119T031408 (unless Uli takes compassion on us and changes
time_t to a u64). We check that the previous day is okay. */
{ 2038, 0, 18, 23, 59, 59}
};
int tidx;
time_t now, atime;
struct tm tbuf, tbuf2, *tp;
for (tidx=0; tidx < DIM (tvalues); tidx++)
{
if (tvalues[tidx].year == -1)
{
now = time (NULL);
}
else if (tvalues[tidx].year == -2)
{
now = tvalues[tidx].mon;
}
else
{
memset (&tbuf, 0, sizeof tbuf);
tbuf.tm_year = tvalues[tidx].year - 1900;
tbuf.tm_mon = tvalues[tidx].mon;
tbuf.tm_mday = tvalues[tidx].mday;
tbuf.tm_hour = tvalues[tidx].hour;
tbuf.tm_min = tvalues[tidx].min;
tbuf.tm_sec = tvalues[tidx].sec;
#ifdef HAVE_TIMEGM
now = timegm (&tbuf);
#else
now = mktime (&tbuf);
#endif
}
if (now == (time_t)(-1))
fail (tidx);
tp = gmtime (&now);
if (!tp)
fail (tidx);
- tbuf = *tp;
- tbuf2 = tbuf;
+ else
+ {
+ tbuf = *tp;
+ tbuf2 = tbuf;
#ifdef HAVE_TIMEGM
- atime = timegm (&tbuf);
+ atime = timegm (&tbuf);
#else
- atime = mktime (&tbuf);
+ atime = mktime (&tbuf);
#endif
- if (atime == (time_t)(-1))
- fail (tidx);
- if (atime != now)
- fail (tidx);
-
- tp = gmtime (&atime);
- if (!tp)
- fail (tidx);
- if (cmp_time_s (tp, &tbuf))
- fail (tidx);
- if (cmp_time_s (tp, &tbuf2))
- fail (tidx);
+ if (atime == (time_t)(-1))
+ fail (tidx);
+ else if (atime != now)
+ fail (tidx);
+
+ tp = gmtime (&atime);
+ if (!tp)
+ fail (tidx);
+ else if (cmp_time_s (tp, &tbuf))
+ fail (tidx);
+ else if (cmp_time_s (tp, &tbuf2))
+ fail (tidx);
+ }
}
}
int
main (int argc, char **argv)
{
(void)argc;
(void)argv;
/* If we do not have timegm, we use mktime. However, we need to use
UTC in this case so that the 20380118T235959 test does not fail
for other timezones. */
#ifndef HAVE_TIMEGM
# ifdef HAVE_SETENV
setenv ("TZ", "UTC", 1);
#else
putenv (xstrdup ("TZ=UTC"));
#endif
tzset ();
#endif
test_timegm ();
return 0;
}

File Metadata

Mime Type
text/x-diff
Expires
Thu, Jul 17, 12:19 AM (6 h, 27 m)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
ec/d6/4fa637fc4da1dd552c7322e218cc

Event Timeline