github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/strace/syscalls_test.go (about)

     1  // Copyright 2021 the u-root 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  package strace
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  
    11  	"golang.org/x/sys/unix"
    12  )
    13  
    14  func TestByName(t *testing.T) {
    15  	for _, tt := range []struct {
    16  		name string
    17  		val  uintptr
    18  		ret  error
    19  	}{
    20  		{name: "open", val: unix.SYS_OPEN, ret: nil},
    21  		{name: "Xopen", val: unix.SYS_OPEN, ret: fmt.Errorf("Xopen:not found")},
    22  	} {
    23  		n, err := ByName(tt.name)
    24  		if err != nil && tt.ret == nil {
    25  			t.Errorf("ByName(%s): %v != %v", tt.name, err, tt.ret)
    26  		}
    27  		if err == nil && tt.ret != nil {
    28  			t.Errorf("ByName(%s): %v != %v", tt.name, err, tt.ret)
    29  		}
    30  		if err == nil && n != tt.val {
    31  			t.Errorf("ByName(%s): %v != %v", tt.name, n, tt.val)
    32  		}
    33  	}
    34  }
    35  
    36  func TestByNum(t *testing.T) {
    37  	for _, tt := range []struct {
    38  		name string
    39  		val  uintptr
    40  		ret  error
    41  	}{
    42  		{name: "open", val: unix.SYS_OPEN, ret: nil},
    43  		{name: "bogus", val: 10000000, ret: fmt.Errorf("Xopen:not found")},
    44  	} {
    45  		n, err := ByNumber(tt.val)
    46  		if err != nil && tt.ret == nil {
    47  			t.Errorf("Bynumber(%d): %v != %v", tt.val, err, tt.ret)
    48  		}
    49  		if err == nil && tt.ret != nil {
    50  			t.Errorf("Bynumber(%d): %v != %v", tt.val, err, tt.ret)
    51  		}
    52  		if err == nil && n != tt.name {
    53  			t.Errorf("Bynumber(%d): %v != %v", tt.val, n, tt.name)
    54  		}
    55  	}
    56  }