From d414ee5852791a3cf4f1c3faac2c605edc3dd5f9 Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Wed, 4 Mar 2015 14:14:18 +0100 Subject: [PATCH] tests: add tests for mutable tree. Signed-off-by: Giuseppe Scrivano --- Makefile-tests.am | 5 +- tests/test-mutable-tree.c | 194 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 198 insertions(+), 1 deletion(-) create mode 100644 tests/test-mutable-tree.c diff --git a/Makefile-tests.am b/Makefile-tests.am index 1c3b50b8..b59e1db5 100644 --- a/Makefile-tests.am +++ b/Makefile-tests.am @@ -106,7 +106,7 @@ endif # "make check" do not depend from --enable-installed-tests noinst_PROGRAMS += tests/test-rollsum -TESTS = tests/test-varint tests/test-ot-unix-utils tests/test-bsdiff +TESTS = tests/test-varint tests/test-ot-unix-utils tests/test-bsdiff tests/test-mutable-tree check_PROGRAMS = $(TESTS) TESTS_CFLAGS = $(ostree_bin_shared_cflags) $(OT_INTERNAL_GIO_UNIX_CFLAGS) TESTS_LDADD = $(ostree_bin_shared_ldadd) $(OT_INTERNAL_GIO_UNIX_LIBS) @@ -115,6 +115,9 @@ tests_test_rollsum_SOURCES = src/libostree/ostree-rollsum.c tests/test-rollsum.c tests_test_rollsum_CFLAGS = $(TESTS_CFLAGS) tests_test_rollsum_LDADD = libbupsplit.la $(TESTS_LDADD) +tests_test_mutable_tree_CFLAGS = $(TESTS_CFLAGS) +tests_test_mutable_tree_LDADD = $(TESTS_LDADD) + tests_test_ot_unix_utils_CFLAGS = $(TESTS_CFLAGS) tests_test_ot_unix_utils_LDADD = $(TESTS_LDADD) diff --git a/tests/test-mutable-tree.c b/tests/test-mutable-tree.c new file mode 100644 index 00000000..ef2b6b5b --- /dev/null +++ b/tests/test-mutable-tree.c @@ -0,0 +1,194 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2015 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "config.h" +#include "libgsystem.h" +#include "ostree-mutable-tree.h" +#include +#include +#include +#include +#include "ot-unix-utils.h" + +static void +test_metadata_checksum (void) +{ + const char *checksum = "12345678901234567890123456789012"; + gs_unref_object OstreeMutableTree *tree = ostree_mutable_tree_new (); + + g_assert_null (ostree_mutable_tree_get_metadata_checksum (tree)); + + ostree_mutable_tree_set_metadata_checksum (tree, checksum); + + g_assert_cmpstr (checksum, ==, ostree_mutable_tree_get_metadata_checksum (tree)); +} + +static void +test_mutable_tree_walk (void) +{ + gs_unref_object OstreeMutableTree *tree = ostree_mutable_tree_new (); + gs_unref_object OstreeMutableTree *parent = NULL; + gs_unref_ptrarray GPtrArray *split_path = NULL; + GError *error = NULL; + const char *pathname = "a/b/c/d/e/f/g/i"; + const char *checksum = "01234567890123456789012345678901"; + + g_assert (ot_util_path_split_validate (pathname, &split_path, &error)); + + g_assert (ostree_mutable_tree_ensure_parent_dirs (tree, split_path, + checksum, &parent, + &error)); + { + gs_unref_object OstreeMutableTree *subdir = NULL; + g_assert (ostree_mutable_tree_walk (tree, split_path, 0, &subdir, &error)); + g_assert_nonnull (subdir); + } + + { + gs_unref_object OstreeMutableTree *subdir = NULL; + g_assert_false (ostree_mutable_tree_walk (tree, split_path, 1, &subdir, &error)); + g_assert_null (subdir); + g_clear_error (&error); + } + + { + gs_unref_object OstreeMutableTree *subdir = NULL; + g_assert_false (ostree_mutable_tree_walk (tree, split_path, 10, &subdir, &error)); + g_assert_null (subdir); + g_clear_error (&error); + } + + { + gs_unref_object OstreeMutableTree *subdir = NULL; + gs_unref_object OstreeMutableTree *a = NULL; + gs_free char *source_checksum = NULL; + ostree_mutable_tree_lookup (tree, "a", &source_checksum, &a, &error); + g_assert (ostree_mutable_tree_walk (a, split_path, 1, &subdir, &error)); + g_assert (subdir); + } +} + +static void +test_ensure_parent_dirs (void) +{ + gs_unref_object OstreeMutableTree *tree = ostree_mutable_tree_new (); + gs_unref_object OstreeMutableTree *parent = NULL; + gs_unref_ptrarray GPtrArray *split_path = NULL; + GError *error = NULL; + const char *pathname = "/foo/bar/baz"; + const char *checksum = "01234567890123456789012345678901"; + gs_free char *source_checksum = NULL; + gs_unref_object OstreeMutableTree *source_subdir = NULL; + gs_free char *source_checksum2 = NULL; + gs_unref_object OstreeMutableTree *source_subdir2 = NULL; + + g_assert (ot_util_path_split_validate (pathname, &split_path, &error)); + + g_assert (ostree_mutable_tree_ensure_parent_dirs (tree, split_path, + checksum, &parent, + &error)); + + g_assert (ostree_mutable_tree_lookup (tree, "foo", &source_checksum, + &source_subdir, &error)); + + g_assert_false (ostree_mutable_tree_lookup (tree, "bar", &source_checksum2, + &source_subdir2, &error)); +} + +static void +test_ensure_dir (void) +{ + gs_unref_object OstreeMutableTree *tree = ostree_mutable_tree_new (); + gs_unref_object OstreeMutableTree *parent = NULL; + gs_unref_ptrarray GPtrArray *split_path = NULL; + GError *error = NULL; + const char *dirname = "foo"; + const char *filename = "bar"; + const char *checksum = "01234567890123456789012345678901"; + gs_free char *source_checksum = NULL; + gs_unref_object OstreeMutableTree *source_subdir = NULL; + + g_assert (ostree_mutable_tree_ensure_dir (tree, dirname, &parent, &error)); + g_assert (ostree_mutable_tree_lookup (tree, dirname, &source_checksum, &source_subdir, &error)); + + g_assert (ostree_mutable_tree_replace_file (tree, filename, checksum, &error)); + g_assert_false (ostree_mutable_tree_ensure_dir (tree, filename, &parent, &error)); +} + +static void +test_replace_file (void) +{ + gs_unref_object OstreeMutableTree *tree = ostree_mutable_tree_new (); + gs_unref_object OstreeMutableTree *parent = NULL; + gs_unref_ptrarray GPtrArray *split_path = NULL; + GError *error = NULL; + const char *filename = "bar"; + const char *checksum = "01234567890123456789012345678901"; + const char *checksum2 = "ABCDEF01234567890123456789012345"; + + g_assert (ostree_mutable_tree_replace_file (tree, filename, checksum, &error)); + { + gs_free char *out_checksum = NULL; + gs_unref_object OstreeMutableTree *subdir = NULL; + g_assert (ostree_mutable_tree_lookup (tree, filename, &out_checksum, &subdir, &error)); + g_assert_cmpstr (checksum, ==, out_checksum); + } + + g_assert (ostree_mutable_tree_replace_file (tree, filename, checksum2, &error)); + { + gs_free char *out_checksum = NULL; + gs_unref_object OstreeMutableTree *subdir = NULL; + g_assert (ostree_mutable_tree_lookup (tree, filename, &out_checksum, &subdir, &error)); + g_assert_cmpstr (checksum2, ==, out_checksum); + } +} + +static void +test_contents_checksum (void) +{ + const char *checksum = "01234567890123456789012345678901"; + const char *subdir_checksum = "ABCD0123456789012345678901234567"; + gs_unref_object OstreeMutableTree *tree = ostree_mutable_tree_new (); + gs_unref_object OstreeMutableTree *subdir = NULL; + GError *error = NULL; + g_assert_null (ostree_mutable_tree_get_contents_checksum (tree)); + + ostree_mutable_tree_set_contents_checksum (tree, checksum); + g_assert_cmpstr (checksum, ==, ostree_mutable_tree_get_contents_checksum (tree)); + + g_assert (ostree_mutable_tree_ensure_dir (tree, "subdir", &subdir, &error)); + g_assert_nonnull (subdir); + + ostree_mutable_tree_set_contents_checksum (subdir, subdir_checksum); + g_assert_cmpstr (subdir_checksum, ==, ostree_mutable_tree_get_contents_checksum (subdir)); + g_assert_null (ostree_mutable_tree_get_contents_checksum (tree)); +} + +int main (int argc, char **argv) +{ + g_test_init (&argc, &argv, NULL); + g_test_add_func ("/mutable-tree/metadata-checksum", test_metadata_checksum); + g_test_add_func ("/mutable-tree/contents-checksum", test_contents_checksum); + g_test_add_func ("/mutable-tree/parent-dirs", test_ensure_parent_dirs); + g_test_add_func ("/mutable-tree/walk", test_mutable_tree_walk); + g_test_add_func ("/mutable-tree/ensure-dir", test_ensure_dir); + g_test_add_func ("/mutable-tree/replace-file", test_replace_file); + return g_test_run(); +}