@@ -21,6 +21,7 @@ package peer
2121import (
2222 "context"
2323 "fmt"
24+ "strings"
2425 "testing"
2526
2627 "google.golang.org/grpc/credentials"
@@ -32,7 +33,7 @@ type testAuthInfo struct {
3233}
3334
3435func (ta testAuthInfo ) AuthType () string {
35- return "testAuthInfo"
36+ return fmt . Sprintf ( "testAuthInfo-%d" , ta . SecurityLevel )
3637}
3738
3839func TestPeerSecurityLevel (t * testing.T ) {
@@ -68,17 +69,19 @@ func TestPeerSecurityLevel(t *testing.T) {
6869 },
6970 }
7071 for _ , tc := range testCases {
71- ctx := NewContext (context .Background (), & Peer {AuthInfo : testAuthInfo {credentials.CommonAuthInfo {SecurityLevel : tc .authLevel }}})
72- p , ok := FromContext (ctx )
73- if ! ok {
74- t .Fatalf ("Unable to get peer from context" )
75- }
76- err := credentials .CheckSecurityLevel (p .AuthInfo , tc .testLevel )
77- if tc .want && (err != nil ) {
78- t .Fatalf ("CheckSeurityLevel(%s, %s) returned failure but want success" , tc .authLevel .String (), tc .testLevel .String ())
79- } else if ! tc .want && (err == nil ) {
80- t .Fatalf ("CheckSeurityLevel(%s, %s) returned success but want failure" , tc .authLevel .String (), tc .testLevel .String ())
81- }
72+ t .Run (tc .authLevel .String ()+ "-" + tc .testLevel .String (), func (t * testing.T ) {
73+ ctx := NewContext (context .Background (), & Peer {AuthInfo : testAuthInfo {credentials.CommonAuthInfo {SecurityLevel : tc .authLevel }}})
74+ p , ok := FromContext (ctx )
75+ if ! ok {
76+ t .Fatalf ("Unable to get peer from context" )
77+ }
78+ err := credentials .CheckSecurityLevel (p .AuthInfo , tc .testLevel )
79+ if tc .want && (err != nil ) {
80+ t .Fatalf ("CheckSeurityLevel(%s, %s) returned failure but want success" , tc .authLevel .String (), tc .testLevel .String ())
81+ } else if ! tc .want && (err == nil ) {
82+ t .Fatalf ("CheckSeurityLevel(%s, %s) returned success but want failure" , tc .authLevel .String (), tc .testLevel .String ())
83+ }
84+ })
8285 }
8386}
8487
@@ -91,55 +94,51 @@ func (a *addr) String() string { return a.ipAddress }
9194
9295func TestPeerStringer (t * testing.T ) {
9396 testCases := []struct {
94- addr * addr
95- authLevel credentials.SecurityLevel
96- want string
97+ peer * Peer
98+ want string
9799 }{
98100 {
99- addr : & addr {"example.com:1234" },
100- authLevel : credentials .PrivacyAndIntegrity ,
101- want : "Peer{Addr: 'example.com:1234', AuthInfo: 'testAuthInfo'}" ,
101+ peer : & Peer {Addr : & addr {"example.com:1234" }, AuthInfo : testAuthInfo {credentials.CommonAuthInfo {SecurityLevel : credentials .PrivacyAndIntegrity }}},
102+ want : "Peer{Addr: 'example.com:1234', LocalAddr: <nil>, AuthInfo: 'testAuthInfo-3'}" ,
102103 },
103104 {
104- addr : & addr {"1.2.3.4:1234" },
105- authLevel : - 1 ,
106- want : "Peer{Addr: '1.2.3.4:1234', AuthInfo: <nil>}" ,
105+ peer : & Peer {Addr : & addr {"example.com:1234" }, LocalAddr : & addr {"example.com:1234" }, AuthInfo : testAuthInfo {credentials.CommonAuthInfo {SecurityLevel : credentials .PrivacyAndIntegrity }}},
106+ want : "Peer{Addr: 'example.com:1234', LocalAddr: 'example.com:1234', AuthInfo: 'testAuthInfo-3'}" ,
107107 },
108108 {
109- authLevel : credentials .InvalidSecurityLevel ,
110- want : "Peer{Addr: <nil>, AuthInfo: 'testAuthInfo'}" ,
109+ peer : & Peer {Addr : & addr {"1.2.3.4:1234" }, AuthInfo : testAuthInfo {credentials.CommonAuthInfo {}}},
110+ want : "Peer{Addr: '1.2.3.4:1234', LocalAddr: <nil>, AuthInfo: 'testAuthInfo-0'}" ,
111+ },
112+ {
113+ peer : & Peer {AuthInfo : testAuthInfo {}},
114+ want : "Peer{Addr: <nil>, LocalAddr: <nil>, AuthInfo: 'testAuthInfo-0'}" ,
111115 },
112116 {
113- authLevel : - 1 ,
114- want : "Peer{Addr: <nil>, AuthInfo: <nil>}" ,
117+ peer : & Peer {},
118+ want : "Peer{Addr: <nil>, LocalAddr: <nil>, AuthInfo: <nil>}" ,
119+ },
120+ {
121+ peer : nil ,
122+ want : "Peer<nil>" ,
115123 },
116124 }
117125 for _ , tc := range testCases {
118- ctx := NewContext (context .Background (), & Peer {Addr : tc .addr , AuthInfo : testAuthInfo {credentials.CommonAuthInfo {SecurityLevel : tc .authLevel }}})
119- p , ok := FromContext (ctx )
120- if tc .authLevel == - 1 {
121- p .AuthInfo = nil
122- }
123- if tc .addr == nil {
124- p .Addr = nil
125- }
126- if ! ok {
127- t .Fatalf ("Unable to get peer from context" )
128- }
129- if p .String () != tc .want {
130- t .Fatalf ("Error using peer String(): expected %q, got %q" , tc .want , p .String ())
131- }
126+ t .Run (strings .ReplaceAll (tc .want , " " , "" ), func (t * testing.T ) {
127+ ctx := NewContext (context .Background (), tc .peer )
128+ p , ok := FromContext (ctx )
129+ if ! ok {
130+ t .Fatalf ("Unable to get peer from context" )
131+ }
132+ if p .String () != tc .want {
133+ t .Fatalf ("Error using peer String(): expected %q, got %q" , tc .want , p .String ())
134+ }
135+ })
136+ }
137+ }
138+
139+ func TestPeerStringerOnContext (t * testing.T ) {
140+ ctx := NewContext (context .Background (), & Peer {Addr : & addr {"1.2.3.4:1234" }, AuthInfo : testAuthInfo {credentials.CommonAuthInfo {SecurityLevel : credentials .PrivacyAndIntegrity }}})
141+ if fmt .Sprintf ("%v" , ctx ) != "context.Background.WithValue(type peer.peerKey, val Peer{Addr: '1.2.3.4:1234', LocalAddr: <nil>, AuthInfo: 'testAuthInfo-3'})" {
142+ t .Fatalf ("Error printing context with embedded Peer. Got: %v" , ctx )
132143 }
133- t .Run ("test String on nil Peer" , func (st * testing.T ) {
134- var test * Peer
135- if test .String () != "Peer<nil>" {
136- st .Fatalf ("Error using String on nil Peer. Expected 'Peer<nil>', got: '%s'" , test .String ())
137- }
138- })
139- t .Run ("test Stringer on context" , func (st * testing.T ) {
140- ctx := NewContext (context .Background (), & Peer {Addr : & addr {"1.2.3.4:1234" }, AuthInfo : testAuthInfo {credentials.CommonAuthInfo {SecurityLevel : credentials .PrivacyAndIntegrity }}})
141- if fmt .Sprintf ("%v" , ctx ) != "context.Background.WithValue(type peer.peerKey, val Peer{Addr: '1.2.3.4:1234', AuthInfo: 'testAuthInfo'})" {
142- st .Fatalf ("Error printing context with embedded Peer. Got: %v" , ctx )
143- }
144- })
145144}
0 commit comments