github.com/ratrocket/u-root@v0.0.0-20180201221235-1cf9f48ee2cf/pkg/ldd/ldd_test.go (about)

     1  package ldd
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  // TestLdd tests Ldd against /bin/date.
    12  // This is just about guaranteed to have
    13  // some output on most linux systems.
    14  func TestLdd(t *testing.T) {
    15  	n, err := Ldd([]string{"/bin/date"})
    16  	if err != nil {
    17  		t.Fatalf("Ldd on /bin/date: want nil, got %v", err)
    18  	}
    19  	t.Logf("TestLdd: /bin/date has deps of")
    20  	for i := range n {
    21  		t.Logf("\t%v", n[i])
    22  	}
    23  }
    24  
    25  // lddOne is a helper that runs Ldd on one file. It returns
    26  // the list so we can use elements from the list on another
    27  // test. We do it this way because, unlike /bin/date, there's
    28  // almost nothing else we can assume exists, e.g. /lib/libc.so
    29  // is a different name on almost every *ix* system.
    30  func lddOne(name string) ([]string, error) {
    31  	var libMap = make(map[string]bool)
    32  	n, err := Ldd([]string{name})
    33  	if err != nil {
    34  		return nil, fmt.Errorf("Ldd on %v: want nil, got %v", name, err)
    35  	}
    36  	l, err := List([]string{name})
    37  	if err != nil {
    38  		return nil, fmt.Errorf("LddList on %v: want nil, got %v", name, err)
    39  	}
    40  	if len(n) != len(l) {
    41  		return nil, fmt.Errorf("%v: Len of Ldd(%v) and LddList(%v): want same, got different", name, len(n), len(l))
    42  	}
    43  	for i := range n {
    44  		libMap[n[i].FullName] = true
    45  	}
    46  	for i := range n {
    47  		if !libMap[l[i]] {
    48  			return nil, fmt.Errorf("%v: %v was in LddList but not in Ldd", name, l[i])
    49  		}
    50  	}
    51  	return l, nil
    52  }
    53  
    54  // TestLddList tests that the LddList is the
    55  // same as the info returned by Ldd
    56  func TestLddList(t *testing.T) {
    57  	n, err := lddOne("/bin/date")
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  	// Find the first name in the array that contains "lib"
    62  	// Test 'em all
    63  	for _, f := range n {
    64  		if !strings.Contains(f, "lib") {
    65  			continue
    66  		}
    67  		t.Logf("Test %v", f)
    68  		n, err := lddOne(f)
    69  		if err != nil {
    70  			t.Error(err)
    71  		}
    72  		t.Logf("%v has deps of %v", f, n)
    73  	}
    74  }
    75  
    76  // This could have been a great test, if ld.so actually followed ITS OWN DOCS
    77  // and used LD_LIBRARY_PATH. It doesn't.
    78  func testLddBadSo(t *testing.T) {
    79  	tempDir, err := ioutil.TempDir("", "ldd")
    80  	if err != nil {
    81  		t.Fatalf("TempDir: %v", err)
    82  	}
    83  	defer os.RemoveAll(tempDir)
    84  	if err := os.Setenv("LD_LIBRARY_PATH", tempDir); err != nil {
    85  		t.Fatalf("Setting LDD_LIBRARY_PATH to %v: want nil, got %v", tempDir, err)
    86  	}
    87  	if _, err := Ldd([]string{"/bin/date"}); err == nil {
    88  		t.Fatalf("Ldd on /bin/date: want err, got nil")
    89  	}
    90  	t.Logf("Err on bad dir is %v", err)
    91  
    92  }