github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/os/pipe_test.go (about)

     1  //go:build windows || darwin || (linux && !baremetal && !wasi)
     2  
     3  // Copyright 2021 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // Test pipes on Unix and Windows systems.
     8  
     9  package os_test
    10  
    11  import (
    12  	"bytes"
    13  	"os"
    14  	"testing"
    15  )
    16  
    17  // TestSmokePipe is a simple smoke test for Pipe().
    18  func TestSmokePipe(t *testing.T) {
    19  	// Procedure:
    20  	// 1. Get the bytes
    21  	// 2. Light the bytes on fire
    22  	// 3. Smoke the bytes
    23  
    24  	r, w, err := os.Pipe()
    25  	if err != nil {
    26  		t.Fatal(err)
    27  		return // TODO: remove once Fatal is fatal
    28  	}
    29  	defer r.Close()
    30  	defer w.Close()
    31  
    32  	msg := []byte("Sed nvlla nisi ardva virtvs")
    33  	n, err := w.Write(msg)
    34  	if err != nil {
    35  		t.Errorf("Writing to fresh pipe failed, error %v", err)
    36  	}
    37  	want := len(msg)
    38  	if n != want {
    39  		t.Errorf("Writing to fresh pipe wrote %d bytes, expected %d", n, want)
    40  	}
    41  
    42  	buf := make([]byte, 2*len(msg))
    43  	n, err = r.Read(buf)
    44  	if err != nil {
    45  		t.Errorf("Reading from pipe failed, error %v", err)
    46  	}
    47  	if n != want {
    48  		t.Errorf("Reading from pipe got %d bytes, expected %d", n, want)
    49  	}
    50  	// Read() does not set len(buf), so do it here.
    51  	buf = buf[:n]
    52  	if !bytes.Equal(buf, msg) {
    53  		t.Errorf("Reading from fresh pipe got wrong bytes")
    54  	}
    55  }