| 1 | // Copyright 2026 The frp Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //go:build !frps |
| 16 | |
| 17 | package proxy |
| 18 | |
| 19 | import ( |
| 20 | "net" |
| 21 | "testing" |
| 22 | "time" |
| 23 | |
| 24 | "github.com/stretchr/testify/require" |
| 25 | |
| 26 | "github.com/fatedier/frp/pkg/msg" |
| 27 | "github.com/fatedier/frp/pkg/proto/wire" |
| 28 | ) |
| 29 | |
| 30 | func TestReadNatHoleSidUsesSelectedWireProtocol(t *testing.T) { |
| 31 | for _, tc := range []struct { |
| 32 | name string |
| 33 | wireProtocol string |
| 34 | }{ |
| 35 | {name: "v2", wireProtocol: wire.ProtocolV2}, |
| 36 | {name: "v1", wireProtocol: wire.ProtocolV1}, |
| 37 | {name: "default", wireProtocol: ""}, |
| 38 | } { |
| 39 | t.Run(tc.name, func(t *testing.T) { |
| 40 | client, server := net.Pipe() |
| 41 | defer client.Close() |
| 42 | defer server.Close() |
| 43 | setPipeDeadline(t, client, server) |
| 44 | |
| 45 | errCh := make(chan error, 1) |
| 46 | go func() { |
| 47 | writer := msg.NewConn(server, msg.NewReadWriter(server, tc.wireProtocol)) |
| 48 | errCh <- writer.WriteMsg(&msg.NatHoleSid{Sid: "sid"}) |
| 49 | }() |
| 50 | |
| 51 | out, err := readNatHoleSid(client, tc.wireProtocol) |
| 52 | require.NoError(t, err) |
| 53 | require.Equal(t, "sid", out.Sid) |
| 54 | require.NoError(t, <-errCh) |
| 55 | }) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func setPipeDeadline(t *testing.T, conns ...net.Conn) { |
| 60 | t.Helper() |
| 61 | |
| 62 | deadline := time.Now().Add(time.Second) |
| 63 | for _, conn := range conns { |
| 64 | require.NoError(t, conn.SetDeadline(deadline)) |
| 65 | } |
| 66 | } |