-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Satisfy the Stringer interface for Peer and Metadata
Effectively allowing to print context containing Peers properly Also enabling logging of Metadata in an easier way RELEASE NOTES: - peer/peer: implement the Stringer interface for pretty-printing Peers - metadata/metadata: implement the Stringer interface for pretty printing metadata Signed-off-by: Yolan Romailler <anomalroil@users.noreply.github.com>
- Loading branch information
1 parent
fc8da03
commit bc88d17
Showing
4 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package peer | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"google.golang.org/grpc/credentials" | ||
) | ||
|
||
// A struct that implements AuthInfo interface and implements CommonAuthInfo() method. | ||
type testAuthInfo struct { | ||
credentials.CommonAuthInfo | ||
} | ||
|
||
func (ta testAuthInfo) AuthType() string { | ||
return "testAuthInfo" | ||
} | ||
|
||
func TestPeerSecurityLevel(t *testing.T) { | ||
testCases := []struct { | ||
authLevel credentials.SecurityLevel | ||
testLevel credentials.SecurityLevel | ||
want bool | ||
}{ | ||
{ | ||
authLevel: credentials.PrivacyAndIntegrity, | ||
testLevel: credentials.PrivacyAndIntegrity, | ||
want: true, | ||
}, | ||
{ | ||
authLevel: credentials.IntegrityOnly, | ||
testLevel: credentials.PrivacyAndIntegrity, | ||
want: false, | ||
}, | ||
{ | ||
authLevel: credentials.IntegrityOnly, | ||
testLevel: credentials.NoSecurity, | ||
want: true, | ||
}, | ||
{ | ||
authLevel: credentials.InvalidSecurityLevel, | ||
testLevel: credentials.IntegrityOnly, | ||
want: true, | ||
}, | ||
{ | ||
authLevel: credentials.InvalidSecurityLevel, | ||
testLevel: credentials.PrivacyAndIntegrity, | ||
want: true, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
ctx := NewContext(context.Background(), &Peer{AuthInfo: testAuthInfo{credentials.CommonAuthInfo{SecurityLevel: tc.authLevel}}}) | ||
p, ok := FromContext(ctx) | ||
if !ok { | ||
t.Fatalf("Unable to get peer from context") | ||
} | ||
err := credentials.CheckSecurityLevel(p.AuthInfo, tc.testLevel) | ||
if tc.want && (err != nil) { | ||
t.Fatalf("CheckSeurityLevel(%s, %s) returned failure but want success", tc.authLevel.String(), tc.testLevel.String()) | ||
} else if !tc.want && (err == nil) { | ||
t.Fatalf("CheckSeurityLevel(%s, %s) returned success but want failure", tc.authLevel.String(), tc.testLevel.String()) | ||
} | ||
} | ||
} | ||
|
||
type addr struct { | ||
ipAddress string | ||
} | ||
|
||
func (addr) Network() string { return "" } | ||
func (a *addr) String() string { return a.ipAddress } | ||
|
||
func TestPeerStringer(t *testing.T) { | ||
testCases := []struct { | ||
addr *addr | ||
authLevel credentials.SecurityLevel | ||
want string | ||
}{ | ||
{ | ||
addr: &addr{"example.com:1234"}, | ||
authLevel: credentials.PrivacyAndIntegrity, | ||
want: "Peer{Addr: 'example.com:1234', AuthInfo: 'testAuthInfo'}", | ||
}, | ||
{ | ||
addr: &addr{"1.2.3.4:1234"}, | ||
authLevel: -1, | ||
want: "Peer{Addr: '1.2.3.4:1234'}", | ||
}, | ||
{ | ||
authLevel: credentials.InvalidSecurityLevel, | ||
want: "Peer{AuthInfo: 'testAuthInfo'}", | ||
}, | ||
{ | ||
authLevel: -1, | ||
want: "Peer{}", | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
ctx := NewContext(context.Background(), &Peer{Addr: tc.addr, AuthInfo: testAuthInfo{credentials.CommonAuthInfo{SecurityLevel: tc.authLevel}}}) | ||
p, ok := FromContext(ctx) | ||
if tc.authLevel == -1 { | ||
p.AuthInfo = nil | ||
} | ||
if tc.addr == nil { | ||
p.Addr = nil | ||
} | ||
if !ok { | ||
t.Fatalf("Unable to get peer from context") | ||
} | ||
if p.String() != tc.want { | ||
t.Fatalf("Error using peer String(): expected %q, got %q", tc.want, p.String()) | ||
} | ||
} | ||
t.Run("test String on nil Peer", func(st *testing.T) { | ||
var test *Peer | ||
if test.String() != "Peer<nil>" { | ||
st.Fatalf("Error using String on nil Peer. Expected 'Peer<nil>', got: '%s'", test.String()) | ||
} | ||
}) | ||
t.Run("test Stringer on context", func(st *testing.T) { | ||
ctx := NewContext(context.Background(), &Peer{Addr: &addr{"1.2.3.4:1234"}, AuthInfo: testAuthInfo{credentials.CommonAuthInfo{SecurityLevel: credentials.PrivacyAndIntegrity}}}) | ||
if fmt.Sprintf("%v", ctx) != "context.Background.WithValue(type peer.peerKey, val Peer{Addr: '1.2.3.4:1234', AuthInfo: 'testAuthInfo'})" { | ||
st.Fatalf("Error printing context with embedded Peer. Got: %v", ctx) | ||
} | ||
}) | ||
} |