github.com/hashicorp/terraform-plugin-sdk@v1.17.2/helper/validation/network_test.go (about)

     1  package validation
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestValidateIsIPAddress(t *testing.T) {
     8  	cases := map[string]struct {
     9  		Value interface{}
    10  		Error bool
    11  	}{
    12  		"NotString": {
    13  			Value: 777,
    14  			Error: true,
    15  		},
    16  		"Empty": {
    17  			Value: "",
    18  			Error: true,
    19  		},
    20  		"MissingOctet": {
    21  			Value: "1.2.3",
    22  			Error: true,
    23  		},
    24  		"Chars": {
    25  			Value: "1.2.3.no",
    26  			Error: true,
    27  		},
    28  		"Text": {
    29  			Value: "text",
    30  			Error: true,
    31  		},
    32  		"256": {
    33  			Value: "256.1.1.1",
    34  			Error: true,
    35  		},
    36  		"CIDR": {
    37  			Value: "1.1.1.0/20",
    38  			Error: true,
    39  		},
    40  		"Zeros": {
    41  			Value: "0.0.0.0",
    42  			Error: false,
    43  		},
    44  		"Valid": {
    45  			Value: "1.2.3.4",
    46  			Error: false,
    47  		},
    48  		"Valid10s": {
    49  			Value: "12.34.43.21",
    50  			Error: false,
    51  		},
    52  		"Valid100s": {
    53  			Value: "100.123.199.0",
    54  			Error: false,
    55  		},
    56  		"Valid255": {
    57  			Value: "255.255.255.255",
    58  			Error: false,
    59  		},
    60  		"ZeroIPv6": {
    61  			Value: "0:0:0:0:0:0:0:0",
    62  			Error: false,
    63  		},
    64  		"ValidIPv6": {
    65  			Value: "2001:0db8:85a3:0:0:8a2e:0370:7334",
    66  			Error: false,
    67  		},
    68  	}
    69  
    70  	for tn, tc := range cases {
    71  		t.Run(tn, func(t *testing.T) {
    72  			_, errors := IsIPAddress(tc.Value, tn)
    73  
    74  			if len(errors) > 0 && !tc.Error {
    75  				t.Errorf("IsIPAddress(%s) produced an unexpected error", tc.Value)
    76  			} else if len(errors) == 0 && tc.Error {
    77  				t.Errorf("IsIPAddress(%s) did not error", tc.Value)
    78  			}
    79  		})
    80  	}
    81  }
    82  
    83  func TestValidateIsIPv4Address(t *testing.T) {
    84  	cases := map[string]struct {
    85  		Value interface{}
    86  		Error bool
    87  	}{
    88  		"NotString": {
    89  			Value: 777,
    90  			Error: true,
    91  		},
    92  		"Empty": {
    93  			Value: "",
    94  			Error: true,
    95  		},
    96  		"Chars": {
    97  			Value: "1.2.3.no",
    98  			Error: true,
    99  		},
   100  		"MissingOctet": {
   101  			Value: "1.2.3",
   102  			Error: true,
   103  		},
   104  		"Text": {
   105  			Value: "text",
   106  			Error: true,
   107  		},
   108  		"IPv6": {
   109  			Value: "2001:0db8:85a3:0:0:8a2e:0370:7334",
   110  			Error: true,
   111  		},
   112  		"256": {
   113  			Value: "256.1.1.1",
   114  			Error: true,
   115  		},
   116  		"CIDR": {
   117  			Value: "1.1.1.0/20",
   118  			Error: true,
   119  		},
   120  		"Zeros": {
   121  			Value: "0.0.0.0",
   122  			Error: false,
   123  		},
   124  		"Valid": {
   125  			Value: "1.2.3.4",
   126  			Error: false,
   127  		},
   128  		"Valid10s": {
   129  			Value: "12.34.43.21",
   130  			Error: false,
   131  		},
   132  		"Valid100s": {
   133  			Value: "100.123.199.0",
   134  			Error: false,
   135  		},
   136  		"Valid255": {
   137  			Value: "255.255.255.255",
   138  			Error: false,
   139  		},
   140  	}
   141  
   142  	for tn, tc := range cases {
   143  		t.Run(tn, func(t *testing.T) {
   144  			_, errors := IsIPv4Address(tc.Value, tn)
   145  
   146  			if len(errors) > 0 && !tc.Error {
   147  				t.Errorf("IsIPv4Address(%s) produced an unexpected error", tc.Value)
   148  			} else if len(errors) == 0 && tc.Error {
   149  				t.Errorf("IsIPv4Address(%s) did not error", tc.Value)
   150  			}
   151  		})
   152  	}
   153  }
   154  
   155  func TestValidateIsIPv6Address(t *testing.T) {
   156  	cases := map[string]struct {
   157  		Value interface{}
   158  		Error bool
   159  	}{
   160  		"NotString": {
   161  			Value: 777,
   162  			Error: true,
   163  		},
   164  		"Empty": {
   165  			Value: "",
   166  			Error: true,
   167  		},
   168  		"ZeroIpv4": {
   169  			Value: "0.0.0.0",
   170  			Error: false,
   171  		},
   172  		"NotARealAddress": {
   173  			Value: "not:a:real:address:1:2:3:4",
   174  			Error: true,
   175  		},
   176  		"Text": {
   177  			Value: "text",
   178  			Error: true,
   179  		},
   180  		"IPv4": {
   181  			Value: "1.2.3.4",
   182  			Error: false,
   183  		},
   184  		"Colons": {
   185  			Value: "::",
   186  			Error: false,
   187  		},
   188  		"ZeroIPv6": {
   189  			Value: "0:0:0:0:0:0:0:0",
   190  			Error: false,
   191  		},
   192  		"Valid1": {
   193  			Value: "2001:0db8:85a3:0:0:8a2e:0370:7334",
   194  			Error: false,
   195  		},
   196  		"Valid2": {
   197  			Value: "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
   198  			Error: false,
   199  		},
   200  	}
   201  
   202  	for tn, tc := range cases {
   203  		t.Run(tn, func(t *testing.T) {
   204  			_, errors := IsIPv6Address(tc.Value, tn)
   205  
   206  			if len(errors) > 0 && !tc.Error {
   207  				t.Errorf("IsIPv6Address(%s) produced an unexpected error", tc.Value)
   208  			} else if len(errors) == 0 && tc.Error {
   209  				t.Errorf("IsIPv6Address(%s) did not error", tc.Value)
   210  			}
   211  		})
   212  	}
   213  }
   214  
   215  func TestValidateIsIPv4Range(t *testing.T) {
   216  	cases := map[string]struct {
   217  		Value interface{}
   218  		Error bool
   219  	}{
   220  		"NotString": {
   221  			Value: 777,
   222  			Error: true,
   223  		},
   224  		"Empty": {
   225  			Value: "",
   226  			Error: true,
   227  		},
   228  		"Zeros": {
   229  			Value: "0.0.0.0",
   230  			Error: true,
   231  		},
   232  		"CIDR": {
   233  			Value: "127.0.0.1/8",
   234  			Error: true,
   235  		},
   236  		"SingleIP": {
   237  			Value: "127.0.0.1",
   238  			Error: true,
   239  		},
   240  		"BackwardsRange": {
   241  			Value: "10.0.0.0-7.0.0.0",
   242  			Error: true,
   243  		},
   244  		"ValidRange": {
   245  			Value: "10.0.0.1-70.0.0.0",
   246  			Error: false,
   247  		},
   248  	}
   249  
   250  	for tn, tc := range cases {
   251  		t.Run(tn, func(t *testing.T) {
   252  			_, errors := IsIPv4Range(tc.Value, tn)
   253  
   254  			if len(errors) > 0 && !tc.Error {
   255  				t.Errorf("IsIPv4Range(%s) produced an unexpected error", tc.Value)
   256  			} else if len(errors) == 0 && tc.Error {
   257  				t.Errorf("IsIPv4Range(%s) did not error", tc.Value)
   258  			}
   259  		})
   260  	}
   261  }
   262  
   263  func TestValidateIsCIDR(t *testing.T) {
   264  	cases := map[string]struct {
   265  		Value interface{}
   266  		Error bool
   267  	}{
   268  		"NotString": {
   269  			Value: 777,
   270  			Error: true,
   271  		},
   272  		"Empty": {
   273  			Value: "",
   274  			Error: true,
   275  		},
   276  		"Zeros": {
   277  			Value: "0.0.0.0",
   278  			Error: true,
   279  		},
   280  		"Slash8": {
   281  			Value: "127.0.0.1/8",
   282  			Error: false,
   283  		},
   284  		"Slash33": {
   285  			Value: "127.0.0.1/33",
   286  			Error: true,
   287  		},
   288  		"Slash-1": {
   289  			Value: "127.0.0.1/-1",
   290  			Error: true,
   291  		},
   292  	}
   293  
   294  	for tn, tc := range cases {
   295  		t.Run(tn, func(t *testing.T) {
   296  			_, errors := IsCIDR(tc.Value, tn)
   297  
   298  			if len(errors) > 0 && !tc.Error {
   299  				t.Errorf("IsCIDR(%s) produced an unexpected error", tc.Value)
   300  			} else if len(errors) == 0 && tc.Error {
   301  				t.Errorf("IsCIDR(%s) did not error", tc.Value)
   302  			}
   303  		})
   304  	}
   305  }
   306  
   307  func TestValidationIsMACAddress(t *testing.T) {
   308  	cases := map[string]struct {
   309  		Value interface{}
   310  		Error bool
   311  	}{
   312  		"NotString": {
   313  			Value: 777,
   314  			Error: true,
   315  		},
   316  		"Empty": {
   317  			Value: "",
   318  			Error: true,
   319  		},
   320  		"Text": {
   321  			Value: "text d",
   322  			Error: true,
   323  		},
   324  		"Gibberish": {
   325  			Value: "12:34:no",
   326  			Error: true,
   327  		},
   328  		"InvalidOctetSize": {
   329  			Value: "123:34:56:78:90:ab",
   330  			Error: true,
   331  		},
   332  		"InvalidOctetChars": {
   333  			Value: "12:34:56:78:90:NO",
   334  			Error: true,
   335  		},
   336  		"ValidLowercase": {
   337  			Value: "12:34:56:78:90:ab",
   338  			Error: false,
   339  		},
   340  		"ValidUppercase": {
   341  			Value: "ab:cd:ef:AB:CD:EF",
   342  			Error: false,
   343  		},
   344  	}
   345  
   346  	for tn, tc := range cases {
   347  		t.Run(tn, func(t *testing.T) {
   348  			_, errors := IsMACAddress(tc.Value, tn)
   349  
   350  			if len(errors) > 0 && !tc.Error {
   351  				t.Errorf("IsMACAddress(%s) produced an unexpected error", tc.Value)
   352  			} else if len(errors) == 0 && tc.Error {
   353  				t.Errorf("IsMACAddress(%s) did not error", tc.Value)
   354  			}
   355  		})
   356  	}
   357  }
   358  
   359  func TestValidationIsPortNumber(t *testing.T) {
   360  	cases := map[string]struct {
   361  		Value interface{}
   362  		Error bool
   363  	}{
   364  		"NotInt": {
   365  			Value: "kt",
   366  			Error: true,
   367  		},
   368  		"Negative": {
   369  			Value: -1,
   370  			Error: true,
   371  		},
   372  		"Zero": {
   373  			Value: 0,
   374  			Error: true,
   375  		},
   376  		"One": {
   377  			Value: 1,
   378  			Error: false,
   379  		},
   380  		"Valid": {
   381  			Value: 8477,
   382  			Error: false,
   383  		},
   384  		"MaxPort": {
   385  			Value: 65535,
   386  			Error: false,
   387  		},
   388  		"OneToHigh": {
   389  			Value: 65536,
   390  			Error: true,
   391  		},
   392  		"HugeNumber": {
   393  			Value: 7000000,
   394  			Error: true,
   395  		},
   396  	}
   397  
   398  	for tn, tc := range cases {
   399  		t.Run(tn, func(t *testing.T) {
   400  			_, errors := IsPortNumber(tc.Value, tn)
   401  
   402  			if len(errors) > 0 && !tc.Error {
   403  				t.Errorf("IsPortNumber(%s) produced an unexpected error", tc.Value)
   404  			} else if len(errors) == 0 && tc.Error {
   405  				t.Errorf("IsPortNumber(%s) did not error", tc.Value)
   406  			}
   407  		})
   408  	}
   409  }
   410  
   411  func TestValidationIsPortNumberOrZero(t *testing.T) {
   412  	cases := map[string]struct {
   413  		Value interface{}
   414  		Error bool
   415  	}{
   416  		"NotInt": {
   417  			Value: "kt",
   418  			Error: true,
   419  		},
   420  		"Negative": {
   421  			Value: -1,
   422  			Error: true,
   423  		},
   424  		"Zero": {
   425  			Value: 0,
   426  			Error: false,
   427  		},
   428  		"One": {
   429  			Value: 1,
   430  			Error: false,
   431  		},
   432  		"Valid": {
   433  			Value: 8477,
   434  			Error: false,
   435  		},
   436  		"MaxPort": {
   437  			Value: 65535,
   438  			Error: false,
   439  		},
   440  		"OneToHigh": {
   441  			Value: 65536,
   442  			Error: true,
   443  		},
   444  		"HugeNumber": {
   445  			Value: 7000000,
   446  			Error: true,
   447  		},
   448  	}
   449  
   450  	for tn, tc := range cases {
   451  		t.Run(tn, func(t *testing.T) {
   452  			_, errors := IsPortNumberOrZero(tc.Value, tn)
   453  
   454  			if len(errors) > 0 && !tc.Error {
   455  				t.Errorf("IsPortNumberOrZero(%s) produced an unexpected error", tc.Value)
   456  			} else if len(errors) == 0 && tc.Error {
   457  				t.Errorf("IsPortNumberOrZero(%s) did not error", tc.Value)
   458  			}
   459  		})
   460  	}
   461  }