Files
navi/internal/netcheck/ping_test.go
T
M4andCursor 57d719f02f
ci / test (macos-latest) (push) Waiting to run
ci / test (ubuntu-latest) (push) Waiting to run
ci / test (windows-latest) (push) Waiting to run
Release 3.8.2.11: idle rev-cache, port-safe Connect, single-instance tray UX.
Faster unchanged polls; orphan-core cleanup on listen ports; clearer busy-port errors; cores only from binDir; incremental server list; CSP without Google Fonts; single-instance activates existing window; close-to-tray without kill-others or balloon.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-01 17:27:47 +03:00

131 lines
3.1 KiB
Go

package netcheck
import (
"context"
"net"
"testing"
"time"
"vpnclient/internal/config"
)
func TestFriendlyDialError(t *testing.T) {
err := &netError{s: "dial tcp 1.2.3.4:22514: connectex: No connection could be made because the target machine actively refused it."}
got := friendlyDialError(err, true)
if got == "" || got == err.Error() {
t.Fatalf("got %q", got)
}
}
type netError struct{ s string }
func (e *netError) Error() string { return e.s }
func (e *netError) Timeout() bool { return false }
func (e *netError) Temporary() bool { return false }
func TestPingProfileEmpty(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r := PingProfile(ctx, "x", config.ProtocolHysteria2, "")
if r.OK || r.Error == "" {
t.Fatalf("%+v", r)
}
}
func TestSplitHostPort(t *testing.T) {
h, p := splitHostPort("example.com:8443", "443")
if h != "example.com" || p != "8443" {
t.Fatalf("%s %s", h, p)
}
h, p = splitHostPort("example.com", "443")
if h != "example.com" || p != "443" {
t.Fatalf("%s %s", h, p)
}
h, p = splitHostPort("[2001:db8::1]:443", "80")
if h != "2001:db8::1" || p != "443" {
t.Fatalf("%s %s", h, p)
}
}
func TestPingHostEmpty(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r := PingHost(ctx, "x", config.ProtocolNaive, "")
if r.OK || r.Error == "" {
t.Fatalf("%+v", r)
}
}
func TestProbeUDPSoftOKWhenSilent(t *testing.T) {
pc, err := net.ListenPacket("udp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer pc.Close()
// Intentionally do not Read — mimics Hy2/AWG ignoring garbage probes.
_, port, err := net.SplitHostPort(pc.LocalAddr().String())
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
start := time.Now()
rtt, soft, err := probeUDP(ctx, "127.0.0.1", port)
if err != nil {
t.Fatalf("expected soft-ok, got err=%v", err)
}
if !soft {
t.Fatal("expected soft=true")
}
if rtt <= 0 {
t.Fatalf("rtt=%v", rtt)
}
if time.Since(start) > 900*time.Millisecond {
t.Fatalf("soft-ok waited too long: %v (should not use long timeout)", time.Since(start))
}
res := PingProfile(ctx, "silent", config.ProtocolHysteria2, "hysteria2://x@127.0.0.1:"+port+"/")
if !res.OK || !res.Soft {
t.Fatalf("PingProfile soft-ok failed: %+v", res)
}
if res.Ms < 1 {
t.Fatalf("ms=%d", res.Ms)
}
if _, ok := BestOK([]Result{res}); ok {
t.Fatal("BestOK must ignore soft-up")
}
}
func TestProbeUDPReplyOK(t *testing.T) {
pc, err := net.ListenPacket("udp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer pc.Close()
go func() {
buf := make([]byte, 64)
n, addr, err := pc.ReadFrom(buf)
if err != nil || n == 0 {
return
}
_, _ = pc.WriteTo([]byte{1}, addr)
}()
_, port, err := net.SplitHostPort(pc.LocalAddr().String())
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
rtt, soft, err := probeUDP(ctx, "127.0.0.1", port)
if err != nil {
t.Fatal(err)
}
if soft {
t.Fatal("reply should be hard-ok")
}
if rtt <= 0 {
t.Fatalf("rtt=%v", rtt)
}
}