Skip to content

Commit c7cbcbb

Browse files
kolyshkingopherbot
authored andcommitted
unix: add TestSockaddrALG
Change-Id: I5e5c173289831100360e35cdf2ddd46b173695f9 Reviewed-on: https://go-review.googlesource.com/c/sys/+/527837 Reviewed-by: Bryan Mills <[email protected]> Reviewed-by: Tobias Klauser <[email protected]> Run-TryBot: Kirill Kolyshkin <[email protected]> TryBot-Result: Gopher Robot <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 8d9dcc4 commit c7cbcbb

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

unix/syscall_linux_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package unix_test
1010
import (
1111
"bufio"
1212
"bytes"
13+
"encoding/hex"
1314
"errors"
1415
"fmt"
1516
"io"
@@ -1182,3 +1183,42 @@ func TestReadvAllocate(t *testing.T) {
11821183
unix.Preadv2(fd, iovs, 0, 0)
11831184
})
11841185
}
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

Comments
 (0)