From c9d968e276fbf6dd76056fdfe92f636e9205dac1 Mon Sep 17 00:00:00 2001 From: Sean McGrail Date: Wed, 30 Jun 2021 12:40:09 -0700 Subject: [PATCH] Additional Test Cases --- internal/ini/doc.go | 33 +++++++++++++------ internal/ini/ini_parser.go | 12 ------- .../ini/testdata/invalid/bad_section_name | 1 + internal/ini/testdata/invalid/bad_syntax_2 | 1 + internal/ini/testdata/invalid/invalid_keys | 2 ++ internal/ini/testdata/valid/op_sep_in_values | 32 +++++++++++++++--- .../testdata/valid/op_sep_in_values_expected | 30 ++++++++++++++--- internal/ini/visitor.go | 3 ++ internal/ini/walker_test.go | 12 +++++++ 9 files changed, 96 insertions(+), 30 deletions(-) create mode 100644 internal/ini/testdata/invalid/bad_section_name create mode 100644 internal/ini/testdata/invalid/bad_syntax_2 create mode 100644 internal/ini/testdata/invalid/invalid_keys diff --git a/internal/ini/doc.go b/internal/ini/doc.go index 25ce0fe134d..1e55bbd07b9 100644 --- a/internal/ini/doc.go +++ b/internal/ini/doc.go @@ -13,17 +13,30 @@ // } // // Below is the BNF that describes this parser -// Grammar: -// stmt -> value stmt' -// stmt' -> epsilon | op stmt -// value -> number | string | boolean | quoted_string +// Grammar: +// stmt -> section | stmt' +// stmt' -> epsilon | expr +// expr -> value (stmt)* | equal_expr (stmt)* +// equal_expr -> value ( ':' | '=' ) equal_expr' +// equal_expr' -> number | string | quoted_string +// quoted_string -> " quoted_string' +// quoted_string' -> string quoted_string_end +// quoted_string_end -> " // -// section -> [ section' -// section' -> value section_close -// section_close -> ] +// section -> [ section' +// section' -> section_value section_close +// section_value -> number | string_subset | boolean | quoted_string_subset +// quoted_string_subset -> " quoted_string_subset' +// quoted_string_subset' -> string_subset quoted_string_end +// quoted_string_subset -> " +// section_close -> ] // -// SkipState will skip (NL WS)+ +// value -> number | string_subset | boolean +// string -> ? UTF-8 Code-Points except '\n' (U+000A) and '\r\n' (U+000D U+000A) ? +// string_subset -> ? Code-points excepted by grammar except ':' (U+003A), '=' (U+003D), '[' (U+005B), and ']' (U+005D) ? // -// comment -> # comment' | ; comment' -// comment' -> epsilon | value +// SkipState will skip (NL WS)+ +// +// comment -> # comment' | ; comment' +// comment' -> epsilon | value package ini diff --git a/internal/ini/ini_parser.go b/internal/ini/ini_parser.go index 557bf4c3917..12fc7d5aa49 100644 --- a/internal/ini/ini_parser.go +++ b/internal/ini/ini_parser.go @@ -209,18 +209,6 @@ loop: case ValueState: // ValueState requires the previous state to either be an equal expression // or an expression statement. - // - // This grammar occurs when the RHS is a number, word, or quoted string. - // equal_expr -> lit op equal_expr' - // equal_expr' -> number | string | quoted_string - // quoted_string -> " quoted_string' - // quoted_string' -> string quoted_string_end - // quoted_string_end -> " - // - // otherwise - // expr_stmt -> equal_expr (expr_stmt')* - // expr_stmt' -> ws S | op S | MarkComplete - // S -> equal_expr' expr_stmt' switch k.Kind { case ASTKindEqualExpr: // assigning a value to some key diff --git a/internal/ini/testdata/invalid/bad_section_name b/internal/ini/testdata/invalid/bad_section_name new file mode 100644 index 00000000000..ac64f5dd685 --- /dev/null +++ b/internal/ini/testdata/invalid/bad_section_name @@ -0,0 +1 @@ +[ :=foo ] diff --git a/internal/ini/testdata/invalid/bad_syntax_2 b/internal/ini/testdata/invalid/bad_syntax_2 new file mode 100644 index 00000000000..4d3de17351b --- /dev/null +++ b/internal/ini/testdata/invalid/bad_syntax_2 @@ -0,0 +1 @@ +[ foo ]] diff --git a/internal/ini/testdata/invalid/invalid_keys b/internal/ini/testdata/invalid/invalid_keys new file mode 100644 index 00000000000..b4978931da9 --- /dev/null +++ b/internal/ini/testdata/invalid/invalid_keys @@ -0,0 +1,2 @@ +[assumerole] +key[id] = value diff --git a/internal/ini/testdata/valid/op_sep_in_values b/internal/ini/testdata/valid/op_sep_in_values index b13d052a52e..28811e527b8 100644 --- a/internal/ini/testdata/valid/op_sep_in_values +++ b/internal/ini/testdata/valid/op_sep_in_values @@ -1,6 +1,30 @@ -[default] +[case1] sepInValue = =:[foo]]bar[ -output = json -[assumerole] +key:= value1 + +[case2] sepInValue==:[foo]]bar[ -output = json +key = value2 + +[case3] +sepInValue = [] +key== value3 + +[case4] +sepInValue = [value] x=a +key:=value4 + +[case5] +key : value5 + +[case6] +s3 = + [nested6] + key = valuen6 +key :=value6 + +[case7] +s3 = +key :value7 +[sub7] +key ==values7 diff --git a/internal/ini/testdata/valid/op_sep_in_values_expected b/internal/ini/testdata/valid/op_sep_in_values_expected index 7326bac27b3..a2609269eb4 100644 --- a/internal/ini/testdata/valid/op_sep_in_values_expected +++ b/internal/ini/testdata/valid/op_sep_in_values_expected @@ -1,10 +1,32 @@ { - "default": { + "case1": { "sepinvalue": "=:[foo]]bar[", - "output": "json" + "key": "= value1" }, - "assumerole": { + "case2": { "sepinvalue": "=:[foo]]bar[", - "output": "json" + "key": "value2" + }, + "case3": { + "sepinvalue": "[]", + "key": "= value3" + }, + "case4": { + "sepinvalue": "[value] x=a", + "key": "=value4" + }, + "case5": { + "key": "value5" + }, + "case6": { + "s3": "", + "key": "=value6" + }, + "case7": { + "s3": "", + "key": "value7" + }, + "sub7": { + "key": "=values7" } } diff --git a/internal/ini/visitor.go b/internal/ini/visitor.go index 3d4ada85415..a07a6373897 100644 --- a/internal/ini/visitor.go +++ b/internal/ini/visitor.go @@ -63,6 +63,9 @@ func (v *DefaultVisitor) VisitExpr(expr AST) error { rhs := children[1] + // The right-hand value side the equality expression is allowed to contain '[', ']', ':', '=' in the values. + // If the token is not either a literal or one of the token types that identifies those four additional + // tokens then error. if !(rhs.Root.Type() == TokenLit || rhs.Root.Type() == TokenOp || rhs.Root.Type() == TokenSep) { return NewParseError("unexpected token type") } diff --git a/internal/ini/walker_test.go b/internal/ini/walker_test.go index 5ceaa24b780..4d56e022f8d 100644 --- a/internal/ini/walker_test.go +++ b/internal/ini/walker_test.go @@ -106,6 +106,10 @@ func TestInvalidDataFiles(t *testing.T) { path: "./testdata/invalid/bad_syntax_1", expectedParseError: true, }, + { + path: "./testdata/invalid/bad_syntax_2", + expectedParseError: true, + }, { path: "./testdata/invalid/incomplete_section_profile", expectedParseError: true, @@ -114,6 +118,14 @@ func TestInvalidDataFiles(t *testing.T) { path: "./testdata/invalid/syntax_error_comment", expectedParseError: true, }, + { + path: "./testdata/invalid/invalid_keys", + expectedParseError: true, + }, + { + path: "./testdata/invalid/bad_section_name", + expectedParseError: true, + }, } for i, c := range cases {