nanomsg.org/go/mangos/v2@v2.0.9-0.20200203084354-8a092611e461/transport/ipc/ipc_windows_test.go (about)

     1  // Copyright 2019 The Mangos Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use file except in compliance with the License.
     5  // You may obtain a copy of the license at
     6  //
     7  //    http://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  // +build windows
    16  
    17  package ipc
    18  
    19  import (
    20  	"github.com/Microsoft/go-winio"
    21  	"nanomsg.org/go/mangos/v2"
    22  	. "nanomsg.org/go/mangos/v2/internal/test"
    23  	"net"
    24  	"testing"
    25  	"time"
    26  )
    27  
    28  func TestIpcListenerOptions(t *testing.T) {
    29  	sock := GetMockSocket()
    30  	l, e := tran.NewListener(AddrTestIPC(), sock)
    31  	MustSucceed(t, e)
    32  
    33  	// Security Descriptor
    34  	sd := "O:AOG:DAD:(A;;RPWPCCDCLCSWRCWDWOGA;;;S-1-0-0)"
    35  	MustBeError(t, l.SetOption(OptionSecurityDescriptor, 0), mangos.ErrBadValue)
    36  	MustBeError(t, l.SetOption(OptionSecurityDescriptor, true), mangos.ErrBadValue)
    37  	MustSucceed(t, l.SetOption(OptionSecurityDescriptor, sd)) // SDDL not validated
    38  	v, e := l.GetOption(OptionSecurityDescriptor)
    39  	MustSucceed(t, e)
    40  	sd2, ok := v.(string)
    41  	MustBeTrue(t, ok)
    42  	MustBeTrue(t, sd2 == sd)
    43  
    44  	for _, opt := range []string{OptionInputBufferSize, OptionOutputBufferSize} {
    45  		MustBeError(t, l.SetOption(opt, "string"), mangos.ErrBadValue)
    46  		MustBeError(t, l.SetOption(opt, true), mangos.ErrBadValue)
    47  		MustSucceed(t, l.SetOption(opt, int32(16384)))
    48  		v, e = l.GetOption(opt)
    49  		MustSucceed(t, e)
    50  		v2, ok := v.(int32)
    51  		MustBeTrue(t, ok)
    52  		MustBeTrue(t, v2 == 16384)
    53  	}
    54  }
    55  
    56  type testAddr string
    57  
    58  func (a testAddr) testDial() (net.Conn, error) {
    59  	path := "\\\\.\\pipe\\" + a[len("ipc://"):]
    60  	return winio.DialPipe(string(path), nil)
    61  }
    62  
    63  func TestIpcAbortHandshake(t *testing.T) {
    64  	sock := GetMockSocket()
    65  	defer MustClose(t, sock)
    66  	addr := AddrTestIPC()
    67  	// Small buffer size so we can see the effect of early close
    68  	l, e := sock.NewListener(addr, nil)
    69  	MustSucceed(t, e)
    70  	MustSucceed(t, l.SetOption(OptionOutputBufferSize, int32(2)))
    71  	MustSucceed(t, l.Listen())
    72  	c, e := testAddr(addr).testDial()
    73  	MustSucceed(t, e)
    74  	MustSucceed(t, c.Close())
    75  }
    76  
    77  func TestIpcBadHandshake(t *testing.T) {
    78  	sock := GetMockSocket()
    79  	defer MustClose(t, sock)
    80  	addr := AddrTestIPC()
    81  	l, e := sock.NewListener(addr, nil)
    82  	MustSucceed(t, e)
    83  	MustSucceed(t, l.Listen())
    84  	TranSendConnBadHandshakes(t, testAddr(addr).testDial)
    85  }
    86  
    87  func TestIpcBadRecv(t *testing.T) {
    88  	sock := GetMockSocket()
    89  	defer MustClose(t, sock)
    90  	addr := AddrTestIPC()
    91  	l, e := sock.NewListener(addr, nil)
    92  	MustSucceed(t, e)
    93  	MustSucceed(t, l.Listen())
    94  	TranSendBadMessages(t, sock.Info().Peer, true, testAddr(addr).testDial)
    95  }
    96  
    97  func TestIpcSendAbort(t *testing.T) {
    98  	sock := GetMockSocket()
    99  	defer MustClose(t, sock)
   100  	addr := AddrTestIPC()
   101  	l, e := sock.NewListener(addr, nil)
   102  	MustSucceed(t, e)
   103  	MustSucceed(t, l.SetOption(OptionOutputBufferSize, int32(128)))
   104  	MustSucceed(t, l.Listen())
   105  	c, e := testAddr(addr).testDial()
   106  	MustSucceed(t, e)
   107  	TranConnHandshake(t, c, sock.Info().Peer)
   108  	MustSend(t, sock, make([]byte, 65536))
   109  	time.Sleep(time.Millisecond * 100)
   110  	MustSucceed(t, c.Close())
   111  }