github.com/liloew/wireguard-go@v0.0.0-20220224014633-9cd745e6f114/device/endpoint_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  	"math/rand"
    10  
    11  	"golang.zx2c4.com/go118/netip"
    12  )
    13  
    14  type DummyEndpoint struct {
    15  	src, dst netip.Addr
    16  }
    17  
    18  func CreateDummyEndpoint() (*DummyEndpoint, error) {
    19  	var src, dst [16]byte
    20  	if _, err := rand.Read(src[:]); err != nil {
    21  		return nil, err
    22  	}
    23  	_, err := rand.Read(dst[:])
    24  	return &DummyEndpoint{netip.AddrFrom16(src), netip.AddrFrom16(dst)}, err
    25  }
    26  
    27  func (e *DummyEndpoint) ClearSrc() {}
    28  
    29  func (e *DummyEndpoint) SrcToString() string {
    30  	return netip.AddrPortFrom(e.SrcIP(), 1000).String()
    31  }
    32  
    33  func (e *DummyEndpoint) DstToString() string {
    34  	return netip.AddrPortFrom(e.DstIP(), 1000).String()
    35  }
    36  
    37  func (e *DummyEndpoint) DstToBytes() []byte {
    38  	out := e.DstIP().AsSlice()
    39  	out = append(out, byte(1000&0xff))
    40  	out = append(out, byte((1000>>8)&0xff))
    41  	return out
    42  }
    43  
    44  func (e *DummyEndpoint) DstIP() netip.Addr {
    45  	return e.dst
    46  }
    47  
    48  func (e *DummyEndpoint) SrcIP() netip.Addr {
    49  	return e.src
    50  }