google.golang.org/grpc@v1.72.2/peer/peer_test.go (about) 1 /* 2 * 3 * Copyright 2024 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 package peer 20 21 import ( 22 "context" 23 "fmt" 24 "testing" 25 "time" 26 27 "google.golang.org/grpc/credentials" 28 ) 29 30 const defaultTestTimeout = 10 * time.Second 31 32 // A struct that implements AuthInfo interface and implements CommonAuthInfo() method. 33 type testAuthInfo struct { 34 credentials.CommonAuthInfo 35 } 36 37 func (ta testAuthInfo) AuthType() string { 38 return fmt.Sprintf("testAuthInfo-%d", ta.SecurityLevel) 39 } 40 41 type addr struct { 42 ipAddress string 43 } 44 45 func (addr) Network() string { return "" } 46 47 func (a *addr) String() string { return a.ipAddress } 48 49 func TestPeerStringer(t *testing.T) { 50 testCases := []struct { 51 name string 52 peer *Peer 53 want string 54 }{ 55 { 56 name: "+Addr-LocalAddr+ValidAuth", 57 peer: &Peer{Addr: &addr{"example.com:1234"}, AuthInfo: testAuthInfo{credentials.CommonAuthInfo{SecurityLevel: credentials.PrivacyAndIntegrity}}}, 58 want: "Peer{Addr: 'example.com:1234', LocalAddr: <nil>, AuthInfo: 'testAuthInfo-3'}", 59 }, 60 { 61 name: "+Addr+LocalAddr+ValidAuth", 62 peer: &Peer{Addr: &addr{"example.com:1234"}, LocalAddr: &addr{"example.com:1234"}, AuthInfo: testAuthInfo{credentials.CommonAuthInfo{SecurityLevel: credentials.PrivacyAndIntegrity}}}, 63 want: "Peer{Addr: 'example.com:1234', LocalAddr: 'example.com:1234', AuthInfo: 'testAuthInfo-3'}", 64 }, 65 { 66 name: "+Addr-LocalAddr+emptyAuth", 67 peer: &Peer{Addr: &addr{"1.2.3.4:1234"}, AuthInfo: testAuthInfo{credentials.CommonAuthInfo{}}}, 68 want: "Peer{Addr: '1.2.3.4:1234', LocalAddr: <nil>, AuthInfo: 'testAuthInfo-0'}", 69 }, 70 { 71 name: "-Addr-LocalAddr+emptyAuth", 72 peer: &Peer{AuthInfo: testAuthInfo{}}, 73 want: "Peer{Addr: <nil>, LocalAddr: <nil>, AuthInfo: 'testAuthInfo-0'}", 74 }, 75 { 76 name: "zeroedPeer", 77 peer: &Peer{}, 78 want: "Peer{Addr: <nil>, LocalAddr: <nil>, AuthInfo: <nil>}", 79 }, 80 { 81 name: "nilPeer", 82 peer: nil, 83 want: "Peer<nil>", 84 }, 85 } 86 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) 87 defer cancel() 88 for _, tc := range testCases { 89 t.Run(tc.name, func(t *testing.T) { 90 ctx = NewContext(ctx, tc.peer) 91 92 p, ok := FromContext(ctx) 93 if !ok { 94 t.Fatalf("Unable to get peer from context") 95 } 96 if p.String() != tc.want { 97 t.Fatalf("Error using peer String(): expected %q, got %q", tc.want, p.String()) 98 } 99 }) 100 } 101 }