github.com/hugelgupf/u-root@v0.0.0-20191023214958-4807c632154c/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  	"strings"
    10  	"testing"
    11  )
    12  
    13  // TestLdd tests Ldd against /bin/date.
    14  // This is just about guaranteed to have
    15  // some output on most linux systems.
    16  func TestLdd(t *testing.T) {
    17  	n, err := Ldd([]string{"/bin/date"})
    18  	if err != nil {
    19  		t.Fatalf("Ldd on /bin/date: want nil, got %v", err)
    20  	}
    21  	t.Logf("TestLdd: /bin/date has deps of")
    22  	for i := range n {
    23  		t.Logf("\t%v", n[i])
    24  	}
    25  }
    26  
    27  // lddOne is a helper that runs Ldd on one file. It returns
    28  // the list so we can use elements from the list on another
    29  // test. We do it this way because, unlike /bin/date, there's
    30  // almost nothing else we can assume exists, e.g. /lib/libc.so
    31  // is a different name on almost every *ix* system.
    32  func lddOne(name string) ([]string, error) {
    33  	var libMap = make(map[string]bool)
    34  	n, err := Ldd([]string{name})
    35  	if err != nil {
    36  		return nil, fmt.Errorf("Ldd on %v: want nil, got %v", name, err)
    37  	}
    38  	l, err := List([]string{name})
    39  	if err != nil {
    40  		return nil, fmt.Errorf("LddList on %v: want nil, got %v", name, err)
    41  	}
    42  	if len(n) != len(l) {
    43  		return nil, fmt.Errorf("%v: Len of Ldd(%v) and LddList(%v): want same, got different", name, len(n), len(l))
    44  	}
    45  	for i := range n {
    46  		libMap[n[i].FullName] = true
    47  	}
    48  	for i := range n {
    49  		if !libMap[l[i]] {
    50  			return nil, fmt.Errorf("%v: %v was in LddList but not in Ldd", name, l[i])
    51  		}
    52  	}
    53  	return l, nil
    54  }
    55  
    56  // TestLddList tests that the LddList is the
    57  // same as the info returned by Ldd
    58  func TestLddList(t *testing.T) {
    59  	n, err := lddOne("/bin/date")
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  	// Find the first name in the array that contains "lib"
    64  	// Test 'em all
    65  	for _, f := range n {
    66  		if !strings.Contains(f, "lib") {
    67  			continue
    68  		}
    69  		t.Logf("Test %v", f)
    70  		n, err := lddOne(f)
    71  		if err != nil {
    72  			t.Error(err)
    73  		}
    74  		t.Logf("%v has deps of %v", f, n)
    75  	}
    76  }