diff options
author | Stef Walter <stefw@redhat.com> | 2013-04-03 10:50:59 +0200 |
---|---|---|
committer | Stef Walter <stefw@gnome.org> | 2013-04-03 12:45:43 +0200 |
commit | a63311a0f3f2669138d09ff8f618fd4d12fa0c3d (patch) | |
tree | d5a9b8cd32dda2e0e1eff1a8393b5dcb2174f86b /common/tests | |
parent | c3f1b0a45eb1c28b6f025f8ae56c3b020801b6aa (diff) |
More compatible path munging and handling code
Centralize the path handling code, so we can remove unixy assumptions
and have a chance of running on Windows. The current goal is to run
all the tests on Windows.
Includes some code from LRN <lrn1986@gmail.com>
https://bugs.freedesktop.org/show_bug.cgi?id=63062
Diffstat (limited to 'common/tests')
-rw-r--r-- | common/tests/Makefile.am | 1 | ||||
-rw-r--r-- | common/tests/test-compat.c | 32 | ||||
-rw-r--r-- | common/tests/test-path.c | 202 |
3 files changed, 203 insertions, 32 deletions
diff --git a/common/tests/Makefile.am b/common/tests/Makefile.am index ba9a72f..5e84439 100644 --- a/common/tests/Makefile.am +++ b/common/tests/Makefile.am @@ -22,6 +22,7 @@ CHECK_PROGS = \ test-buffer \ test-lexer \ test-url \ + test-path \ $(NULL) noinst_PROGRAMS = \ diff --git a/common/tests/test-compat.c b/common/tests/test-compat.c index a94aaeb..066e723 100644 --- a/common/tests/test-compat.c +++ b/common/tests/test-compat.c @@ -42,37 +42,6 @@ #include "compat.h" static void -test_basename (CuTest *tc) -{ - struct { - const char *in; - const char *out; - } fixtures[] = { - { "/this/is/a/path", "path" }, - { "/this/is/a/folder/", "folder" }, - { "folder/", "folder" }, - { "/", "" }, - { "this", "this" }, -#ifdef OS_WIN32 - { "\\this\\is\\a\\path", "path" }, - { "\\this\\is\\a\\folder\\", "folder" }, - { "folder\\", "folder" }, - { "\\", "" }, -#endif - { NULL }, - }; - - char *out; - int i; - - for (i = 0; fixtures[i].in != NULL; i++) { - out = p11_basename (fixtures[i].in); - CuAssertStrEquals (tc, fixtures[i].out, out); - free (out); - } -} - -static void test_strndup (CuTest *tc) { char unterminated[] = { 't', 'e', 's', 't', 'e', 'r', 'o', 'n', 'i', 'o' }; @@ -94,7 +63,6 @@ main (void) CuSuite* suite = CuSuiteNew (); int ret; - SUITE_ADD_TEST (suite, test_basename); SUITE_ADD_TEST (suite, test_strndup); CuSuiteRun (suite); diff --git a/common/tests/test-path.c b/common/tests/test-path.c new file mode 100644 index 0000000..8263d1f --- /dev/null +++ b/common/tests/test-path.c @@ -0,0 +1,202 @@ +/* + * Copyright (c) 2013 Red Hat Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * Author: Stef Walter <stefw@redhat.com> + */ + +#include "config.h" +#include "CuTest.h" + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include "compat.h" +#include "path.h" + +static void +test_base (CuTest *tc) +{ + struct { + const char *in; + const char *out; + } fixtures[] = { + { "/this/is/a/path", "path" }, + { "/this/is/a/folder/", "folder" }, + { "folder/", "folder" }, + { "/", "" }, + { "this", "this" }, +#ifdef OS_WIN32 + { "\\this\\is\\a\\path", "path" }, + { "\\this\\is\\a\\folder\\", "folder" }, + { "C:\\this\\is\\a\\path", "path" }, + { "D:\\this\\is\\a\\folder\\", "folder" }, + { "folder\\", "folder" }, + { "\\", "" }, +#endif + { NULL }, + }; + + char *out; + int i; + + for (i = 0; fixtures[i].in != NULL; i++) { + out = p11_path_base (fixtures[i].in); + CuAssertStrEquals (tc, fixtures[i].out, out); + free (out); + } +} + +static void +check_equals_and_free_msg (CuTest *tc, + const char *file, + int line, + const char *ex, + char *ac) +{ + CuAssertStrEquals_LineMsg (tc, file, line, NULL, ex, ac); + free (ac); +} + +#define check_equals_and_free(tc, ex, ac) \ + check_equals_and_free_msg ((tc), __FILE__, __LINE__, (ex), (ac)) + +static void +test_build (CuTest *tc) +{ +#ifdef OS_UNIX + check_equals_and_free (tc, "/root/second", + p11_path_build ("/root", "second", NULL)); + check_equals_and_free (tc, "/root/second", + p11_path_build ("/root", "/second", NULL)); + check_equals_and_free (tc, "/root/second", + p11_path_build ("/root/", "second", NULL)); + check_equals_and_free (tc, "/root/second/third", + p11_path_build ("/root", "second", "third", NULL)); + check_equals_and_free (tc, "/root/second/third", + p11_path_build ("/root", "/second/third", NULL)); +#else /* OS_WIN32 */ + check_equals_and_free (tc, "C:\\root\\second", + p11_path_build ("C:\\root", "second", NULL)); + check_equals_and_free (tc, "C:\\root\\second", + p11_path_build ("C:\\root", "\\second", NULL)); + check_equals_and_free (tc, "C:\\root\\second", + p11_path_build ("C:\\root\\", "second", NULL)); + check_equals_and_free (tc, "C:\\root\\second\\third", + p11_path_build ("C:\\root", "second", "third", NULL)); + check_equals_and_free (tc, "C:\\root\\second/third", + p11_path_build ("C:\\root", "second/third", NULL)); +#endif +} + +static void +test_expand (CuTest *tc) +{ + char *path; + +#ifdef OS_UNIX + putenv ("HOME=/home/blah"); + check_equals_and_free (tc, "/home/blah/my/path", + p11_path_expand ("$HOME/my/path")); + check_equals_and_free (tc, "/home/blah/my/path", + p11_path_expand ("~/my/path")); + putenv ("TEMP=/tmpdir"); + check_equals_and_free (tc, "/tmpdir/my/path", + p11_path_expand ("$TEMP/my/path")); +#else /* OS_WIN32 */ + putenv ("HOME=C:\\Users\\blah"); + check_equals_and_free (tc, "C:\\Users\\blah\\path", + p11_path_expand ("$HOME/path")); + check_equals_and_free (tc, "C:\\Users\\blah\\path", + p11_path_expand ("$HOME\\path")); + check_equals_and_free (tc, "C:\\Users\\blah\\path", + p11_path_expand ("~/path")); + check_equals_and_free (tc, "C:\\Users\\blah\\path", + p11_path_expand ("~\\path")); + + putenv ("TEMP=C:\\Temp Directory"); + check_equals_and_free (tc, "C:\\Temp Directory\\path", + p11_path_expand ("$TEMP/path")); + check_equals_and_free (tc, "C:\\Temp Directory\\path", + p11_path_expand ("$TEMP\\path")); +#endif + + putenv("HOME="); + path = p11_path_expand ("$HOME/this/is/my/path"); + CuAssertTrue (tc, strstr (path, "this/is/my/path") != NULL); + free (path); + + putenv("HOME="); + path = p11_path_expand ("~/this/is/my/path"); + CuAssertTrue (tc, strstr (path, "this/is/my/path") != NULL); + free (path); + + putenv("TEMP="); + path = p11_path_expand ("$TEMP/this/is/my/path"); + CuAssertTrue (tc, strstr (path, "this/is/my/path") != NULL); + free (path); +} + +static void +test_absolute (CuTest *tc) +{ +#ifdef OS_UNIX + CuAssertTrue (tc, p11_path_absolute ("/home")); + CuAssertTrue (tc, !p11_path_absolute ("home")); +#else /* OS_WIN32 */ + CuAssertTrue (tc, p11_path_absolute ("C:\\home")); + CuAssertTrue (tc, !p11_path_absolute ("home")); + CuAssertTrue (tc, !p11_path_absolute ("/home")); +#endif +} + +int +main (void) +{ + CuString *output = CuStringNew (); + CuSuite* suite = CuSuiteNew (); + int ret; + + SUITE_ADD_TEST (suite, test_base); + SUITE_ADD_TEST (suite, test_build); + SUITE_ADD_TEST (suite, test_expand); + SUITE_ADD_TEST (suite, test_absolute); + + CuSuiteRun (suite); + CuSuiteSummary (suite, output); + CuSuiteDetails (suite, output); + printf ("%s\n", output->buffer); + ret = suite->failCount; + CuSuiteDelete (suite); + CuStringDelete (output); + + return ret; +} |