Skip to content

Commit

Permalink
usm: sowatcher: tests: Added a UT to cover long paths
Browse files Browse the repository at this point in the history
  • Loading branch information
guyarb committed Dec 10, 2024
1 parent 77088a3 commit 6e65069
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions pkg/network/usm/sharedlibraries/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,53 @@ func (s *SharedLibrarySuite) TestSharedLibraryDetection() {
}, time.Second*10, 100*time.Millisecond)
}

func (s *SharedLibrarySuite) TestLongPath() {
t := s.T()

const (
fileName = "foo-libssl.so"
nullTerminatorLength = len("\x00")
)
padLength := LibPathMaxSize - len(fileName) - len(t.TempDir()) - len("_") - len(string(filepath.Separator)) - nullTerminatorLength
fooPath1, fooPathID1 := createTempTestFile(t, strings.Repeat("a", padLength)+"_"+fileName)
// fooPath2 is longer than the limit we have, thus it will be ignored.
fooPath2, fooPathID2 := createTempTestFile(t, strings.Repeat("a", padLength+1)+"_"+fileName)

registerRecorder := new(utils.CallbackRecorder)
unregisterRecorder := new(utils.CallbackRecorder)

watcher, err := NewWatcher(utils.NewUSMEmptyConfig(), LibsetCrypto,
Rule{
Re: regexp.MustCompile(`foo-libssl.so`),
RegisterCB: registerRecorder.Callback(),
UnregisterCB: unregisterRecorder.Callback(),
},
)
require.NoError(t, err)
watcher.Start()
t.Cleanup(watcher.Stop)

// create files
command1, err := fileopener.OpenFromAnotherProcess(t, fooPath1)
require.NoError(t, err)

command2, err := fileopener.OpenFromAnotherProcess(t, fooPath2)
require.NoError(t, err)

require.Eventuallyf(t, func() bool {
return registerRecorder.CallsForPathID(fooPathID1) == 1 &&
registerRecorder.CallsForPathID(fooPathID2) == 0
}, time.Second*10, 100*time.Millisecond, "")

require.NoError(t, command1.Process.Kill())
require.NoError(t, command2.Process.Kill())

require.Eventually(t, func() bool {
return unregisterRecorder.CallsForPathID(fooPathID1) == 1 &&
unregisterRecorder.CallsForPathID(fooPathID2) == 0
}, time.Second*10, 100*time.Millisecond)
}

func (s *SharedLibrarySuite) TestSharedLibraryDetectionWithPIDAndRootNamespace() {
t := s.T()
_, err := os.Stat("/usr/bin/busybox")
Expand Down

0 comments on commit 6e65069

Please sign in to comment.