unix: add TestSockaddrALG
Change-Id: I5e5c173289831100360e35cdf2ddd46b173695f9
Reviewed-on: https://go-review.googlesource.com/c/sys/+/527837
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
Run-TryBot: Kirill Kolyshkin <kolyshkin@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
diff --git a/unix/syscall_linux_test.go b/unix/syscall_linux_test.go
index 200ccd8..36b1e7f 100644
--- a/unix/syscall_linux_test.go
+++ b/unix/syscall_linux_test.go
@@ -10,6 +10,7 @@
import (
"bufio"
"bytes"
+ "encoding/hex"
"errors"
"fmt"
"io"
@@ -1182,3 +1183,42 @@
unix.Preadv2(fd, iovs, 0, 0)
})
}
+
+func TestSockaddrALG(t *testing.T) {
+ // Open a socket to perform SHA1 hashing.
+ fd, err := unix.Socket(unix.AF_ALG, unix.SOCK_SEQPACKET, 0)
+ if err != nil {
+ t.Skip("socket(AF_ALG):", err)
+ }
+ defer unix.Close(fd)
+ addr := &unix.SockaddrALG{Type: "hash", Name: "sha1"}
+ if err := unix.Bind(fd, addr); err != nil {
+ t.Fatal("bind:", err)
+ }
+ // Need to call accept(2) with the second and third arguments as 0,
+ // which is not possible via unix.Accept, thus the use of unix.Syscall.
+ hashfd, _, errno := unix.Syscall6(unix.SYS_ACCEPT4, uintptr(fd), 0, 0, 0, 0, 0)
+ if errno != 0 {
+ t.Fatal("accept:", errno)
+ }
+
+ hash := os.NewFile(hashfd, "sha1")
+ defer hash.Close()
+
+ // Hash an input string and read the results.
+ const (
+ input = "Hello, world."
+ exp = "2ae01472317d1935a84797ec1983ae243fc6aa28"
+ )
+ if _, err := hash.WriteString(input); err != nil {
+ t.Fatal(err)
+ }
+ b := make([]byte, 20)
+ if _, err := hash.Read(b); err != nil {
+ t.Fatal(err)
+ }
+ got := hex.EncodeToString(b)
+ if got != exp {
+ t.Fatalf("got: %q, want: %q", got, exp)
+ }
+}