aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/os/pipe_test.go
blob: 98c86089fde4799c860de8849ad3ca5d6d0a19c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//go:build windows || darwin || (linux && !baremetal && !wasip1 && !wasip2)

// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Test pipes on Unix and Windows systems.

package os_test

import (
	"bytes"
	"os"
	"testing"
)

// TestSmokePipe is a simple smoke test for Pipe().
func TestSmokePipe(t *testing.T) {
	// Procedure:
	// 1. Get the bytes
	// 2. Light the bytes on fire
	// 3. Smoke the bytes

	r, w, err := os.Pipe()
	if err != nil {
		t.Fatal(err)
		return // TODO: remove once Fatal is fatal
	}
	defer r.Close()
	defer w.Close()

	msg := []byte("Sed nvlla nisi ardva virtvs")
	n, err := w.Write(msg)
	if err != nil {
		t.Errorf("Writing to fresh pipe failed, error %v", err)
	}
	want := len(msg)
	if n != want {
		t.Errorf("Writing to fresh pipe wrote %d bytes, expected %d", n, want)
	}

	buf := make([]byte, 2*len(msg))
	n, err = r.Read(buf)
	if err != nil {
		t.Errorf("Reading from pipe failed, error %v", err)
	}
	if n != want {
		t.Errorf("Reading from pipe got %d bytes, expected %d", n, want)
	}
	// Read() does not set len(buf), so do it here.
	buf = buf[:n]
	if !bytes.Equal(buf, msg) {
		t.Errorf("Reading from fresh pipe got wrong bytes")
	}
}