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