aboutsummaryrefslogtreecommitdiffstats
path: root/device/endpoint_test.go
blob: 93a4998d4ffb663766c4f86f1ea7255a1d66a5b3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* SPDX-License-Identifier: MIT
 *
 * Copyright (C) 2017-2023 WireGuard LLC. All Rights Reserved.
 */

package device

import (
	"math/rand"
	"net/netip"
)

type DummyEndpoint struct {
	src, dst netip.Addr
}

func CreateDummyEndpoint() (*DummyEndpoint, error) {
	var src, dst [16]byte
	if _, err := rand.Read(src[:]); err != nil {
		return nil, err
	}
	_, err := rand.Read(dst[:])
	return &DummyEndpoint{netip.AddrFrom16(src), netip.AddrFrom16(dst)}, err
}

func (e *DummyEndpoint) ClearSrc() {}

func (e *DummyEndpoint) SrcToString() string {
	return netip.AddrPortFrom(e.SrcIP(), 1000).String()
}

func (e *DummyEndpoint) DstToString() string {
	return netip.AddrPortFrom(e.DstIP(), 1000).String()
}

func (e *DummyEndpoint) DstToBytes() []byte {
	out := e.DstIP().AsSlice()
	out = append(out, byte(1000&0xff))
	out = append(out, byte((1000>>8)&0xff))
	return out
}

func (e *DummyEndpoint) DstIP() netip.Addr {
	return e.dst
}

func (e *DummyEndpoint) SrcIP() netip.Addr {
	return e.src
}