Source file src/internal/runtime/wasitest/tcpecho_test.go

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package wasi_test
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"internal/testenv"
    11  	"math/rand"
    12  	"net"
    13  	"os"
    14  	"os/exec"
    15  	"testing"
    16  	"time"
    17  )
    18  
    19  func TestTCPEcho(t *testing.T) {
    20  	if target != "wasip1/wasm" {
    21  		t.Skip()
    22  	}
    23  
    24  	testenv.MustHaveGoRun(t)
    25  
    26  	// We're unable to use port 0 here (let the OS choose a spare port).
    27  	// Although the WASM runtime accepts port 0, and the WASM module listens
    28  	// successfully, there's no way for this test to query the selected port
    29  	// so that it can connect to the WASM module. The WASM module itself
    30  	// cannot access any information about the socket due to limitations
    31  	// with WASI preview 1 networking, and the WASM runtimes do not log the
    32  	// port when you pre-open a socket. So, we probe for a free port here.
    33  	// Given there's an unavoidable race condition, the test is disabled by
    34  	// default.
    35  	if os.Getenv("GOWASIENABLERACYTEST") != "1" {
    36  		t.Skip("skipping WASI test with unavoidable race condition")
    37  	}
    38  	var host string
    39  	port := rand.Intn(10000) + 40000
    40  	for attempts := 0; attempts < 10; attempts++ {
    41  		host = fmt.Sprintf("127.0.0.1:%d", port)
    42  		l, err := net.Listen("tcp", host)
    43  		if err == nil {
    44  			l.Close()
    45  			break
    46  		}
    47  		port++
    48  	}
    49  
    50  	subProcess := exec.Command(testenv.GoToolPath(t), "run", "./testdata/tcpecho.go")
    51  
    52  	subProcess.Env = append(os.Environ(), "GOOS=wasip1", "GOARCH=wasm")
    53  
    54  	switch os.Getenv("GOWASIRUNTIME") {
    55  	case "wazero":
    56  		subProcess.Env = append(subProcess.Env, "GOWASIRUNTIMEARGS=--listen="+host)
    57  	case "wasmtime", "":
    58  		subProcess.Env = append(subProcess.Env, "GOWASIRUNTIMEARGS=--tcplisten="+host)
    59  	default:
    60  		t.Skip("WASI runtime does not support sockets")
    61  	}
    62  
    63  	var b bytes.Buffer
    64  	subProcess.Stdout = &b
    65  	subProcess.Stderr = &b
    66  
    67  	if err := subProcess.Start(); err != nil {
    68  		t.Log(b.String())
    69  		t.Fatal(err)
    70  	}
    71  	defer subProcess.Process.Kill()
    72  
    73  	var conn net.Conn
    74  	for {
    75  		var err error
    76  		conn, err = net.Dial("tcp", host)
    77  		if err == nil {
    78  			break
    79  		}
    80  		time.Sleep(500 * time.Millisecond)
    81  	}
    82  	defer conn.Close()
    83  
    84  	payload := []byte("foobar")
    85  	if _, err := conn.Write(payload); err != nil {
    86  		t.Fatal(err)
    87  	}
    88  	var buf [256]byte
    89  	n, err := conn.Read(buf[:])
    90  	if err != nil {
    91  		t.Fatal(err)
    92  	}
    93  	if string(buf[:n]) != string(payload) {
    94  		t.Error("unexpected payload")
    95  		t.Logf("expect: %d bytes (%v)", len(payload), payload)
    96  		t.Logf("actual: %d bytes (%v)", n, buf[:n])
    97  	}
    98  }
    99  

View as plain text