github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/cmds/core/base64/base64_test.go (about) 1 // Copyright 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 package main 6 7 import ( 8 "bytes" 9 "errors" 10 "fmt" 11 "io/ioutil" 12 "os" 13 "path/filepath" 14 "testing" 15 ) 16 17 type failer struct { 18 } 19 20 // Write implements io.Writer, and always fails with os.ErrInvalid 21 func (failer) Write([]byte) (int, error) { 22 return -1, os.ErrInvalid 23 } 24 25 func TestBase64(t *testing.T) { 26 var tests = []struct { 27 in []byte 28 out []byte 29 args []string 30 }{ 31 { 32 in: []byte(`DESCRIPTION 33 Base64 encode or decode FILE, or standard input, to standard output. 34 35 With no FILE, or when FILE is -, read standard input. 36 37 Mandatory arguments to long options are mandatory for short options too. 38 `), 39 out: []byte(`REVTQ1JJUFRJT04KICAgICAgIEJhc2U2NCBlbmNvZGUgb3IgZGVjb2RlIEZJTEUsIG9yIHN0YW5kYXJkIGlucHV0LCB0byBzdGFuZGFyZCBvdXRwdXQuCgogICAgICAgV2l0aCBubyBGSUxFLCBvciB3aGVuIEZJTEUgaXMgLSwgcmVhZCBzdGFuZGFyZCBpbnB1dC4KCiAgICAgICBNYW5kYXRvcnkgYXJndW1lbnRzIHRvIGxvbmcgb3B0aW9ucyBhcmUgbWFuZGF0b3J5IGZvciBzaG9ydCBvcHRpb25zIHRvby4K`), 40 }, 41 } 42 d, err := ioutil.TempDir("", "base64") 43 if err != nil { 44 t.Fatalf(`TempDir("", "base64"): %v != nil`, err) 45 } 46 for _, tt := range tests { 47 nin := filepath.Join(d, "in") 48 if err := ioutil.WriteFile(nin, tt.in, 0666); err != nil { 49 t.Fatalf(`WriteFile(%q, %v, 0666): %v != nil`, nin, tt.in, err) 50 } 51 nout := filepath.Join(d, "out") 52 if err := ioutil.WriteFile(nout, tt.out, 0666); err != nil { 53 t.Fatalf(`WriteFile(%q, %v, 0666): %v != nil`, nout, tt.out, err) 54 } 55 56 // Loop over encodes, then loop over decodes 57 for _, n := range [][]string{{nin}, {}} { 58 t.Run(fmt.Sprintf("run with file name %q", n), func(t *testing.T) { 59 var o bytes.Buffer 60 // n.b. the bytes.NewBuffer is ignored in all but one case ... 61 if err := run(bytes.NewBuffer(tt.in), &o, false, n...); err != nil { 62 t.Errorf("Encode: got %v, want nil", err) 63 return 64 } 65 if !bytes.Equal(o.Bytes(), tt.out) { 66 t.Errorf("Encode: %q != %q", o.Bytes(), tt.out) 67 } 68 }) 69 } 70 71 for _, n := range [][]string{{nout}, {}} { 72 t.Run(fmt.Sprintf("run with file name %q", n), func(t *testing.T) { 73 var o bytes.Buffer 74 // n.b. the bytes.NewBuffer is ignored in all but one case ... 75 if err := run(bytes.NewBuffer(tt.out), &o, true, n...); err != nil { 76 t.Errorf("Decode: got %v, want nil", err) 77 return 78 } 79 if !bytes.Equal(o.Bytes(), tt.in) { 80 t.Errorf("Decode: %q != %q", o.Bytes(), tt.out) 81 } 82 }) 83 } 84 } 85 // Try opening a file we know does not exist. 86 n := filepath.Join(d, "nosuchfile") 87 t.Run(fmt.Sprintf("bad file %q", n), func(t *testing.T) { 88 // n.b. the bytes.NewBuffer is ignored in all but one case ... 89 if err := run(nil, nil, false, n); err == nil { 90 t.Errorf("run(%q, nil, nil, false): nil != an error", n) 91 } 92 }) 93 94 // Try with a bad length 95 t.Run("bad data", func(t *testing.T) { 96 var bad = bytes.NewBuffer([]byte{'t'}) 97 var o bytes.Buffer 98 // n.b. the bytes.NewBuffer is ignored in all but one case ... 99 if err := run(bad, &o, true); err == nil { 100 t.Errorf(`run("", zero-length buffer, zero-length-buffer, false): nil != an error`) 101 } 102 }) 103 104 } 105 106 func TestBadWriter(t *testing.T) { 107 if err := run(bytes.NewBufferString("hi there"), failer{}, false); !errors.Is(err, os.ErrInvalid) { 108 t.Errorf(`bytes.NewBufferString("hi there"), failer{}, false): got %v, want %v`, err, os.ErrInvalid) 109 } 110 } 111 func TestBadUsage(t *testing.T) { 112 var tests = []struct { 113 args []string 114 err error 115 }{ 116 {args: []string{"x", "y"}, err: errBadUsage}, 117 } 118 119 for _, tt := range tests { 120 if err := run(nil, nil, false, tt.args...); !errors.Is(err, tt.err) { 121 t.Errorf(`run(nil, nil, false, %q): got %v, want %v`, tt.args, err, tt.err) 122 } 123 } 124 }