review fixes and a testcase

This commit is contained in:
BVK Chaitanya 2010-08-09 21:42:24 +05:30
parent b0ecfcd360
commit 67a9e4d95d
8 changed files with 119 additions and 17 deletions

View File

@ -19,6 +19,11 @@ functional_test_mod_SOURCES = tests/lib/functional_test.c tests/lib/test.c
functional_test_mod_CFLAGS = $(COMMON_CFLAGS)
functional_test_mod_LDFLAGS = $(COMMON_LDFLAGS)
pkglib_MODULES += test_blockarg.mod
test_blockarg_mod_SOURCES = tests/test_blockarg.c
test_blockarg_mod_CFLAGS = $(COMMON_CFLAGS)
test_blockarg_mod_LDFLAGS = $(COMMON_LDFLAGS)
# Rules for unit tests
check_UTILITIES += example_unit_test
example_unit_test_SOURCES = tests/example_unit_test.c kern/list.c kern/misc.c tests/lib/test.c tests/lib/unit_test.c
@ -74,6 +79,9 @@ grub_script_comments_SOURCES = tests/grub_script_comments.in
check_SCRIPTS += grub_script_functions
grub_script_functions_SOURCES = tests/grub_script_functions.in
check_SCRIPTS += grub_script_blockarg
grub_script_blockarg_SOURCES = tests/grub_script_blockarg.in
# List of tests to execute on "make check"
# SCRIPTED_TESTS = example_scripted_test
# SCRIPTED_TESTS += example_grub_script_test
@ -91,6 +99,7 @@ SCRIPTED_TESTS += grub_script_final_semicolon
SCRIPTED_TESTS += grub_script_dollar
SCRIPTED_TESTS += grub_script_comments
SCRIPTED_TESTS += grub_script_functions
SCRIPTED_TESTS += grub_script_blockarg
# dependencies between tests and testing-tools
$(SCRIPTED_TESTS): grub-shell grub-shell-tester

View File

@ -43,8 +43,8 @@ struct grub_script
struct grub_script_mem *mem;
struct grub_script_cmd *cmd;
/* Other grub_script's from block arguments. */
struct grub_script *siblings;
/* grub_scripts from block arguments. */
struct grub_script *next_siblings;
struct grub_script *children;
};
@ -371,7 +371,7 @@ grub_err_t
grub_normal_parse_line (char *line, grub_reader_getline_t getline);
static inline struct grub_script *
grub_script_get (struct grub_script *script)
grub_script_ref (struct grub_script *script)
{
if (script)
script->refcnt++;
@ -379,7 +379,7 @@ grub_script_get (struct grub_script *script)
}
static inline void
grub_script_put (struct grub_script *script)
grub_script_unref (struct grub_script *script)
{
if (! script)
return;

View File

@ -201,7 +201,7 @@ grub_script_arglist_to_argv (struct grub_script_arglist *arglist,
grub_script_argv_append (&result, arg->str) ||
grub_script_argv_append (&result, "}"))
goto fail;
result.script = grub_script_get (arg->script);
result.script = arg->script;
break;
case GRUB_SCRIPT_ARG_TYPE_TEXT:

View File

@ -34,7 +34,7 @@ grub_normal_parse_line (char *line, grub_reader_getline_t getline)
grub_script_execute (parsed_script);
/* The parsed script was executed, throw it away. */
grub_script_put (parsed_script);
grub_script_unref (parsed_script);
}
return grub_errno;

View File

@ -208,9 +208,9 @@ block: "{"
/* restore old scripts; append $$->script to siblings. */
state->scripts = $<scripts>2 ?: $$->script;
if (s) {
while (s->siblings)
s = s->siblings;
s->siblings = $$->script;
while (s->next_siblings)
s = s->next_siblings;
s->next_siblings = $$->script;
}
}
@ -243,11 +243,12 @@ grubcmd: word arguments0 block0
if ($3)
x = grub_script_add_arglist (state, $2, $3);
if ($1 && x) {
$1->next = x;
$1->argcount += x->argcount;
x->argcount = 0;
}
if ($1 && x)
{
$1->next = x;
$1->argcount += x->argcount;
x->argcount = 0;
}
$$ = grub_script_create_cmdline (state, $1);
}
;

View File

@ -105,8 +105,8 @@ grub_script_free (struct grub_script *script)
s = script->children;
while (s) {
t = s->siblings;
grub_script_put (s);
t = s->next_siblings;
grub_script_unref (s);
s = t;
}
grub_free (script);
@ -355,8 +355,8 @@ grub_script_create (struct grub_script_cmd *cmd, struct grub_script_mem *mem)
parsed->mem = mem;
parsed->cmd = cmd;
parsed->refcnt = 0;
parsed->siblings = 0;
parsed->children = 0;
parsed->next_siblings = 0;
return parsed;
}

View File

@ -0,0 +1,41 @@
#! /bin/bash
# Run GRUB script in a Qemu instance
# Copyright (C) 2010 Free Software Foundation, Inc.
#
# GRUB is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GRUB 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GRUB. If not, see <http://www.gnu.org/licenses/>.
error_if_not () {
if test "$1" != "$2"; then
echo "[$1]" != "[$2]"
exit 1
fi
}
cmd='test_blockarg { true }'
v=`echo "$cmd" | @builddir@/grub-shell`
error_if_not "$v" '{ true }'
tmp=`mktemp`
cmd='test_blockarg { test_blockarg { true } }'
echo "$cmd" | @builddir@/grub-shell >$tmp
error_if_not "`head -n1 $tmp|tail -n1`" '{ test_blockarg { true } }'
error_if_not "`head -n2 $tmp|tail -n1`" '{ true }'
cmd='test_blockarg { test_blockarg { test_blockarg { true } }; test_blockarg { true } }'
echo "$cmd" | @builddir@/grub-shell >$tmp
error_if_not "`head -n1 $tmp|tail -n1`" '{ test_blockarg { test_blockarg { true } }; test_blockarg { true } }'
error_if_not "`head -n2 $tmp|tail -n1`" '{ test_blockarg { true } }'
error_if_not "`head -n3 $tmp|tail -n1`" '{ true }'
error_if_not "`head -n4 $tmp|tail -n1`" '{ true }'

51
tests/test_blockarg.c Normal file
View File

@ -0,0 +1,51 @@
/* test_blockarg.c - print and execute block argument */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2010 Free Software Foundation, Inc.
*
* GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/dl.h>
#include <grub/err.h>
#include <grub/misc.h>
#include <grub/i18n.h>
#include <grub/extcmd.h>
#include <grub/script_sh.h>
static grub_err_t
test_blockarg (grub_extcmd_context_t ctxt, int argc, char **args)
{
if (! ctxt->script)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "no block parameter");
grub_printf ("%s\n", args[argc - 1]);
grub_script_execute (ctxt->script);
return GRUB_ERR_NONE;
}
static grub_extcmd_t cmd;
GRUB_MOD_INIT(test_blockarg)
{
cmd = grub_register_extcmd ("test_blockarg", test_blockarg,
GRUB_COMMAND_FLAG_BOTH | GRUB_COMMAND_FLAG_BLOCKS,
N_("BLOCK"),
N_("Print and execute block argument."), 0);
}
GRUB_MOD_FINI(test_blockarg)
{
grub_unregister_extcmd (cmd);
}