github.com/jlowellwofford/u-root@v1.0.0/pkg/ldd/ldd_test.go (about)

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