spiffe/spire

Making code involving syscalls testable in `peertracker`

Open

#5,755 opened on Jan 1, 2025

View on GitHub
 (1 comment) (0 reactions) (0 assignees)Go (631 forks)auto 404
help wantedpriority/backlog

Repository metrics

Stars
 (2,443 stars)
PR merge metrics
 (PR metrics pending)

Description

  • Version: v1.11.1
  • Platform: N/A
  • Subsystem: Windows syscalls in peertracker

Relates to https://github.com/spiffe/spire/pull/5749

Some code which makes direct syscalls is hard to test, e.g. compareObjectHandles which behaves differently on different Windows systems.

https://github.com/spiffe/spire/blob/802ea39381d033e0c5a8a855c23de10ac84797cf/pkg/common/peertracker/npipe_windows.go#L67-L76

One way might be to change these sorts of funcs into methods on a type that allows swapping out syscall.SyscallN and other currently global variables for something that can be overridden at test time, so that surrounding logic can be tested (example in https://github.com/spiffe/spire/pull/5749#discussion_r1898703413)

func (s syscaller) compareObjectHandles(firstHandle, secondHandle windows.Handle) error {
	if s.procCompareObjectHandlesErr != nil {
		return s.procCompareObjectHandlesErr
	}
	r1, _, e1 := s.syscallN(*s.procCompareObjectHandlesAddr, uintptr(firstHandle), uintptr(secondHandle))
	if r1 == 0 {
		return e1
	}
	return nil
}

Contributor guide