github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/kernel/contexttest/contexttest.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package contexttest provides a test context.Context which includes
    16  // a dummy kernel pointing to a valid platform.
    17  package contexttest
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/SagerNet/gvisor/pkg/context"
    23  	"github.com/SagerNet/gvisor/pkg/sentry/contexttest"
    24  	"github.com/SagerNet/gvisor/pkg/sentry/kernel"
    25  	"github.com/SagerNet/gvisor/pkg/sentry/pgalloc"
    26  	"github.com/SagerNet/gvisor/pkg/sentry/platform"
    27  )
    28  
    29  // Context returns a Context that may be used in tests. Uses ptrace as the
    30  // platform.Platform, and provides a stub kernel that only serves to point to
    31  // the platform.
    32  func Context(tb testing.TB) context.Context {
    33  	ctx := contexttest.Context(tb)
    34  	k := &kernel.Kernel{
    35  		Platform: platform.FromContext(ctx),
    36  	}
    37  	k.SetMemoryFile(pgalloc.MemoryFileFromContext(ctx))
    38  	ctx.(*contexttest.TestContext).RegisterValue(kernel.CtxKernel, k)
    39  	return ctx
    40  }