diff options
| author | Taras Madan <tarasmadan@google.com> | 2022-03-30 11:50:14 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-30 11:50:14 +0200 |
| commit | 42718dd659525414aa0bf2794688ac94a32f7764 (patch) | |
| tree | 669d7e5f67dc6203fd4f4f3b26a538d99fd6c877 /syz-verifier | |
| parent | 6bdac76629d4d80501671c0c312d6b81411481e7 (diff) | |
syz-verifier/exectask.go: add tests
Diffstat (limited to 'syz-verifier')
| -rw-r--r-- | syz-verifier/exectask.go | 14 | ||||
| -rw-r--r-- | syz-verifier/exectask_test.go | 71 |
2 files changed, 84 insertions, 1 deletions
diff --git a/syz-verifier/exectask.go b/syz-verifier/exectask.go index 052469ed7..db947cbcd 100644 --- a/syz-verifier/exectask.go +++ b/syz-verifier/exectask.go @@ -66,6 +66,12 @@ func MakeExecTask(prog *prog.Prog) *ExecTask { return task } +func ExecTasksQueued() int { + ChanMapMutex.Lock() + defer ChanMapMutex.Unlock() + return len(TaskIDToExecResultChan) +} + func DeleteExecTask(task *ExecTask) { ChanMapMutex.Lock() defer ChanMapMutex.Unlock() @@ -88,22 +94,28 @@ func MakeExecTaskQueue() *ExecTaskQueue { // ExecTaskQueue respects the pq.priority. Internally it is a thread-safe PQ. type ExecTaskQueue struct { pq ExecTaskPriorityQueue + mu sync.Mutex } // PopTask return false if no tasks are available. func (q *ExecTaskQueue) PopTask() (*ExecTask, bool) { + q.mu.Lock() + defer q.mu.Unlock() if q.pq.Len() == 0 { return nil, false } - return heap.Pop(&q.pq).(*ExecTask), true } func (q *ExecTaskQueue) PushTask(task *ExecTask) { + q.mu.Lock() + defer q.mu.Unlock() heap.Push(&q.pq, task) } func (q *ExecTaskQueue) Len() int { + q.mu.Lock() + defer q.mu.Unlock() return q.pq.Len() } diff --git a/syz-verifier/exectask_test.go b/syz-verifier/exectask_test.go new file mode 100644 index 000000000..af7825bda --- /dev/null +++ b/syz-verifier/exectask_test.go @@ -0,0 +1,71 @@ +// Copyright 2022 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. + +package main + +import ( + "testing" +) + +func TestExecTask_MakeDelete(t *testing.T) { + program := getTestProgram(t) + if l := ExecTasksQueued(); l != 0 { + t.Errorf("expected to see empty map, current size is %v", l) + } + task := MakeExecTask(program) + if l := ExecTasksQueued(); l != 1 { + t.Errorf("expected map len is 0, current size is %v", l) + } + DeleteExecTask(task) + if l := ExecTasksQueued(); l != 0 { + t.Errorf("expected map len is 0, current size is %v", l) + } +} + +func TestExecTask_ToRPC(t *testing.T) { + program := getTestProgram(t) + task := MakeExecTask(program) + if task.ToRPC() == nil { + t.Errorf("rpcView generation failed") + } +} + +func TestGetExecResultChan(t *testing.T) { + if l := ExecTasksQueued(); l != 0 { + t.Errorf("expected to see empty map, current size is %v", l) + } + ch := GetExecResultChan(100) + if l := ExecTasksQueued(); l != 0 { + t.Errorf("expected to see empty map, current size is %v", l) + } + if ch != nil { + t.Errorf("expected to see nil channel") + } +} + +func TestExecTaskQueue_PushTask(t *testing.T) { + q := MakeExecTaskQueue() + if l := q.Len(); l != 0 { + t.Errorf("expected to see zero len, current is %v", l) + } + q.PushTask(MakeExecTask(getTestProgram(t))) + if l := q.Len(); l != 1 { + t.Errorf("expected to see single element, current size is %v", l) + } +} + +func TestExecTaskQueue_PopTask(t *testing.T) { + q := MakeExecTaskQueue() + task, gotResult := q.PopTask() + if task != nil || gotResult != false { + t.Errorf("empty queue operation error") + } + program := getTestProgram(t) + q.PushTask(MakeExecTask(program)) + q.PushTask(MakeExecTask(program)) + q.PushTask(MakeExecTask(program)) + task, gotResult = q.PopTask() + if task == nil || gotResult == false { + t.Errorf("non-empty task or error was expected") + } +} |
