package macosdriver import "testing" func TestCloneSSHPort(t *testing.T) { never := func(int) bool { return false } all := func(int) bool { return true } for _, tt := range []struct { name string sourcePort int reserved map[int]bool inUse func(int) bool want int wantErr bool }{ {"source without forwarding stays off", 0, nil, never, 0, false}, {"next free port after the source", 2222, nil, never, 2223, false}, {"skips ports that are already bound", 2222, nil, func(p int) bool { return p <= 2225 }, 2226, false}, {"skips ports reserved by stopped registered VMs", 2222, map[int]bool{2223: true, 2224: true}, never, 2225, false}, {"skips both reserved and bound ports", 2222, map[int]bool{2223: true}, func(p int) bool { return p == 2224 }, 2225, false}, {"returns an error when the range is exhausted", 65533, nil, all, 0, true}, } { t.Run(tt.name, func(t *testing.T) { got, err := cloneSSHPort(tt.sourcePort, tt.reserved, tt.inUse) if tt.wantErr { if err == nil { t.Fatalf("cloneSSHPort(%d) = %d, nil; want error", tt.sourcePort, got) } return } if err != nil { t.Fatalf("cloneSSHPort(%d) unexpected error: %v", tt.sourcePort, err) } if got != tt.want { t.Fatalf("cloneSSHPort(%d) = %d, want %d", tt.sourcePort, got, tt.want) } }) } }