Add extra check for rofi_theme_parse_prepare_file

This commit is contained in:
Dave Davenport 2017-06-14 16:19:17 +02:00
parent c851134411
commit 22aacb8f94
4 changed files with 63 additions and 20 deletions

View File

@ -484,4 +484,14 @@ void rofi_theme_convert_old ( void );
* @returns path to theme or copy of filename if not found.
*/
char *helper_get_theme_path ( const char *file );
/**
* @param file File name to prepare.
* @param parent_file Filename of parent file.
*
* Tries to find full path relative to parent file.
*
* @returns full path to file.
*/
char * rofi_theme_parse_prepare_file ( const char *file, const char *parent_file );
#endif

View File

@ -34,7 +34,6 @@
%{
#include <stdio.h>
#include <glib.h>
#include <gio/gio.h>
#include <helper.h>
#include <math.h>
#include <strings.h>
@ -84,7 +83,6 @@ GQueue *queue = NULL;
ParseObject *current = NULL;
static char * rofi_theme_parse_prepare_file ( const char *file, const char *parent_file );
static double rofi_theme_parse_convert_hex ( char high, char low)
@ -562,24 +560,6 @@ if ( queue == NULL ){
%%
static char * rofi_theme_parse_prepare_file ( const char *file, const char *parent_file )
{
char *filename = rofi_expand_path ( file );
// If no absolute path specified, expand it.
if ( parent_file != NULL && ! g_path_is_absolute ( filename ) ) {
char *basedir = g_path_get_dirname ( parent_file );
char *path = g_build_filename ( basedir, filename, NULL );
g_free ( filename);
filename = path;
g_free ( basedir );
}
GFile *gf = g_file_new_for_path ( filename );
g_free(filename);
filename = g_file_get_path ( gf );
g_object_unref ( gf );
return filename;
}
gboolean rofi_theme_parse_file ( const char *file )
{
char *file2 = helper_get_theme_path ( file );

View File

@ -32,6 +32,8 @@
#include <stdlib.h>
#include <errno.h>
#include <string.h>
// GFile stuff.
#include <gio/gio.h>
#include "theme.h"
#include "theme-parser.h"
#include "helper.h"
@ -779,3 +781,22 @@ void rofi_theme_convert_old ( void )
}
}
#endif // THEME_CONVERTER
char * rofi_theme_parse_prepare_file ( const char *file, const char *parent_file )
{
char *filename = rofi_expand_path ( file );
// If no absolute path specified, expand it.
if ( parent_file != NULL && ! g_path_is_absolute ( filename ) ) {
char *basedir = g_path_get_dirname ( parent_file );
char *path = g_build_filename ( basedir, filename, NULL );
g_free ( filename);
filename = path;
g_free ( basedir );
}
GFile *gf = g_file_new_for_path ( filename );
g_free(filename);
filename = g_file_get_path ( gf );
g_object_unref ( gf );
return filename;
}

View File

@ -177,6 +177,9 @@ START_TEST ( test_core_comments )
ck_assert_ptr_null ( rofi_theme->parent );
ck_assert_str_eq ( rofi_theme->name, "Root" );
// Test comment on last lines
rofi_theme_parse_string ( "// c++ style" );
rofi_theme_parse_string ( "/* c style" );
}
END_TEST
START_TEST ( test_core_newline )
@ -1191,6 +1194,30 @@ START_TEST ( test_import_error )
}
END_TEST
START_TEST ( test_prepare_path )
{
char *current_dir = g_get_current_dir ();
ck_assert_ptr_nonnull ( current_dir );
char *f = rofi_theme_parse_prepare_file ( "../", NULL );
ck_assert_ptr_nonnull ( f );
ck_assert_int_eq ( *f, '/');
ck_assert_str_ne ( f, current_dir);
ck_assert ( g_str_has_prefix( current_dir, f ) == TRUE );
g_free(f);
f = rofi_theme_parse_prepare_file ( "../", "/tmp/" );
ck_assert_ptr_nonnull ( f );
ck_assert_str_eq ( f, "/");
g_free ( f );
f = rofi_theme_parse_prepare_file ( "/tmp/test.rasi" , "/random/");
ck_assert_ptr_nonnull ( f );
ck_assert_str_eq ( f, "/tmp/test.rasi" );
g_free ( f );
g_free ( current_dir );
}
END_TEST
static Suite * theme_parser_suite (void)
{
Suite *s;
@ -1329,6 +1356,11 @@ static Suite * theme_parser_suite (void)
tcase_add_test ( tc_prop_import, test_import_error);
suite_add_tcase(s, tc_prop_import );
}
{
TCase *tc_prepare_path = tcase_create("prepare_path");
tcase_add_test ( tc_prepare_path, test_prepare_path);
suite_add_tcase(s, tc_prepare_path );
}
return s;
}