prog: improve TestDeserializeHelper

1. Allow to not provide Out if it's the same as In.
2. Always check Out.
This commit is contained in:
Dmitry Vyukov 2020-03-22 14:23:35 +01:00
parent ef871b2e4f
commit 2ab437bb1e
4 changed files with 36 additions and 42 deletions

View File

@ -141,7 +141,8 @@ func TestCallSetRandom(t *testing.T) {
func TestDeserialize(t *testing.T) {
TestDeserializeHelper(t, "test", "64", nil, []DeserializeTest{
{
In: `test$struct(&(0x7f0000000000)={0x0, {0x0}})`,
In: `test$struct(&(0x7f0000000000)={0x0, {0x0}})`,
Out: `test$struct(&(0x7f0000000000))`,
},
{
In: `test$struct(&(0x7f0000000000)=0x0)`,
@ -149,34 +150,40 @@ func TestDeserialize(t *testing.T) {
StrictErr: "wrong int arg",
},
{
In: `test$regression1(&(0x7f0000000000)=[{"000000"}, {"0000000000"}])`,
In: `test$regression1(&(0x7f0000000000)=[{"000000"}, {"0000000000"}])`,
Out: `test$regression1(&(0x7f0000000000)=[{}, {}])`,
},
{
In: `test$regression2(&(0x7f0000000000)=[0x1, 0x2, 0x3, 0x4, 0x5, 0x6])`,
In: `test$regression2(&(0x7f0000000000)=[0x1, 0x2, 0x3, 0x4, 0x5, 0x6])`,
Out: `test$regression2(&(0x7f0000000000)=[0x1, 0x2, 0x3, 0x4])`,
},
{
In: `test_excessive_args1(0x0, 0x1, {0x1, &(0x7f0000000000)=[0x1, 0x2]})`,
Out: `test_excessive_args1()`,
StrictErr: "excessive syscall arguments",
},
{
In: `test_excessive_args2(0x0, 0x1, {0x1, &(0x7f0000000000)={0x1, 0x2}})`,
Out: `test_excessive_args2(0x0)`,
StrictErr: "excessive syscall arguments",
},
{
In: `test_excessive_args2(0x0, 0x1, {0x1, &(0x7f0000000000)=nil})`,
Out: `test_excessive_args2(0x0)`,
StrictErr: "excessive syscall arguments",
},
{
In: `test_excessive_args2(0x0, &(0x7f0000000000), 0x0)`,
Out: `test_excessive_args2(0x0)`,
StrictErr: "excessive syscall arguments",
},
{
In: `test$excessive_fields1(&(0x7f0000000000)={0x1, &(0x7f0000000000)=[{0x0}, 0x2]}, {0x1, 0x2, [0x1, 0x2]})`,
Out: `test$excessive_fields1(&(0x7f0000000000)={0x1})`,
StrictErr: "excessive struct excessive_fields fields",
},
{
In: `test$excessive_fields1(0x0)`,
Out: `test$excessive_fields1(0x0)`,
In: `test$excessive_fields1(0x0)`,
},
{
In: `test$excessive_fields1(r0)`,
@ -233,16 +240,10 @@ func TestDeserialize(t *testing.T) {
StrictErr: "wrong int arg",
},
{
In: `test$excessive_fields1(0x0)`,
Out: `test$excessive_fields1(0x0)`,
In: `test$excessive_fields1(0xffffffffffffffff)`,
},
{
In: `test$excessive_fields1(0xffffffffffffffff)`,
Out: `test$excessive_fields1(0xffffffffffffffff)`,
},
{
In: `test$excessive_fields1(0xfffffffffffffffe)`,
Out: `test$excessive_fields1(0xfffffffffffffffe)`,
In: `test$excessive_fields1(0xfffffffffffffffe)`,
},
{
In: `test$excessive_fields1(0xfffffffffffffffd)`,
@ -278,8 +279,7 @@ func TestDeserialize(t *testing.T) {
StrictErr: `out arg const[1, const] has non-default value: 2`,
},
{
In: `test$str1(&(0x7f0000000000)='foo\x00')`,
Out: `test$str1(&(0x7f0000000000)='foo\x00')`,
In: `test$str1(&(0x7f0000000000)='foo\x00')`,
},
{
In: `test$str1(&(0x7f0000000000)='bar\x00')`,
@ -287,8 +287,7 @@ func TestDeserialize(t *testing.T) {
StrictErr: `bad string value "bar\x00", expect ["foo\x00"]`,
},
{
In: `test$str2(&(0x7f0000000000)='bar\x00')`,
Out: `test$str2(&(0x7f0000000000)='bar\x00')`,
In: `test$str2(&(0x7f0000000000)='bar\x00')`,
},
{
In: `test$str2(&(0x7f0000000000)='baz\x00')`,

View File

@ -20,9 +20,9 @@ func InitTargetTest(t *testing.T, os, arch string) *Target {
type DeserializeTest struct {
In string
Out string
Out string // if not set, equals to In
Err string
StrictErr string
StrictErr string // if not set, equals to Err
}
func TestDeserializeHelper(t *testing.T, OS, arch string, transform func(*Target, *Prog), tests []DeserializeTest) {
@ -36,6 +36,12 @@ func TestDeserializeHelper(t *testing.T, OS, arch string, transform func(*Target
if test.Err != "" && test.Out != "" {
t.Fatalf("both Err and Out are set")
}
if test.In == test.Out {
t.Fatalf("In and Out are equal, remove Out in such case\n%v", test.In)
}
if test.Out == "" {
test.Out = test.In
}
for _, mode := range []DeserializeMode{NonStrict, Strict} {
p, err := target.Deserialize([]byte(test.In), mode)
wantErr := test.Err
@ -61,7 +67,7 @@ func TestDeserializeHelper(t *testing.T, OS, arch string, transform func(*Target
}
output := strings.TrimSpace(string(p.Serialize()))
want := strings.TrimSpace(test.Out)
if want != "" && want != output {
if want != output {
t.Fatalf("wrong serialized data:\n%s\nexpect:\n%s\n", output, want)
}
p.SerializeForExec(buf)

View File

@ -21,8 +21,7 @@ func TestNeutralize(t *testing.T) {
Out: `syslog(0x9, 0x0, 0x0)`,
},
{
In: `syslog(0x1, 0x0, 0x0)`,
Out: `syslog(0x1, 0x0, 0x0)`,
In: `syslog(0x1, 0x0, 0x0)`,
},
{
@ -34,16 +33,14 @@ func TestNeutralize(t *testing.T) {
Out: `ptrace$peek(0xffffffffffffffff, 0x0, &(0x7f0000000000))`,
},
{
In: `ptrace(0x1, 0x0)`,
Out: `ptrace(0x1, 0x0)`,
In: `ptrace(0x1, 0x0)`,
},
{
In: `arch_prctl$ARCH_SET_GS(0xf00000001002, 0x0)`,
Out: `arch_prctl$ARCH_SET_GS(0x1001, 0x0)`,
},
{
In: `arch_prctl$ARCH_SET_GS(0x1003, 0x0)`,
Out: `arch_prctl$ARCH_SET_GS(0x1003, 0x0)`,
In: `arch_prctl$ARCH_SET_GS(0x1003, 0x0)`,
},
{
In: `ioctl(0x0, 0x200000c0045877, 0x0)`,
@ -58,32 +55,27 @@ func TestNeutralize(t *testing.T) {
Out: `fanotify_mark(0x1, 0x2, 0x4078e029, 0x3, 0x0)`,
},
{
In: `fanotify_mark(0xffffffffffffffff, 0xffffffffffffffff, 0xfffffffffff8ffff, 0xffffffffffffffff, 0x0)`,
Out: `fanotify_mark(0xffffffffffffffff, 0xffffffffffffffff, 0xfffffffffff8ffff, 0xffffffffffffffff, 0x0)`,
In: `fanotify_mark(0xffffffffffffffff, 0xffffffffffffffff, 0xfffffffffff8ffff, 0xffffffffffffffff, 0x0)`,
},
{
In: `syz_init_net_socket$bt_hci(0x1, 0x0, 0x0)`,
Out: `syz_init_net_socket$bt_hci(0xffffffffffffffff, 0x0, 0x0)`,
},
{
In: `syz_init_net_socket$bt_hci(0x27, 0x0, 0x0)`,
Out: `syz_init_net_socket$bt_hci(0x27, 0x0, 0x0)`,
In: `syz_init_net_socket$bt_hci(0x27, 0x0, 0x0)`,
},
{
In: `syz_init_net_socket$bt_hci(0x1a, 0x0, 0x0)`,
Out: `syz_init_net_socket$bt_hci(0x1a, 0x0, 0x0)`,
In: `syz_init_net_socket$bt_hci(0x1a, 0x0, 0x0)`,
},
{
In: `syz_init_net_socket$bt_hci(0x1f, 0x0, 0x0)`,
Out: `syz_init_net_socket$bt_hci(0x1f, 0x0, 0x0)`,
In: `syz_init_net_socket$bt_hci(0x1f, 0x0, 0x0)`,
},
{
In: `mmap(0x0, 0x0, 0x0, 0x0, 0x0, 0x0)`,
Out: `mmap(0x0, 0x0, 0x0, 0x10, 0x0, 0x0)`,
},
{
In: `mremap(0x0, 0x0, 0x0, 0xcc, 0x0)`,
Out: `mremap(0x0, 0x0, 0x0, 0xcc, 0x0)`,
In: `mremap(0x0, 0x0, 0x0, 0xcc, 0x0)`,
},
{
In: `mremap(0x0, 0x0, 0x0, 0xcd, 0x0)`,

View File

@ -36,13 +36,11 @@ func TestNeutralize(t *testing.T) {
},
{
// major=22, minor=0
In: `mknod(0x0, 0x0, 0x1600)`,
Out: `mknod(0x0, 0x0, 0x1600)`,
In: `mknod(0x0, 0x0, 0x1600)`,
},
{
// major=4, minor=0
In: `mknod(0x0, 0x0, 0x400)`,
Out: `mknod(0x0, 0x0, 0x400)`,
In: `mknod(0x0, 0x0, 0x400)`,
},
{
// major=4, minor=1
@ -76,8 +74,7 @@ func TestNeutralize(t *testing.T) {
},
{
// RLIMIT_CPU
In: `setrlimit(0x0, &(0x7f0000cc0ff0)={0x1, 0x1})`,
Out: `setrlimit(0x0, &(0x7f0000cc0ff0)={0x1, 0x1})`,
In: `setrlimit(0x0, &(0x7f0000cc0ff0)={0x1, 0x1})`,
},
})
}