github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/cmds/exp/validate/validate_test.go (about)

     1  // Copyright 2016 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 main
     6  
     7  import (
     8  	"io/ioutil"
     9  	"os"
    10  	"path/filepath"
    11  	"syscall"
    12  	"testing"
    13  
    14  	"github.com/u-root/u-root/pkg/testutil"
    15  )
    16  
    17  type file struct {
    18  	name string
    19  	a    string
    20  	val  []byte
    21  	o    string
    22  	e    string
    23  	x    int // XXX wrong for Plan 9 and Harvey
    24  }
    25  
    26  func TestValidate(t *testing.T) {
    27  	var data = []byte(`127.0.0.1	localhost
    28  127.0.1.1	akaros
    29  192.168.28.16	ak
    30  192.168.28.131	uroot
    31  
    32  # The following lines are desirable for IPv6 capable hosts
    33  ::1     localhost ip6-localhost ip6-loopback
    34  ff02::1 ip6-allnodes
    35  ff02::2 ip6-allrouters
    36  `)
    37  	var tests = []file{
    38  		{name: "hosts.sha1", val: []byte("3f397a3b3a7450075da91b078afa35b794cf6088  hosts"), o: "SHA1\n"},
    39  	}
    40  
    41  	tmpDir, err := ioutil.TempDir("", "validatetest")
    42  	if err != nil {
    43  		t.Fatal("TempDir failed: ", err)
    44  	}
    45  	defer os.RemoveAll(tmpDir)
    46  	if err := ioutil.WriteFile(filepath.Join(tmpDir, "hosts"), data, 0444); err != nil {
    47  		t.Fatalf("Can't set up data file: %v", err)
    48  	}
    49  
    50  	for _, v := range tests {
    51  		if err := ioutil.WriteFile(filepath.Join(tmpDir, v.name), v.val, 0444); err != nil {
    52  			t.Fatalf("Can't set up hash file: %v", err)
    53  		}
    54  
    55  		c := testutil.Command(t, filepath.Join(tmpDir, v.name), filepath.Join(tmpDir, "hosts"))
    56  		ep, err := c.StderrPipe()
    57  		if err != nil {
    58  			t.Fatalf("Can't start StderrPipe: %v", err)
    59  		}
    60  		op, err := c.StdoutPipe()
    61  		if err != nil {
    62  			t.Fatalf("Can't start StdoutPipe: %v", err)
    63  		}
    64  
    65  		if err := c.Start(); err != nil {
    66  			t.Fatalf("Can't start %v: %v", c, err)
    67  		}
    68  		e, err := ioutil.ReadAll(ep)
    69  		if err != nil {
    70  			t.Fatalf("Can't get stderr of %v: %v", c, err)
    71  		}
    72  		o, err := ioutil.ReadAll(op)
    73  		if err != nil {
    74  			t.Fatalf("Can't get sdout of %v: %v", c, err)
    75  		}
    76  
    77  		if err = c.Wait(); err != nil {
    78  			t.Fatalf("Can's Wait %v: %v", c, err)
    79  		}
    80  
    81  		// TODO: fix this for Plan 9/Harvey
    82  		s := c.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()
    83  
    84  		if s != v.x {
    85  			t.Errorf("Validate %v hosts %v (%v): want (exit: %v), got (exit %v), output %v", v.a, v.name, string(v.val), v.x, s, string(o))
    86  			continue
    87  		}
    88  
    89  		if err != nil && string(e) != v.e {
    90  			t.Errorf("Validate %v hosts %v (%v): want stderr: %v, got %v)", v.a, v.name, string(v.val), v.e, string(o))
    91  			continue
    92  		}
    93  
    94  		if string(o) != v.o {
    95  			t.Errorf("Validate %v hosts %v (%v): want stdout: %v, got %v)", v.a, v.name, string(v.val), v.o, string(o))
    96  			continue
    97  		}
    98  
    99  		t.Logf("Validate %v hosts %v: %v", v.a, v.name, string(o))
   100  	}
   101  }
   102  
   103  func TestMain(m *testing.M) {
   104  	testutil.Run(m, main)
   105  }