github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/ldd/ldd_darwin_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 ldd
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  )
    11  
    12  // lddOne is a helper that runs Ldd on one file. It returns
    13  // the list so we can use elements from the list on another
    14  // test. We do it this way because, unlike /bin/date, there's
    15  // almost nothing else we can assume exists, e.g. /lib/libc.so
    16  // is a different name on almost every *ix* system.
    17  func lddOne(name string) ([]string, error) {
    18  	libMap := make(map[string]bool)
    19  	n, err := Ldd([]string{name})
    20  	if err != nil {
    21  		return nil, fmt.Errorf("Ldd on %v: want nil, got %v", name, err)
    22  	}
    23  	l, err := List([]string{name})
    24  	if err != nil {
    25  		return nil, fmt.Errorf("LddList on %v: want nil, got %v", name, err)
    26  	}
    27  	if len(n) != len(l) {
    28  		return nil, fmt.Errorf("%v: Len of Ldd(%v) and LddList(%v): want same, got different", name, len(n), len(l))
    29  	}
    30  	for i := range n {
    31  		libMap[n[i].FullName] = true
    32  	}
    33  	for i := range n {
    34  		if !libMap[l[i]] {
    35  			return nil, fmt.Errorf("%v: %v was in LddList but not in Ldd", name, l[i])
    36  		}
    37  	}
    38  	return l, nil
    39  }
    40  
    41  // TestLddList tests that the LddList is the
    42  // same as the info returned by Ldd
    43  func TestLddList(t *testing.T) {
    44  	n, err := lddOne("/etc/resolv.conf")
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  
    49  	for _, f := range n {
    50  		t.Logf("Test %v", f)
    51  		n, err := lddOne(f)
    52  		if err != nil {
    53  			t.Error(err)
    54  		}
    55  		t.Logf("%v has deps of %v", f, n)
    56  	}
    57  }