blob: 26aabf6c7dd212904f244b0887a5802f3fb2e450 [file] [log] [blame]
Jiri Simsad7616c92015-03-24 23:44:30 -07001// Copyright 2015 The Vanadium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Matt Rosencrantz94502cf2015-03-18 09:43:44 -07005package rpc
Robin Thellendd24f0842014-09-23 10:27:29 -07006
7import (
Bogdan Caprita1ec40522014-12-10 14:56:33 -08008 "io"
Robin Thellendd24f0842014-09-23 10:27:29 -07009 "reflect"
10 "sort"
11 "testing"
12
Suharsh Sivakumar2ad4e102015-03-17 21:23:37 -070013 "v.io/v23"
Todd Wang54feabe2015-04-15 23:38:26 -070014 "v.io/v23/context"
Jiri Simsa6ac95222015-02-23 16:11:49 -080015 "v.io/v23/naming"
16 "v.io/v23/options"
Matt Rosencrantz94502cf2015-03-18 09:43:44 -070017 "v.io/v23/rpc"
Cosmos Nicolaoue9c622d2015-07-10 11:09:42 -070018
Jiri Simsaffceefa2015-02-28 11:03:34 -080019 "v.io/x/ref/lib/stats"
Suharsh Sivakumardcc11d72015-05-11 12:19:20 -070020 "v.io/x/ref/runtime/internal/rpc/stream/manager"
21 tnaming "v.io/x/ref/runtime/internal/testing/mocks/naming"
Todd Wang1ea8f192015-04-03 17:31:51 -070022 "v.io/x/ref/services/debug/debuglib"
Asim Shankar4a698282015-03-21 21:59:18 -070023 "v.io/x/ref/test/testutil"
Robin Thellendd24f0842014-09-23 10:27:29 -070024)
25
26func TestDebugServer(t *testing.T) {
Suharsh Sivakumar2ad4e102015-03-17 21:23:37 -070027 ctx, shutdown := initForTest()
28 defer shutdown()
Asim Shankar8f05c222014-10-06 22:08:19 -070029 // Setup the client and server principals, with the client willing to share its
30 // blessing with the server.
31 var (
Asim Shankar4a698282015-03-21 21:59:18 -070032 pclient = testutil.NewPrincipal("client")
33 pserver = testutil.NewPrincipal("server")
Asim Shankar8f05c222014-10-06 22:08:19 -070034 bclient = bless(pserver, pclient, "client") // server/client blessing.
Cosmos Nicolaoue9c622d2015-07-10 11:09:42 -070035 sctx, _ = v23.WithPrincipal(ctx, pserver)
36 cctx, _ = v23.WithPrincipal(ctx, pclient)
Asim Shankar8f05c222014-10-06 22:08:19 -070037 )
38 pclient.AddToRoots(bclient) // Client recognizes "server" as a root of blessings.
39 pclient.BlessingStore().Set(bclient, "server") // Client presents bclient to server
40
Cosmos Nicolaou64d573d2015-07-13 16:22:18 -070041 debugDisp := debuglib.NewDispatcher(nil)
Cosmos Nicolaou8246a8b2014-11-01 09:32:36 -070042
Cosmos Nicolaoue9c622d2015-07-10 11:09:42 -070043 sm := manager.InternalNew(ctx, naming.FixedRoutingID(0x555555555))
Robin Thellendd24f0842014-09-23 10:27:29 -070044 defer sm.Shutdown()
45 ns := tnaming.NewSimpleNamespace()
Cosmos Nicolaoue9c622d2015-07-10 11:09:42 -070046
47 server, err := testInternalNewServer(sctx, sm, ns, ReservedNameDispatcher{debugDisp})
Robin Thellendd24f0842014-09-23 10:27:29 -070048 if err != nil {
49 t.Fatalf("InternalNewServer failed: %v", err)
50 }
51 defer server.Stop()
Cosmos Nicolaou28dabfc2014-12-15 22:51:07 -080052 eps, err := server.Listen(listenSpec)
Robin Thellendd24f0842014-09-23 10:27:29 -070053 if err != nil {
54 t.Fatalf("server.Listen failed: %v", err)
55 }
Cosmos Nicolaou9fbe7d22015-01-25 22:13:13 -080056 if err := server.Serve("", &testObject{}, nil); err != nil {
57 t.Fatalf("server.Serve failed: %v", err)
58 }
Cosmos Nicolaoue9c622d2015-07-10 11:09:42 -070059
Suharsh Sivakumar2ad4e102015-03-17 21:23:37 -070060 client, err := InternalNewClient(sm, ns)
Robin Thellendd24f0842014-09-23 10:27:29 -070061 if err != nil {
62 t.Fatalf("InternalNewClient failed: %v", err)
63 }
64 defer client.Close()
Cosmos Nicolaou28dabfc2014-12-15 22:51:07 -080065 ep := eps[0]
Robin Thellendd24f0842014-09-23 10:27:29 -070066 // Call the Foo method on ""
67 {
Robin Thellendd24f0842014-09-23 10:27:29 -070068 var value string
Cosmos Nicolaoue9c622d2015-07-10 11:09:42 -070069 if err := client.Call(cctx, ep.Name(), "Foo", nil, []interface{}{&value}); err != nil {
Suharsh Sivakumar1abd5a82015-04-10 00:24:14 -070070 t.Fatalf("client.Call failed: %v", err)
Robin Thellendd24f0842014-09-23 10:27:29 -070071 }
72 if want := "BAR"; value != want {
73 t.Errorf("unexpected value: Got %v, want %v", value, want)
74 }
75 }
Robin Thellendd24f0842014-09-23 10:27:29 -070076 // Call Value on __debug/stats/testing/foo
77 {
78 foo := stats.NewString("testing/foo")
79 foo.Set("The quick brown fox jumps over the lazy dog")
David Why Use Two When One Will Do Presotto3da1c792014-10-03 11:15:53 -070080 addr := naming.JoinAddressName(ep.String(), "__debug/stats/testing/foo")
Robin Thellendd24f0842014-09-23 10:27:29 -070081 var value string
Cosmos Nicolaoue9c622d2015-07-10 11:09:42 -070082 if err := client.Call(cctx, addr, "Value", nil, []interface{}{&value}, options.NoResolve{}); err != nil {
Suharsh Sivakumar1abd5a82015-04-10 00:24:14 -070083 t.Fatalf("client.Call failed: %v", err)
Robin Thellendd24f0842014-09-23 10:27:29 -070084 }
85 if want := foo.Value(); value != want {
86 t.Errorf("unexpected result: Got %v, want %v", value, want)
87 }
88 }
Robin Thellendc26c32e2014-10-06 17:44:04 -070089
90 // Call Glob
91 testcases := []struct {
92 name, pattern string
93 expected []string
94 }{
95 {"", "*", []string{}},
96 {"", "__*", []string{"__debug"}},
Matt Rosencrantzb30286b2014-11-10 14:52:17 -080097 {"", "__*/*", []string{"__debug/logs", "__debug/pprof", "__debug/stats", "__debug/vtrace"}},
98 {"__debug", "*", []string{"logs", "pprof", "stats", "vtrace"}},
Robin Thellendc26c32e2014-10-06 17:44:04 -070099 }
100 for _, tc := range testcases {
David Why Use Two When One Will Do Presottoadf0ca12014-11-13 10:49:01 -0800101 addr := naming.JoinAddressName(ep.String(), tc.name)
Cosmos Nicolaoue9c622d2015-07-10 11:09:42 -0700102 call, err := client.StartCall(cctx, addr, rpc.GlobMethod, []interface{}{tc.pattern}, options.NoResolve{})
Robin Thellendc26c32e2014-10-06 17:44:04 -0700103 if err != nil {
Robin Thellend877ac012014-12-10 14:19:28 -0800104 t.Fatalf("client.StartCall failed for %q: %v", tc.name, err)
Robin Thellendc26c32e2014-10-06 17:44:04 -0700105 }
106 results := []string{}
107 for {
Todd Wang2331dd02015-03-17 15:38:39 -0700108 var gr naming.GlobReply
David Why Use Two When One Will Do Presottoe5e62d02015-02-19 14:35:20 -0800109 if err := call.Recv(&gr); err != nil {
Bogdan Caprita1ec40522014-12-10 14:56:33 -0800110 if err != io.EOF {
111 t.Fatalf("Recv failed for %q: %v. Results received thus far: %q", tc.name, err, results)
112 }
Robin Thellendc26c32e2014-10-06 17:44:04 -0700113 break
114 }
David Why Use Two When One Will Do Presottoe5e62d02015-02-19 14:35:20 -0800115 switch v := gr.(type) {
Todd Wang2331dd02015-03-17 15:38:39 -0700116 case naming.GlobReplyEntry:
David Why Use Two When One Will Do Presottoe5e62d02015-02-19 14:35:20 -0800117 results = append(results, v.Value.Name)
118 }
Robin Thellendc26c32e2014-10-06 17:44:04 -0700119 }
Todd Wange77f9952015-02-18 13:20:50 -0800120 if err := call.Finish(); err != nil {
121 t.Fatalf("call.Finish failed for %q: %v", tc.name, err)
Robin Thellendc26c32e2014-10-06 17:44:04 -0700122 }
123 sort.Strings(results)
124 if !reflect.DeepEqual(tc.expected, results) {
Robin Thellend877ac012014-12-10 14:19:28 -0800125 t.Errorf("unexpected results for %q. Got %v, want %v", tc.name, results, tc.expected)
Robin Thellendc26c32e2014-10-06 17:44:04 -0700126 }
127 }
Robin Thellendd24f0842014-09-23 10:27:29 -0700128}
129
130type testObject struct {
131}
132
Todd Wang54feabe2015-04-15 23:38:26 -0700133func (o testObject) Foo(*context.T, rpc.ServerCall) (string, error) {
Todd Wange77f9952015-02-18 13:20:50 -0800134 return "BAR", nil
Robin Thellendd24f0842014-09-23 10:27:29 -0700135}