-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinux_test.go
65 lines (59 loc) · 1.08 KB
/
linux_test.go
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
package pun
import (
"context"
"log"
"math/rand"
"net"
"os"
"os/signal"
"syscall"
"testing"
)
func TestLinuxPlatform(t *testing.T) {
appCtx := context.Background()
config := Config{
Name: "TestPun",
MTU: 1500,
}
if IPv4, CIDRv4, err := net.ParseCIDR("10.10.10.14/24"); err != nil {
log.Fatal(err)
} else {
config.CIDRv4 = *CIDRv4
config.CIDRv4.IP = IPv4
}
device, err := New(&config, appCtx)
if err != nil {
log.Fatal(err)
}
out, in := device.OpenStream()
go func() {
for {
data := <-out
log.Println("origin", data)
}
}()
go func() {
for {
packet := make([]byte, 4*1024)
length := fillRandomBytes(packet)
in <- packet[:length]
}
}()
stream, err := device.OpenExtraStream()
if err != nil {
log.Fatal(err)
}
data := <-stream.OutputStream
log.Println("extra", data)
stream.Close()
log.Println("waiting for signal")
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
}
func fillRandomBytes(packet []byte) int {
for i := range packet {
packet[i] = byte(rand.Intn(256))
}
return len(packet)
}