1// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// +build freebsd
6
7package syscall_test
8
9import (
10	"fmt"
11	"syscall"
12	"testing"
13	"unsafe"
14)
15
16func TestConvertFromDirent11(t *testing.T) {
17	const (
18		filenameFmt  = "%04d"
19		numFiles     = 64
20		fixedHdrSize = int(unsafe.Offsetof(syscall.Dirent_freebsd11{}.Name))
21	)
22
23	namlen := len(fmt.Sprintf(filenameFmt, 0))
24	reclen := syscall.Roundup(fixedHdrSize+namlen+1, 4)
25	old := make([]byte, numFiles*reclen)
26	for i := 0; i < numFiles; i++ {
27		dent := syscall.Dirent_freebsd11{
28			Fileno: uint32(i + 1),
29			Reclen: uint16(reclen),
30			Type:   syscall.DT_REG,
31			Namlen: uint8(namlen),
32		}
33		rec := make([]byte, reclen)
34		copy(rec, (*[fixedHdrSize]byte)(unsafe.Pointer(&dent))[:])
35		copy(rec[fixedHdrSize:], fmt.Sprintf(filenameFmt, i+1))
36		copy(old[i*reclen:], rec)
37	}
38
39	buf := make([]byte, 2*len(old))
40	n := syscall.ConvertFromDirents11(buf, old)
41
42	names := make([]string, 0, numFiles)
43	_, _, names = syscall.ParseDirent(buf[:n], -1, names)
44
45	if len(names) != numFiles {
46		t.Errorf("expected %d files, have %d; names: %q", numFiles, len(names), names)
47	}
48
49	for i, name := range names {
50		if expected := fmt.Sprintf(filenameFmt, i+1); name != expected {
51			t.Errorf("expected names[%d] to be %q; got %q", i, expected, name)
52		}
53	}
54}
55