| 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 | package msg |
| 16 | |
| 17 | import ( |
| 18 | "net" |
| 19 | "testing" |
| 20 | |
| 21 | "github.com/stretchr/testify/require" |
| 22 | |
| 23 | "github.com/fatedier/frp/pkg/proto/wire" |
| 24 | ) |
| 25 | |
| 26 | func TestConnReadWriteMsg(t *testing.T) { |
| 27 | tests := []struct { |
| 28 | name string |
| 29 | protocol string |
| 30 | }{ |
| 31 | {name: "v1", protocol: wire.ProtocolV1}, |
| 32 | {name: "v2", protocol: wire.ProtocolV2}, |
| 33 | } |
| 34 | |
| 35 | for _, tt := range tests { |
| 36 | t.Run(tt.name, func(t *testing.T) { |
| 37 | client, server := net.Pipe() |
| 38 | defer client.Close() |
| 39 | defer server.Close() |
| 40 | |
| 41 | clientConn := NewConn(client, NewReadWriter(client, tt.protocol)) |
| 42 | serverConn := NewConn(server, NewReadWriter(server, tt.protocol)) |
| 43 | |
| 44 | in := &Ping{PrivilegeKey: "key", Timestamp: 123} |
| 45 | errCh := make(chan error, 1) |
| 46 | go func() { |
| 47 | errCh <- clientConn.WriteMsg(in) |
| 48 | }() |
| 49 | |
| 50 | out, err := serverConn.ReadMsg() |
| 51 | require.NoError(t, err) |
| 52 | require.Equal(t, in, out) |
| 53 | require.NoError(t, <-errCh) |
| 54 | }) |
| 55 | } |
| 56 | } |