github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/pkg/scanners/azure/functions/base64_test.go (about)

     1  package functions
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func Test_Base64Call(t *testing.T) {
    12  	tests := []struct {
    13  		name     string
    14  		args     []interface{}
    15  		expected string
    16  	}{
    17  		{
    18  			name: "simple base64 call",
    19  			args: []interface{}{
    20  				"hello, world",
    21  			},
    22  			expected: "aGVsbG8sIHdvcmxk",
    23  		},
    24  	}
    25  
    26  	for _, tt := range tests {
    27  		t.Run(tt.name, func(t *testing.T) {
    28  			actual := Base64(tt.args...)
    29  			assert.Equal(t, tt.expected, actual)
    30  		})
    31  	}
    32  
    33  }
    34  
    35  func Test_Base64ToStringCall(t *testing.T) {
    36  	tests := []struct {
    37  		name     string
    38  		args     []interface{}
    39  		expected string
    40  	}{
    41  		{
    42  			name: "simple base64ToString call",
    43  			args: []interface{}{
    44  				"aGVsbG8sIHdvcmxk",
    45  			},
    46  			expected: "hello, world",
    47  		},
    48  	}
    49  
    50  	for _, tt := range tests {
    51  		t.Run(tt.name, func(t *testing.T) {
    52  			actual := Base64ToString(tt.args...)
    53  			assert.Equal(t, tt.expected, actual)
    54  		})
    55  	}
    56  
    57  }
    58  
    59  func Test_Base64ToJsonCall(t *testing.T) {
    60  
    61  	tests := []struct {
    62  		name     string
    63  		args     []interface{}
    64  		expected string
    65  	}{
    66  		{
    67  			name: "simple base64ToJson call",
    68  			args: []interface{}{
    69  				"eyJoZWxsbyI6ICJ3b3JsZCJ9",
    70  			},
    71  			expected: `{"hello":"world"}`,
    72  		},
    73  	}
    74  
    75  	for _, tt := range tests {
    76  		t.Run(tt.name, func(t *testing.T) {
    77  			actual := Base64ToJson(tt.args...)
    78  
    79  			actualContent, err := json.Marshal(actual)
    80  			require.NoError(t, err)
    81  
    82  			assert.Equal(t, tt.expected, string(actualContent))
    83  		})
    84  	}
    85  }