github.com/google/fleetspeak@v0.1.15-0.20240426164851-4f31f62c1aea/fleetspeak/src/windows/wnixsocket/wnixsocket_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 wnixsocket 18 19 import ( 20 "io" 21 "testing" 22 "time" 23 24 "github.com/google/fleetspeak/fleetspeak/src/client/socketservice/checks" 25 "github.com/google/fleetspeak/fleetspeak/src/comtesting" 26 ) 27 28 func TestWnixsocket(t *testing.T) { 29 tmpDir, tmpDirCleanup := comtesting.GetTempDir("wnixsocket") 30 defer tmpDirCleanup() 31 socketPath := tmpDir + `\TestWnixsocketSocket` 32 33 l, err := Listen(socketPath) 34 if err != nil { 35 t.Error(err) 36 } 37 38 if err = checks.CheckSocketFile(socketPath); err != nil { 39 t.Error(err) 40 } 41 42 const ( 43 clientMsg = "Hello Wnixsocket server, Wnixsocket client here." 44 serverMsg = "Hello Wnixsocket client, Wnixsocket server here." 45 ) 46 47 done := make(chan struct{}) 48 go func() { 49 conn, err := Dial(socketPath, time.Second) 50 if err != nil { 51 t.Error(err) 52 } 53 54 if n, err := conn.Write([]byte(clientMsg)); err != nil { 55 t.Errorf("Wnixsocket .Write([]byte(%q)): (%d, %v)", clientMsg, n, err) 56 } 57 58 msgBuf, err := io.ReadAll(conn) 59 if err != nil { 60 t.Error(err) 61 } 62 63 if err = conn.Close(); err != nil { 64 t.Error(err) 65 } 66 67 if got, want := string(msgBuf), serverMsg; got != want { 68 t.Errorf("Unexpected message from server; got [%q], want [%q]", got, want) 69 } 70 71 close(done) 72 }() 73 74 conn, err := l.Accept() 75 if err != nil { 76 t.Fatal(err) 77 } 78 79 if err = l.Close(); err != nil { 80 t.Error(err) 81 } 82 83 msgBuf := make([]byte, len(clientMsg)) 84 n, err := io.ReadFull(conn, msgBuf) 85 if err != nil { 86 t.Errorf("io.ReadFull(...): (%d, %v)", n, err) 87 } 88 89 // Writes are blocking, so the order is important. 90 if n, err := conn.Write([]byte(serverMsg)); err != nil { 91 t.Errorf("Wnixsocket .Write([]byte(%q)): (%d, %v)", serverMsg, n, err) 92 } 93 94 if err = conn.Close(); err != nil { 95 t.Error(err) 96 } 97 98 if got, want := string(msgBuf), clientMsg; got != want { 99 t.Errorf("Unexpected message from client; got [%q], want [%q]", got, want) 100 } 101 102 <-done 103 }