github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fsimpl/devpts/devpts_test.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 devpts
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/SagerNet/gvisor/pkg/abi/linux"
    21  	"github.com/SagerNet/gvisor/pkg/sentry/contexttest"
    22  	"github.com/SagerNet/gvisor/pkg/usermem"
    23  )
    24  
    25  func TestSimpleMasterToReplica(t *testing.T) {
    26  	ld := newLineDiscipline(linux.DefaultReplicaTermios)
    27  	ctx := contexttest.Context(t)
    28  	inBytes := []byte("hello, tty\n")
    29  	src := usermem.BytesIOSequence(inBytes)
    30  	outBytes := make([]byte, 32)
    31  	dst := usermem.BytesIOSequence(outBytes)
    32  
    33  	// Write to the input queue.
    34  	nw, err := ld.inputQueueWrite(ctx, src)
    35  	if err != nil {
    36  		t.Fatalf("error writing to input queue: %v", err)
    37  	}
    38  	if nw != int64(len(inBytes)) {
    39  		t.Fatalf("wrote wrong length: got %d, want %d", nw, len(inBytes))
    40  	}
    41  
    42  	// Read from the input queue.
    43  	nr, err := ld.inputQueueRead(ctx, dst)
    44  	if err != nil {
    45  		t.Fatalf("error reading from input queue: %v", err)
    46  	}
    47  	if nr != int64(len(inBytes)) {
    48  		t.Fatalf("read wrong length: got %d, want %d", nr, len(inBytes))
    49  	}
    50  
    51  	outStr := string(outBytes[:nr])
    52  	inStr := string(inBytes)
    53  	if outStr != inStr {
    54  		t.Fatalf("written and read strings do not match: got %q, want %q", outStr, inStr)
    55  	}
    56  }