github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/cmds/exp/pox/pox_vm_test.go (about)

     1  // Copyright 2012-2021 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 !race
     6  // +build !race
     7  
     8  package main
     9  
    10  import (
    11  	"os"
    12  	"path/filepath"
    13  	"strings"
    14  	"testing"
    15  
    16  	"github.com/mvdan/u-root-coreutils/pkg/qemu"
    17  	"github.com/mvdan/u-root-coreutils/pkg/testutil"
    18  	"github.com/mvdan/u-root-coreutils/pkg/vmtest"
    19  )
    20  
    21  func TestIntegrationPox(t *testing.T) {
    22  	o := &vmtest.Options{
    23  		QEMUOpts: qemu.Options{},
    24  	}
    25  	vmtest.GolangTest(t, []string{"github.com/mvdan/u-root-coreutils/cmds/exp/pox"}, o)
    26  }
    27  
    28  func TestPox(t *testing.T) {
    29  	testutil.SkipIfNotRoot(t)
    30  	f := filepath.Join(t.TempDir(), "x.tcz")
    31  	tmpFile, err := os.Create("/bin/bash")
    32  	if err != nil {
    33  		t.Errorf("Couldn't create /bin/bash: %v", err)
    34  	}
    35  	defer tmpFile.Close()
    36  	for _, tt := range []struct {
    37  		name    string
    38  		args    []string
    39  		verbose bool
    40  		run     bool
    41  		create  bool
    42  		file    string
    43  		extra   string
    44  		wantErr string
    45  	}{
    46  		{
    47  			name:    "err in usage",
    48  			args:    []string{"/bin/bash"},
    49  			file:    f,
    50  			wantErr: "pox [-[-verbose]|v] -[-run|r] | -[-create]|c  [-[-file]|f tcz-file] file [...file]",
    51  		},
    52  		{
    53  			name:    "err in pox.Create",
    54  			args:    []string{},
    55  			create:  true,
    56  			file:    f,
    57  			wantErr: "pox [-[-verbose]|v] -[-run|r] | -[-create]|c  [-[-file]|f tcz-file] file [...file]",
    58  		},
    59  		{
    60  			name:    "err in extraMounts(*extra)",
    61  			args:    []string{"/bin/bash"},
    62  			extra:   "::",
    63  			file:    f,
    64  			wantErr: "[\"\" \"\" \"\"] is not in the form src:target",
    65  		},
    66  		{
    67  			name:    "err in extraMounts(os.Getenv('POX_EXTRA'))",
    68  			args:    []string{"/bin/bash"},
    69  			verbose: true,
    70  			file:    f,
    71  			wantErr: "[\"\" \"\" \"\"] is not in the form src:target",
    72  		},
    73  	} {
    74  		t.Run(tt.name, func(t *testing.T) {
    75  			*verbose = tt.verbose
    76  			*run = tt.run
    77  			*create = tt.create
    78  			*file = tt.file
    79  			*extra = tt.extra
    80  			if tt.name == "err in extraMounts(os.Getenv('POX_EXTRA'))" {
    81  				os.Setenv("POX_EXTRA", "::")
    82  			}
    83  			if got := pox(tt.args...); got != nil {
    84  				if !strings.Contains(got.Error(), tt.wantErr) {
    85  					t.Errorf("pox() = %q, want: %q", got.Error(), tt.wantErr)
    86  				}
    87  			}
    88  		})
    89  	}
    90  }
    91  
    92  func TestPoxCreate(t *testing.T) {
    93  	testutil.SkipIfNotRoot(t)
    94  	f := filepath.Join(t.TempDir(), "x.tcz")
    95  	tmpFile, err := os.Create("/bin/bash")
    96  	if err != nil {
    97  		t.Errorf("Couldn't create /bin/bash: %v", err)
    98  	}
    99  	defer tmpFile.Close()
   100  	for _, tt := range []struct {
   101  		name    string
   102  		args    []string
   103  		zip     bool
   104  		self    bool
   105  		file    string
   106  		wantErr string
   107  	}{
   108  		{
   109  			name:    "len(bin) == 0",
   110  			args:    []string{},
   111  			wantErr: "pox [-[-verbose]|v] -[-run|r] | -[-create]|c  [-[-file]|f tcz-file] file [...file]",
   112  		},
   113  		{
   114  			name:    "error in ldd.Ldd",
   115  			args:    []string{""},
   116  			wantErr: "running ldd on []: lstat : no such file or directory ",
   117  		},
   118  		{
   119  			name:    "self = false, zip = false, err in c.CombinedOutput()",
   120  			args:    []string{"/bin/bash"},
   121  			wantErr: "executable file not found in $PATH",
   122  		},
   123  		{
   124  			name: "self = true, zip = false, no err",
   125  			args: []string{"/bin/bash"},
   126  			self: true,
   127  			file: f,
   128  		},
   129  		{
   130  			name:    "self = true, zip = false, err in cp.Copy",
   131  			args:    []string{"/bin/bash"},
   132  			self:    true,
   133  			file:    "",
   134  			wantErr: "open : no such file or directory",
   135  		},
   136  		{
   137  			name:    "self = false, zip = true, no err",
   138  			args:    []string{"/bin/bash"},
   139  			zip:     true,
   140  			file:    f,
   141  			wantErr: "open : no such file or directory",
   142  		},
   143  		{
   144  			name:    "self = false, zip = true, err in uzip.ToZip",
   145  			args:    []string{"/bin/bash"},
   146  			zip:     true,
   147  			file:    "",
   148  			wantErr: "open : no such file or directory",
   149  		},
   150  	} {
   151  		t.Run(tt.name, func(t *testing.T) {
   152  			*zip = tt.zip
   153  			*self = tt.self
   154  			*file = tt.file
   155  			if got := poxCreate(tt.args...); got != nil {
   156  				if !strings.Contains(got.Error(), tt.wantErr) {
   157  					t.Errorf("poxCreate() = %q, want: %q", got.Error(), tt.wantErr)
   158  				}
   159  			}
   160  		})
   161  	}
   162  }
   163  
   164  func TestPoxRun(t *testing.T) {
   165  	testutil.SkipIfNotRoot(t)
   166  	f := filepath.Join(t.TempDir(), "x.tcz")
   167  	if _, err := os.Create(f); err != nil {
   168  		t.Errorf("Couldn't create file: %v", err)
   169  	}
   170  	for _, tt := range []struct {
   171  		name    string
   172  		args    []string
   173  		zip     bool
   174  		file    string
   175  		wantErr string
   176  	}{
   177  		{
   178  			name:    "len(args) == 0",
   179  			args:    []string{},
   180  			wantErr: "pox [-[-verbose]|v] -[-run|r] | -[-create]|c  [-[-file]|f tcz-file] file [...file]",
   181  		},
   182  		{
   183  			name:    "zip = true with error",
   184  			args:    []string{"/bin/bash"},
   185  			zip:     true,
   186  			file:    f,
   187  			wantErr: "zip: not a valid zip file",
   188  		},
   189  		{
   190  			name:    "zip = false with error",
   191  			args:    []string{"/bin/bash"},
   192  			file:    "/mnt",
   193  			wantErr: "open /mnt: no such file or directory",
   194  		},
   195  	} {
   196  		t.Run(tt.name, func(t *testing.T) {
   197  			*zip = tt.zip
   198  			*file = tt.file
   199  			if got := poxRun(tt.args...); got != nil {
   200  				if !strings.Contains(got.Error(), tt.wantErr) {
   201  					t.Errorf("poxRun() = %q, want: %q", got.Error(), tt.wantErr)
   202  				}
   203  			}
   204  		})
   205  	}
   206  }