safesocket: make clear which net.Conns are winio types

Follow-up to earlier #9049.

Updates #9049

Change-Id: I121fbd2468770233a23ab5ee3df42698ca1dabc2
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2023-10-26 09:14:17 -07:00
committed by Brad Fitzpatrick
parent 95671b71a6
commit b4be4f089f
3 changed files with 117 additions and 23 deletions

View File

@@ -3,7 +3,12 @@
package safesocket
import "tailscale.com/util/winutil"
import (
"fmt"
"testing"
"tailscale.com/util/winutil"
)
func init() {
// downgradeSDDL is a test helper that downgrades the windowsSDDL variable if
@@ -20,3 +25,84 @@ func init() {
return func() {}
}
}
// TestExpectedWindowsTypes is a copy of TestBasics specialized for Windows with
// type assertions about the types of listeners and conns we expect.
func TestExpectedWindowsTypes(t *testing.T) {
t.Cleanup(downgradeSDDL())
const sock = `\\.\pipe\tailscale-test`
ln, err := Listen(sock)
if err != nil {
t.Fatal(err)
}
if got, want := fmt.Sprintf("%T", ln), "*safesocket.winIOPipeListener"; got != want {
t.Errorf("got listener type %q; want %q", got, want)
}
errs := make(chan error, 2)
go func() {
s, err := ln.Accept()
if err != nil {
errs <- err
return
}
ln.Close()
wcc, ok := s.(*WindowsClientConn)
if !ok {
s.Close()
errs <- fmt.Errorf("accepted type %T; want WindowsClientConn", s)
return
}
if wcc.winioPipeConn.Fd() == 0 {
t.Error("accepted conn had unexpected zero fd")
}
if wcc.token == 0 {
t.Error("accepted conn had unexpected zero token")
}
s.Write([]byte("hello"))
b := make([]byte, 1024)
n, err := s.Read(b)
if err != nil {
errs <- err
return
}
t.Logf("server read %d bytes.", n)
if string(b[:n]) != "world" {
errs <- fmt.Errorf("got %#v, expected %#v\n", string(b[:n]), "world")
return
}
s.Close()
errs <- nil
}()
go func() {
s := DefaultConnectionStrategy(sock)
c, err := Connect(s)
if err != nil {
errs <- err
return
}
c.Write([]byte("world"))
b := make([]byte, 1024)
n, err := c.Read(b)
if err != nil {
errs <- err
return
}
if string(b[:n]) != "hello" {
errs <- fmt.Errorf("got %#v, expected %#v\n", string(b[:n]), "hello")
}
c.Close()
errs <- nil
}()
for i := 0; i < 2; i++ {
if err := <-errs; err != nil {
t.Fatal(err)
}
}
}