1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
// Copyright 2018 Tobias Klauser. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
package sysconf_test
import (
"bytes"
"os/exec"
"strconv"
"strings"
"testing"
"github.com/tklauser/go-sysconf"
)
func TestSysconf(t *testing.T) {
// Just test the basic functionality here. The comparison tests against
// C.sysconf are in the test directory.
val, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
if err != nil {
t.Fatalf("Sysconf(SC_CLK_TCK): %v", err)
}
t.Logf("clock ticks = %v", val)
_, err = sysconf.Sysconf(-1)
if err == nil {
t.Errorf("Sysconf(-1) returned %v, want non-nil", err)
}
}
func TestOpenMax(t *testing.T) {
openMax, err := sysconf.Sysconf(sysconf.SC_OPEN_MAX)
if err != nil {
t.Fatalf("Sysconf(SC_OPEN_MAX): %v", err)
}
// from https://pubs.opengroup.org/onlinepubs/009695399/basedefs/limits.h.html
_POSIX_OPEN_MAX := int64(20)
// according to sysconf(3), OPEN_MAX must be ≥ _POSIX_OPEN_MAX
if openMax < _POSIX_OPEN_MAX {
t.Errorf("Sysconf(SC_OPEN_MAX) (%d) expected to be greater or equal _POSIX_OPEN_MAX (%d)",
openMax, _POSIX_OPEN_MAX)
}
}
func TestGetconf(t *testing.T) {
testCases := []struct {
goVar int
name string
}{
{sysconf.SC_CLK_TCK, "CLK_TCK"},
{sysconf.SC_HOST_NAME_MAX, "HOST_NAME_MAX"},
//{sysconf.SC_OPEN_MAX, "OPEN_MAX"},
{sysconf.SC_PAGE_SIZE, "PAGE_SIZE"},
}
getconf, err := exec.LookPath("getconf")
if err != nil {
t.Skipf("getconf not found in PATH: %v", err)
}
for _, tc := range testCases {
cmd := exec.Command(getconf, tc.name)
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
// Ignore getconf errors and skip the test
t.Skipf("failed to invoke getconf: %v", err)
break
}
want, err := strconv.ParseInt(strings.TrimSpace(out.String()), 10, 64)
if err != nil {
t.Errorf("strconv.ParseInt: %v", err)
}
got, err := sysconf.Sysconf(tc.goVar)
if err != nil {
t.Errorf("Sysconf(%s/%d): %v", tc.name, tc.goVar, err)
}
if got != want {
t.Errorf("Sysconf(%v/%d) returned %v, want %v", tc.name, tc.goVar, got, want)
}
}
}
|