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>
This commit is contained in:
Kir Kolyshkin
2023-09-12 20:20:38 -07:00
committed by Gopher Robot
parent 8d9dcc467b
commit c7cbcbbfad

View File

@@ -10,6 +10,7 @@ package unix_test
import (
"bufio"
"bytes"
"encoding/hex"
"errors"
"fmt"
"io"
@@ -1182,3 +1183,42 @@ func TestReadvAllocate(t *testing.T) {
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)
}
}