Something went wrong. Try again.
forked from shirou/gopsutil
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112// SPDX-License-Identifier: BSD-3-Clausepackage process
import ( "encoding/binary" "os" "syscall" "testing" "unicode/utf16" "unsafe"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "golang.org/x/sys/windows")
func TestParseCommandLineInformation(t *testing.T) { ntUnicodeStringHeaderSize := int(unsafe.Offsetof(windows.NTUnicodeString{}.Buffer)) + int(unsafe.Sizeof((*uint16)(nil)))
buildCommandLineInfoBuf := func(s string) []byte { utf16Data := utf16.Encode([]rune(s)) dataBytes := make([]byte, len(utf16Data)*2) for i, u := range utf16Data { binary.LittleEndian.PutUint16(dataBytes[i*2:], u) } buf := make([]byte, ntUnicodeStringHeaderSize+len(dataBytes)) binary.LittleEndian.PutUint16(buf[0:2], uint16(len(dataBytes))) copy(buf[ntUnicodeStringHeaderSize:], dataBytes) return buf } tests := []struct { name string buf []byte expect string expectErr bool }{ { name: "valid command line", buf: buildCommandLineInfoBuf("notepad.exe file.txt"), expect: "notepad.exe file.txt", }, { name: "empty command line", buf: buildCommandLineInfoBuf(""), expect: "", }, { // The kernel sizes the buffer by returnLength, which is not // necessarily header + Length, so anything past Length has to be // ignored. The filler is non-zero on purpose: a slice that ran to // the end of the buffer would otherwise decode to the same string. name: "buffer padded past the declared length", buf: func() []byte { b := buildCommandLineInfoBuf("notepad.exe file.txt") return append(b, 0xAA, 0xBB, 0xCC, 0xDD) }(), expect: "notepad.exe file.txt", }, { name: "buffer too small for length field", buf: []byte{0x01}, expectErr: true, }, { name: "truncated buffer (length claims more than available)", buf: func() []byte { b := buildCommandLineInfoBuf("some command") return b[:len(b)-4] }(), expectErr: true, }, }
for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := parseCommandLineInformation(tt.buf) if tt.expectErr { assert.Error(t, err) return } require.NoError(t, err) assert.Equal(t, tt.expect, got) }) }}
// TestGetProcessCommandLineNativeMatchesPEB verifies that the native// NtQueryInformationProcess path and the PEB read path return the same// command line for the current process.func TestGetProcessCommandLineNativeMatchesPEB(t *testing.T) { pid := int32(os.Getpid()) h, err := windows.OpenProcess( windows.PROCESS_QUERY_INFORMATION|windows.PROCESS_VM_READ, false, uint32(pid), ) require.NoError(t, err) defer syscall.CloseHandle(syscall.Handle(h))
pebCmdline, err := getProcessCommandLinePEB(h) require.NoError(t, err) // A test binary always has a command line. Without this, a regression that // made both paths return "" (a Length read at the wrong offset, say) would // still satisfy the comparison below. require.NotEmpty(t, pebCmdline)
nativeCmdline, err := getProcessCommandLineNative(h, pid) require.NoError(t, err) assert.Equal(t, pebCmdline, nativeCmdline)}