|
| 1 | +/* |
| 2 | +Copyright 2020 The Tekton Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package subcommands |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/base64" |
| 21 | + "errors" |
| 22 | + "io/ioutil" |
| 23 | + "os" |
| 24 | + "path/filepath" |
| 25 | + "testing" |
| 26 | +) |
| 27 | + |
| 28 | +func TestDecodeScript(t *testing.T) { |
| 29 | + encoded := "IyEvdXNyL2Jpbi9lbnYgc2gKZWNobyAiSGVsbG8gV29ybGQhIgo=" |
| 30 | + decoded := `#!/usr/bin/env sh |
| 31 | +echo "Hello World!" |
| 32 | +` |
| 33 | + mode := os.FileMode(0600) |
| 34 | + expectedPermissions := os.FileMode(0600) |
| 35 | + |
| 36 | + tmp, err := ioutil.TempDir("", "decode-script-test-*") |
| 37 | + if err != nil { |
| 38 | + t.Fatalf("error creating temp file: %v", err) |
| 39 | + } |
| 40 | + src := filepath.Join(tmp, "script.txt") |
| 41 | + defer func() { |
| 42 | + if err := os.Remove(src); err != nil { |
| 43 | + t.Errorf("temporary script file %q was not cleaned up: %v", src, err) |
| 44 | + } |
| 45 | + }() |
| 46 | + if err = ioutil.WriteFile(src, []byte(encoded), mode); err != nil { |
| 47 | + t.Fatalf("error writing encoded script: %v", err) |
| 48 | + } |
| 49 | + |
| 50 | + if err = decodeScript(src); err != nil { |
| 51 | + t.Errorf("unexpected error decoding script: %v", err) |
| 52 | + } |
| 53 | + |
| 54 | + file, err := os.Open(src) |
| 55 | + if err != nil { |
| 56 | + t.Fatalf("unexpected error opening decoded script: %v", err) |
| 57 | + } |
| 58 | + defer file.Close() |
| 59 | + info, err := file.Stat() |
| 60 | + if err != nil { |
| 61 | + t.Fatalf("unexpected error statting decoded script: %v", err) |
| 62 | + } |
| 63 | + mod := info.Mode() |
| 64 | + b, err := ioutil.ReadAll(file) |
| 65 | + if err != nil { |
| 66 | + t.Fatalf("unexpected error reading content of decoded script: %v", err) |
| 67 | + } |
| 68 | + if string(b) != decoded { |
| 69 | + t.Errorf("expected decoded value %q received %q", decoded, string(b)) |
| 70 | + } |
| 71 | + if mod != expectedPermissions { |
| 72 | + t.Errorf("expected mode %#o received %#o", expectedPermissions, mod) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestDecodeScriptMissingFileError(t *testing.T) { |
| 77 | + b, mod, err := decodeScriptFromFile("/path/to/non-existent/file") |
| 78 | + if !errors.Is(err, os.ErrNotExist) { |
| 79 | + t.Errorf("expected error %q received %q", os.ErrNotExist, err) |
| 80 | + } |
| 81 | + if b != nil || mod != 0 { |
| 82 | + t.Errorf("unexpected non-zero bytes or file mode returned") |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func TestDecodeScriptInvalidBase64(t *testing.T) { |
| 87 | + invalidData := []byte("!") |
| 88 | + expectedError := base64.CorruptInputError(0) |
| 89 | + |
| 90 | + src, err := ioutil.TempFile("", "decode-script-test-*") |
| 91 | + if err != nil { |
| 92 | + t.Fatalf("error creating temp file: %v", err) |
| 93 | + } |
| 94 | + defer func() { |
| 95 | + if err := os.Remove(src.Name()); err != nil { |
| 96 | + t.Errorf("temporary file %q was not cleaned up: %v", src.Name(), err) |
| 97 | + } |
| 98 | + }() |
| 99 | + if _, err := src.Write(invalidData); err != nil { |
| 100 | + t.Fatalf("error writing invalid base64 data: %v", err) |
| 101 | + } |
| 102 | + src.Close() |
| 103 | + |
| 104 | + b, mod, err := decodeScriptFromFile(src.Name()) |
| 105 | + if b != nil || mod != 0 { |
| 106 | + t.Errorf("unexpected non-zero bytes or file mode returned") |
| 107 | + } |
| 108 | + if !errors.Is(err, expectedError) { |
| 109 | + t.Errorf("expected error %q received %q", expectedError, err) |
| 110 | + } |
| 111 | +} |
0 commit comments