aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/subsystem/linux/path_coincidence_test.go
blob: cc6f81d6a7650281e10d8b21d9fa1f3f9e72efdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright 2023 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 linux

import (
	"testing"
	"testing/fstest"

	"github.com/google/syzkaller/pkg/subsystem"
	"github.com/stretchr/testify/assert"
)

func TestBuildCoincidenceMatrix(t *testing.T) {
	vfs := &subsystem.Subsystem{PathRules: []subsystem.PathRule{
		{IncludeRegexp: `^fs/`},
	}}
	ext4 := &subsystem.Subsystem{PathRules: []subsystem.PathRule{
		{IncludeRegexp: `^fs/ext4/`},
	}}
	ntfs := &subsystem.Subsystem{PathRules: []subsystem.PathRule{
		{IncludeRegexp: `^fs/ntfs/`},
	}}
	kernel := &subsystem.Subsystem{PathRules: []subsystem.PathRule{
		{IncludeRegexp: `.*`},
	}}

	fs := fstest.MapFS{
		".git/obj/12345": {},
		"fs/inode.c":     {},
		"fs/ext4/file.c": {},
		"fs/ntfs/file.c": {},
		"fs/fat/file.c":  {},
		"net/socket.c":   {},
	}
	matrix, _, err := BuildCoincidenceMatrix(fs, []*subsystem.Subsystem{vfs, ntfs, ext4, kernel}, nil)
	assert.NoError(t, err)

	// Test total counts.
	assert.Equal(t, 5, matrix.Count(kernel))
	assert.Equal(t, 4, matrix.Count(vfs))
	assert.Equal(t, 1, matrix.Count(ext4))

	// Test pairwise counts.
	assert.Equal(t, 1, matrix.Get(vfs, ext4))
	assert.Equal(t, 1, matrix.Get(vfs, ntfs))
	assert.Equal(t, 0, matrix.Get(ext4, ntfs))
	assert.Equal(t, 4, matrix.Get(kernel, vfs))
}