github.com/greenpau/go-authcrunch@v1.1.4/pkg/authn/cache/sandbox_test.go (about)

     1  // Copyright 2022 Paul Greenberg greenpau@outlook.com
     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 cache
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  	"strings"
    21  	"testing"
    22  	// "time"
    23  
    24  	"github.com/greenpau/go-authcrunch/internal/tests"
    25  	"github.com/greenpau/go-authcrunch/pkg/util"
    26  )
    27  
    28  func TestParseSandboxID(t *testing.T) {
    29  	testcases := []struct {
    30  		name      string
    31  		id        string
    32  		shouldErr bool
    33  		err       error
    34  	}{
    35  		{
    36  			name: "valid sandbox id",
    37  			id:   util.GetRandomStringFromRange(32, 96),
    38  		},
    39  		{
    40  			name:      "sandbox id is too short",
    41  			id:        "foobar",
    42  			shouldErr: true,
    43  			err:       errors.New("cached id length is outside of 32-96 character range"),
    44  		},
    45  		{
    46  			name:      "sandbox id is too long",
    47  			id:        strings.Repeat("foobar", 128),
    48  			shouldErr: true,
    49  			err:       errors.New("cached id length is outside of 32-96 character range"),
    50  		},
    51  		{
    52  			name:      "sandbox id is invalid character",
    53  			id:        strings.Repeat("foobar", 6) + " " + strings.Repeat("foobar", 6),
    54  			shouldErr: true,
    55  			err:       errors.New("cached id contains invalid characters"),
    56  		},
    57  	}
    58  
    59  	for i, tc := range testcases {
    60  		t.Run(tc.name, func(t *testing.T) {
    61  			msgs := []string{fmt.Sprintf("test %d, name: %s", i, tc.name)}
    62  			err := parseCacheID(tc.id)
    63  			if tests.EvalErrWithLog(t, err, "parse sandbox id", tc.shouldErr, tc.err, msgs) {
    64  				return
    65  			}
    66  		})
    67  	}
    68  }
    69  
    70  func TestNewSandboxCache(t *testing.T) {
    71  	testcases := []struct {
    72  		name      string
    73  		input     map[string]interface{}
    74  		want      map[string]interface{}
    75  		shouldErr bool
    76  		err       error
    77  	}{
    78  		{
    79  			name: "valid configuration options",
    80  			want: map[string]interface{}{
    81  				"cleanup_interval":   1,
    82  				"max_entry_lifetime": 60,
    83  			},
    84  			input: map[string]interface{}{
    85  				"cleanup_interval":   1,
    86  				"max_entry_lifetime": 60,
    87  			},
    88  		},
    89  		{
    90  			name: "invalid cleanup interval with zero value",
    91  			input: map[string]interface{}{
    92  				"cleanup_interval": 0,
    93  			},
    94  			shouldErr: true,
    95  			err:       errors.New("sandbox cache cleanup interval must be equal to or greater than 0"),
    96  		},
    97  		{
    98  			name: "invalid max entry lifetime with unsupported value",
    99  			input: map[string]interface{}{
   100  				"cleanup_interval":   1,
   101  				"max_entry_lifetime": 15,
   102  			},
   103  			shouldErr: true,
   104  			err:       errors.New("sandbox cache max entry lifetime must be equal to or greater than 60 seconds"),
   105  		},
   106  	}
   107  
   108  	for i, tc := range testcases {
   109  		t.Run(tc.name, func(t *testing.T) {
   110  			msgs := []string{fmt.Sprintf("test %d, name: %s", i, tc.name)}
   111  			c := NewSandboxCache()
   112  			for k, v := range tc.input {
   113  				var err error
   114  				switch k {
   115  				case "cleanup_interval":
   116  					err = c.SetCleanupInterval(v.(int))
   117  				case "max_entry_lifetime":
   118  					err = c.SetMaxEntryLifetime(v.(int))
   119  				}
   120  				if err != nil {
   121  					if tests.EvalErrWithLog(t, err, "sandbox cache", tc.shouldErr, tc.err, msgs) {
   122  						return
   123  					}
   124  				}
   125  			}
   126  			got := make(map[string]interface{})
   127  			got["cleanup_interval"] = c.GetCleanupInterval()
   128  			got["max_entry_lifetime"] = c.GetMaxEntryLifetime()
   129  			tests.EvalObjectsWithLog(t, "sandbox cache", tc.want, got, msgs)
   130  		})
   131  	}
   132  }