Skip to content

Commit

Permalink
unix: add TestSockaddrALG
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
kolyshkin authored and gopherbot committed Sep 15, 2023
1 parent 8d9dcc4 commit c7cbcbb
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions unix/syscall_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package unix_test
import (
"bufio"
"bytes"
"encoding/hex"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -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)
}
}

0 comments on commit c7cbcbb

Please sign in to comment.