github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/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  // +build freebsd linux
     6  
     7  package ldd
     8  
     9  import (
    10  	"fmt"
    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  }