Skip to content

Commit

Permalink
test: add function to test processes memory pages reading features
Browse files Browse the repository at this point in the history
This commit introduces a new function `readMemoryPages` that reads
the process arguments and environment variables from memory pages
and compares them with the `environ` and `cmdline` files of the process.

Signed-off-by: Kouame Behouba Manasse <[email protected]>
  • Loading branch information
behouba committed Jun 16, 2023
1 parent 8d118f4 commit 4c337d0
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
2 changes: 2 additions & 0 deletions test/crit/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ test-imgs: ../loop/loop
mkdir -p $@
$(CRIU) dump -v4 -o dump.log -D $@ -t $(PID)
$(CRIU) restore -v4 -o restore.log -D $@ -d
cp /proc/${PID}/environ $@
cp /proc/${PID}/cmdline $@
pkill -9 loop

../../crit/bin/crit:
Expand Down
65 changes: 65 additions & 0 deletions test/crit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand All @@ -12,6 +13,7 @@ import (

"github.com/checkpoint-restore/go-criu/v6/crit"
"github.com/checkpoint-restore/go-criu/v6/crit/cli"
"github.com/checkpoint-restore/go-criu/v6/crit/images/pstree"
)

const testImgDir = "test-imgs"
Expand All @@ -26,6 +28,11 @@ func main() {
if err = recodeImgs(imgs); err != nil {
log.Fatal(err)
}

// Run test for memory pages reading features
if err = readMemoryPages(); err != nil {
log.Fatal(err)
}
log.Println("=== PASS")
}

Expand Down Expand Up @@ -124,3 +131,61 @@ nextFile:

return imgs, nil
}

// readMemoryPages reads and compares process arguments
// and environment variables from memory pages and corresponding test files.
func readMemoryPages() error {
psTreeFile, err := os.Open(filepath.Join(testImgDir, "pstree.img"))
if err != nil {
return err
}
defer psTreeFile.Close()

c := crit.New(psTreeFile, nil, testImgDir, false, true)

psTreeImg, err := c.Decode(&pstree.PstreeEntry{})
if err != nil {
return err
}

pid := psTreeImg.Entries[0].Message.(*pstree.PstreeEntry).GetPid()

mr, err := crit.NewMemoryReader(testImgDir, pid, os.Getpagesize())
if err != nil {
return err
}

// Retrieve process arguments from memory pages
argsBuff, err := mr.GetPsArgs()
if err != nil {
return err
}

// Read process environment variables from the environ test file
testFileArgs, err := ioutil.ReadFile(filepath.Join(testImgDir, "cmdline"))
if err != nil {
return err
}

if !bytes.Equal(testFileArgs, argsBuff.Bytes()) {
return errors.New("process arguments do not match")
}

// Retrieve process environment variables from memory pages
envVarsBuffer, err := mr.GetPsEnvVars()
if err != nil {
return err
}

// Read process environment variables from the environ test file
envVarsTestFile, err := ioutil.ReadFile(filepath.Join(testImgDir, "environ"))
if err != nil {
return err
}

if !bytes.Equal(envVarsTestFile, envVarsBuffer.Bytes()) {
return errors.New("process environment variables do not match")
}

return nil
}

0 comments on commit 4c337d0

Please sign in to comment.