Skip to content

Commit

Permalink
test(crit): add unit test for SearchPattern method of MemoryReader
Browse files Browse the repository at this point in the history
Signed-off-by: Kouame Behouba Manasse <behouba@gmail.com>
  • Loading branch information
behouba committed Mar 14, 2024
1 parent 624d499 commit 86ddd3c
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions crit/mempages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,66 @@ func TestGetShmemSize(t *testing.T) {
}
}

func TestSearchPattern(t *testing.T) {
pid, err := getTestImgPID()
if err != nil {
t.Fatal(err)
}

mr, err := NewMemoryReader(testImgsDir, pid, sysPageSize)
if err != nil {
t.Fatal(err)
}

testCases := []struct {
name string
pattern string
context int
expectedError error
}{
{
name: "PATH environment variable",
pattern: "PATH=",
expectedError: nil,
},
{
name: "PATH environment variable regex",
pattern: `\bPATH=([^\s]+)\b`,
expectedError: nil,
},
{
name: "PATH environment variable regex with 10 bytes context",
pattern: `\bPATH=([^\s]+)\b`,
context: 10,
expectedError: nil,
},
{
name: "PATH environment variable regex with negative context",
pattern: `\bPATH=([^\s]+)\b`,
context: -1,
expectedError: errors.New("context size cannot be negative"),
},
{
name: "non existing pattern",
pattern: "NON_EXISTENT_PATTERN",
expectedError: errors.New("pattern not found in process memory"),
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
results, err := mr.SearchPattern(tc.pattern, tc.context)
if err != nil && tc.expectedError == nil {
t.Errorf("Unexpected error for pattern %s: %v", tc.pattern, err)
}

if tc.expectedError == nil && len(results) == 0 {
t.Errorf("Expected to find the %s pattern in memory pages", tc.pattern)
}
})
}
}

// helper function to get the PID from the test-imgs directory
func getTestImgPID() (uint32, error) {
psTreeImg, err := getImg(filepath.Join(testImgsDir, "pstree.img"), &pstree.PstreeEntry{})
Expand Down

0 comments on commit 86ddd3c

Please sign in to comment.