Black Lives Matter. Support the Equal Justice Initiative.

Source file src/syscall/syscall_freebsd_test.go

Documentation: syscall

     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  //go:build freebsd
     6  // +build freebsd
     7  
     8  package syscall_test
     9  
    10  import (
    11  	"fmt"
    12  	"syscall"
    13  	"testing"
    14  	"unsafe"
    15  )
    16  
    17  func TestConvertFromDirent11(t *testing.T) {
    18  	const (
    19  		filenameFmt  = "%04d"
    20  		numFiles     = 64
    21  		fixedHdrSize = int(unsafe.Offsetof(syscall.Dirent_freebsd11{}.Name))
    22  	)
    23  
    24  	namlen := len(fmt.Sprintf(filenameFmt, 0))
    25  	reclen := syscall.Roundup(fixedHdrSize+namlen+1, 4)
    26  	old := make([]byte, numFiles*reclen)
    27  	for i := 0; i < numFiles; i++ {
    28  		dent := syscall.Dirent_freebsd11{
    29  			Fileno: uint32(i + 1),
    30  			Reclen: uint16(reclen),
    31  			Type:   syscall.DT_REG,
    32  			Namlen: uint8(namlen),
    33  		}
    34  		rec := make([]byte, reclen)
    35  		copy(rec, (*[fixedHdrSize]byte)(unsafe.Pointer(&dent))[:])
    36  		copy(rec[fixedHdrSize:], fmt.Sprintf(filenameFmt, i+1))
    37  		copy(old[i*reclen:], rec)
    38  	}
    39  
    40  	buf := make([]byte, 2*len(old))
    41  	n := syscall.ConvertFromDirents11(buf, old)
    42  
    43  	names := make([]string, 0, numFiles)
    44  	_, _, names = syscall.ParseDirent(buf[:n], -1, names)
    45  
    46  	if len(names) != numFiles {
    47  		t.Errorf("expected %d files, have %d; names: %q", numFiles, len(names), names)
    48  	}
    49  
    50  	for i, name := range names {
    51  		if expected := fmt.Sprintf(filenameFmt, i+1); name != expected {
    52  			t.Errorf("expected names[%d] to be %q; got %q", i, expected, name)
    53  		}
    54  	}
    55  }
    56  

View as plain text