github.com/tailscale/wireguard-go@v0.0.20201119-0.20210522003738-46b531feb08a/device/bind_test.go (about)

     1  /* SPDX-License-Identifier: MIT
     2   *
     3   * Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved.
     4   */
     5  
     6  package device
     7  
     8  import (
     9  	"errors"
    10  
    11  	"github.com/tailscale/wireguard-go/conn"
    12  )
    13  
    14  type DummyDatagram struct {
    15  	msg      []byte
    16  	endpoint conn.Endpoint
    17  }
    18  
    19  type DummyBind struct {
    20  	in6    chan DummyDatagram
    21  	in4    chan DummyDatagram
    22  	closed bool
    23  }
    24  
    25  func (b *DummyBind) SetMark(v uint32) error {
    26  	return nil
    27  }
    28  
    29  func (b *DummyBind) ReceiveIPv6(buff []byte) (int, conn.Endpoint, error) {
    30  	datagram, ok := <-b.in6
    31  	if !ok {
    32  		return 0, nil, errors.New("closed")
    33  	}
    34  	copy(buff, datagram.msg)
    35  	return len(datagram.msg), datagram.endpoint, nil
    36  }
    37  
    38  func (b *DummyBind) ReceiveIPv4(buff []byte) (int, conn.Endpoint, error) {
    39  	datagram, ok := <-b.in4
    40  	if !ok {
    41  		return 0, nil, errors.New("closed")
    42  	}
    43  	copy(buff, datagram.msg)
    44  	return len(datagram.msg), datagram.endpoint, nil
    45  }
    46  
    47  func (b *DummyBind) Close() error {
    48  	close(b.in6)
    49  	close(b.in4)
    50  	b.closed = true
    51  	return nil
    52  }
    53  
    54  func (b *DummyBind) Send(buff []byte, end conn.Endpoint) error {
    55  	return nil
    56  }