diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2020-03-16 09:23:07 +0100 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2020-03-17 21:19:13 +0100 |
| commit | 0a4d69469bf7e77f26f3036fbb183ecf73368a5d (patch) | |
| tree | 78dd2496dd711c2611dfedb9c9ce56fb26564d06 | |
| parent | 1ea952c9ffd767584c60a91d6f6d8ca603817c50 (diff) | |
prog: factor out common code in tests
Factor out a common test helper for tests that deserialize and check programs.
| -rw-r--r-- | prog/encoding_test.go | 117 | ||||
| -rw-r--r-- | prog/size_test.go | 150 |
2 files changed, 123 insertions, 144 deletions
diff --git a/prog/encoding_test.go b/prog/encoding_test.go index fda0f855f..d7f907e37 100644 --- a/prog/encoding_test.go +++ b/prog/encoding_test.go @@ -140,13 +140,7 @@ func TestCallSetRandom(t *testing.T) { } func TestDeserialize(t *testing.T) { - target := initTargetTest(t, "test", "64") - tests := []struct { - input string - output string - err string - strictErr string - }{ + testDeserialize(t, nil, []deserializeTest{ { input: `test$struct(&(0x7f0000000000)={0x0, {0x0}})`, }, @@ -302,74 +296,73 @@ func TestDeserialize(t *testing.T) { output: `test$str2(&(0x7f0000000000)='foo\x00')`, strictErr: `bad string value "baz\x00", expect ["foo\x00" "bar\x00"]`, }, - } + }) +} + +type deserializeTest struct { + input string + output string + err string + strictErr string +} + +func testDeserialize(t *testing.T, transform func(*Target, *Prog), tests []deserializeTest) { + target := initTargetTest(t, "test", "64") buf := make([]byte, ExecBufferSize) - for _, test := range tests { - if test.strictErr == "" { - test.strictErr = test.err - } - if test.err != "" && test.output != "" { - t.Errorf("both err and output are set") - continue - } - for _, mode := range []DeserializeMode{NonStrict, Strict} { - p, err := target.Deserialize([]byte(test.input), mode) - wantErr := test.err - if mode == Strict { - wantErr = test.strictErr + for testidx, test := range tests { + t.Run(fmt.Sprint(testidx), func(t *testing.T) { + if test.strictErr == "" { + test.strictErr = test.err } - if err != nil { - if wantErr == "" { - t.Errorf("deserialization failed with\n%s\ndata:\n%s\n", - err, test.input) - continue - } - if !strings.Contains(err.Error(), wantErr) { - t.Errorf("deserialization failed with\n%s\nwhich doesn't match\n%s\ndata:\n%s", - err, wantErr, test.input) - continue - } - } else { - if wantErr != "" { - t.Errorf("deserialization should have failed with:\n%s\ndata:\n%s\n", - wantErr, test.input) - continue + if test.err != "" && test.output != "" { + t.Fatalf("both err and output are set") + } + for _, mode := range []DeserializeMode{NonStrict, Strict} { + p, err := target.Deserialize([]byte(test.input), mode) + wantErr := test.err + if mode == Strict { + wantErr = test.strictErr } - output := strings.TrimSpace(string(p.Serialize())) - if test.output != "" && test.output != output { - t.Errorf("wrong serialized data:\n%s\nexpect:\n%s\n", - output, test.output) - continue + if err != nil { + if wantErr == "" { + t.Fatalf("deserialization failed with\n%s\ndata:\n%s\n", + err, test.input) + } + if !strings.Contains(err.Error(), wantErr) { + t.Fatalf("deserialization failed with\n%s\nwhich doesn't match\n%s\ndata:\n%s", + err, wantErr, test.input) + } + } else { + if wantErr != "" { + t.Fatalf("deserialization should have failed with:\n%s\ndata:\n%s\n", + wantErr, test.input) + } + if transform != nil { + transform(target, p) + } + output := strings.TrimSpace(string(p.Serialize())) + if test.output != "" && test.output != output { + t.Fatalf("wrong serialized data:\n%s\nexpect:\n%s\n", + output, test.output) + } + p.SerializeForExec(buf) } - p.SerializeForExec(buf) } - } + }) } } func TestSerializeDeserialize(t *testing.T) { - target := initTargetTest(t, "test", "64") - tests := [][2]string{ + testDeserialize(t, nil, []deserializeTest{ { - `serialize0(&(0x7f0000408000)={"6861736800000000000000000000", "48490000"})`, - `serialize0(&(0x7f0000408000)={'hash\x00', 'HI\x00'})`, + input: `serialize0(&(0x7f0000408000)={"6861736800000000000000000000", "48490000"})`, + output: `serialize0(&(0x7f0000408000)={'hash\x00', 'HI\x00'})`, }, { - `serialize1(&(0x7f0000000000)="0000000000000000", 0x8)`, - `serialize1(&(0x7f0000000000)=""/8, 0x8)`, + input: `serialize1(&(0x7f0000000000)="0000000000000000", 0x8)`, + output: `serialize1(&(0x7f0000000000)=""/8, 0x8)`, }, - } - for _, test := range tests { - p, err := target.Deserialize([]byte(test[0]), Strict) - if err != nil { - t.Fatal(err) - } - data := p.Serialize() - test[1] += "\n" - if string(data) != test[1] { - t.Fatalf("\ngot : %s\nwant: %s", data, test[1]) - } - } + }) } func TestSerializeDeserializeRandom(t *testing.T) { diff --git a/prog/size_test.go b/prog/size_test.go index 5356c5263..7fed10fdc 100644 --- a/prog/size_test.go +++ b/prog/size_test.go @@ -5,7 +5,6 @@ package prog import ( "bytes" - "strings" "testing" ) @@ -29,148 +28,135 @@ func TestAssignSizeRandom(t *testing.T) { } func TestAssignSize(t *testing.T) { - target := initTargetTest(t, "test", "64") // nolint: lll - tests := []struct { - unsizedProg string - sizedProg string - }{ + testDeserialize(t, func(target *Target, p *Prog) { + for _, call := range p.Calls { + target.assignSizesCall(call) + } + }, []deserializeTest{ { - "test$length0(&(0x7f0000000000)={0xff, 0x0})", - "test$length0(&(0x7f0000000000)={0xff, 0x2})", + input: "test$length0(&(0x7f0000000000)={0xff, 0x0})", + output: "test$length0(&(0x7f0000000000)={0xff, 0x2})", }, { - "test$length1(&(0x7f0000001000)={0xff, 0x0})", - "test$length1(&(0x7f0000001000)={0xff, 0x4})", + input: "test$length1(&(0x7f0000001000)={0xff, 0x0})", + output: "test$length1(&(0x7f0000001000)={0xff, 0x4})", }, { - "test$length2(&(0x7f0000001000)={0xff, 0x0})", - "test$length2(&(0x7f0000001000)={0xff, 0x8})", + input: "test$length2(&(0x7f0000001000)={0xff, 0x0})", + output: "test$length2(&(0x7f0000001000)={0xff, 0x8})", }, { - "test$length3(&(0x7f0000005000)={0xff, 0x0, 0x0})", - "test$length3(&(0x7f0000005000)={0xff, 0x4, 0x2})", + input: "test$length3(&(0x7f0000005000)={0xff, 0x0, 0x0})", + output: "test$length3(&(0x7f0000005000)={0xff, 0x4, 0x2})", }, { - "test$length4(&(0x7f0000003000)={0x0, 0x0})", - "test$length4(&(0x7f0000003000)={0x2, 0x2})", + input: "test$length4(&(0x7f0000003000)={0x0, 0x0})", + output: "test$length4(&(0x7f0000003000)={0x2, 0x2})", }, { - "test$length5(&(0x7f0000002000)={0xff, 0x0})", - "test$length5(&(0x7f0000002000)={0xff, 0x4})", + input: "test$length5(&(0x7f0000002000)={0xff, 0x0})", + output: "test$length5(&(0x7f0000002000)={0xff, 0x4})", }, { - "test$length6(&(0x7f0000002000)={[0xff, 0xff, 0xff, 0xff], 0x0})", - "test$length6(&(0x7f0000002000)={[0xff, 0xff, 0xff, 0xff], 0x4})", + input: "test$length6(&(0x7f0000002000)={[0xff, 0xff, 0xff, 0xff], 0x0})", + output: "test$length6(&(0x7f0000002000)={[0xff, 0xff, 0xff, 0xff], 0x4})", }, { - "test$length7(&(0x7f0000003000)={[0xff, 0xff, 0xff, 0xff], 0x0})", - "test$length7(&(0x7f0000003000)={[0xff, 0xff, 0xff, 0xff], 0x8})", + input: "test$length7(&(0x7f0000003000)={[0xff, 0xff, 0xff, 0xff], 0x0})", + output: "test$length7(&(0x7f0000003000)={[0xff, 0xff, 0xff, 0xff], 0x8})", }, { - "test$length8(&(0x7f000001f000)={0x00, {0xff, 0x0, 0x00, [0xff, 0xff, 0xff]}, [{0xff, 0x0, 0x00, [0xff, 0xff, 0xff]}], 0x00, 0x0, [0xff, 0xff]})", - "test$length8(&(0x7f000001f000)={0x32, {0xff, 0x1, 0x10, [0xff, 0xff, 0xff]}, [{0xff, 0x1, 0x10, [0xff, 0xff, 0xff]}], 0x10, 0x1, [0xff, 0xff]})", + input: "test$length8(&(0x7f000001f000)={0x00, {0xff, 0x0, 0x00, [0xff, 0xff, 0xff]}, [{0xff, 0x0, 0x00, [0xff, 0xff, 0xff]}], 0x00, 0x0, [0xff, 0xff]})", + output: "test$length8(&(0x7f000001f000)={0x32, {0xff, 0x1, 0x10, [0xff, 0xff, 0xff]}, [{0xff, 0x1, 0x10, [0xff, 0xff, 0xff]}], 0x10, 0x1, [0xff, 0xff]})", }, { - "test$length9(&(0x7f000001f000)={&(0x7f0000000000/0x5000)=nil, 0x0000})", - "test$length9(&(0x7f000001f000)={&(0x7f0000000000/0x5000)=nil, 0x5000})", + input: "test$length9(&(0x7f000001f000)={&(0x7f0000000000/0x5000)=nil, 0x0000})", + output: "test$length9(&(0x7f000001f000)={&(0x7f0000000000/0x5000)=nil, 0x5000})", }, { - "test$length10(&(0x7f0000000000/0x5000)=nil, 0x0000, 0x0000, 0x0000, 0x0000)", - "test$length10(&(0x7f0000000000/0x5000)=nil, 0x5000, 0x5000, 0x2800, 0x1400)", + input: "test$length10(&(0x7f0000000000/0x5000)=nil, 0x0000, 0x0000, 0x0000, 0x0000)", + output: "test$length10(&(0x7f0000000000/0x5000)=nil, 0x5000, 0x5000, 0x2800, 0x1400)", }, { - "test$length11(&(0x7f0000000000)={0xff, 0xff, [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]}, 0x00)", - "test$length11(&(0x7f0000000000)={0xff, 0xff, [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]}, 0x30)", + input: "test$length11(&(0x7f0000000000)={0xff, 0xff, [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]}, 0x00)", + output: "test$length11(&(0x7f0000000000)={0xff, 0xff, [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]}, 0x30)", }, { - "test$length12(&(0x7f0000000000)={0xff, 0xff, [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]}, 0x00)", - "test$length12(&(0x7f0000000000)={0xff, 0xff, [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]}, 0x30)", + input: "test$length12(&(0x7f0000000000)={0xff, 0xff, [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]}, 0x00)", + output: "test$length12(&(0x7f0000000000)={0xff, 0xff, [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]}, 0x30)", }, { - "test$length13(&(0x7f0000000000)={0xff, 0xff, [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]}, &(0x7f0000001000)=0x00)", - "test$length13(&(0x7f0000000000)={0xff, 0xff, [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]}, &(0x7f0000001000)=0x30)", + input: "test$length13(&(0x7f0000000000)={0xff, 0xff, [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]}, &(0x7f0000001000)=0x00)", + output: "test$length13(&(0x7f0000000000)={0xff, 0xff, [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]}, &(0x7f0000001000)=0x30)", }, { - "test$length14(&(0x7f0000000000)={0xff, 0xff, [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]}, &(0x7f0000001000)=0x00)", - "test$length14(&(0x7f0000000000)={0xff, 0xff, [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]}, &(0x7f0000001000)=0x30)", + input: "test$length14(&(0x7f0000000000)={0xff, 0xff, [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]}, &(0x7f0000001000)=0x00)", + output: "test$length14(&(0x7f0000000000)={0xff, 0xff, [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]}, &(0x7f0000001000)=0x30)", }, { - "test_length15(0xff, 0x0)", - "test_length15(0xff, 0x2)", + input: "test_length15(0xff, 0x0)", + output: "test_length15(0xff, 0x2)", }, { - "test$length16(&(0x7f0000000000)={[0x42, 0x42], 0xff, 0xff, 0xff, 0xff, 0xff})", - "test$length16(&(0x7f0000000000)={[0x42, 0x42], 0x2, 0x10, 0x8, 0x4, 0x2})", + input: "test$length16(&(0x7f0000000000)={[0x42, 0x42], 0xff, 0xff, 0xff, 0xff, 0xff})", + output: "test$length16(&(0x7f0000000000)={[0x42, 0x42], 0x2, 0x10, 0x8, 0x4, 0x2})", }, { - "test$length17(&(0x7f0000000000)={0x42, 0xff, 0xff, 0xff, 0xff})", - "test$length17(&(0x7f0000000000)={0x42, 0x8, 0x4, 0x2, 0x1})", + input: "test$length17(&(0x7f0000000000)={0x42, 0xff, 0xff, 0xff, 0xff})", + output: "test$length17(&(0x7f0000000000)={0x42, 0x8, 0x4, 0x2, 0x1})", }, { - "test$length18(&(0x7f0000000000)={0x42, 0xff, 0xff, 0xff, 0xff})", - "test$length18(&(0x7f0000000000)={0x42, 0x8, 0x4, 0x2, 0x1})", + input: "test$length18(&(0x7f0000000000)={0x42, 0xff, 0xff, 0xff, 0xff})", + output: "test$length18(&(0x7f0000000000)={0x42, 0x8, 0x4, 0x2, 0x1})", }, { - "test$length19(&(0x7f0000000000)={{0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0xff}, 0xff, 0xff, 0xff})", - "test$length19(&(0x7f0000000000)={{0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x14}, 0x14, 0x14, 0x5})", + input: "test$length19(&(0x7f0000000000)={{0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0xff}, 0xff, 0xff, 0xff})", + output: "test$length19(&(0x7f0000000000)={{0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x14}, 0x14, 0x14, 0x5})", }, { - "test$length20(&(0x7f0000000000)={{{0xff, 0xff, 0xff, 0xff}, 0xff, 0xff, 0xff}, 0xff, 0xff})", - "test$length20(&(0x7f0000000000)={{{0x4, 0x4, 0x7, 0x9}, 0x7, 0x7, 0x9}, 0x9, 0x9})", + input: "test$length20(&(0x7f0000000000)={{{0xff, 0xff, 0xff, 0xff}, 0xff, 0xff, 0xff}, 0xff, 0xff})", + output: "test$length20(&(0x7f0000000000)={{{0x4, 0x4, 0x7, 0x9}, 0x7, 0x7, 0x9}, 0x9, 0x9})", }, { - "test$length21(&(0x7f0000000000)=0x0, 0x0)", - "test$length21(&(0x7f0000000000), 0x40)", + input: "test$length21(&(0x7f0000000000)=0x0, 0x0)", + output: "test$length21(&(0x7f0000000000), 0x40)", }, { - "test$length22(&(0x7f0000000000)='12345', 0x0)", - "test$length22(&(0x7f0000000000)='12345', 0x28)", + input: "test$length22(&(0x7f0000000000)='12345', 0x0)", + output: "test$length22(&(0x7f0000000000)='12345', 0x28)", }, { - "test$length23(&(0x7f0000000000)={0x1, {0x2, 0x0}})", - "test$length23(&(0x7f0000000000)={0x1, {0x2, 0x6}})", + input: "test$length23(&(0x7f0000000000)={0x1, {0x2, 0x0}})", + output: "test$length23(&(0x7f0000000000)={0x1, {0x2, 0x6}})", }, { - "test$length24(&(0x7f0000000000)={{0x0, {0x0}}, {0x0, {0x0}}})", - "test$length24(&(0x7f0000000000)={{0x0, {0x8}}, {0x0, {0x10}}})", + input: "test$length24(&(0x7f0000000000)={{0x0, {0x0}}, {0x0, {0x0}}})", + output: "test$length24(&(0x7f0000000000)={{0x0, {0x8}}, {0x0, {0x10}}})", }, { - "test$length26(&(0x7f0000000000), 0x0)", - "test$length26(&(0x7f0000000000), 0x8)", + input: "test$length26(&(0x7f0000000000), 0x0)", + output: "test$length26(&(0x7f0000000000), 0x8)", }, { - "test$length27(&(0x7f0000000000), 0x0)", - "test$length27(&(0x7f0000000000), 0x2a)", + input: "test$length27(&(0x7f0000000000), 0x0)", + output: "test$length27(&(0x7f0000000000), 0x2a)", }, { - "test$length28(&(0x7f0000000000), 0x0)", - "test$length28(&(0x7f0000000000), 0x2a)", + input: "test$length28(&(0x7f0000000000), 0x0)", + output: "test$length28(&(0x7f0000000000), 0x2a)", }, { - "test$length29(&(0x7f0000000000)={'./a\\x00', './b/c\\x00', 0x0, 0x0, 0x0})", - "test$length29(&(0x7f0000000000)={'./a\\x00', './b/c\\x00', 0xa, 0x14, 0x21})", + input: "test$length29(&(0x7f0000000000)={'./a\\x00', './b/c\\x00', 0x0, 0x0, 0x0})", + output: "test$length29(&(0x7f0000000000)={'./a\\x00', './b/c\\x00', 0xa, 0x14, 0x21})", }, { - "test$length30(&(0x7f0000000000)={{{0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, {'a', 'aaa', 'aaaaa', 'aaaaaa'}, &(0x7f0000000000)={'a', 'aaa', 'aaaaa', 'aaaaaa'}, &(0x7f0000000000)=&(0x7f0000000000)={'a', 'aaa', 'aaaaa', 'aaaaaa'}, 0x0}, 0x0}, 0x0, &(0x7f0000000000)=0x0, 0x0)", - "test$length30(&(0x7f0000000000)={{{0x0, 0x18, 0x1, 0x3, 0x5, 0x6}, {'a', 'aaa', 'aaaaa', 'aaaaaa'}, &(0x7f0000000000)={'a', 'aaa', 'aaaaa', 'aaaaaa'}, &(0x7f0000000000)=&(0x7f0000000000)={'a', 'aaa', 'aaaaa', 'aaaaaa'}, 0x2}, 0x4}, 0x40, &(0x7f0000000000)=0x18, 0x2)", + input: "test$length30(&(0x7f0000000000)={{{0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, {'a', 'aaa', 'aaaaa', 'aaaaaa'}, &(0x7f0000000000)={'a', 'aaa', 'aaaaa', 'aaaaaa'}, &(0x7f0000000000)=&(0x7f0000000000)={'a', 'aaa', 'aaaaa', 'aaaaaa'}, 0x0}, 0x0}, 0x0, &(0x7f0000000000)=0x0, 0x0)", + output: "test$length30(&(0x7f0000000000)={{{0x0, 0x18, 0x1, 0x3, 0x5, 0x6}, {'a', 'aaa', 'aaaaa', 'aaaaaa'}, &(0x7f0000000000)={'a', 'aaa', 'aaaaa', 'aaaaaa'}, &(0x7f0000000000)=&(0x7f0000000000)={'a', 'aaa', 'aaaaa', 'aaaaaa'}, 0x2}, 0x4}, 0x40, &(0x7f0000000000)=0x18, 0x2)", }, { - "test$offsetof0(&(0x7f0000000000)={0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0})", - "test$offsetof0(&(0x7f0000000000)={0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x6, 0x8, 0x10, 0x18, 0x18, 0x20})", + input: "test$offsetof0(&(0x7f0000000000)={0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0})", + output: "test$offsetof0(&(0x7f0000000000)={0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x6, 0x8, 0x10, 0x18, 0x18, 0x20})", }, - } - for i, test := range tests { - p, err := target.Deserialize([]byte(test.unsizedProg), Strict) - if err != nil { - t.Fatalf("failed to deserialize prog %v: %v", i, err) - } - for _, call := range p.Calls { - target.assignSizesCall(call) - } - p1 := strings.TrimSpace(string(p.Serialize())) - if p1 != test.sizedProg { - t.Fatalf("failed to assign sizes in prog %v\ngot %v\nwant %v", i, p1, test.sizedProg) - } - } + }) } |
