go_study/fabric-main/common/util/net_test.go

74 lines
1.6 KiB
Go

/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package util
import (
"context"
"crypto/sha256"
"crypto/tls"
"crypto/x509"
"testing"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/peer"
)
type addr struct{}
func (*addr) Network() string {
return ""
}
func (*addr) String() string {
return "1.2.3.4:5000"
}
func TestExtractAddress(t *testing.T) {
ctx := context.Background()
require.Zero(t, ExtractRemoteAddress(ctx))
ctx = peer.NewContext(ctx, &peer.Peer{
Addr: &addr{},
})
require.Equal(t, "1.2.3.4:5000", ExtractRemoteAddress(ctx))
}
func TestExtractCertificateHashFromContext(t *testing.T) {
require.Nil(t, ExtractCertificateHashFromContext(context.Background()))
p := &peer.Peer{}
ctx := peer.NewContext(context.Background(), p)
require.Nil(t, ExtractCertificateHashFromContext(ctx))
p.AuthInfo = &nonTLSConnection{}
ctx = peer.NewContext(context.Background(), p)
require.Nil(t, ExtractCertificateHashFromContext(ctx))
p.AuthInfo = credentials.TLSInfo{}
ctx = peer.NewContext(context.Background(), p)
require.Nil(t, ExtractCertificateHashFromContext(ctx))
p.AuthInfo = credentials.TLSInfo{
State: tls.ConnectionState{
PeerCertificates: []*x509.Certificate{
{Raw: []byte{1, 2, 3}},
},
},
}
ctx = peer.NewContext(context.Background(), p)
h := sha256.New()
h.Write([]byte{1, 2, 3})
require.Equal(t, h.Sum(nil), ExtractCertificateHashFromContext(ctx))
}
type nonTLSConnection struct{}
func (*nonTLSConnection) AuthType() string {
return ""
}