2017-09-21 22:26:06 +03:00
/*
2015-03-04 16:14:18 +03:00
* Copyright ( C ) 2015 Red Hat , Inc .
*
2018-01-30 22:26:26 +03:00
* SPDX - License - Identifier : LGPL - 2.0 +
*
2015-03-04 16:14:18 +03:00
* 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"
2015-05-05 01:08:49 +03:00
# include "libglnx.h"
2015-03-04 16:14:18 +03:00
# include "ostree-mutable-tree.h"
# include <glib.h>
# include <stdlib.h>
# include <gio/gio.h>
# include <string.h>
# include "ot-unix-utils.h"
static void
test_metadata_checksum ( void )
{
2018-06-26 00:56:10 +03:00
g_autoptr ( GError ) error = NULL ;
2015-03-04 16:14:18 +03:00
const char * checksum = " 12345678901234567890123456789012 " ;
2015-05-05 17:33:53 +03:00
glnx_unref_object OstreeMutableTree * tree = ostree_mutable_tree_new ( ) ;
2015-03-04 16:14:18 +03:00
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 ) ) ;
2018-06-26 00:56:10 +03:00
/* If a child tree's metadata changes the parent tree's contents needs to be
* recalculated */
glnx_unref_object OstreeMutableTree * subdir = NULL ;
g_assert ( ostree_mutable_tree_ensure_dir ( tree , " subdir " , & subdir , & error ) ) ;
g_assert_nonnull ( subdir ) ;
ostree_mutable_tree_set_contents_checksum (
subdir , " 11111111111111111111111111111111 " ) ;
ostree_mutable_tree_set_metadata_checksum (
subdir , " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " ) ;
ostree_mutable_tree_set_contents_checksum (
tree , " abcdefabcdefabcdefabcdefabcdefab " ) ;
g_assert_cmpstr ( ostree_mutable_tree_get_contents_checksum ( tree ) , = = ,
" abcdefabcdefabcdefabcdefabcdefab " ) ;
ostree_mutable_tree_set_metadata_checksum (
subdir , " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb " ) ;
g_assert_null ( ostree_mutable_tree_get_contents_checksum ( tree ) ) ;
g_assert_cmpstr ( ostree_mutable_tree_get_contents_checksum ( subdir ) , = = ,
" 11111111111111111111111111111111 " ) ;
2015-03-04 16:14:18 +03:00
}
static void
test_mutable_tree_walk ( void )
{
2015-05-05 17:33:53 +03:00
glnx_unref_object OstreeMutableTree * tree = ostree_mutable_tree_new ( ) ;
glnx_unref_object OstreeMutableTree * parent = NULL ;
2015-05-05 18:44:40 +03:00
g_autoptr ( GPtrArray ) split_path = NULL ;
2015-03-04 16:14:18 +03:00
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 ) ) ;
{
2015-05-05 17:33:53 +03:00
glnx_unref_object OstreeMutableTree * subdir = NULL ;
2015-03-04 16:14:18 +03:00
g_assert ( ostree_mutable_tree_walk ( tree , split_path , 0 , & subdir , & error ) ) ;
g_assert_nonnull ( subdir ) ;
}
{
2015-05-05 17:33:53 +03:00
glnx_unref_object OstreeMutableTree * subdir = NULL ;
2015-03-04 16:14:18 +03:00
g_assert_false ( ostree_mutable_tree_walk ( tree , split_path , 1 , & subdir , & error ) ) ;
g_assert_null ( subdir ) ;
g_clear_error ( & error ) ;
}
{
2015-05-05 17:33:53 +03:00
glnx_unref_object OstreeMutableTree * subdir = NULL ;
glnx_unref_object OstreeMutableTree * a = NULL ;
2015-05-05 00:58:26 +03:00
g_autofree char * source_checksum = NULL ;
2017-07-31 16:27:56 +03:00
g_assert ( ostree_mutable_tree_lookup ( tree , " a " , & source_checksum , & a , & error ) ) ;
g_assert_no_error ( error ) ;
2015-03-04 16:14:18 +03:00
g_assert ( ostree_mutable_tree_walk ( a , split_path , 1 , & subdir , & error ) ) ;
2017-07-31 16:27:56 +03:00
g_assert_no_error ( error ) ;
2015-03-04 16:14:18 +03:00
g_assert ( subdir ) ;
}
}
static void
test_ensure_parent_dirs ( void )
{
2015-05-05 17:33:53 +03:00
glnx_unref_object OstreeMutableTree * tree = ostree_mutable_tree_new ( ) ;
glnx_unref_object OstreeMutableTree * parent = NULL ;
2015-05-05 18:44:40 +03:00
g_autoptr ( GPtrArray ) split_path = NULL ;
2016-12-02 21:45:04 +03:00
g_autoptr ( GError ) error = NULL ;
2015-03-04 16:14:18 +03:00
const char * pathname = " /foo/bar/baz " ;
const char * checksum = " 01234567890123456789012345678901 " ;
2015-05-05 00:58:26 +03:00
g_autofree char * source_checksum = NULL ;
2015-05-05 17:33:53 +03:00
glnx_unref_object OstreeMutableTree * source_subdir = NULL ;
2015-05-05 00:58:26 +03:00
g_autofree char * source_checksum2 = NULL ;
2015-05-05 17:33:53 +03:00
glnx_unref_object OstreeMutableTree * source_subdir2 = NULL ;
2015-03-04 16:14:18 +03:00
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 ) ) ;
2016-12-02 21:45:04 +03:00
g_clear_error ( & error ) ;
2015-03-04 16:14:18 +03:00
}
static void
test_ensure_dir ( void )
{
2015-05-05 17:33:53 +03:00
glnx_unref_object OstreeMutableTree * tree = ostree_mutable_tree_new ( ) ;
glnx_unref_object OstreeMutableTree * parent = NULL ;
2016-12-02 21:45:04 +03:00
g_autoptr ( GError ) error = NULL ;
2015-03-04 16:14:18 +03:00
const char * dirname = " foo " ;
const char * filename = " bar " ;
const char * checksum = " 01234567890123456789012345678901 " ;
2015-05-05 00:58:26 +03:00
g_autofree char * source_checksum = NULL ;
2015-05-05 17:33:53 +03:00
glnx_unref_object OstreeMutableTree * source_subdir = NULL ;
2015-03-04 16:14:18 +03:00
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 ) ) ;
2016-12-02 21:45:04 +03:00
g_clear_error ( & error ) ;
2015-03-04 16:14:18 +03:00
}
static void
test_replace_file ( void )
{
2015-05-05 17:33:53 +03:00
glnx_unref_object OstreeMutableTree * tree = ostree_mutable_tree_new ( ) ;
2016-12-02 21:45:04 +03:00
g_autoptr ( GError ) error = NULL ;
2015-03-04 16:14:18 +03:00
const char * filename = " bar " ;
const char * checksum = " 01234567890123456789012345678901 " ;
const char * checksum2 = " ABCDEF01234567890123456789012345 " ;
g_assert ( ostree_mutable_tree_replace_file ( tree , filename , checksum , & error ) ) ;
{
2015-05-05 00:58:26 +03:00
g_autofree char * out_checksum = NULL ;
2015-05-05 17:33:53 +03:00
glnx_unref_object OstreeMutableTree * subdir = NULL ;
2015-03-04 16:14:18 +03:00
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 ) ) ;
{
2015-05-05 00:58:26 +03:00
g_autofree char * out_checksum = NULL ;
2015-05-05 17:33:53 +03:00
glnx_unref_object OstreeMutableTree * subdir = NULL ;
2015-03-04 16:14:18 +03:00
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 " ;
2015-05-05 17:33:53 +03:00
glnx_unref_object OstreeMutableTree * tree = ostree_mutable_tree_new ( ) ;
glnx_unref_object OstreeMutableTree * subdir = NULL ;
2015-03-04 16:14:18 +03:00
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 ) ) ;
2018-06-26 00:56:10 +03:00
g_assert ( ostree_mutable_tree_ensure_dir ( tree , " subdir " , & subdir , NULL ) ) ;
2015-03-04 16:14:18 +03:00
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 ( ) ;
}