|
Packit Service |
3749ba |
/*
|
|
Packit Service |
3749ba |
* Copyright (c) 2013 Red Hat Inc.
|
|
Packit Service |
3749ba |
*
|
|
Packit Service |
3749ba |
* Redistribution and use in source and binary forms, with or without
|
|
Packit Service |
3749ba |
* modification, are permitted provided that the following conditions
|
|
Packit Service |
3749ba |
* are met:
|
|
Packit Service |
3749ba |
*
|
|
Packit Service |
3749ba |
* * Redistributions of source code must retain the above
|
|
Packit Service |
3749ba |
* copyright notice, this list of conditions and the
|
|
Packit Service |
3749ba |
* following disclaimer.
|
|
Packit Service |
3749ba |
* * Redistributions in binary form must reproduce the
|
|
Packit Service |
3749ba |
* above copyright notice, this list of conditions and
|
|
Packit Service |
3749ba |
* the following disclaimer in the documentation and/or
|
|
Packit Service |
3749ba |
* other materials provided with the distribution.
|
|
Packit Service |
3749ba |
* * The names of contributors to this software may not be
|
|
Packit Service |
3749ba |
* used to endorse or promote products derived from this
|
|
Packit Service |
3749ba |
* software without specific prior written permission.
|
|
Packit Service |
3749ba |
*
|
|
Packit Service |
3749ba |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
Packit Service |
3749ba |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
Packit Service |
3749ba |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
Packit Service |
3749ba |
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
Packit Service |
3749ba |
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
Packit Service |
3749ba |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
Packit Service |
3749ba |
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
Packit Service |
3749ba |
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
Packit Service |
3749ba |
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
Packit Service |
3749ba |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
Packit Service |
3749ba |
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
|
|
Packit Service |
3749ba |
* DAMAGE.
|
|
Packit Service |
3749ba |
*
|
|
Packit Service |
3749ba |
* Author: Stef Walter <stefw@redhat.com>
|
|
Packit Service |
3749ba |
*/
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
#include "config.h"
|
|
Packit Service |
3749ba |
#include "test.h"
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
#include <stdlib.h>
|
|
Packit Service |
3749ba |
#include <stdio.h>
|
|
Packit Service |
3749ba |
#include <string.h>
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
#include "compat.h"
|
|
Packit Service |
3749ba |
#include "path.h"
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
static void
|
|
Packit Service |
3749ba |
test_base (void)
|
|
Packit Service |
3749ba |
{
|
|
Packit Service |
3749ba |
struct {
|
|
Packit Service |
3749ba |
const char *in;
|
|
Packit Service |
3749ba |
const char *out;
|
|
Packit Service |
3749ba |
} fixtures[] = {
|
|
Packit Service |
3749ba |
{ "/this/is/a/path", "path" },
|
|
Packit Service |
3749ba |
{ "/this/is/a/folder/", "folder" },
|
|
Packit Service |
3749ba |
{ "folder/", "folder" },
|
|
Packit Service |
3749ba |
{ "/", "" },
|
|
Packit Service |
3749ba |
{ "this", "this" },
|
|
Packit Service |
3749ba |
#ifdef OS_WIN32
|
|
Packit Service |
3749ba |
{ "\\this\\is\\a\\path", "path" },
|
|
Packit Service |
3749ba |
{ "\\this\\is\\a\\folder\\", "folder" },
|
|
Packit Service |
3749ba |
{ "C:\\this\\is\\a\\path", "path" },
|
|
Packit Service |
3749ba |
{ "D:\\this\\is\\a\\folder\\", "folder" },
|
|
Packit Service |
3749ba |
{ "folder\\", "folder" },
|
|
Packit Service |
3749ba |
{ "\\", "" },
|
|
Packit Service |
3749ba |
#endif
|
|
Packit Service |
3749ba |
{ NULL },
|
|
Packit Service |
3749ba |
};
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
char *out;
|
|
Packit Service |
3749ba |
int i;
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
for (i = 0; fixtures[i].in != NULL; i++) {
|
|
Packit Service |
3749ba |
out = p11_path_base (fixtures[i].in);
|
|
Packit Service |
3749ba |
assert_str_eq (fixtures[i].out, out);
|
|
Packit Service |
3749ba |
free (out);
|
|
Packit Service |
3749ba |
}
|
|
Packit Service |
3749ba |
}
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
#define assert_str_eq_free(ex, ac) \
|
|
Packit Service |
3749ba |
do { const char *__s1 = (ex); \
|
|
Packit Service |
3749ba |
char *__s2 = (ac); \
|
|
Packit Service |
3749ba |
if (__s1 && __s2 && strcmp (__s1, __s2) == 0) ; else \
|
|
Packit Service |
3749ba |
p11_test_fail (__FILE__, __LINE__, __FUNCTION__, "assertion failed (%s == %s): (%s == %s)", \
|
|
Packit Service |
3749ba |
#ex, #ac, __s1 ? __s1 : "(null)", __s2 ? __s2 : "(null)"); \
|
|
Packit Service |
3749ba |
free (__s2); \
|
|
Packit Service |
3749ba |
} while (0)
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
static void
|
|
Packit Service |
3749ba |
test_build (void)
|
|
Packit Service |
3749ba |
{
|
|
Packit Service |
3749ba |
#ifdef OS_UNIX
|
|
Packit Service |
3749ba |
assert_str_eq_free ("/root/second",
|
|
Packit Service |
3749ba |
p11_path_build ("/root", "second", NULL));
|
|
Packit Service |
3749ba |
assert_str_eq_free ("/root/second",
|
|
Packit Service |
3749ba |
p11_path_build ("/root", "/second", NULL));
|
|
Packit Service |
3749ba |
assert_str_eq_free ("/root/second",
|
|
Packit Service |
3749ba |
p11_path_build ("/root/", "second", NULL));
|
|
Packit Service |
3749ba |
assert_str_eq_free ("/root/second/third",
|
|
Packit Service |
3749ba |
p11_path_build ("/root", "second", "third", NULL));
|
|
Packit Service |
3749ba |
assert_str_eq_free ("/root/second/third",
|
|
Packit Service |
3749ba |
p11_path_build ("/root", "/second/third", NULL));
|
|
Packit Service |
3749ba |
#else /* OS_WIN32 */
|
|
Packit Service |
3749ba |
assert_str_eq_free ("C:\\root\\second",
|
|
Packit Service |
3749ba |
p11_path_build ("C:\\root", "second", NULL));
|
|
Packit Service |
3749ba |
assert_str_eq_free ("C:\\root\\second",
|
|
Packit Service |
3749ba |
p11_path_build ("C:\\root", "\\second", NULL));
|
|
Packit Service |
3749ba |
assert_str_eq_free ("C:\\root\\second",
|
|
Packit Service |
3749ba |
p11_path_build ("C:\\root\\", "second", NULL));
|
|
Packit Service |
3749ba |
assert_str_eq_free ("C:\\root\\second\\third",
|
|
Packit Service |
3749ba |
p11_path_build ("C:\\root", "second", "third", NULL));
|
|
Packit Service |
3749ba |
assert_str_eq_free ("C:\\root\\second/third",
|
|
Packit Service |
3749ba |
p11_path_build ("C:\\root", "second/third", NULL));
|
|
Packit Service |
3749ba |
#endif
|
|
Packit Service |
3749ba |
}
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
static void
|
|
Packit Service |
3749ba |
test_expand (void)
|
|
Packit Service |
3749ba |
{
|
|
Packit Service |
3749ba |
char *path;
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
#ifdef OS_UNIX
|
|
Packit Service |
3749ba |
putenv ("HOME=/home/blah");
|
|
Packit Service |
3749ba |
assert_str_eq_free ("/home/blah/my/path",
|
|
Packit Service |
3749ba |
p11_path_expand ("~/my/path"));
|
|
Packit Service |
3749ba |
assert_str_eq_free ("/home/blah",
|
|
Packit Service |
3749ba |
p11_path_expand ("~"));
|
|
Packit Service |
3749ba |
putenv ("XDG_CONFIG_HOME=/my");
|
|
Packit Service |
3749ba |
assert_str_eq_free ("/my/path",
|
|
Packit Service |
3749ba |
p11_path_expand ("~/.config/path"));
|
|
Packit Service |
3749ba |
putenv ("XDG_CONFIG_HOME=");
|
|
Packit Service |
3749ba |
assert_str_eq_free ("/home/blah/.config/path",
|
|
Packit Service |
3749ba |
p11_path_expand ("~/.config/path"));
|
|
Packit Service |
3749ba |
#else /* OS_WIN32 */
|
|
Packit Service |
3749ba |
putenv ("HOME=C:\\Users\\blah");
|
|
Packit Service |
3749ba |
assert_str_eq_free ("C:\\Users\\blah\\my/path",
|
|
Packit Service |
3749ba |
p11_path_expand ("~/my/path"));
|
|
Packit Service |
3749ba |
assert_str_eq_free ("C:\\Users\\blah\\path",
|
|
Packit Service |
3749ba |
p11_path_expand ("~\\path"));
|
|
Packit Service |
3749ba |
#endif
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
putenv("HOME=");
|
|
Packit Service |
3749ba |
path = p11_path_expand ("~/this/is/my/path");
|
|
Packit Service |
3749ba |
assert (strstr (path, "this/is/my/path") != NULL);
|
|
Packit Service |
3749ba |
free (path);
|
|
Packit Service |
3749ba |
}
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
static void
|
|
Packit Service |
3749ba |
test_absolute (void)
|
|
Packit Service |
3749ba |
{
|
|
Packit Service |
3749ba |
#ifdef OS_UNIX
|
|
Packit Service |
3749ba |
assert (p11_path_absolute ("/home"));
|
|
Packit Service |
3749ba |
assert (!p11_path_absolute ("home"));
|
|
Packit Service |
3749ba |
#else /* OS_WIN32 */
|
|
Packit Service |
3749ba |
assert (p11_path_absolute ("C:\\home"));
|
|
Packit Service |
3749ba |
assert (!p11_path_absolute ("home"));
|
|
Packit Service |
3749ba |
assert (p11_path_absolute ("/home"));
|
|
Packit Service |
3749ba |
#endif
|
|
Packit Service |
3749ba |
}
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
static void
|
|
Packit Service |
3749ba |
test_parent (void)
|
|
Packit Service |
3749ba |
{
|
|
Packit Service |
3749ba |
assert_str_eq_free ("/", p11_path_parent ("/root"));
|
|
Packit Service |
3749ba |
assert_str_eq_free ("/", p11_path_parent ("/root/"));
|
|
Packit Service |
3749ba |
assert_str_eq_free ("/", p11_path_parent ("/root//"));
|
|
Packit Service |
3749ba |
assert_str_eq_free ("/root", p11_path_parent ("/root/second"));
|
|
Packit Service |
3749ba |
assert_str_eq_free ("/root", p11_path_parent ("/root//second"));
|
|
Packit Service |
3749ba |
assert_str_eq_free ("/root", p11_path_parent ("/root//second//"));
|
|
Packit Service |
3749ba |
assert_str_eq_free ("/root", p11_path_parent ("/root///second"));
|
|
Packit Service |
3749ba |
assert_str_eq_free ("/root/second", p11_path_parent ("/root/second/test.file"));
|
|
Packit Service |
3749ba |
assert_ptr_eq (NULL, p11_path_parent ("/"));
|
|
Packit Service |
3749ba |
assert_ptr_eq (NULL, p11_path_parent ("//"));
|
|
Packit Service |
3749ba |
assert_ptr_eq (NULL, p11_path_parent (""));
|
|
Packit Service |
3749ba |
}
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
static void
|
|
Packit Service |
3749ba |
test_prefix (void)
|
|
Packit Service |
3749ba |
{
|
|
Packit Service |
3749ba |
assert (p11_path_prefix ("/test/second", "/test"));
|
|
Packit Service |
3749ba |
assert (!p11_path_prefix ("/test", "/test"));
|
|
Packit Service |
3749ba |
assert (!p11_path_prefix ("/different/prefix", "/test"));
|
|
Packit Service |
3749ba |
assert (!p11_path_prefix ("/te", "/test"));
|
|
Packit Service |
3749ba |
assert (!p11_path_prefix ("/test", "/test/blah"));
|
|
Packit Service |
3749ba |
assert (p11_path_prefix ("/test/other/second", "/test"));
|
|
Packit Service |
3749ba |
assert (p11_path_prefix ("/test//other//second", "/test"));
|
|
Packit Service |
3749ba |
}
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
static void
|
|
Packit Service |
3749ba |
test_canon (void)
|
|
Packit Service |
3749ba |
{
|
|
Packit Service |
3749ba |
char *test;
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
test = strdup ("2309haonutb;AOE@#$O ");
|
|
Packit Service |
3749ba |
p11_path_canon (test);
|
|
Packit Service |
3749ba |
assert_str_eq (test, "2309haonutb_AOE___O_");
|
|
Packit Service |
3749ba |
free (test);
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
test = strdup ("22@# %ATI@#$onot");
|
|
Packit Service |
3749ba |
p11_path_canon (test);
|
|
Packit Service |
3749ba |
assert_str_eq (test, "22____ATI___onot");
|
|
Packit Service |
3749ba |
free (test);
|
|
Packit Service |
3749ba |
}
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
static void
|
|
Packit Service |
3749ba |
test_encode (void)
|
|
Packit Service |
3749ba |
{
|
|
Packit Service |
3749ba |
char *test;
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
test = p11_path_encode ("2309haonutb;/AOE@#$O ");
|
|
Packit Service |
3749ba |
assert_str_eq (test, "2309haonutb%3B/AOE%40%23%24O%20");
|
|
Packit Service |
3749ba |
free (test);
|
|
Packit Service |
3749ba |
}
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
static void
|
|
Packit Service |
3749ba |
test_decode (void)
|
|
Packit Service |
3749ba |
{
|
|
Packit Service |
3749ba |
char *test;
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
test = p11_path_decode ("2309haonutb%3b/AOE%40%23%24O%20");
|
|
Packit Service |
3749ba |
assert_str_eq (test, "2309haonutb;/AOE@#$O ");
|
|
Packit Service |
3749ba |
free (test);
|
|
Packit Service |
3749ba |
}
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
int
|
|
Packit Service |
3749ba |
main (int argc,
|
|
Packit Service |
3749ba |
char *argv[])
|
|
Packit Service |
3749ba |
{
|
|
Packit Service |
3749ba |
p11_test (test_base, "/path/base");
|
|
Packit Service |
3749ba |
p11_test (test_build, "/path/build");
|
|
Packit Service |
3749ba |
p11_test (test_expand, "/path/expand");
|
|
Packit Service |
3749ba |
p11_test (test_absolute, "/path/absolute");
|
|
Packit Service |
3749ba |
p11_test (test_parent, "/path/parent");
|
|
Packit Service |
3749ba |
p11_test (test_prefix, "/path/prefix");
|
|
Packit Service |
3749ba |
p11_test (test_canon, "/path/canon");
|
|
Packit Service |
3749ba |
p11_test (test_encode, "/path/encode");
|
|
Packit Service |
3749ba |
p11_test (test_decode, "/path/decode");
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
return p11_test_run (argc, argv);
|
|
Packit Service |
3749ba |
}
|