summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/context/contexttest
diff options
context:
space:
mode:
authorRahat Mahmood <rahat@google.com>2018-12-12 17:47:01 -0800
committerShentubot <shentubot@google.com>2018-12-12 17:47:55 -0800
commitccce1d4281ce82fe551d7c8569fe3a545c62e296 (patch)
treecbcfbd64ba151097c5734102f6d1e1dfecdcd160 /pkg/sentry/context/contexttest
parentf93c288dd70846f335239e2d0cb351135a756f51 (diff)
Filesystems shouldn't be saving references to Platform.
Platform objects are not savable, storing references to them in filesystem datastructures would cause save to fail if someone actually passed in a Platform. Current implementations work because everywhere a Platform is expected, we currently pass in a Kernel object which embeds Platform and thus satisfies the interface. Eliminate this indirection and save pointers to Kernel directly. PiperOrigin-RevId: 225288336 Change-Id: Ica399ff43f425e15bc150a0d7102196c3d54a2ab
Diffstat (limited to 'pkg/sentry/context/contexttest')
-rw-r--r--pkg/sentry/context/contexttest/contexttest.go32
1 files changed, 24 insertions, 8 deletions
diff --git a/pkg/sentry/context/contexttest/contexttest.go b/pkg/sentry/context/contexttest/contexttest.go
index d2f084ed7..d5fd9f165 100644
--- a/pkg/sentry/context/contexttest/contexttest.go
+++ b/pkg/sentry/context/contexttest/contexttest.go
@@ -31,23 +31,30 @@ import (
// Context returns a Context that may be used in tests. Uses ptrace as the
// platform.Platform.
+//
+// Note that some filesystems may require a minimal kernel for testing, which
+// this test context does not provide. For such tests, see kernel/contexttest.
func Context(tb testing.TB) context.Context {
p, err := ptrace.New()
if err != nil {
tb.Fatal(err)
}
// Test usage of context.Background is fine.
- return &testContext{
- Context: context.Background(),
- l: limits.NewLimitSet(),
- platform: p,
+ return &TestContext{
+ Context: context.Background(),
+ l: limits.NewLimitSet(),
+ platform: p,
+ otherValues: make(map[interface{}]interface{}),
}
}
-type testContext struct {
+// TestContext represents a context with minimal functionality suitable for
+// running tests.
+type TestContext struct {
context.Context
- l *limits.LimitSet
- platform platform.Platform
+ l *limits.LimitSet
+ platform platform.Platform
+ otherValues map[interface{}]interface{}
}
// globalUniqueID tracks incremental unique identifiers for tests.
@@ -76,8 +83,14 @@ func (hostClock) Now() ktime.Time {
return ktime.FromNanoseconds(time.Now().UnixNano())
}
+// RegisterValue registers additional values with this test context. Useful for
+// providing values from external packages that contexttest can't depend on.
+func (t *TestContext) RegisterValue(key, value interface{}) {
+ t.otherValues[key] = value
+}
+
// Value implements context.Context.
-func (t *testContext) Value(key interface{}) interface{} {
+func (t *TestContext) Value(key interface{}) interface{} {
switch key {
case limits.CtxLimits:
return t.l
@@ -92,6 +105,9 @@ func (t *testContext) Value(key interface{}) interface{} {
case ktime.CtxRealtimeClock:
return hostClock{}
default:
+ if val, ok := t.otherValues[key]; ok {
+ return val
+ }
return t.Context.Value(key)
}
}