|
Packit Service |
2a3f3d |
/*
|
|
Packit Service |
2a3f3d |
* Copyright © 2017 Endless Mobile, Inc.
|
|
Packit Service |
2a3f3d |
*
|
|
Packit Service |
2a3f3d |
* SPDX-License-Identifier: LGPL-2.0+
|
|
Packit Service |
2a3f3d |
*
|
|
Packit Service |
2a3f3d |
* This library is free software; you can redistribute it and/or
|
|
Packit Service |
2a3f3d |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit Service |
2a3f3d |
* License as published by the Free Software Foundation; either
|
|
Packit Service |
2a3f3d |
* version 2 of the License, or (at your option) any later version.
|
|
Packit Service |
2a3f3d |
*
|
|
Packit Service |
2a3f3d |
* This library is distributed in the hope that it will be useful,
|
|
Packit Service |
2a3f3d |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
2a3f3d |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
2a3f3d |
* Lesser General Public License for more details.
|
|
Packit Service |
2a3f3d |
*
|
|
Packit Service |
2a3f3d |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
2a3f3d |
* License along with this library; if not, write to the
|
|
Packit Service |
2a3f3d |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
Packit Service |
2a3f3d |
* Boston, MA 02111-1307, USA.
|
|
Packit Service |
2a3f3d |
*
|
|
Packit Service |
2a3f3d |
* Authors:
|
|
Packit Service |
2a3f3d |
* - Philip Withnall <withnall@endlessm.com>
|
|
Packit Service |
2a3f3d |
*/
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
#include "config.h"
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
#include <gio/gio.h>
|
|
Packit Service |
2a3f3d |
#include <glib.h>
|
|
Packit Service |
2a3f3d |
#include <glib-object.h>
|
|
Packit Service |
2a3f3d |
#include <libglnx.h>
|
|
Packit Service |
2a3f3d |
#include <locale.h>
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
#include "ostree-autocleanups.h"
|
|
Packit Service |
2a3f3d |
#include "ostree-types.h"
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
/* Test fixture. Creates a temporary directory. */
|
|
Packit Service |
2a3f3d |
typedef struct
|
|
Packit Service |
2a3f3d |
{
|
|
Packit Service |
2a3f3d |
GLnxTmpDir tmpdir; /* (owned) */
|
|
Packit Service |
2a3f3d |
} Fixture;
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
static void
|
|
Packit Service |
2a3f3d |
setup (Fixture *fixture,
|
|
Packit Service |
2a3f3d |
gconstpointer test_data)
|
|
Packit Service |
2a3f3d |
{
|
|
Packit Service |
2a3f3d |
g_autoptr(GError) error = NULL;
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
(void) glnx_mkdtemp ("test-repo-XXXXXX", 0700, &fixture->tmpdir, &error);
|
|
Packit Service |
2a3f3d |
g_assert_no_error (error);
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
g_test_message ("Using temporary directory: %s", fixture->tmpdir.path);
|
|
Packit Service |
2a3f3d |
}
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
static void
|
|
Packit Service |
2a3f3d |
teardown (Fixture *fixture,
|
|
Packit Service |
2a3f3d |
gconstpointer test_data)
|
|
Packit Service |
2a3f3d |
{
|
|
Packit Service |
2a3f3d |
/* Recursively remove the temporary directory. */
|
|
Packit Service |
2a3f3d |
(void) glnx_tmpdir_delete (&fixture->tmpdir, NULL, NULL);
|
|
Packit Service |
2a3f3d |
}
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
/* Test that the hash values for two #OstreeRepo instances pointing at the same
|
|
Packit Service |
2a3f3d |
* repository are equal. We can’t test anything else, since hash collisions are
|
|
Packit Service |
2a3f3d |
* always a possibility. */
|
|
Packit Service |
2a3f3d |
static void
|
|
Packit Service |
2a3f3d |
test_repo_hash (Fixture *fixture,
|
|
Packit Service |
2a3f3d |
gconstpointer test_data)
|
|
Packit Service |
2a3f3d |
{
|
|
Packit Service |
2a3f3d |
g_autoptr(GError) error = NULL;
|
|
Packit Service |
2a3f3d |
g_autoptr(OstreeRepo) repo1 = ostree_repo_create_at (fixture->tmpdir.fd, ".",
|
|
Packit Service |
2a3f3d |
OSTREE_REPO_MODE_ARCHIVE,
|
|
Packit Service |
2a3f3d |
NULL,
|
|
Packit Service |
2a3f3d |
NULL, &error);
|
|
Packit Service |
2a3f3d |
g_assert_no_error (error);
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
g_autoptr(OstreeRepo) repo2 = ostree_repo_open_at (fixture->tmpdir.fd, ".",
|
|
Packit Service |
2a3f3d |
NULL, &error);
|
|
Packit Service |
2a3f3d |
g_assert_no_error (error);
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
g_assert_cmpuint (ostree_repo_hash (repo1), ==, ostree_repo_hash (repo2));
|
|
Packit Service |
2a3f3d |
}
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
/* Test that trying to hash a closed repo results in an assertion failure. */
|
|
Packit Service |
2a3f3d |
static void
|
|
Packit Service |
2a3f3d |
test_repo_hash_closed (Fixture *fixture,
|
|
Packit Service |
2a3f3d |
gconstpointer test_data)
|
|
Packit Service |
2a3f3d |
{
|
|
Packit Service |
2a3f3d |
if (g_test_subprocess ())
|
|
Packit Service |
2a3f3d |
{
|
|
Packit Service |
2a3f3d |
g_autoptr(GFile) repo_path = g_file_new_for_path (fixture->tmpdir.path);
|
|
Packit Service |
2a3f3d |
g_autoptr(OstreeRepo) repo = ostree_repo_new (repo_path);
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
ostree_repo_hash (repo);
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
return;
|
|
Packit Service |
2a3f3d |
}
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
g_test_trap_subprocess (NULL, 0, 0);
|
|
Packit Service |
2a3f3d |
g_test_trap_assert_failed ();
|
|
Packit Service |
2a3f3d |
g_test_trap_assert_stderr ("*ERROR*ostree_repo_hash: assertion failed:*");
|
|
Packit Service |
2a3f3d |
}
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
/* Test that various repositories test equal (or not) with each other. */
|
|
Packit Service |
2a3f3d |
static void
|
|
Packit Service |
2a3f3d |
test_repo_equal (Fixture *fixture,
|
|
Packit Service |
2a3f3d |
gconstpointer test_data)
|
|
Packit Service |
2a3f3d |
{
|
|
Packit Service |
2a3f3d |
g_autoptr(GError) error = NULL;
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
/* Create a few separate repos and some #OstreeRepo objects for them. */
|
|
Packit Service |
2a3f3d |
glnx_ensure_dir (fixture->tmpdir.fd, "repo1", 0755, &error);
|
|
Packit Service |
2a3f3d |
g_assert_no_error (error);
|
|
Packit Service |
2a3f3d |
glnx_ensure_dir (fixture->tmpdir.fd, "repo2", 0755, &error);
|
|
Packit Service |
2a3f3d |
g_assert_no_error (error);
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
g_autoptr(OstreeRepo) repo1 = ostree_repo_create_at (fixture->tmpdir.fd, "repo1",
|
|
Packit Service |
2a3f3d |
OSTREE_REPO_MODE_ARCHIVE,
|
|
Packit Service |
2a3f3d |
NULL,
|
|
Packit Service |
2a3f3d |
NULL, &error);
|
|
Packit Service |
2a3f3d |
g_assert_no_error (error);
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
g_autoptr(OstreeRepo) repo1_alias = ostree_repo_open_at (fixture->tmpdir.fd, "repo1",
|
|
Packit Service |
2a3f3d |
NULL, &error);
|
|
Packit Service |
2a3f3d |
g_assert_no_error (error);
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
g_autoptr(OstreeRepo) repo2 = ostree_repo_create_at (fixture->tmpdir.fd, "repo2",
|
|
Packit Service |
2a3f3d |
OSTREE_REPO_MODE_ARCHIVE,
|
|
Packit Service |
2a3f3d |
NULL,
|
|
Packit Service |
2a3f3d |
NULL, &error);
|
|
Packit Service |
2a3f3d |
g_assert_no_error (error);
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
g_autoptr(GFile) closed_repo_path = g_file_new_for_path (fixture->tmpdir.path);
|
|
Packit Service |
2a3f3d |
g_autoptr(OstreeRepo) closed_repo = ostree_repo_new (closed_repo_path);
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
/* Test various equalities. */
|
|
Packit Service |
2a3f3d |
g_assert_true (ostree_repo_equal (repo1, repo1));
|
|
Packit Service |
2a3f3d |
g_assert_true (ostree_repo_equal (repo1_alias, repo1_alias));
|
|
Packit Service |
2a3f3d |
g_assert_true (ostree_repo_equal (repo1, repo1_alias));
|
|
Packit Service |
2a3f3d |
g_assert_true (ostree_repo_equal (repo1_alias, repo1));
|
|
Packit Service |
2a3f3d |
g_assert_true (ostree_repo_equal (repo2, repo2));
|
|
Packit Service |
2a3f3d |
g_assert_false (ostree_repo_equal (repo1, repo2));
|
|
Packit Service |
2a3f3d |
g_assert_false (ostree_repo_equal (repo1_alias, repo2));
|
|
Packit Service |
2a3f3d |
g_assert_false (ostree_repo_equal (repo2, repo1));
|
|
Packit Service |
2a3f3d |
g_assert_false (ostree_repo_equal (repo2, repo1_alias));
|
|
Packit Service |
2a3f3d |
g_assert_false (ostree_repo_equal (repo1, closed_repo));
|
|
Packit Service |
2a3f3d |
g_assert_false (ostree_repo_equal (repo1_alias, closed_repo));
|
|
Packit Service |
2a3f3d |
g_assert_false (ostree_repo_equal (closed_repo, repo1));
|
|
Packit Service |
2a3f3d |
g_assert_false (ostree_repo_equal (closed_repo, repo1_alias));
|
|
Packit Service |
2a3f3d |
g_assert_false (ostree_repo_equal (repo2, closed_repo));
|
|
Packit Service |
2a3f3d |
g_assert_false (ostree_repo_equal (closed_repo, repo2));
|
|
Packit Service |
2a3f3d |
g_assert_false (ostree_repo_equal (closed_repo, closed_repo));
|
|
Packit Service |
2a3f3d |
}
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
static void
|
|
Packit Service |
2a3f3d |
test_repo_get_min_free_space (Fixture *fixture,
|
|
Packit Service |
2a3f3d |
gconstpointer test_data)
|
|
Packit Service |
2a3f3d |
{
|
|
Packit Service |
2a3f3d |
g_autoptr (GKeyFile) config = NULL;
|
|
Packit Service |
2a3f3d |
g_autoptr(GError) error = NULL;
|
|
Packit Service |
2a3f3d |
guint64 bytes = 0;
|
|
Packit Service |
2a3f3d |
typedef struct
|
|
Packit Service |
2a3f3d |
{
|
|
Packit Service |
2a3f3d |
const char *val;
|
|
Packit Service |
2a3f3d |
gboolean should_succeed;
|
|
Packit Service |
2a3f3d |
} min_free_space_value;
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
g_autoptr(OstreeRepo) repo = ostree_repo_create_at (fixture->tmpdir.fd, ".",
|
|
Packit Service |
2a3f3d |
OSTREE_REPO_MODE_ARCHIVE,
|
|
Packit Service |
2a3f3d |
NULL,
|
|
Packit Service |
2a3f3d |
NULL, &error);
|
|
Packit Service |
2a3f3d |
g_assert_no_error (error);
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
min_free_space_value values_to_test[] = {
|
|
Packit Service |
2a3f3d |
{"500MB", TRUE },
|
|
Packit Service |
2a3f3d |
{ "0MB", TRUE },
|
|
Packit Service |
2a3f3d |
{ "17179869185GB", FALSE }, /* Overflow parameter: bytes > G_MAXUINT64 */
|
|
Packit Service |
2a3f3d |
{ NULL, FALSE }
|
|
Packit Service |
2a3f3d |
};
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
config = ostree_repo_copy_config (repo);
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
for (guint i = 0; values_to_test[i].val != NULL; i++)
|
|
Packit Service |
2a3f3d |
{
|
|
Packit Service |
2a3f3d |
g_key_file_remove_key (config, "core", "min-free-space-size", NULL);
|
|
Packit Service |
2a3f3d |
g_key_file_set_string (config, "core", "min-free-space-size", values_to_test[i].val);
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
ostree_repo_write_config (repo, config, &error);
|
|
Packit Service |
2a3f3d |
g_assert_no_error (error);
|
|
Packit Service |
2a3f3d |
ostree_repo_reload_config (repo, NULL, &error);
|
|
Packit Service |
2a3f3d |
g_assert_no_error (error);
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
ostree_repo_get_min_free_space_bytes (repo, &bytes, &error);
|
|
Packit Service |
2a3f3d |
if (values_to_test[i].should_succeed)
|
|
Packit Service |
2a3f3d |
g_assert_no_error (error);
|
|
Packit Service |
2a3f3d |
else
|
|
Packit Service |
2a3f3d |
continue;
|
|
Packit Service |
2a3f3d |
}
|
|
Packit Service |
2a3f3d |
}
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
int
|
|
Packit Service |
2a3f3d |
main (int argc,
|
|
Packit Service |
2a3f3d |
char **argv)
|
|
Packit Service |
2a3f3d |
{
|
|
Packit Service |
2a3f3d |
setlocale (LC_ALL, "");
|
|
Packit Service |
2a3f3d |
g_test_init (&argc, &argv, NULL);
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
g_test_add ("/repo/hash", Fixture, NULL, setup,
|
|
Packit Service |
2a3f3d |
test_repo_hash, teardown);
|
|
Packit Service |
2a3f3d |
g_test_add ("/repo/hash/closed", Fixture, NULL, setup,
|
|
Packit Service |
2a3f3d |
test_repo_hash_closed, teardown);
|
|
Packit Service |
2a3f3d |
g_test_add ("/repo/equal", Fixture, NULL, setup,
|
|
Packit Service |
2a3f3d |
test_repo_equal, teardown);
|
|
Packit Service |
2a3f3d |
g_test_add ("/repo/get_min_free_space", Fixture, NULL, setup,
|
|
Packit Service |
2a3f3d |
test_repo_get_min_free_space, teardown);
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
|
|
Packit Service |
2a3f3d |
return g_test_run ();
|
|
Packit Service |
2a3f3d |
}
|