github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/travis/yaml/secure_test.go (about)

     1  // Copyright 2022 Harness, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package yaml
    16  
    17  import (
    18  	"encoding/json"
    19  	"testing"
    20  
    21  	"github.com/google/go-cmp/cmp"
    22  	"gopkg.in/yaml.v3"
    23  )
    24  
    25  func TestSecure_UnmarshalYAML(t *testing.T) {
    26  	tests := []struct {
    27  		yaml string
    28  		want Secure
    29  	}{
    30  		// test string value
    31  		{
    32  			yaml: `"pa55word"`,
    33  			want: Secure{
    34  				Decrypted: "pa55word",
    35  			},
    36  		},
    37  		// test encrypted value
    38  		{
    39  			yaml: `{secure: mcUCykGm4bUZ3CaW6AxrIMFzuAYjA98VIz6YmYTmM0}`,
    40  			want: Secure{
    41  				Encrypted: "mcUCykGm4bUZ3CaW6AxrIMFzuAYjA98VIz6YmYTmM0",
    42  			},
    43  		},
    44  	}
    45  
    46  	for i, test := range tests {
    47  		got := new(Secure)
    48  		if err := yaml.Unmarshal([]byte(test.yaml), got); err != nil {
    49  			t.Log(test.yaml)
    50  			t.Error(err)
    51  			return
    52  		}
    53  		if diff := cmp.Diff(got, &test.want); diff != "" {
    54  			t.Log(test.yaml)
    55  			t.Errorf("Unexpected parsing results for test %v", i)
    56  			t.Log(diff)
    57  		}
    58  	}
    59  }
    60  
    61  func TestSecure_UnmarshalYAML_Error(t *testing.T) {
    62  	err := yaml.Unmarshal([]byte("[[]]"), new(Secure))
    63  	if err == nil || err.Error() != "failed to unmarshal secure variable" {
    64  		t.Errorf("Expect error, got %s", err)
    65  	}
    66  }
    67  
    68  func TestSecure_MarshalYAML(t *testing.T) {
    69  	tests := []struct {
    70  		before Secure
    71  		after  string
    72  	}{
    73  		{
    74  			before: Secure{Decrypted: "pa55word"},
    75  			after:  "pa55word\n",
    76  		},
    77  		{
    78  			before: Secure{Encrypted: "pa55word"},
    79  			after:  "secure: pa55word\n",
    80  		},
    81  	}
    82  
    83  	for _, test := range tests {
    84  		after, err := yaml.Marshal(&test.before)
    85  		if err != nil {
    86  			t.Error(err)
    87  			return
    88  		}
    89  		if got, want := string(after), test.after; got != want {
    90  			t.Errorf("want yaml %q, got %q", want, got)
    91  		}
    92  	}
    93  }
    94  
    95  func TestSecure_UnmarshalJSON(t *testing.T) {
    96  	tests := []struct {
    97  		json string
    98  		want Secure
    99  	}{
   100  		// test string value
   101  		{
   102  			json: `"pa55word"`,
   103  			want: Secure{
   104  				Decrypted: "pa55word",
   105  			},
   106  		},
   107  		// test encrypted value
   108  		{
   109  			json: `{"secure":"mcUCykGm4bUZ3CaW6AxrIMFzuAYjA98VIz6YmYTmM0"}`,
   110  			want: Secure{
   111  				Encrypted: "mcUCykGm4bUZ3CaW6AxrIMFzuAYjA98VIz6YmYTmM0",
   112  			},
   113  		},
   114  	}
   115  
   116  	for i, test := range tests {
   117  		got := new(Secure)
   118  		if err := json.Unmarshal([]byte(test.json), got); err != nil {
   119  			t.Log(test.json)
   120  			t.Error(err)
   121  			return
   122  		}
   123  		if diff := cmp.Diff(got, &test.want); diff != "" {
   124  			t.Log(test.json)
   125  			t.Errorf("Unexpected parsing results for test %v", i)
   126  			t.Log(diff)
   127  		}
   128  	}
   129  }
   130  
   131  func TestSecure_UnmarshalJSON_Error(t *testing.T) {
   132  	err := json.Unmarshal([]byte("[[]]"), new(Secure))
   133  	if err == nil || err.Error() != "failed to unmarshal secure variable" {
   134  		t.Errorf("Expect error, got %s", err)
   135  	}
   136  }
   137  
   138  func TestSecure_MarshalJSON(t *testing.T) {
   139  	tests := []struct {
   140  		before Secure
   141  		after  string
   142  	}{
   143  		{
   144  			before: Secure{Decrypted: "pa55word"},
   145  			after:  `"pa55word"`,
   146  		},
   147  		{
   148  			before: Secure{Encrypted: "pa55word"},
   149  			after:  `{"secure":"pa55word"}`,
   150  		},
   151  	}
   152  
   153  	for _, test := range tests {
   154  		after, err := json.Marshal(&test.before)
   155  		if err != nil {
   156  			t.Error(err)
   157  			return
   158  		}
   159  		if got, want := string(after), test.after; got != want {
   160  			t.Errorf("want json %s, got %s", want, got)
   161  		}
   162  	}
   163  }