forked from mdlayher/alg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alg_linux_test.go
121 lines (99 loc) · 2.68 KB
/
alg_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
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
//+build linux
package alg
import (
"encoding/hex"
"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, _ := testLinuxHash(t, addr)
b := []byte("hello world")
if _, err := h.Write(b); err != nil {
t.Fatalf("failed to write: %v", err)
}
}
func TestLinuxConnSum(t *testing.T) {
addr := &unix.SockaddrALG{
Type: "hash",
Name: "sha1",
}
h, _ := testLinuxHash(t, addr)
sum := h.Sum(nil)
hex.EncodeToString(sum)
if want, got := "da39a3ee5e6b4b0d3255bfef95601890afd80709", hex.EncodeToString(sum); want != got {
t.Fatalf("unexpected sum bytes:\n- want: %v\n- got: %v",
want, got)
}
}
func testLinuxHash(t *testing.T, addr *unix.SockaddrALG) (Hash, *sysSocket) {
fd, err := unix.Socket(unix.AF_ALG, unix.SOCK_SEQPACKET, 0)
if err != nil {
t.Fatalf("failed to create socket: %v", err)
}
c, err := bind(&sysSocket{fd: fd}, 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.(*sysSocket)
}
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) FD() int { return 0 }
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 }