diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2020-05-01 08:49:17 +0200 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2020-05-01 08:50:11 +0200 |
| commit | 143a10e9d6320fa7e38693bd8df375fcf4446ae6 (patch) | |
| tree | 77f27cf67fef355b5da79378e928f1302deb8200 /pkg/ast | |
| parent | 6ae554d32449c4a9a20ed810025ba795a79e5bbe (diff) | |
pkg/ast: extend tests
Test more functions that we currently don't test.
Diffstat (limited to 'pkg/ast')
| -rw-r--r-- | pkg/ast/parser_test.go | 30 | ||||
| -rw-r--r-- | pkg/ast/testdata/all.txt | 80 | ||||
| -rw-r--r-- | pkg/ast/testdata/errors.txt | 83 |
3 files changed, 114 insertions, 79 deletions
diff --git a/pkg/ast/parser_test.go b/pkg/ast/parser_test.go index edbabf3d3..b5861b5a8 100644 --- a/pkg/ast/parser_test.go +++ b/pkg/ast/parser_test.go @@ -17,6 +17,7 @@ func TestParseAll(t *testing.T) { if err != nil || len(files) == 0 { t.Fatalf("failed to read sys dir: %v", err) } + files = append(files, filepath.FromSlash("testdata/all.txt")) for _, file := range files { data, err := ioutil.ReadFile(file) if err != nil { @@ -52,6 +53,35 @@ func TestParseAll(t *testing.T) { if !bytes.Equal(data, data3) { t.Fatalf("Clone lost data") } + nodes0 := 0 + desc.Walk(func(n Node) { + nodes0++ + if SerializeNode(n) == "" { + t.Fatalf("empty serialized node: %#v", n) + } + }) + nodes1 := 0 + desc.Walk(Recursive(func(n Node) { + nodes1++ + pos, typ, _ := n.Info() + if typ == "" { + t.Fatalf("%v: node has empty typ=%q: %#v", pos, typ, n) + } + })) + nodes2 := 0 + desc.Walk(PostRecursive(func(n Node) { + nodes2++ + })) + if nodes0 != len(desc.Nodes) || nodes1 <= len(desc.Nodes) || nodes1 != nodes2 { + t.Fatalf("bad walk: desc=%v, top=%v recursive=%v, postrecursive=%v", + len(desc.Nodes), nodes0, nodes1, nodes2) + } + desc4 := desc.Filter(func(n Node) bool { return true }) + desc5 := desc.Filter(func(n Node) bool { return false }) + if len(desc4.Nodes) != len(desc.Nodes) || len(desc5.Nodes) != 0 { + t.Fatalf("Filter is broken: desc=%v desc4=%v desc5=%v", + len(desc.Nodes), len(desc4.Nodes), len(desc5.Nodes)) + } }) } } diff --git a/pkg/ast/testdata/all.txt b/pkg/ast/testdata/all.txt index eccdda712..c9398a830 100644 --- a/pkg/ast/testdata/all.txt +++ b/pkg/ast/testdata/all.txt @@ -1,82 +1,4 @@ # Copyright 2017 syzkaller project authors. All rights reserved. # Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. -0x42 ### unexpected int, expecting comment, define, include, resource, identifier -foo ### unexpected '\n', expecting '(', '{', '[', '=' -% ### illegal character U+0025 '%' - -int_flags0 = 0, 0x1, 0xab -int_flags1 = 123ab0x ### bad integer "123ab0x" -int_flags1 == 0, 1 ### unexpected '=', expecting int, identifier, string -int_flags = 0, "foo" ### unexpected string, expecting int, identifier -int_flags2 = ' ### char literal is not terminated -int_flags3 = 'a ### char literal is not terminated -int_flags3 = 'a, 1 ### char literal is not terminated -int_flags4 = 1, -2- ### bad integer "-2-" - -str_flags0 = "foo", "bar" -str_flags1 = "non terminated ### string literal is not terminated -str_flags2 = "bad chars здесь" ### illegal character U+00D0 'Ð' in string literal -str_flags3 = "string", not a string ### unexpected identifier, expecting string, hex string -str_flags4 = "string", 42 ### unexpected int, expecting string, hex string - -call(foo ,int32 , bar int32) ### unexpected ',', expecting int, identifier, string -call(foo int32:"bar") ### unexpected string, expecting int, identifier -call(a int32, b len[a:"bar"]) ### unexpected string, expecting int, identifier -call() (attr) -call() (attr1, attr2[arg1, "arg2"]) -call() ("attr1") -call() (42) -call() ( ### unexpected '\n', expecting int, identifier, string -call() () ### unexpected ')', expecting int, identifier, string - -define FOO bar - -foo(x int32[1:2:3, opt]) -foo2(x int32[1[2]:2]) ### unexpected ':', expecting ']' - -s0 { - f0 string[""] -} - -sf0 = "", "1" - -include <linux/foo.h> -include "linux/foo.h" -incdir </foo/bar> -incdir "/foo/bar" - -s2 { - f1 int8 - - # comment - - f2 int8 - - # comment - -} - -s3 { - f1 int8 -} [attribute[1, "foo"], another[and[another]]] - -type mybool8 int8 -type net_port proc[1, 2, int16be] -type mybool16 ### unexpected '\n', expecting '[', identifier -type type4:4 int32 ### unexpected ':', expecting '[', identifier - -type templ0[] int8 ### unexpected ']', expecting identifier -type templ1[A,] int8 ### unexpected ']', expecting identifier -type templ2[,] int8 ### unexpected ',', expecting identifier -type templ3[ ### unexpected '\n', expecting identifier -type templ4[A] ### unexpected '\n', expecting int, identifier, string -type templ5[A] const[A] -type templ6[A, B] const[A, B] -type templ7[0] ptr[in, int8] ### unexpected int, expecting identifier - -type templ_struct0[A, B] { - len len[parent, int16] - typ const[A, int16] - data B -} [align_4] +incdir <some/path> diff --git a/pkg/ast/testdata/errors.txt b/pkg/ast/testdata/errors.txt new file mode 100644 index 000000000..30af16d9a --- /dev/null +++ b/pkg/ast/testdata/errors.txt @@ -0,0 +1,83 @@ +# Copyright 2017 syzkaller project authors. All rights reserved. +# Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +0x42 ### unexpected int, expecting comment, define, include, resource, identifier +foo ### unexpected '\n', expecting '(', '{', '[', '=' +% ### illegal character U+0025 '%' + +int_flags0 = 0, 0x1, 0xab +int_flags1 = 123ab0x ### bad integer "123ab0x" +int_flags1 == 0, 1 ### unexpected '=', expecting int, identifier, string +int_flags = 0, "foo" ### unexpected string, expecting int, identifier +int_flags2 = ' ### char literal is not terminated +int_flags3 = 'a ### char literal is not terminated +int_flags3 = 'a, 1 ### char literal is not terminated +int_flags4 = 1, -2- ### bad integer "-2-" + +str_flags0 = "foo", "bar" +str_flags1 = "non terminated ### string literal is not terminated +str_flags2 = "bad chars здесь" ### illegal character U+00D0 'Ð' in string literal +str_flags3 = "string", not a string ### unexpected identifier, expecting string, hex string +str_flags4 = "string", 42 ### unexpected int, expecting string, hex string +str_flags5 = `x` ### bad hex string literal: encoding/hex: invalid byte: U+0078 'x' + +call(foo ,int32 , bar int32) ### unexpected ',', expecting int, identifier, string +call(foo int32:"bar") ### unexpected string, expecting int, identifier +call(a int32, b len[a:"bar"]) ### unexpected string, expecting int, identifier +call() (attr) +call() (attr1, attr2[arg1, "arg2"]) +call() ("attr1") +call() (42) +call() ( ### unexpected '\n', expecting int, identifier, string +call() () ### unexpected ')', expecting int, identifier, string + +define FOO bar + +foo(x int32[1:2:3, opt]) +foo2(x int32[1[2]:2]) ### unexpected ':', expecting ']' + +s0 { + f0 string[""] +} + +sf0 = "", "1" + +include <linux/foo.h> +include "linux/foo.h" +incdir </foo/bar> +incdir "/foo/bar" + +s2 { + f1 int8 + + # comment + + f2 int8 + + # comment + +} + +s3 { + f1 int8 +} [attribute[1, "foo"], another[and[another]]] + +type mybool8 int8 +type net_port proc[1, 2, int16be] +type mybool16 ### unexpected '\n', expecting '[', identifier +type type4:4 int32 ### unexpected ':', expecting '[', identifier + +type templ0[] int8 ### unexpected ']', expecting identifier +type templ1[A,] int8 ### unexpected ']', expecting identifier +type templ2[,] int8 ### unexpected ',', expecting identifier +type templ3[ ### unexpected '\n', expecting identifier +type templ4[A] ### unexpected '\n', expecting int, identifier, string +type templ5[A] const[A] +type templ6[A, B] const[A, B] +type templ7[0] ptr[in, int8] ### unexpected int, expecting identifier + +type templ_struct0[A, B] { + len len[parent, int16] + typ const[A, int16] + data B +} [align_4] |
