github.com/google/fleetspeak@v0.1.15-0.20240426164851-4f31f62c1aea/fleetspeak/src/windows/hashpipe/hashpipe_windows_test.go (about)

     1  // Copyright 2017 Google Inc.
     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  //     https://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  //go:build windows
    16  
    17  package hashpipe
    18  
    19  import (
    20  	"io"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/Microsoft/go-winio"
    25  	"github.com/google/fleetspeak/fleetspeak/src/client/socketservice/checks"
    26  )
    27  
    28  func TestHashpipe(t *testing.T) {
    29  	l, path, err := ListenPipe(nil)
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  
    34  	t.Logf("Using pipe address: [%v]", path)
    35  
    36  	if err = checks.CheckSocketPipe(path); err != nil {
    37  		t.Error(err)
    38  	}
    39  
    40  	const (
    41  		clientMsg = "Hello hashpipe server, hashpipe client here."
    42  		serverMsg = "Hello hashpipe client, hashpipe server here."
    43  	)
    44  
    45  	done := make(chan struct{})
    46  	go func() {
    47  		timeout := time.Second
    48  		conn, err := winio.DialPipe(path, &timeout)
    49  		if err != nil {
    50  			t.Error(err)
    51  		}
    52  
    53  		if n, err := conn.Write([]byte(clientMsg)); err != nil {
    54  			t.Errorf("Winio pipe .Write([]byte(%q)): (%d, %v)", clientMsg, n, err)
    55  		}
    56  
    57  		msgBuf, err := io.ReadAll(conn)
    58  		if err != nil {
    59  			t.Error(err)
    60  		}
    61  
    62  		if err = conn.Close(); err != nil {
    63  			t.Error(err)
    64  		}
    65  
    66  		if got, want := string(msgBuf), serverMsg; got != want {
    67  			t.Errorf("Unexpected message from server; got [%q], want [%q]", got, want)
    68  		}
    69  
    70  		close(done)
    71  	}()
    72  
    73  	conn, err := l.Accept()
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  
    78  	if err = l.Close(); err != nil {
    79  		t.Error(err)
    80  	}
    81  
    82  	msgBuf := make([]byte, len(clientMsg))
    83  	n, err := io.ReadFull(conn, msgBuf)
    84  	if err != nil {
    85  		t.Errorf("io.ReadFull(...): (%d, %v)", n, err)
    86  	}
    87  
    88  	// Writes are blocking, so the order is important.
    89  	if n, err := conn.Write([]byte(serverMsg)); err != nil {
    90  		t.Errorf("Winio pipe .Write([]byte(%q)): (%d, %v)", serverMsg, n, err)
    91  	}
    92  
    93  	if err = conn.Close(); err != nil {
    94  		t.Error(err)
    95  	}
    96  
    97  	if got, want := string(msgBuf), clientMsg; got != want {
    98  		t.Errorf("Unexpected message from client; got [%q], want [%q]", got, want)
    99  	}
   100  
   101  	<-done
   102  }