aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2020-03-22 14:23:35 +0100
committerDmitry Vyukov <dvyukov@google.com>2020-03-24 08:43:00 +0100
commit2ab437bb1e9f9f34d2e080ad11c944464a8bd944 (patch)
tree568bcf0cdf14d2df812018ab48277eaff0f21632
parentef871b2e4ff7b6a8f84a6a0a0fd3e9c3ed8e67b4 (diff)
prog: improve TestDeserializeHelper
1. Allow to not provide Out if it's the same as In. 2. Always check Out.
-rw-r--r--prog/encoding_test.go33
-rw-r--r--prog/test_util.go12
-rw-r--r--sys/linux/init_test.go24
-rw-r--r--sys/openbsd/init_test.go9
4 files changed, 36 insertions, 42 deletions
diff --git a/prog/encoding_test.go b/prog/encoding_test.go
index ed079a37c..b5d17d007 100644
--- a/prog/encoding_test.go
+++ b/prog/encoding_test.go
@@ -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)`,
- Out: `test$excessive_fields1(0xffffffffffffffff)`,
+ In: `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')`,
diff --git a/prog/test_util.go b/prog/test_util.go
index bb96a64e8..7f391792c 100644
--- a/prog/test_util.go
+++ b/prog/test_util.go
@@ -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)
diff --git a/sys/linux/init_test.go b/sys/linux/init_test.go
index fb3068295..ba6dd9462 100644
--- a/sys/linux/init_test.go
+++ b/sys/linux/init_test.go
@@ -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)`,
diff --git a/sys/openbsd/init_test.go b/sys/openbsd/init_test.go
index 30937bade..8ca641f16 100644
--- a/sys/openbsd/init_test.go
+++ b/sys/openbsd/init_test.go
@@ -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})`,
},
})
}