• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2015 syzkaller project authors. All rights reserved.
2// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
3
4package prog
5
6import (
7	"testing"
8)
9
10func TestParseSingle(t *testing.T) {
11	t.Parallel()
12	target, err := GetTarget("linux", "amd64")
13	if err != nil {
14		t.Fatal(err)
15	}
16	const execLog = `getpid()
17gettid()
18`
19	entries := target.ParseLog([]byte(execLog))
20	if len(entries) != 1 {
21		t.Fatalf("got %v programs, want 1", len(entries))
22	}
23	ent := entries[0]
24	if ent.Start != 0 {
25		t.Fatalf("start offset %v, want 0", ent.Start)
26	}
27	if ent.End != len(execLog) {
28		t.Fatalf("end offset %v, want %v", ent.End, len(execLog))
29	}
30	if ent.Proc != 0 {
31		t.Fatalf("proc %v, want 0", ent.Proc)
32	}
33	if ent.Fault || ent.FaultCall != 0 || ent.FaultNth != 0 {
34		t.Fatalf("fault injection enabled")
35	}
36	want := "getpid-gettid"
37	got := ent.P.String()
38	if got != want {
39		t.Fatalf("bad program: %s, want %s", got, want)
40	}
41}
42
43func TestParseMulti(t *testing.T) {
44	t.Parallel()
45	target, err := GetTarget("linux", "amd64")
46	if err != nil {
47		t.Fatal(err)
48	}
49	entries := target.ParseLog([]byte(execLog))
50	if len(entries) != 5 {
51		for i, ent := range entries {
52			t.Logf("program #%v: %v\n", i, ent.P)
53		}
54		t.Fatalf("got %v programs, want 5", len(entries))
55	}
56	off := 0
57	for _, ent := range entries {
58		if off > ent.Start || ent.Start > ent.End || ent.End > len(execLog) {
59			t.Fatalf("bad offsets")
60		}
61	}
62	if entries[0].Proc != 0 ||
63		entries[1].Proc != 1 ||
64		entries[2].Proc != 2 ||
65		entries[3].Proc != 33 ||
66		entries[4].Proc != 9 {
67		t.Fatalf("bad procs")
68	}
69	for i, ent := range entries {
70		if ent.Fault || ent.FaultCall != 0 || ent.FaultNth != 0 {
71			t.Fatalf("prog %v has fault injection enabled", i)
72		}
73	}
74	if s := entries[0].P.String(); s != "getpid-gettid" {
75		t.Fatalf("bad program 0: %s", s)
76	}
77	if s := entries[1].P.String(); s != "getpid-gettid-munlockall" {
78		t.Fatalf("bad program 0: %s", s)
79	}
80	if s := entries[2].P.String(); s != "getpid-gettid" {
81		t.Fatalf("bad program 1: %s", s)
82	}
83	if s := entries[3].P.String(); s != "gettid-getpid" {
84		t.Fatalf("bad program 2: %s", s)
85	}
86	if s := entries[4].P.String(); s != "munlockall" {
87		t.Fatalf("bad program 3: %s", s)
88	}
89}
90
91const execLog = `
92getpid()
93gettid()
942015/12/21 12:18:05 executing program 1:
95getpid()
96[ 2351.935478] Modules linked in:
97gettid()
98munlockall()
992015/12/21 12:18:05 executing program 2:
100[ 2351.935478] Modules linked in:
101getpid()
102gettid()
1032015/12/21 12:18:05 executing program 33:
104gettid()
105getpid()
106[ 2351.935478] Modules linked in:
1072015/12/21 12:18:05 executing program 9:
108munlockall()
109`
110
111func TestParseFault(t *testing.T) {
112	t.Parallel()
113	target, err := GetTarget("linux", "amd64")
114	if err != nil {
115		t.Fatal(err)
116	}
117	const execLog = `2015/12/21 12:18:05 executing program 1 (fault-call:1 fault-nth:55):
118gettid()
119getpid()
120`
121	entries := target.ParseLog([]byte(execLog))
122	if len(entries) != 1 {
123		t.Fatalf("got %v programs, want 1", len(entries))
124	}
125	ent := entries[0]
126	if !ent.Fault {
127		t.Fatalf("fault injection is not enabled")
128	}
129	if ent.FaultCall != 1 {
130		t.Fatalf("fault call: got %v, want 1", ent.FaultCall)
131	}
132	if ent.FaultNth != 55 {
133		t.Fatalf("fault nth: got %v, want 55", ent.FaultNth)
134	}
135	want := "gettid-getpid"
136	got := ent.P.String()
137	if got != want {
138		t.Fatalf("bad program: %s, want %s", got, want)
139	}
140}
141