-
Notifications
You must be signed in to change notification settings - Fork 2
/
integration_test.go
82 lines (67 loc) · 1.62 KB
/
integration_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main_test
import (
"bytes"
"net"
"os"
"testing"
"time"
"github.com/pin/tftp/v3"
"github.com/puhitaku/tftp-now/server"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"golang.org/x/sync/errgroup"
)
var _ = func() any {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout})
os.Remove("testfile")
return nil
}()
func TestWriteRead(t *testing.T) {
eg := errgroup.Group{}
svr := tftp.NewServer(server.ReadHandler, server.WriteHandler)
addr := "localhost:10069"
a, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
t.Fatalf("failed to resolve addr: %s", err)
}
conn, err := net.ListenUDP("udp", a)
if err != nil {
t.Fatalf("failed to listen: %s", err)
}
eg.Go(func() error {
return svr.Serve(conn)
})
time.Sleep(100 * time.Millisecond)
cli, err := tftp.NewClient(addr)
if err != nil {
t.Fatalf("failed to create a client: %s", err)
}
rf, err := cli.Send("testfile", "octet")
if err != nil {
t.Fatalf("failed to start sending: %s", err)
}
defer os.Remove("testfile")
body := []byte{0, 1, 2, 3}
readBuf := bytes.NewBuffer(body)
_, err = rf.ReadFrom(readBuf)
if err != nil {
t.Fatalf("failed to send: %s", err)
}
wt, err := cli.Receive("testfile", "octet")
if err != nil {
t.Fatalf("failed to start receiving: %s", err)
}
writeBuf := bytes.NewBuffer(nil)
_, err = wt.WriteTo(writeBuf)
if err != nil {
t.Fatalf("failed to receive: %s", err)
}
if b := writeBuf.Bytes(); !bytes.Equal(body, b) {
t.Errorf("received data differ, expect: %+v actual: %+v", body, b)
}
svr.Shutdown()
err = eg.Wait()
if err != nil {
t.Fatalf("server returned an error: %s", err)
}
}