-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathalg_linux_test.go
More file actions
132 lines (106 loc) · 2.85 KB
/
alg_linux_test.go
File metadata and controls
132 lines (106 loc) · 2.85 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
//+build linux
package alg
import (
"bytes"
"reflect"
"testing"
"golang.org/x/sys/unix"
)
func TestLinuxConn_bind(t *testing.T) {
addr := &unix.SockaddrALG{
Type: "hash",
Name: "sha1",
}
s := &testSocket{}
if _, err := bind(s, addr); err != nil {
t.Fatalf("failed to bind: %v", err)
}
if want, got := addr, s.bind; !reflect.DeepEqual(want, got) {
t.Fatalf("unexpected bind address:\n- want: %#v\n- got: %#v",
want, got)
}
}
func TestLinuxConnWrite(t *testing.T) {
addr := &unix.SockaddrALG{
Type: "hash",
Name: "sha1",
}
h, s := testLinuxHash(t, addr)
b := []byte("hello world")
if _, err := h.Write(b); err != nil {
t.Fatalf("failed to write: %v", err)
}
if want, got := b, s.sendto.p; !bytes.Equal(want, got) {
t.Fatalf("unexpected sendto bytes:\n- want: %v\n- got: %v",
want, got)
}
if want, got := unix.MSG_MORE, s.sendto.flags; want != got {
t.Fatalf("unexpected sendto flags:\n- want: %v\n- got: %v",
want, got)
}
if want, got := addr, s.sendto.to; !reflect.DeepEqual(want, got) {
t.Fatalf("unexpected sendto addr:\n- want: %v\n- got: %v",
want, got)
}
}
func TestLinuxConnSum(t *testing.T) {
addr := &unix.SockaddrALG{
Type: "hash",
Name: "sha1",
}
h, s := testLinuxHash(t, addr)
s.read = []byte("deadbeef")
sum := h.Sum([]byte("foo"))
if want, got := []byte("foodeadbeef"), sum; !bytes.Equal(want, got) {
t.Fatalf("unexpected sum bytes:\n- want: %v\n- got: %v",
want, got)
}
}
func testLinuxHash(t *testing.T, addr *unix.SockaddrALG) (Hash, *testSocket) {
s := &testSocket{}
c, err := bind(s, addr)
if err != nil {
t.Fatalf("failed to bind: %v", err)
}
hash, err := c.Hash(0, 0)
if err != nil {
t.Fatalf("failed to create hash: %v", err)
}
// A little gross, but gets the job done.
return hash, hash.(*ihash).s.(*testSocket)
}
var _ socket = &testSocket{}
type testSocket struct {
bind unix.Sockaddr
sendto struct {
p []byte
flags int
to unix.Sockaddr
}
read []byte
noopSocket
}
func (s *testSocket) Accept() (socket, error) {
return &testSocket{}, nil
}
func (s *testSocket) Bind(sa unix.Sockaddr) error {
s.bind = sa
return nil
}
func (s *testSocket) Read(p []byte) (int, error) {
n := copy(p, s.read)
return n, nil
}
func (s *testSocket) Sendto(p []byte, flags int, to unix.Sockaddr) error {
s.sendto.p = p
s.sendto.flags = flags
s.sendto.to = to
return nil
}
var _ socket = &noopSocket{}
type noopSocket struct{}
func (s *noopSocket) Accept() (socket, error) { return nil, nil }
func (s *noopSocket) Bind(sa unix.Sockaddr) error { return nil }
func (s *noopSocket) Close() error { return nil }
func (s *noopSocket) Read(p []byte) (int, error) { return 0, nil }
func (s *noopSocket) Sendto(p []byte, flags int, to unix.Sockaddr) error { return nil }