go-hep.org/x/hep@v0.38.1/xrootd/login_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 // import "go-hep.org/x/hep/xrootd"
     6  
     7  import (
     8  	"context"
     9  	"net"
    10  	"os"
    11  	"reflect"
    12  	"testing"
    13  
    14  	"go-hep.org/x/hep/xrootd/xrdproto"
    15  	"go-hep.org/x/hep/xrootd/xrdproto/login"
    16  )
    17  
    18  func TestSession_Login_Mock(t *testing.T) {
    19  	username := "gopher"
    20  	token := "token"
    21  
    22  	var usernameBytes [8]byte
    23  	copy(usernameBytes[:], username)
    24  
    25  	var want = login.Response{
    26  		SessionID:           [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
    27  		SecurityInformation: []byte("&P=unix"),
    28  	}
    29  
    30  	var wantRequest = login.Request{
    31  		Pid:          int32(os.Getpid()),
    32  		Username:     usernameBytes,
    33  		Capabilities: 4,
    34  		Token:        []byte(token),
    35  	}
    36  
    37  	serverFunc := func(cancel func(), conn net.Conn) {
    38  		data, err := xrdproto.ReadRequest(conn)
    39  		if err != nil {
    40  			cancel()
    41  			t.Fatalf("could not read request: %v", err)
    42  		}
    43  
    44  		var gotRequest login.Request
    45  		gotHeader, err := unmarshalRequest(data, &gotRequest)
    46  		if err != nil {
    47  			cancel()
    48  			t.Fatalf("could not unmarshal request: %v", err)
    49  		}
    50  
    51  		if !reflect.DeepEqual(gotRequest, wantRequest) {
    52  			cancel()
    53  			t.Fatalf("request info does not match:\ngot = %v\nwant= %v", gotRequest, wantRequest)
    54  		}
    55  
    56  		err = xrdproto.WriteResponse(conn, gotHeader.StreamID, xrdproto.Ok, want)
    57  		if err != nil {
    58  			cancel()
    59  			t.Fatalf("could not write response: %v", err)
    60  		}
    61  	}
    62  
    63  	clientFunc := func(cancel func(), client *Client) {
    64  		got, err := client.sessions[client.initialSessionID].Login(context.Background(), username, token)
    65  		if err != nil {
    66  			t.Fatalf("invalid login call: %v", err)
    67  		}
    68  		if !reflect.DeepEqual(got, want) {
    69  			t.Fatalf("login info does not match:\ngot = %v\nwant = %v", got, want)
    70  		}
    71  	}
    72  
    73  	testClientWithMockServer(serverFunc, clientFunc)
    74  }