blob: 464ab113b40981ccb6f2be57070c8e81e3f51361 [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 Rosencrantz19329122015-01-23 22:18:49 -08005package fake
6
7import (
Jiri Simsa6ac95222015-02-23 16:11:49 -08008 "v.io/v23"
9 "v.io/v23/context"
Bogdan Caprita95bca142015-06-29 17:16:05 -070010 "v.io/v23/namespace"
Robert Kroeger316442c2015-03-26 13:56:11 -070011 "v.io/v23/rpc"
Jiri Simsa6ac95222015-02-23 16:11:49 -080012 "v.io/v23/security"
Cosmos Nicolaoue9c622d2015-07-10 11:09:42 -070013 "v.io/x/ref/internal/logger"
Bogdan Caprita95bca142015-06-29 17:16:05 -070014 "v.io/x/ref/lib/apilog"
Bogdan Caprita95bca142015-06-29 17:16:05 -070015 tnaming "v.io/x/ref/runtime/internal/testing/mocks/naming"
Matt Rosencrantzb9af7a82015-08-17 17:41:29 -070016 "v.io/x/ref/test/testutil"
Matt Rosencrantz19329122015-01-23 22:18:49 -080017)
18
19type contextKey int
20
21const (
22 clientKey = contextKey(iota)
23 principalKey
24 loggerKey
25 backgroundKey
Matt Rosencrantza1eadad2015-09-03 13:44:25 -070026 listenSpecKey
27 flowManagerKey
Matt Rosencrantz19329122015-01-23 22:18:49 -080028)
29
Bogdan Caprita95bca142015-06-29 17:16:05 -070030type Runtime struct {
31 ns namespace.T
32}
Matt Rosencrantz19329122015-01-23 22:18:49 -080033
Matt Rosencrantz0a2500b2015-02-26 17:22:17 -080034func new(ctx *context.T) (*Runtime, *context.T, v23.Shutdown, error) {
Matt Rosencrantzb9af7a82015-08-17 17:41:29 -070035 ctx = context.WithValue(ctx, principalKey, testutil.NewPrincipal("fake"))
Matt Rosencrantz07712e12015-07-31 18:45:25 -070036 ctx = context.WithLogger(ctx, logger.Global())
Bogdan Caprita95bca142015-06-29 17:16:05 -070037 return &Runtime{ns: tnaming.NewSimpleNamespace()}, ctx, func() {}, nil
Matt Rosencrantz19329122015-01-23 22:18:49 -080038}
39
Matt Rosencrantz31995882015-01-27 20:00:48 -080040func (r *Runtime) Init(ctx *context.T) error {
Matt Rosencrantz07712e12015-07-31 18:45:25 -070041 return nil
Matt Rosencrantz31995882015-01-27 20:00:48 -080042}
43
Todd Wangad492042015-04-17 15:58:40 -070044func (r *Runtime) WithPrincipal(ctx *context.T, principal security.Principal) (*context.T, error) {
Bogdan Caprita95bca142015-06-29 17:16:05 -070045 defer apilog.LogCallf(ctx, "principal=%v", principal)(ctx, "") // gologcop: DO NOT EDIT, MUST BE FIRST STATEMENT
Matt Rosencrantz19329122015-01-23 22:18:49 -080046 return context.WithValue(ctx, principalKey, principal), nil
47}
48
49func (r *Runtime) GetPrincipal(ctx *context.T) security.Principal {
Cosmos Nicolaou0e4e3922015-06-10 16:30:09 -070050 // nologcall
Matt Rosencrantz19329122015-01-23 22:18:49 -080051 p, _ := ctx.Value(principalKey).(security.Principal)
52 return p
53}
54
Jiri Simsa6ac95222015-02-23 16:11:49 -080055func (r *Runtime) GetAppCycle(ctx *context.T) v23.AppCycle {
Cosmos Nicolaou0e4e3922015-06-10 16:30:09 -070056 // nologcall
Matt Rosencrantz19329122015-01-23 22:18:49 -080057 panic("unimplemented")
58}
59
Todd Wangad492042015-04-17 15:58:40 -070060func (r *Runtime) WithBackgroundContext(ctx *context.T) *context.T {
Bogdan Caprita95bca142015-06-29 17:16:05 -070061 defer apilog.LogCall(ctx)(ctx) // gologcop: DO NOT EDIT, MUST BE FIRST STATEMENT
Matt Rosencrantz19329122015-01-23 22:18:49 -080062 // Note we add an extra context with a nil value here.
63 // This prevents users from travelling back through the
64 // chain of background contexts.
65 ctx = context.WithValue(ctx, backgroundKey, nil)
66 return context.WithValue(ctx, backgroundKey, ctx)
67}
68
69func (r *Runtime) GetBackgroundContext(ctx *context.T) *context.T {
Cosmos Nicolaou0e4e3922015-06-10 16:30:09 -070070 // nologcall
Matt Rosencrantz19329122015-01-23 22:18:49 -080071 bctx, _ := ctx.Value(backgroundKey).(*context.T)
72 if bctx == nil {
73 // There should always be a background context. If we don't find
74 // it, that means that the user passed us the background context
75 // in hopes of following the chain. Instead we just give them
76 // back what they sent in, which is correct.
77 return ctx
78 }
79 return bctx
80}
Robert Kroeger316442c2015-03-26 13:56:11 -070081
Todd Wangad492042015-04-17 15:58:40 -070082func (*Runtime) WithReservedNameDispatcher(ctx *context.T, d rpc.Dispatcher) *context.T {
Bogdan Caprita95bca142015-06-29 17:16:05 -070083 defer apilog.LogCall(ctx)(ctx) // gologcop: DO NOT EDIT, MUST BE FIRST STATEMENT
Robert Kroeger316442c2015-03-26 13:56:11 -070084 panic("unimplemented")
Robert Kroeger316442c2015-03-26 13:56:11 -070085}
86
87func (*Runtime) GetReservedNameDispatcher(ctx *context.T) rpc.Dispatcher {
Cosmos Nicolaou0e4e3922015-06-10 16:30:09 -070088 // nologcall
Robert Kroeger316442c2015-03-26 13:56:11 -070089 panic("unimplmeneted")
Robert Kroeger316442c2015-03-26 13:56:11 -070090}