go-hep.org/x/hep@v0.38.1/xrootd/xrdproto/admin/admin_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 admin_test 6 7 import ( 8 "reflect" 9 "testing" 10 11 "go-hep.org/x/hep/xrootd/internal/xrdenc" 12 "go-hep.org/x/hep/xrootd/xrdproto" 13 "go-hep.org/x/hep/xrootd/xrdproto/admin" 14 ) 15 16 func TestRequest(t *testing.T) { 17 for _, want := range []admin.Request{ 18 {Req: ""}, 19 {Req: "hello"}, 20 {Req: "sudo ls"}, 21 } { 22 t.Run("", func(t *testing.T) { 23 var ( 24 err error 25 w = new(xrdenc.WBuffer) 26 got admin.Request 27 ) 28 29 if want.ReqID() != admin.RequestID { 30 t.Fatalf("invalid request ID: got=%d want=%d", want.ReqID(), admin.RequestID) 31 } 32 33 if want.ShouldSign() { 34 t.Fatalf("invalid") 35 } 36 37 err = want.MarshalXrd(w) 38 if err != nil { 39 t.Fatalf("could not marshal request: %v", err) 40 } 41 42 r := xrdenc.NewRBuffer(w.Bytes()) 43 err = got.UnmarshalXrd(r) 44 if err != nil { 45 t.Fatalf("could not unmarshal request: %v", err) 46 } 47 48 if !reflect.DeepEqual(got, want) { 49 t.Fatalf("round trip failed:\ngot = %#v\nwant= %#v\n", got, want) 50 } 51 }) 52 } 53 } 54 55 func TestResponse(t *testing.T) { 56 for _, want := range []admin.Response{ 57 {Data: []byte("")}, 58 {Data: []byte("1234")}, 59 {Data: []byte("localhost:1024")}, 60 {Data: []byte("0.0.0.0:1024")}, 61 } { 62 t.Run("", func(t *testing.T) { 63 var ( 64 err error 65 w = new(xrdenc.WBuffer) 66 got admin.Response 67 ) 68 69 if want.RespID() != admin.RequestID { 70 t.Fatalf("invalid response ID: got=%d want=%d", want.RespID(), admin.RequestID) 71 } 72 73 err = want.MarshalXrd(w) 74 if err != nil { 75 t.Fatalf("could not marshal response: %v", err) 76 } 77 78 r := xrdenc.NewRBuffer(w.Bytes()) 79 err = got.UnmarshalXrd(r) 80 if err != nil { 81 t.Fatalf("could not unmarshal response: %v", err) 82 } 83 84 if !reflect.DeepEqual(got, want) { 85 t.Fatalf("round trip failed:\ngot = %#v\nwant= %#v\n", got, want) 86 } 87 }) 88 } 89 } 90 91 var ( 92 _ xrdproto.Request = (*admin.Request)(nil) 93 _ xrdproto.Response = (*admin.Response)(nil) 94 )