2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
package safesocket
|
|
|
|
|
|
|
|
import (
|
2024-06-11 02:38:10 +00:00
|
|
|
"context"
|
2020-02-05 22:16:58 +00:00
|
|
|
"fmt"
|
2021-07-08 23:28:07 +00:00
|
|
|
"path/filepath"
|
2022-11-21 17:00:20 +00:00
|
|
|
"runtime"
|
2020-02-05 22:16:58 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2023-04-14 23:52:44 +00:00
|
|
|
// downgradeSDDL is a no-op test helper on non-Windows systems.
|
|
|
|
var downgradeSDDL = func() func() { return func() {} }
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
func TestBasics(t *testing.T) {
|
2021-07-08 23:28:07 +00:00
|
|
|
// 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()
|
2022-11-21 17:00:20 +00:00
|
|
|
var sock string
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
sock = filepath.Join(dir, "test")
|
|
|
|
} else {
|
|
|
|
sock = fmt.Sprintf(`\\.\pipe\tailscale-test`)
|
2023-04-14 23:52:44 +00:00
|
|
|
t.Cleanup(downgradeSDDL())
|
2022-11-21 17:00:20 +00:00
|
|
|
}
|
2021-07-08 23:28:07 +00:00
|
|
|
|
2023-10-26 16:14:17 +00:00
|
|
|
ln, err := Listen(sock)
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-02-11 18:27:57 +00:00
|
|
|
|
|
|
|
errs := make(chan error, 2)
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
go func() {
|
2023-10-26 16:14:17 +00:00
|
|
|
s, err := ln.Accept()
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
2020-02-11 18:27:57 +00:00
|
|
|
errs <- err
|
|
|
|
return
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2023-10-26 16:14:17 +00:00
|
|
|
ln.Close()
|
2020-02-05 22:16:58 +00:00
|
|
|
s.Write([]byte("hello"))
|
|
|
|
|
|
|
|
b := make([]byte, 1024)
|
|
|
|
n, err := s.Read(b)
|
|
|
|
if err != nil {
|
2020-02-11 18:27:57 +00:00
|
|
|
errs <- err
|
|
|
|
return
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2021-01-29 22:32:56 +00:00
|
|
|
t.Logf("server read %d bytes.", n)
|
2020-02-05 22:16:58 +00:00
|
|
|
if string(b[:n]) != "world" {
|
2020-02-11 18:27:57 +00:00
|
|
|
errs <- fmt.Errorf("got %#v, expected %#v\n", string(b[:n]), "world")
|
|
|
|
return
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
s.Close()
|
2020-02-11 18:27:57 +00:00
|
|
|
errs <- nil
|
2020-02-05 22:16:58 +00:00
|
|
|
}()
|
|
|
|
|
2020-02-11 18:27:57 +00:00
|
|
|
go func() {
|
2024-06-11 02:38:10 +00:00
|
|
|
c, err := ConnectContext(context.Background(), sock)
|
2020-02-11 18:27:57 +00:00
|
|
|
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
|
|
|
|
}()
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2024-04-16 20:15:13 +00:00
|
|
|
for range 2 {
|
2020-02-11 18:27:57 +00:00
|
|
|
if err := <-errs; err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
}
|