pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/knf/validators/network/validators_test.go (about)

     1  package network
     2  
     3  // ////////////////////////////////////////////////////////////////////////////////// //
     4  //                                                                                    //
     5  //                         Copyright (c) 2022 ESSENTIAL KAOS                          //
     6  //      Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>     //
     7  //                                                                                    //
     8  // ////////////////////////////////////////////////////////////////////////////////// //
     9  
    10  import (
    11  	"io/ioutil"
    12  	"testing"
    13  
    14  	"pkg.re/essentialkaos/ek.v12/knf"
    15  
    16  	. "pkg.re/essentialkaos/check.v1"
    17  )
    18  
    19  // ////////////////////////////////////////////////////////////////////////////////// //
    20  
    21  const _CONFIG_DATA = `
    22  [ip]
    23  	test0:
    24  	test1: 127.0.0.1
    25  	test2: 300.0.400.5
    26  
    27  [port]
    28  	test0:
    29  	test1: 1045
    30  	test2: ABCD
    31  	test3: 78361
    32  
    33  [mac]
    34  	test0:
    35  	test1: 00:00:5e:00:53:01
    36  	test2: ABCD
    37  	
    38  [cidr]
    39  	test0:
    40  	test1: 192.0.2.1/24
    41  	test2: 127.0.0.1/200
    42  
    43  [url]
    44  	test0:
    45  	test1: https://google.com
    46  	test2: google.com/abcd.php
    47  `
    48  
    49  // ////////////////////////////////////////////////////////////////////////////////// //
    50  
    51  type ValidatorSuite struct{}
    52  
    53  // ////////////////////////////////////////////////////////////////////////////////// //
    54  
    55  var _ = Suite(&ValidatorSuite{})
    56  
    57  // ////////////////////////////////////////////////////////////////////////////////// //
    58  
    59  func Test(t *testing.T) {
    60  	TestingT(t)
    61  }
    62  
    63  // ////////////////////////////////////////////////////////////////////////////////// //
    64  
    65  func (s *ValidatorSuite) TestIPValidator(c *C) {
    66  	configFile := createConfig(c, _CONFIG_DATA)
    67  
    68  	err := knf.Global(configFile)
    69  	c.Assert(err, IsNil)
    70  
    71  	errs := knf.Validate([]*knf.Validator{
    72  		{"ip:test0", IP, nil},
    73  		{"ip:test1", IP, nil},
    74  	})
    75  
    76  	c.Assert(errs, HasLen, 0)
    77  
    78  	errs = knf.Validate([]*knf.Validator{
    79  		{"ip:test2", IP, nil},
    80  	})
    81  
    82  	c.Assert(errs, HasLen, 1)
    83  }
    84  
    85  func (s *ValidatorSuite) TestPortValidator(c *C) {
    86  	configFile := createConfig(c, _CONFIG_DATA)
    87  
    88  	err := knf.Global(configFile)
    89  	c.Assert(err, IsNil)
    90  
    91  	errs := knf.Validate([]*knf.Validator{
    92  		{"port:test0", Port, nil},
    93  		{"port:test1", Port, nil},
    94  	})
    95  
    96  	c.Assert(errs, HasLen, 0)
    97  
    98  	errs = knf.Validate([]*knf.Validator{
    99  		{"port:test2", Port, nil},
   100  		{"port:test3", Port, nil},
   101  	})
   102  
   103  	c.Assert(errs, HasLen, 2)
   104  }
   105  
   106  func (s *ValidatorSuite) TestMACValidator(c *C) {
   107  	configFile := createConfig(c, _CONFIG_DATA)
   108  
   109  	err := knf.Global(configFile)
   110  	c.Assert(err, IsNil)
   111  
   112  	errs := knf.Validate([]*knf.Validator{
   113  		{"mac:test0", MAC, nil},
   114  		{"mac:test1", MAC, nil},
   115  	})
   116  
   117  	c.Assert(errs, HasLen, 0)
   118  
   119  	errs = knf.Validate([]*knf.Validator{
   120  		{"mac:test2", MAC, nil},
   121  	})
   122  
   123  	c.Assert(errs, HasLen, 1)
   124  }
   125  
   126  func (s *ValidatorSuite) TestCIDRValidator(c *C) {
   127  	configFile := createConfig(c, _CONFIG_DATA)
   128  
   129  	err := knf.Global(configFile)
   130  	c.Assert(err, IsNil)
   131  
   132  	errs := knf.Validate([]*knf.Validator{
   133  		{"cidr:test0", CIDR, nil},
   134  		{"cidr:test1", CIDR, nil},
   135  	})
   136  
   137  	c.Assert(errs, HasLen, 0)
   138  
   139  	errs = knf.Validate([]*knf.Validator{
   140  		{"cidr:test2", CIDR, nil},
   141  	})
   142  
   143  	c.Assert(errs, HasLen, 1)
   144  }
   145  
   146  func (s *ValidatorSuite) TestURLValidator(c *C) {
   147  	configFile := createConfig(c, _CONFIG_DATA)
   148  
   149  	err := knf.Global(configFile)
   150  	c.Assert(err, IsNil)
   151  
   152  	errs := knf.Validate([]*knf.Validator{
   153  		{"url:test0", URL, nil},
   154  		{"url:test1", URL, nil},
   155  	})
   156  
   157  	c.Assert(errs, HasLen, 0)
   158  
   159  	errs = knf.Validate([]*knf.Validator{
   160  		{"url:test2", URL, nil},
   161  	})
   162  
   163  	c.Assert(errs, HasLen, 1)
   164  }
   165  
   166  // ////////////////////////////////////////////////////////////////////////////////// //
   167  
   168  func createConfig(c *C, data string) string {
   169  	configPath := c.MkDir() + "/config.knf"
   170  
   171  	err := ioutil.WriteFile(configPath, []byte(data), 0644)
   172  
   173  	if err != nil {
   174  		c.Fatal(err.Error())
   175  	}
   176  
   177  	return configPath
   178  }