@@ -16,6 +16,7 @@ import (
16
16
"io/ioutil"
17
17
"os"
18
18
"os/exec"
19
+ "os/user"
19
20
"reflect"
20
21
"testing"
21
22
"time"
@@ -42,6 +43,7 @@ import (
42
43
gclient "github.com/jipanyang/gnmi/client/gnmi"
43
44
"github.com/jipanyang/gnxi/utils/xpath"
44
45
gnoi_system_pb "github.com/openconfig/gnoi/system"
46
+ "github.com/agiledragon/gomonkey"
45
47
)
46
48
47
49
var clientTypes = []string {gclient .Type }
@@ -104,6 +106,25 @@ func createServer(t *testing.T, port int64) *Server {
104
106
return s
105
107
}
106
108
109
+ func createAuthServer (t * testing.T , port int64 ) * Server {
110
+ certificate , err := testcert .NewCert ()
111
+ if err != nil {
112
+ t .Errorf ("could not load server key pair: %s" , err )
113
+ }
114
+ tlsCfg := & tls.Config {
115
+ ClientAuth : tls .RequestClientCert ,
116
+ Certificates : []tls.Certificate {certificate },
117
+ }
118
+
119
+ opts := []grpc.ServerOption {grpc .Creds (credentials .NewTLS (tlsCfg ))}
120
+ cfg := & Config {Port : port , UserAuth : AuthTypes {"password" : true , "cert" : true , "jwt" : true }}
121
+ s , err := NewServer (cfg , opts )
122
+ if err != nil {
123
+ t .Errorf ("Failed to create gNMI server: %v" , err )
124
+ }
125
+ return s
126
+ }
127
+
107
128
// runTestGet requests a path from the server by Get grpc call, and compares if
108
129
// the return code and response value are expected.
109
130
func runTestGet (t * testing.T , ctx context.Context , gClient pb.GNMIClient , pathTarget string ,
@@ -2509,8 +2530,57 @@ func TestBulkSet(t *testing.T) {
2509
2530
if ! ok {
2510
2531
t .Fatal ("got a non-grpc error from grpc call" )
2511
2532
}
2533
+ })
2534
+ }
2535
+
2536
+ type loginCreds struct {
2537
+ Username , Password string
2538
+ }
2512
2539
2540
+ func (c * loginCreds ) GetRequestMetadata (context.Context , ... string ) (map [string ]string , error ) {
2541
+ return map [string ]string {
2542
+ "username" : c .Username ,
2543
+ "password" : c .Password ,
2544
+ }, nil
2545
+ }
2546
+
2547
+ func (c * loginCreds ) RequireTransportSecurity () bool {
2548
+ return true
2549
+ }
2550
+
2551
+ func TestAuthCapabilities (t * testing.T ) {
2552
+ mock1 := gomonkey .ApplyFunc (UserPwAuth , func (username string , passwd string ) (bool , error ) {
2553
+ return true , nil
2513
2554
})
2555
+ defer mock1 .Reset ()
2556
+
2557
+ s := createAuthServer (t , 8089 )
2558
+ go runServer (t , s )
2559
+
2560
+ currentUser , _ := user .Current ()
2561
+ tlsConfig := & tls.Config {InsecureSkipVerify : true }
2562
+ cred := & loginCreds {Username : currentUser .Username , Password : "dummy" }
2563
+ opts := []grpc.DialOption {grpc .WithTransportCredentials (credentials .NewTLS (tlsConfig )), grpc .WithPerRPCCredentials (cred )}
2564
+
2565
+ targetAddr := "127.0.0.1:8089"
2566
+ conn , err := grpc .Dial (targetAddr , opts ... )
2567
+ if err != nil {
2568
+ t .Fatalf ("Dialing to %q failed: %v" , targetAddr , err )
2569
+ }
2570
+ defer conn .Close ()
2571
+
2572
+ gClient := pb .NewGNMIClient (conn )
2573
+ ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
2574
+ defer cancel ()
2575
+
2576
+ var req pb.CapabilityRequest
2577
+ resp , err := gClient .Capabilities (ctx , & req )
2578
+ if err != nil {
2579
+ t .Fatalf ("Failed to get Capabilities: %v" , err )
2580
+ }
2581
+ if len (resp .SupportedModels ) == 0 {
2582
+ t .Fatalf ("No Supported Models found!" )
2583
+ }
2514
2584
2515
2585
}
2516
2586
0 commit comments