go-hep.org/x/hep@v0.38.1/xrootd/protocol_mock_test.go (about) 1 // Copyright ©2018 The go-hep Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package xrootd 6 7 import ( 8 "context" 9 "net" 10 "reflect" 11 "testing" 12 13 "go-hep.org/x/hep/xrootd/xrdproto" 14 "go-hep.org/x/hep/xrootd/xrdproto/protocol" 15 ) 16 17 func TestSession_Protocol_WithSecurityInfo(t *testing.T) { 18 var protocolVersion int32 = 0x310 19 20 var want = protocol.Response{ 21 BinaryProtocolVersion: protocolVersion, 22 HasSecurityInfo: true, 23 SecurityLevel: xrdproto.Pedantic, 24 SecurityOverrides: []xrdproto.SecurityOverride{{RequestIndex: 1, RequestLevel: xrdproto.SignNeeded}}, 25 Flags: protocol.IsServer | protocol.IsManager | protocol.IsMeta | protocol.IsProxy | protocol.IsSupervisor, 26 } 27 28 serverFunc := func(cancel func(), conn net.Conn) { 29 data, err := xrdproto.ReadRequest(conn) 30 if err != nil { 31 cancel() 32 t.Fatalf("could not read request: %v", err) 33 } 34 35 var gotRequest protocol.Request 36 gotHeader, err := unmarshalRequest(data, &gotRequest) 37 if err != nil { 38 cancel() 39 t.Fatalf("could not unmarshal request: %v", err) 40 } 41 42 if gotRequest.ClientProtocolVersion != protocolVersion { 43 cancel() 44 t.Fatalf("invalid client protocol version was specified:\nwant = %d\ngot = %d\n", protocolVersion, gotRequest.ClientProtocolVersion) 45 } 46 47 err = xrdproto.WriteResponse(conn, gotHeader.StreamID, xrdproto.Ok, want) 48 if err != nil { 49 cancel() 50 t.Fatalf("could not write response: %v", err) 51 } 52 } 53 54 clientFunc := func(cancel func(), client *Client) { 55 client.sessions[client.initialSessionID].protocolVersion = protocolVersion 56 got, err := client.sessions[client.initialSessionID].Protocol(context.Background()) 57 if err != nil { 58 t.Fatalf("invalid protocol call: %v", err) 59 } 60 if !reflect.DeepEqual(got, want) { 61 t.Fatalf("protocol info does not match:\ngot = %v\nwant = %v", got, want) 62 } 63 } 64 65 testClientWithMockServer(serverFunc, clientFunc) 66 }