mirror of
https://github.com/tailscale/tailscale.git
synced 2024-12-12 11:14:40 +00:00
7c1d6e35a5
Updates #11058 Change-Id: I35e7ef9b90e83cac04ca93fd964ad00ed5b48430 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
85 lines
1.5 KiB
Go
85 lines
1.5 KiB
Go
// Copyright (c) Tailscale Inc & AUTHORS
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package safesocket
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"runtime"
|
|
"testing"
|
|
)
|
|
|
|
// downgradeSDDL is a no-op test helper on non-Windows systems.
|
|
var downgradeSDDL = func() func() { return func() {} }
|
|
|
|
func TestBasics(t *testing.T) {
|
|
// Make the socket in a temp dir rather than the cwd
|
|
// so that the test can be run from a mounted filesystem (#2367).
|
|
dir := t.TempDir()
|
|
var sock string
|
|
if runtime.GOOS != "windows" {
|
|
sock = filepath.Join(dir, "test")
|
|
} else {
|
|
sock = fmt.Sprintf(`\\.\pipe\tailscale-test`)
|
|
t.Cleanup(downgradeSDDL())
|
|
}
|
|
|
|
ln, err := Listen(sock)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
errs := make(chan error, 2)
|
|
|
|
go func() {
|
|
s, err := ln.Accept()
|
|
if err != nil {
|
|
errs <- err
|
|
return
|
|
}
|
|
ln.Close()
|
|
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() {
|
|
c, err := Connect(sock)
|
|
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 range 2 {
|
|
if err := <-errs; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|