-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_test.go
More file actions
142 lines (135 loc) · 3.25 KB
/
Copy pathmodel_test.go
File metadata and controls
142 lines (135 loc) · 3.25 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"math/rand"
"net/netip"
"testing"
"time"
)
func TestAttackRateCurve(t *testing.T) {
model := AttackModel{Config: RuntimeAttack{
PeakRateBPS: 100,
RampUp: 10 * time.Second,
Hold: 20 * time.Second,
RampDown: 10 * time.Second,
}}
checks := []struct {
at time.Duration
want float64
}{
{0, 0},
{5 * time.Second, 50},
{10 * time.Second, 100},
{25 * time.Second, 100},
{35 * time.Second, 50},
{40 * time.Second, 0},
}
for _, check := range checks {
if got := model.RateAt(check.at); got != check.want {
t.Fatalf("RateAt(%s)=%f, expected %f", check.at, got, check.want)
}
}
}
func TestAmbientOneSampleBudget(t *testing.T) {
prefix := netip.MustParsePrefix("10.0.0.1/32")
peer := netip.MustParsePrefix("198.18.0.0/15")
cfg := RuntimeAmbient{
Common: RuntimeCommon{
SamplingRate: 10,
Seed: 1,
Packet: PacketConfig{
MeanSize: 1000,
MinSize: 1000,
MaxSize: 1000,
},
Protocols: ProtocolMix{UDP: 1},
},
Networks: []RuntimeAmbientNetwork{{
Prefix: prefix,
IncomingShare: 1,
RateBPS: 80000,
Hosts: 1,
Correlation: 0.9,
PeerPrefixes: []netip.Prefix{peer},
}},
}
model, err := NewAmbientModel(cfg)
if err != nil {
t.Fatal(err)
}
result, err := model.Tick(time.Second, 10)
if err != nil {
t.Fatal(err)
}
if len(result.Packets) != 1 {
t.Fatalf("generated %d samples, expected 1", len(result.Packets))
}
if result.Packets[0].FrameLength != 1000 {
t.Fatalf("frame length=%d, expected 1000", result.Packets[0].FrameLength)
}
}
func TestAddressGenerationStaysInsidePrefix(t *testing.T) {
rng := rand.New(rand.NewSource(1))
prefixes := []netip.Prefix{
netip.MustParsePrefix("10.0.0.0/24"),
netip.MustParsePrefix("2001:db8:10::/64"),
}
for _, prefix := range prefixes {
addresses, err := generateAddresses(prefix, 32, rng)
if err != nil {
t.Fatal(err)
}
for _, address := range addresses {
if !prefix.Contains(address) {
t.Fatalf("%s is outside %s", address, prefix)
}
}
}
}
func TestAmbientBidirectionalShares(t *testing.T) {
prefix := netip.MustParsePrefix("10.0.0.1/32")
peer := netip.MustParsePrefix("198.18.0.0/15")
cfg := RuntimeAmbient{
Common: RuntimeCommon{
SamplingRate: 10,
Seed: 1,
Packet: PacketConfig{
MeanSize: 1000,
MinSize: 1000,
MaxSize: 1000,
},
Protocols: ProtocolMix{UDP: 1},
},
Networks: []RuntimeAmbientNetwork{{
Prefix: prefix,
IncomingShare: 0.75,
OutgoingShare: 0.25,
RateBPS: 320000,
Hosts: 1,
Correlation: 0.9,
PeerPrefixes: []netip.Prefix{peer},
}},
}
model, err := NewAmbientModel(cfg)
if err != nil {
t.Fatal(err)
}
result, err := model.Tick(time.Second, 10)
if err != nil {
t.Fatal(err)
}
var incoming, outgoing int
for _, packet := range result.Packets {
switch packet.Direction {
case DirectionIncoming:
incoming++
case DirectionOutgoing:
outgoing++
}
}
if incoming != 3 || outgoing != 1 {
t.Fatalf("generated incoming=%d outgoing=%d, expected 3/1", incoming, outgoing)
}
if result.TargetIncomingBPS != 240000 || result.TargetOutgoingBPS != 80000 {
t.Fatalf("target split=%f/%f, expected 240000/80000", result.TargetIncomingBPS, result.TargetOutgoingBPS)
}
}