@@ -10,6 +10,7 @@ package unix_test
10
10
import (
11
11
"bufio"
12
12
"bytes"
13
+ "encoding/hex"
13
14
"errors"
14
15
"fmt"
15
16
"io"
@@ -1182,3 +1183,42 @@ func TestReadvAllocate(t *testing.T) {
1182
1183
unix .Preadv2 (fd , iovs , 0 , 0 )
1183
1184
})
1184
1185
}
1186
+
1187
+ func TestSockaddrALG (t * testing.T ) {
1188
+ // Open a socket to perform SHA1 hashing.
1189
+ fd , err := unix .Socket (unix .AF_ALG , unix .SOCK_SEQPACKET , 0 )
1190
+ if err != nil {
1191
+ t .Skip ("socket(AF_ALG):" , err )
1192
+ }
1193
+ defer unix .Close (fd )
1194
+ addr := & unix.SockaddrALG {Type : "hash" , Name : "sha1" }
1195
+ if err := unix .Bind (fd , addr ); err != nil {
1196
+ t .Fatal ("bind:" , err )
1197
+ }
1198
+ // Need to call accept(2) with the second and third arguments as 0,
1199
+ // which is not possible via unix.Accept, thus the use of unix.Syscall.
1200
+ hashfd , _ , errno := unix .Syscall6 (unix .SYS_ACCEPT4 , uintptr (fd ), 0 , 0 , 0 , 0 , 0 )
1201
+ if errno != 0 {
1202
+ t .Fatal ("accept:" , errno )
1203
+ }
1204
+
1205
+ hash := os .NewFile (hashfd , "sha1" )
1206
+ defer hash .Close ()
1207
+
1208
+ // Hash an input string and read the results.
1209
+ const (
1210
+ input = "Hello, world."
1211
+ exp = "2ae01472317d1935a84797ec1983ae243fc6aa28"
1212
+ )
1213
+ if _ , err := hash .WriteString (input ); err != nil {
1214
+ t .Fatal (err )
1215
+ }
1216
+ b := make ([]byte , 20 )
1217
+ if _ , err := hash .Read (b ); err != nil {
1218
+ t .Fatal (err )
1219
+ }
1220
+ got := hex .EncodeToString (b )
1221
+ if got != exp {
1222
+ t .Fatalf ("got: %q, want: %q" , got , exp )
1223
+ }
1224
+ }
0 commit comments