Jiri Simsa | d7616c9 | 2015-03-24 23:44:30 -0700 | [diff] [blame] | 1 | // 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 Rosencrantz | 1932912 | 2015-01-23 22:18:49 -0800 | [diff] [blame] | 5 | package fake |
| 6 | |
| 7 | import ( |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 8 | "v.io/v23" |
| 9 | "v.io/v23/context" |
Bogdan Caprita | 95bca14 | 2015-06-29 17:16:05 -0700 | [diff] [blame] | 10 | "v.io/v23/namespace" |
Robert Kroeger | 316442c | 2015-03-26 13:56:11 -0700 | [diff] [blame] | 11 | "v.io/v23/rpc" |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 12 | "v.io/v23/security" |
Cosmos Nicolaou | e9c622d | 2015-07-10 11:09:42 -0700 | [diff] [blame] | 13 | "v.io/x/ref/internal/logger" |
Bogdan Caprita | 95bca14 | 2015-06-29 17:16:05 -0700 | [diff] [blame] | 14 | "v.io/x/ref/lib/apilog" |
Bogdan Caprita | 95bca14 | 2015-06-29 17:16:05 -0700 | [diff] [blame] | 15 | tnaming "v.io/x/ref/runtime/internal/testing/mocks/naming" |
Matt Rosencrantz | b9af7a8 | 2015-08-17 17:41:29 -0700 | [diff] [blame] | 16 | "v.io/x/ref/test/testutil" |
Matt Rosencrantz | 1932912 | 2015-01-23 22:18:49 -0800 | [diff] [blame] | 17 | ) |
| 18 | |
| 19 | type contextKey int |
| 20 | |
| 21 | const ( |
| 22 | clientKey = contextKey(iota) |
| 23 | principalKey |
| 24 | loggerKey |
| 25 | backgroundKey |
Matt Rosencrantz | a1eadad | 2015-09-03 13:44:25 -0700 | [diff] [blame] | 26 | listenSpecKey |
| 27 | flowManagerKey |
Matt Rosencrantz | 1932912 | 2015-01-23 22:18:49 -0800 | [diff] [blame] | 28 | ) |
| 29 | |
Bogdan Caprita | 95bca14 | 2015-06-29 17:16:05 -0700 | [diff] [blame] | 30 | type Runtime struct { |
| 31 | ns namespace.T |
| 32 | } |
Matt Rosencrantz | 1932912 | 2015-01-23 22:18:49 -0800 | [diff] [blame] | 33 | |
Matt Rosencrantz | 0a2500b | 2015-02-26 17:22:17 -0800 | [diff] [blame] | 34 | func new(ctx *context.T) (*Runtime, *context.T, v23.Shutdown, error) { |
Matt Rosencrantz | b9af7a8 | 2015-08-17 17:41:29 -0700 | [diff] [blame] | 35 | ctx = context.WithValue(ctx, principalKey, testutil.NewPrincipal("fake")) |
Matt Rosencrantz | 07712e1 | 2015-07-31 18:45:25 -0700 | [diff] [blame] | 36 | ctx = context.WithLogger(ctx, logger.Global()) |
Bogdan Caprita | 95bca14 | 2015-06-29 17:16:05 -0700 | [diff] [blame] | 37 | return &Runtime{ns: tnaming.NewSimpleNamespace()}, ctx, func() {}, nil |
Matt Rosencrantz | 1932912 | 2015-01-23 22:18:49 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Matt Rosencrantz | 3199588 | 2015-01-27 20:00:48 -0800 | [diff] [blame] | 40 | func (r *Runtime) Init(ctx *context.T) error { |
Matt Rosencrantz | 07712e1 | 2015-07-31 18:45:25 -0700 | [diff] [blame] | 41 | return nil |
Matt Rosencrantz | 3199588 | 2015-01-27 20:00:48 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Todd Wang | ad49204 | 2015-04-17 15:58:40 -0700 | [diff] [blame] | 44 | func (r *Runtime) WithPrincipal(ctx *context.T, principal security.Principal) (*context.T, error) { |
Bogdan Caprita | 95bca14 | 2015-06-29 17:16:05 -0700 | [diff] [blame] | 45 | defer apilog.LogCallf(ctx, "principal=%v", principal)(ctx, "") // gologcop: DO NOT EDIT, MUST BE FIRST STATEMENT |
Matt Rosencrantz | 1932912 | 2015-01-23 22:18:49 -0800 | [diff] [blame] | 46 | return context.WithValue(ctx, principalKey, principal), nil |
| 47 | } |
| 48 | |
| 49 | func (r *Runtime) GetPrincipal(ctx *context.T) security.Principal { |
Cosmos Nicolaou | 0e4e392 | 2015-06-10 16:30:09 -0700 | [diff] [blame] | 50 | // nologcall |
Matt Rosencrantz | 1932912 | 2015-01-23 22:18:49 -0800 | [diff] [blame] | 51 | p, _ := ctx.Value(principalKey).(security.Principal) |
| 52 | return p |
| 53 | } |
| 54 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 55 | func (r *Runtime) GetAppCycle(ctx *context.T) v23.AppCycle { |
Cosmos Nicolaou | 0e4e392 | 2015-06-10 16:30:09 -0700 | [diff] [blame] | 56 | // nologcall |
Matt Rosencrantz | 1932912 | 2015-01-23 22:18:49 -0800 | [diff] [blame] | 57 | panic("unimplemented") |
| 58 | } |
| 59 | |
Todd Wang | ad49204 | 2015-04-17 15:58:40 -0700 | [diff] [blame] | 60 | func (r *Runtime) WithBackgroundContext(ctx *context.T) *context.T { |
Bogdan Caprita | 95bca14 | 2015-06-29 17:16:05 -0700 | [diff] [blame] | 61 | defer apilog.LogCall(ctx)(ctx) // gologcop: DO NOT EDIT, MUST BE FIRST STATEMENT |
Matt Rosencrantz | 1932912 | 2015-01-23 22:18:49 -0800 | [diff] [blame] | 62 | // 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 | |
| 69 | func (r *Runtime) GetBackgroundContext(ctx *context.T) *context.T { |
Cosmos Nicolaou | 0e4e392 | 2015-06-10 16:30:09 -0700 | [diff] [blame] | 70 | // nologcall |
Matt Rosencrantz | 1932912 | 2015-01-23 22:18:49 -0800 | [diff] [blame] | 71 | 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 Kroeger | 316442c | 2015-03-26 13:56:11 -0700 | [diff] [blame] | 81 | |
Todd Wang | ad49204 | 2015-04-17 15:58:40 -0700 | [diff] [blame] | 82 | func (*Runtime) WithReservedNameDispatcher(ctx *context.T, d rpc.Dispatcher) *context.T { |
Bogdan Caprita | 95bca14 | 2015-06-29 17:16:05 -0700 | [diff] [blame] | 83 | defer apilog.LogCall(ctx)(ctx) // gologcop: DO NOT EDIT, MUST BE FIRST STATEMENT |
Robert Kroeger | 316442c | 2015-03-26 13:56:11 -0700 | [diff] [blame] | 84 | panic("unimplemented") |
Robert Kroeger | 316442c | 2015-03-26 13:56:11 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | func (*Runtime) GetReservedNameDispatcher(ctx *context.T) rpc.Dispatcher { |
Cosmos Nicolaou | 0e4e392 | 2015-06-10 16:30:09 -0700 | [diff] [blame] | 88 | // nologcall |
Robert Kroeger | 316442c | 2015-03-26 13:56:11 -0700 | [diff] [blame] | 89 | panic("unimplmeneted") |
Robert Kroeger | 316442c | 2015-03-26 13:56:11 -0700 | [diff] [blame] | 90 | } |