Commit 6013fb6e authored by Simon McVittie's avatar Simon McVittie

tests: Add a unit test for library_cmp_by_name

Signed-off-by: Simon McVittie's avatarSimon McVittie <smcv@collabora.com>
parent a58223c3
......@@ -462,7 +462,11 @@ if HAVE_GLIB
test_programs += tests/utils.t
endif
tests_utils_t_SOURCES = tests/utils.c
tests_utils_t_SOURCES = tests/utils.c \
tests/test-helpers.c \
tests/test-helpers.h \
utils/library-cmp.c \
utils/library-cmp.h
tests_utils_t_LDADD = utils/libutils.la $(GLIB_LIBS)
test_scripts = tests/capture-libs.pl \
......
/*
* Copyright © 2019-2020 Collabora Ltd.
*
* SPDX-License-Identifier: MIT
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "test-helpers.h"
#include <ftw.h>
#include <stdio.h>
static int
ftw_remove (const char *path,
const struct stat *sb,
int typeflags,
struct FTW *ftwbuf)
{
if (remove (path) < 0)
return -1;
return 0;
}
gboolean
rm_rf (const char *path)
{
if (nftw (path, ftw_remove, 10, FTW_DEPTH|FTW_MOUNT|FTW_PHYS) < 0)
return FALSE;
return TRUE;
}
/*
* Copyright © 2019 Collabora Ltd.
*
* SPDX-License-Identifier: MIT
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <glib.h>
gboolean rm_rf (const char *path);
/*
* Simplified implementations of some of the GLib test assertion macros,
* for use with older GLib versions.
*/
#ifndef g_assert_true
#define g_assert_true(x) g_assert ((x))
#endif
#ifndef g_assert_false
#define g_assert_false(x) g_assert (!(x))
#endif
#ifndef g_assert_cmpint
#define g_assert_cmpint(a, op, b) g_assert ((a) op (b))
#endif
#ifndef g_assert_cmpmem
#define g_assert_cmpmem(m1, l1, m2, l2) \
g_assert (l1 == l2 && memcmp (m1, m2, l1) == 0)
#endif
#ifndef g_assert_cmpstr
#define g_assert_cmpstr(a, op, b) g_assert (g_strcmp0 ((a), (b)) op 0)
#endif
#ifndef g_assert_nonnull
#define g_assert_nonnull(x) g_assert ((x) != NULL)
#endif
#ifndef g_assert_null
#define g_assert_null(x) g_assert ((x) == NULL)
#endif
#if !GLIB_CHECK_VERSION(2, 38, 0)
#define g_test_skip(msg) g_test_message ("SKIP: %s", msg)
#endif
......@@ -15,12 +15,34 @@
// You should have received a copy of the GNU Lesser General Public
// License along with libcapsule. If not, see <http://www.gnu.org/licenses/>.
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <glib.h>
#include <glib/gstdio.h>
#include "tests/test-helpers.h"
#include "utils/library-cmp.h"
#include "utils/utils.h"
#define assert_with_errno(expr) \
do { \
errno = 0; \
\
if (!(expr)) \
g_error ("Assertion failed: %s: %s", #expr, g_strerror (errno)); \
} while (0)
static void
touch (const char *path)
{
FILE *fh;
assert_with_errno((fh = fopen (path, "w")) != NULL);
fclose (fh);
}
typedef struct
{
int dummy;
......@@ -134,6 +156,133 @@ test_build_filename (Fixture *f,
}
}
typedef struct
{
const char *soname;
const char *in_container;
char cmp;
const char *in_provider;
} CmpByNameTest;
static const CmpByNameTest cmp_by_name_tests[] =
{
{ "libdbus-1.so.3", "libdbus-1.so.3.1", '<', "libdbus-1.so.3.2" },
{ "libdbus-1.so.3", "libdbus-1.so.3.1.2", '>', "libdbus-1.so.3.1.1" },
{ "libdbus-1.so.3", "libdbus-1.so.3.1", '=', "libdbus-1.so.3.1" },
{ "libc.so.6", "libc-2.19.so", '<', "libc-2.22.so" },
{ "libgcc_s.so.1", "libgcc_s-20200703.so.1", '>', "libgcc_s-20120401.so.1" },
{ "libgcc_s.so.1", "libgcc_s-20200703.so.1", '=', NULL },
{ "libgcc_s.so.1", NULL, '=', "libgcc_s-20200703.so.1" },
{ "libgcc_s.so.1", NULL, '=', NULL },
};
static void
test_library_cmp_by_name (Fixture *f,
gconstpointer data)
{
GError *error = NULL;
gchar *tmpdir = g_dir_make_tmp ("libcapsule.XXXXXX", &error);
gchar *container;
gchar *provider;
gsize i;
g_assert_no_error (error);
g_assert_nonnull (tmpdir);
container = g_build_filename (tmpdir, "c", NULL);
provider = g_build_filename (tmpdir, "p", NULL);
assert_with_errno (g_mkdir (container, 0755) == 0);
assert_with_errno (g_mkdir (provider, 0755) == 0);
for (i = 0; i < G_N_ELEMENTS (cmp_by_name_tests); i++)
{
const CmpByNameTest *test = &cmp_by_name_tests[i];
gchar *container_file = NULL;
gchar *container_lib = g_build_filename (container, test->soname, NULL);
gchar *provider_file = NULL;
gchar *provider_lib = g_build_filename (provider, test->soname, NULL);
int result;
assert_with_errno (g_unlink (container_lib) == 0 || errno == ENOENT);
assert_with_errno (g_unlink (provider_lib) == 0 || errno == ENOENT);
if (test->in_container == NULL)
{
touch (container_lib);
}
else
{
container_file = g_build_filename (container, test->in_container, NULL);
assert_with_errno (g_unlink (container_file) == 0 || errno == ENOENT);
touch (container_file);
assert_with_errno (symlink (test->in_container, container_lib) == 0);
}
if (test->in_provider == NULL)
{
touch (provider_lib);
}
else
{
provider_file = g_build_filename (provider, test->in_provider, NULL);
assert_with_errno (g_unlink (provider_file) == 0 || errno == ENOENT);
touch (provider_file);
assert_with_errno (symlink (test->in_provider, provider_lib) == 0);
}
result = library_cmp_by_name (test->soname,
container_lib, container,
provider_lib, provider);
switch (test->cmp)
{
case '<':
if (result >= 0)
g_error ("Expected %s (%s) < %s (%s), got %d",
container_lib,
container_file ? container_file : "regular file",
provider_lib,
provider_file ? provider_file : "regular file",
result);
break;
case '>':
if (result <= 0)
g_error ("Expected %s (%s) > %s (%s), got %d",
container_lib,
container_file ? container_file : "regular file",
provider_lib,
provider_file ? provider_file : "regular file",
result);
break;
case '=':
if (result != 0)
g_error ("Expected %s (%s) == %s (%s), got %d",
container_lib,
container_file ? container_file : "regular file",
provider_lib,
provider_file ? provider_file : "regular file",
result);
break;
default:
g_assert_not_reached ();
}
g_free (container_file);
g_free (container_lib);
g_free (provider_file);
g_free (provider_lib);
}
assert_with_errno (rm_rf (tmpdir));
g_free (container);
g_free (provider);
g_free (tmpdir);
}
static void
test_ptr_list (Fixture *f,
gconstpointer data)
......@@ -191,6 +340,8 @@ main (int argc,
g_test_add ("/build-filename", Fixture, NULL, setup,
test_build_filename, teardown);
g_test_add ("/library-cmp/name", Fixture, NULL,
setup, test_library_cmp_by_name, teardown);
g_test_add ("/ptr-list", Fixture, NULL, setup, test_ptr_list, teardown);
return g_test_run ();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment