2017-09-21 22:26:06 +03:00
/*
2015-03-05 17:40:52 +03:00
* Copyright ( C ) 2015 Red Hat , Inc .
*
2018-01-30 22:26:26 +03:00
* SPDX - License - Identifier : LGPL - 2.0 +
*
2015-03-05 17:40:52 +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
2021-12-07 04:20:55 +03:00
* License along with this library . If not , see < https : //www.gnu.org/licenses/>.
2015-03-05 17:40:52 +03:00
*/
# include "config.h"
2015-05-05 01:08:49 +03:00
# include "libglnx.h"
2023-05-01 21:24:29 +03:00
# include "ot-keyfile-utils.h"
# include <gio/gio.h>
2015-03-05 17:40:52 +03:00
# include <glib.h>
# include <stdlib.h>
# include <string.h>
static GKeyFile * g_keyfile ;
static void
test_get_boolean_with_default ( void )
{
2023-05-01 21:24:29 +03:00
g_autoptr ( GError ) error = NULL ;
2015-03-05 17:40:52 +03:00
gboolean out = FALSE ;
2023-05-01 21:24:29 +03:00
g_assert ( ot_keyfile_get_boolean_with_default ( g_keyfile , " section " , " a_boolean_true " , FALSE ,
& out , & error ) ) ;
2015-03-05 17:40:52 +03:00
g_assert_true ( out ) ;
2023-05-01 21:24:29 +03:00
g_assert ( ot_keyfile_get_boolean_with_default ( g_keyfile , " section " , " a_boolean_false " , TRUE ,
& out , & error ) ) ;
2015-03-05 17:40:52 +03:00
g_assert_false ( out ) ;
2023-05-01 21:24:29 +03:00
g_assert ( ot_keyfile_get_boolean_with_default ( g_keyfile , " section " , " a_not_existing_boolean " ,
TRUE , & out , & error ) ) ;
2015-03-05 17:40:52 +03:00
g_assert_true ( out ) ;
2016-11-29 06:03:53 +03:00
g_clear_error ( & error ) ;
2023-05-01 21:24:29 +03:00
g_assert ( ot_keyfile_get_boolean_with_default ( g_keyfile , " a_fake_section " , " a_boolean_true " ,
FALSE , & out , & error ) ) ;
2015-03-05 17:40:52 +03:00
}
static void
test_get_value_with_default ( void )
{
2023-05-01 21:24:29 +03:00
g_autoptr ( GError ) error = NULL ;
2016-11-29 06:03:53 +03:00
g_autofree char * out = NULL ;
2015-03-05 17:40:52 +03:00
GLogLevelFlags always_fatal_mask ;
const char * section = " section " ;
/* Avoid that g_return_val_if_fail causes the test to fail. */
always_fatal_mask = g_log_set_always_fatal ( 0 ) ;
2023-05-01 21:24:29 +03:00
g_assert_false (
ot_keyfile_get_value_with_default ( g_keyfile , NULL , " value_foo " , " none " , & out , & error ) ) ;
2016-11-29 06:03:53 +03:00
g_clear_pointer ( & out , g_free ) ;
2023-05-01 21:24:29 +03:00
g_assert_false (
ot_keyfile_get_value_with_default ( g_keyfile , section , NULL , " none " , & out , & error ) ) ;
2016-11-29 06:03:53 +03:00
g_clear_pointer ( & out , g_free ) ;
2023-05-01 21:24:29 +03:00
g_assert_false (
ot_keyfile_get_value_with_default ( g_keyfile , section , NULL , " something " , & out , & error ) ) ;
2016-11-29 06:03:53 +03:00
g_clear_pointer ( & out , g_free ) ;
2015-03-05 17:40:52 +03:00
/* Restore the old mask. */
g_log_set_always_fatal ( always_fatal_mask ) ;
2023-05-01 21:24:29 +03:00
g_assert (
ot_keyfile_get_value_with_default ( g_keyfile , section , " value_foo " , " none " , & out , & error ) ) ;
2015-03-05 17:40:52 +03:00
g_assert_cmpstr ( out , = = , " foo " ) ;
2016-11-29 06:03:53 +03:00
g_clear_pointer ( & out , g_free ) ;
2015-03-05 17:40:52 +03:00
2023-05-01 21:24:29 +03:00
g_assert ( ot_keyfile_get_value_with_default ( g_keyfile , section , " a_not_existing_value " ,
" correct " , & out , & error ) ) ;
2015-03-05 17:40:52 +03:00
g_assert_cmpstr ( out , = = , " correct " ) ;
2016-11-29 06:03:53 +03:00
g_clear_pointer ( & out , g_free ) ;
2015-03-05 17:40:52 +03:00
2023-05-01 21:24:29 +03:00
g_assert ( ot_keyfile_get_value_with_default ( g_keyfile , " a_fake_section " , " a_value_true " ,
" no value " , & out , & error ) ) ;
2019-11-07 19:06:39 +03:00
g_assert_cmpstr ( out , = = , " no value " ) ;
2019-02-20 08:56:54 +03:00
g_clear_pointer ( & out , g_free ) ;
}
static void
test_get_value_with_default_group_optional ( void )
{
2023-05-01 21:24:29 +03:00
g_autoptr ( GError ) error = NULL ;
2019-02-20 08:56:54 +03:00
g_autofree char * out = NULL ;
GLogLevelFlags always_fatal_mask ;
const char * section = " section " ;
2023-05-01 21:24:29 +03:00
/* Avoid that g_return_val_if_fail causes the test to fail. */
2019-02-20 08:56:54 +03:00
always_fatal_mask = g_log_set_always_fatal ( 0 ) ;
2023-05-01 21:24:29 +03:00
g_assert_false ( ot_keyfile_get_value_with_default_group_optional ( g_keyfile , NULL , " value_foo " ,
" none " , & out , & error ) ) ;
2019-02-20 08:56:54 +03:00
g_clear_pointer ( & out , g_free ) ;
2023-05-01 21:24:29 +03:00
g_assert_false ( ot_keyfile_get_value_with_default_group_optional ( g_keyfile , section , NULL ,
" none " , & out , & error ) ) ;
2019-02-20 08:56:54 +03:00
g_clear_pointer ( & out , g_free ) ;
2023-05-01 21:24:29 +03:00
g_assert_false ( ot_keyfile_get_value_with_default_group_optional ( g_keyfile , section , NULL ,
" something " , & out , & error ) ) ;
2019-02-20 08:56:54 +03:00
g_clear_pointer ( & out , g_free ) ;
/* Restore the old mask. */
g_log_set_always_fatal ( always_fatal_mask ) ;
2023-05-01 21:24:29 +03:00
g_assert ( ot_keyfile_get_value_with_default_group_optional ( g_keyfile , section , " value_foo " ,
" none " , & out , & error ) ) ;
2019-02-20 08:56:54 +03:00
g_assert_cmpstr ( out , = = , " foo " ) ;
g_clear_pointer ( & out , g_free ) ;
2023-05-01 21:24:29 +03:00
g_assert ( ot_keyfile_get_value_with_default_group_optional (
g_keyfile , section , " a_not_existing_value " , " correct " , & out , & error ) ) ;
2019-02-20 08:56:54 +03:00
g_assert_cmpstr ( out , = = , " correct " ) ;
g_clear_pointer ( & out , g_free ) ;
2023-05-01 21:24:29 +03:00
g_assert ( ot_keyfile_get_value_with_default_group_optional (
g_keyfile , " an_optional_section " , " a_value_true " , " no value " , & out , & error ) ) ;
2016-11-29 06:03:53 +03:00
g_clear_error ( & error ) ;
g_clear_pointer ( & out , g_free ) ;
2015-03-05 17:40:52 +03:00
}
static void
test_copy_group ( void )
{
2019-10-18 17:48:44 +03:00
gsize length , length2 ;
2015-03-05 17:40:52 +03:00
const char * section = " section " ;
GLogLevelFlags always_fatal_mask ;
/* Avoid that g_return_val_if_fail causes the test to fail. */
always_fatal_mask = g_log_set_always_fatal ( 0 ) ;
2023-05-01 21:24:29 +03:00
g_autoptr ( GKeyFile ) tmp = g_key_file_new ( ) ;
2019-10-18 17:48:44 +03:00
2015-03-05 17:40:52 +03:00
g_assert_false ( ot_keyfile_copy_group ( NULL , tmp , section ) ) ;
g_assert_false ( ot_keyfile_copy_group ( g_keyfile , NULL , section ) ) ;
g_assert_false ( ot_keyfile_copy_group ( g_keyfile , tmp , NULL ) ) ;
/* Restore the old mask. */
g_log_set_always_fatal ( always_fatal_mask ) ;
g_assert_true ( ot_keyfile_copy_group ( g_keyfile , tmp , section ) ) ;
2023-05-01 21:24:29 +03:00
g_auto ( GStrv ) keys = g_key_file_get_keys ( g_keyfile , section , & length , NULL ) ;
2019-10-16 22:36:31 +03:00
g_strfreev ( g_key_file_get_keys ( tmp , section , & length2 , NULL ) ) ;
2023-05-01 21:24:29 +03:00
g_assert_cmpint ( length , = = , length2 ) ;
2015-03-05 17:40:52 +03:00
2019-10-18 17:48:44 +03:00
for ( gsize ii = 0 ; ii < length ; ii + + )
2015-03-05 17:40:52 +03:00
{
2019-10-18 17:48:44 +03:00
g_autofree char * value = g_key_file_get_value ( g_keyfile , section , keys [ ii ] , NULL ) ;
g_autofree char * value2 = g_key_file_get_value ( g_keyfile , section , keys [ ii ] , NULL ) ;
2015-03-05 17:40:52 +03:00
g_assert_cmpstr ( value , = = , value2 ) ;
}
}
static void
fill_keyfile ( GKeyFile * file )
{
g_key_file_set_boolean ( file , " section " , " a_boolean_true " , TRUE ) ;
g_key_file_set_boolean ( file , " section " , " a_boolean_false " , FALSE ) ;
g_key_file_set_value ( file , " section " , " value_foo " , " foo " ) ;
g_key_file_set_value ( file , " section " , " value_bar " , " bar " ) ;
}
2023-05-01 21:24:29 +03:00
int
main ( int argc , char * * argv )
2015-03-05 17:40:52 +03:00
{
int ret ;
g_test_init ( & argc , & argv , NULL ) ;
g_keyfile = g_key_file_new ( ) ;
fill_keyfile ( g_keyfile ) ;
g_test_add_func ( " /keyfile-utils/get-boolean-with-default " , test_get_boolean_with_default ) ;
g_test_add_func ( " /keyfile-utils/get-value-with-default " , test_get_value_with_default ) ;
2023-05-01 21:24:29 +03:00
g_test_add_func ( " /keyfile-utils/get-value-with-default-group-optional " ,
test_get_value_with_default_group_optional ) ;
2015-03-05 17:40:52 +03:00
g_test_add_func ( " /keyfile-utils/copy-group " , test_copy_group ) ;
2023-05-01 21:24:29 +03:00
ret = g_test_run ( ) ;
2015-03-05 17:40:52 +03:00
g_key_file_free ( g_keyfile ) ;
return ret ;
}