github.com/yogeshkumararora/slsa-github-generator@v1.10.1-0.20240520161934-11278bd5afb4/internal/utils/marshal_test.go (about)

     1  // Copyright 2022 SLSA Authors
     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 utils
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/google/go-cmp/cmp"
    21  )
    22  
    23  func Test_MarshalToString(t *testing.T) {
    24  	t.Parallel()
    25  
    26  	tests := []struct {
    27  		name      string
    28  		expected  string
    29  		variables []string
    30  	}{
    31  		{
    32  			name:      "single arg",
    33  			variables: []string{"--arg"},
    34  			expected:  "WyItLWFyZyJd",
    35  		},
    36  		{
    37  			name: "list args",
    38  			variables: []string{
    39  				"/usr/lib/google-golang/bin/go",
    40  				"build", "-mod=vendor", "-trimpath",
    41  				"-tags=netgo",
    42  				"-ldflags=-X main.gitVersion=v1.2.3 -X main.gitSomething=somthg",
    43  			},
    44  			expected: "WyIvdXNyL2xpYi9nb29nbGUtZ29sYW5nL2Jpbi9nbyIsImJ1aWxkIiwiLW1vZD12ZW5kb3IiLCItdHJpbXBhdGgiLCItdGF" +
    45  				"ncz1uZXRnbyIsIi1sZGZsYWdzPS1YIG1haW4uZ2l0VmVyc2lvbj12MS4yLjMgLVggbWFpbi5naXRTb21ldGhpbmc9c29tdGhnIl0=",
    46  		},
    47  	}
    48  	for _, tt := range tests {
    49  		tt := tt // Re-initializing variable so it is not changed while executing the closure below
    50  		t.Run(tt.name, func(t *testing.T) {
    51  			t.Parallel()
    52  
    53  			r, err := MarshalToString(tt.variables)
    54  			if err != nil {
    55  				t.Errorf("MarshalToString: %v", err)
    56  			}
    57  			if !cmp.Equal(r, tt.expected) {
    58  				t.Errorf(cmp.Diff(r, tt.expected))
    59  			}
    60  		})
    61  	}
    62  }
    63  
    64  func Test_UnmarshalList(t *testing.T) {
    65  	t.Parallel()
    66  
    67  	tests := []struct {
    68  		name     string
    69  		value    string
    70  		expected []string
    71  	}{
    72  		{
    73  			name:     "single arg",
    74  			expected: []string{"--arg"},
    75  			value:    "WyItLWFyZyJd",
    76  		},
    77  		{
    78  			name:  "invalid",
    79  			value: "blabla",
    80  		},
    81  		{
    82  			name: "list args",
    83  			expected: []string{
    84  				"/usr/lib/google-golang/bin/go",
    85  				"build", "-mod=vendor", "-trimpath",
    86  				"-tags=netgo",
    87  				"-ldflags=-X main.gitVersion=v1.2.3 -X main.gitSomething=somthg",
    88  			},
    89  			value: "WyIvdXNyL2xpYi9nb29nbGUtZ29sYW5nL2Jpbi9nbyIsImJ1aWxkIiwiLW1vZD12ZW5kb3IiLCItdHJpbXBhdGgiLCItdGFncz" +
    90  				"1uZXRnbyIsIi1sZGZsYWdzPS1YIG1haW4uZ2l0VmVyc2lvbj12MS4yLjMgLVggbWFpbi5naXRTb21ldGhpbmc9c29tdGhnIl0=",
    91  		},
    92  	}
    93  	for _, tt := range tests {
    94  		tt := tt // Re-initializing variable so it is not changed while executing the closure below
    95  		t.Run(tt.name, func(t *testing.T) {
    96  			t.Parallel()
    97  
    98  			r, err := UnmarshalList(tt.value)
    99  			if err != nil && len(tt.expected) != 0 {
   100  				t.Errorf("UnmarshalList: %v", err)
   101  			}
   102  
   103  			if !cmp.Equal(r, tt.expected) {
   104  				t.Errorf(cmp.Diff(r, tt.expected))
   105  			}
   106  		})
   107  	}
   108  }