github.com/nginxinc/kubernetes-ingress@v1.12.5/pkg/apis/configuration/validation/common_test.go (about)

     1  package validation
     2  
     3  import (
     4  	"testing"
     5  
     6  	"k8s.io/apimachinery/pkg/util/validation/field"
     7  )
     8  
     9  func createPointerFromInt(n int) *int {
    10  	return &n
    11  }
    12  
    13  func TestValidateVariable(t *testing.T) {
    14  	validVars := map[string]bool{
    15  		"scheme":                 true,
    16  		"http_x_forwarded_proto": true,
    17  		"request_uri":            true,
    18  		"host":                   true,
    19  	}
    20  
    21  	validTests := []string{
    22  		"scheme",
    23  		"http_x_forwarded_proto",
    24  		"request_uri",
    25  		"host",
    26  	}
    27  	for _, nVar := range validTests {
    28  		allErrs := validateVariable(nVar, validVars, field.NewPath("url"))
    29  		if len(allErrs) != 0 {
    30  			t.Errorf("validateVariable(%v) returned errors %v for valid input", nVar, allErrs)
    31  		}
    32  	}
    33  }
    34  
    35  func TestValidateVariableFails(t *testing.T) {
    36  	validVars := map[string]bool{
    37  		"host": true,
    38  	}
    39  	invalidVars := []string{
    40  		"",
    41  		"hostinvalid.com",
    42  		"$a",
    43  		"host${host}",
    44  		"host${host}}",
    45  		"host$${host}",
    46  	}
    47  	for _, nVar := range invalidVars {
    48  		allErrs := validateVariable(nVar, validVars, field.NewPath("url"))
    49  		if len(allErrs) == 0 {
    50  			t.Errorf("validateVariable(%v) returned no errors for invalid input", nVar)
    51  		}
    52  	}
    53  }
    54  
    55  func TestParseSpecialVariable(t *testing.T) {
    56  	tests := []struct {
    57  		specialVar    string
    58  		expectedName  string
    59  		expectedValue string
    60  	}{
    61  		{
    62  			specialVar:    "arg_username",
    63  			expectedName:  "arg",
    64  			expectedValue: "username",
    65  		},
    66  		{
    67  			specialVar:    "arg_user_name",
    68  			expectedName:  "arg",
    69  			expectedValue: "user_name",
    70  		},
    71  		{
    72  			specialVar:    "jwt_header_username",
    73  			expectedName:  "jwt_header",
    74  			expectedValue: "username",
    75  		},
    76  		{
    77  			specialVar:    "jwt_header_user_name",
    78  			expectedName:  "jwt_header",
    79  			expectedValue: "user_name",
    80  		},
    81  		{
    82  			specialVar:    "jwt_claim_username",
    83  			expectedName:  "jwt_claim",
    84  			expectedValue: "username",
    85  		},
    86  		{
    87  			specialVar:    "jwt_claim_user_name",
    88  			expectedName:  "jwt_claim",
    89  			expectedValue: "user_name",
    90  		},
    91  	}
    92  
    93  	for _, test := range tests {
    94  		name, value, allErrs := parseSpecialVariable(test.specialVar, field.NewPath("variable"))
    95  		if name != test.expectedName {
    96  			t.Errorf("parseSpecialVariable(%v) returned name %v but expected %v", test.specialVar, name, test.expectedName)
    97  		}
    98  		if value != test.expectedValue {
    99  			t.Errorf("parseSpecialVariable(%v) returned value %v but expected %v", test.specialVar, value, test.expectedValue)
   100  		}
   101  		if len(allErrs) != 0 {
   102  			t.Errorf("parseSpecialVariable(%v) returned errors for valid case: %v", test.specialVar, allErrs)
   103  		}
   104  	}
   105  }
   106  
   107  func TestParseSpecialVariableFails(t *testing.T) {
   108  	specialVars := []string{
   109  		"arg",
   110  		"jwt_header",
   111  		"jwt_claim",
   112  	}
   113  
   114  	for _, v := range specialVars {
   115  		_, _, allErrs := parseSpecialVariable(v, field.NewPath("variable"))
   116  		if len(allErrs) == 0 {
   117  			t.Errorf("parseSpecialVariable(%v) returned no errors for invalid case", v)
   118  		}
   119  	}
   120  }
   121  
   122  func TestValidateSpecialVariable(t *testing.T) {
   123  	specialVars := []string{
   124  		"arg_username",
   125  		"arg_user_name",
   126  		"http_header_name",
   127  		"cookie_cookie_name",
   128  	}
   129  
   130  	isPlus := false
   131  
   132  	for _, v := range specialVars {
   133  		allErrs := validateSpecialVariable(v, field.NewPath("variable"), isPlus)
   134  		if len(allErrs) != 0 {
   135  			t.Errorf("validateSpecialVariable(%v) returned errors for valid case: %v", v, allErrs)
   136  		}
   137  	}
   138  }
   139  
   140  func TestValidateSpecialVariableForPlus(t *testing.T) {
   141  	specialVars := []string{
   142  		"arg_username",
   143  		"arg_user_name",
   144  		"http_header_name",
   145  		"cookie_cookie_name",
   146  		"jwt_header_alg",
   147  		"jwt_claim_user",
   148  	}
   149  
   150  	isPlus := true
   151  
   152  	for _, v := range specialVars {
   153  		allErrs := validateSpecialVariable(v, field.NewPath("variable"), isPlus)
   154  		if len(allErrs) != 0 {
   155  			t.Errorf("validateSpecialVariable(%v) returned errors for valid case: %v", v, allErrs)
   156  		}
   157  	}
   158  }
   159  
   160  func TestValidateSpecialVariableFails(t *testing.T) {
   161  	specialVars := []string{
   162  		"arg",
   163  		"arg_invalid%",
   164  		"http_header+invalid",
   165  		"cookie_cookie_name?invalid",
   166  		"jwt_header_alg",
   167  		"jwt_claim_user",
   168  		"some_var",
   169  	}
   170  
   171  	isPlus := false
   172  
   173  	for _, v := range specialVars {
   174  		allErrs := validateSpecialVariable(v, field.NewPath("variable"), isPlus)
   175  		if len(allErrs) == 0 {
   176  			t.Errorf("validateSpecialVariable(%v) returned no errors for invalid case", v)
   177  		}
   178  	}
   179  }
   180  
   181  func TestValidateSpecialVariableForPlusFails(t *testing.T) {
   182  	specialVars := []string{
   183  		"arg",
   184  		"arg_invalid%",
   185  		"http_header+invalid",
   186  		"cookie_cookie_name?invalid",
   187  		"jwt_header_+invalid",
   188  		"wt_claim_invalid?",
   189  		"some_var",
   190  	}
   191  
   192  	isPlus := true
   193  
   194  	for _, v := range specialVars {
   195  		allErrs := validateSpecialVariable(v, field.NewPath("variable"), isPlus)
   196  		if len(allErrs) == 0 {
   197  			t.Errorf("validateSpecialVariable(%v) returned no errors for invalid case", v)
   198  		}
   199  	}
   200  }
   201  
   202  func TestValidateStringWithVariables(t *testing.T) {
   203  	isPlus := false
   204  
   205  	testStrings := []string{
   206  		"",
   207  		"${scheme}",
   208  		"${scheme}${host}",
   209  		"foo.bar",
   210  	}
   211  	validVars := map[string]bool{"scheme": true, "host": true}
   212  
   213  	for _, test := range testStrings {
   214  		allErrs := validateStringWithVariables(test, field.NewPath("string"), nil, validVars, isPlus)
   215  		if len(allErrs) != 0 {
   216  			t.Errorf("validateStringWithVariables(%v) returned errors for valid input: %v", test, allErrs)
   217  		}
   218  	}
   219  
   220  	specialVars := []string{"arg", "http", "cookie"}
   221  	testStringsSpecial := []string{
   222  		"${arg_username}",
   223  		"${http_header_name}",
   224  		"${cookie_cookie_name}",
   225  	}
   226  
   227  	for _, test := range testStringsSpecial {
   228  		allErrs := validateStringWithVariables(test, field.NewPath("string"), specialVars, validVars, isPlus)
   229  		if len(allErrs) != 0 {
   230  			t.Errorf("validateStringWithVariables(%v) returned errors for valid input: %v", test, allErrs)
   231  		}
   232  	}
   233  }
   234  
   235  func TestValidateStringWithVariablesFail(t *testing.T) {
   236  	isPlus := false
   237  
   238  	testStrings := []string{
   239  		"$scheme}",
   240  		"${sch${eme}${host}",
   241  		"host$",
   242  		"${host",
   243  		"${invalid}",
   244  	}
   245  	validVars := map[string]bool{"scheme": true, "host": true}
   246  
   247  	for _, test := range testStrings {
   248  		allErrs := validateStringWithVariables(test, field.NewPath("string"), nil, validVars, isPlus)
   249  		if len(allErrs) == 0 {
   250  			t.Errorf("validateStringWithVariables(%v) returned no errors for invalid input", test)
   251  		}
   252  	}
   253  
   254  	specialVars := []string{"arg", "http", "cookie"}
   255  	testStringsSpecial := []string{
   256  		"${arg_username%}",
   257  		"${http_header-name}",
   258  		"${cookie_cookie?name}",
   259  	}
   260  
   261  	for _, test := range testStringsSpecial {
   262  		allErrs := validateStringWithVariables(test, field.NewPath("string"), specialVars, validVars, isPlus)
   263  		if len(allErrs) == 0 {
   264  			t.Errorf("validateStringWithVariables(%v) returned no errors for invalid input", test)
   265  		}
   266  	}
   267  }
   268  
   269  func TestValidateSize(t *testing.T) {
   270  	validInput := []string{"", "4k", "8K", "16m", "32M"}
   271  	for _, test := range validInput {
   272  		allErrs := validateSize(test, field.NewPath("size-field"))
   273  		if len(allErrs) != 0 {
   274  			t.Errorf("validateSize(%q) returned an error for valid input", test)
   275  		}
   276  	}
   277  
   278  	invalidInput := []string{"55mm", "2mG", "6kb", "-5k", "1L", "5G"}
   279  	for _, test := range invalidInput {
   280  		allErrs := validateSize(test, field.NewPath("size-field"))
   281  		if len(allErrs) == 0 {
   282  			t.Errorf("validateSize(%q) didn't return error for invalid input.", test)
   283  		}
   284  	}
   285  }
   286  
   287  func TestValidateTime(t *testing.T) {
   288  	time := "1h 2s"
   289  	allErrs := validateTime(time, field.NewPath("time-field"))
   290  
   291  	if len(allErrs) != 0 {
   292  		t.Errorf("validateTime returned errors %v valid input %v", allErrs, time)
   293  	}
   294  }
   295  
   296  func TestValidateTimeFails(t *testing.T) {
   297  	time := "invalid"
   298  	allErrs := validateTime(time, field.NewPath("time-field"))
   299  
   300  	if len(allErrs) == 0 {
   301  		t.Errorf("validateTime returned no errors for invalid input %v", time)
   302  	}
   303  }
   304  
   305  func TestValidateOffset(t *testing.T) {
   306  	validInput := []string{"", "1", "10k", "11m", "1K", "100M", "5G"}
   307  	for _, test := range validInput {
   308  		allErrs := validateOffset(test, field.NewPath("offset-field"))
   309  		if len(allErrs) != 0 {
   310  			t.Errorf("validateOffset(%q) returned an error for valid input", test)
   311  		}
   312  	}
   313  
   314  	invalidInput := []string{"55mm", "2mG", "6kb", "-5k", "1L", "5Gb"}
   315  	for _, test := range invalidInput {
   316  		allErrs := validateOffset(test, field.NewPath("offset-field"))
   317  		if len(allErrs) == 0 {
   318  			t.Errorf("validateOffset(%q) didn't return error for invalid input.", test)
   319  		}
   320  	}
   321  }