github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/opts/opts_test.go (about)

     1  package opts
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestValidateIPAddress(t *testing.T) {
    10  	if ret, err := ValidateIPAddress(`1.2.3.4`); err != nil || ret == "" {
    11  		t.Fatalf("ValidateIPAddress(`1.2.3.4`) got %s %s", ret, err)
    12  	}
    13  
    14  	if ret, err := ValidateIPAddress(`127.0.0.1`); err != nil || ret == "" {
    15  		t.Fatalf("ValidateIPAddress(`127.0.0.1`) got %s %s", ret, err)
    16  	}
    17  
    18  	if ret, err := ValidateIPAddress(`::1`); err != nil || ret == "" {
    19  		t.Fatalf("ValidateIPAddress(`::1`) got %s %s", ret, err)
    20  	}
    21  
    22  	if ret, err := ValidateIPAddress(`127`); err == nil || ret != "" {
    23  		t.Fatalf("ValidateIPAddress(`127`) got %s %s", ret, err)
    24  	}
    25  
    26  	if ret, err := ValidateIPAddress(`random invalid string`); err == nil || ret != "" {
    27  		t.Fatalf("ValidateIPAddress(`random invalid string`) got %s %s", ret, err)
    28  	}
    29  
    30  }
    31  
    32  func TestMapOpts(t *testing.T) {
    33  	tmpMap := make(map[string]string)
    34  	o := NewMapOpts(tmpMap, logOptsValidator)
    35  	o.Set("max-size=1")
    36  	if o.String() != "map[max-size:1]" {
    37  		t.Errorf("%s != [map[max-size:1]", o.String())
    38  	}
    39  
    40  	o.Set("max-file=2")
    41  	if len(tmpMap) != 2 {
    42  		t.Errorf("map length %d != 2", len(tmpMap))
    43  	}
    44  
    45  	if tmpMap["max-file"] != "2" {
    46  		t.Errorf("max-file = %s != 2", tmpMap["max-file"])
    47  	}
    48  
    49  	if tmpMap["max-size"] != "1" {
    50  		t.Errorf("max-size = %s != 1", tmpMap["max-size"])
    51  	}
    52  	if o.Set("dummy-val=3") == nil {
    53  		t.Error("validator is not being called")
    54  	}
    55  }
    56  
    57  func TestListOptsWithoutValidator(t *testing.T) {
    58  	o := NewListOpts(nil)
    59  	o.Set("foo")
    60  	if o.String() != "[foo]" {
    61  		t.Errorf("%s != [foo]", o.String())
    62  	}
    63  	o.Set("bar")
    64  	if o.Len() != 2 {
    65  		t.Errorf("%d != 2", o.Len())
    66  	}
    67  	o.Set("bar")
    68  	if o.Len() != 3 {
    69  		t.Errorf("%d != 3", o.Len())
    70  	}
    71  	if !o.Get("bar") {
    72  		t.Error("o.Get(\"bar\") == false")
    73  	}
    74  	if o.Get("baz") {
    75  		t.Error("o.Get(\"baz\") == true")
    76  	}
    77  	o.Delete("foo")
    78  	if o.String() != "[bar bar]" {
    79  		t.Errorf("%s != [bar bar]", o.String())
    80  	}
    81  	listOpts := o.GetAll()
    82  	if len(listOpts) != 2 || listOpts[0] != "bar" || listOpts[1] != "bar" {
    83  		t.Errorf("Expected [[bar bar]], got [%v]", listOpts)
    84  	}
    85  	mapListOpts := o.GetMap()
    86  	if len(mapListOpts) != 1 {
    87  		t.Errorf("Expected [map[bar:{}]], got [%v]", mapListOpts)
    88  	}
    89  
    90  }
    91  
    92  func TestListOptsWithValidator(t *testing.T) {
    93  	// Re-using logOptsvalidator (used by MapOpts)
    94  	o := NewListOpts(logOptsValidator)
    95  	o.Set("foo")
    96  	if o.String() != "[]" {
    97  		t.Errorf("%s != []", o.String())
    98  	}
    99  	o.Set("foo=bar")
   100  	if o.String() != "[]" {
   101  		t.Errorf("%s != []", o.String())
   102  	}
   103  	o.Set("max-file=2")
   104  	if o.Len() != 1 {
   105  		t.Errorf("%d != 1", o.Len())
   106  	}
   107  	if !o.Get("max-file=2") {
   108  		t.Error("o.Get(\"max-file=2\") == false")
   109  	}
   110  	if o.Get("baz") {
   111  		t.Error("o.Get(\"baz\") == true")
   112  	}
   113  	o.Delete("max-file=2")
   114  	if o.String() != "[]" {
   115  		t.Errorf("%s != []", o.String())
   116  	}
   117  }
   118  
   119  func TestValidateDNSSearch(t *testing.T) {
   120  	valid := []string{
   121  		`.`,
   122  		`a`,
   123  		`a.`,
   124  		`1.foo`,
   125  		`17.foo`,
   126  		`foo.bar`,
   127  		`foo.bar.baz`,
   128  		`foo.bar.`,
   129  		`foo.bar.baz`,
   130  		`foo1.bar2`,
   131  		`foo1.bar2.baz`,
   132  		`1foo.2bar.`,
   133  		`1foo.2bar.baz`,
   134  		`foo-1.bar-2`,
   135  		`foo-1.bar-2.baz`,
   136  		`foo-1.bar-2.`,
   137  		`foo-1.bar-2.baz`,
   138  		`1-foo.2-bar`,
   139  		`1-foo.2-bar.baz`,
   140  		`1-foo.2-bar.`,
   141  		`1-foo.2-bar.baz`,
   142  	}
   143  
   144  	invalid := []string{
   145  		``,
   146  		` `,
   147  		`  `,
   148  		`17`,
   149  		`17.`,
   150  		`.17`,
   151  		`17-.`,
   152  		`17-.foo`,
   153  		`.foo`,
   154  		`foo-.bar`,
   155  		`-foo.bar`,
   156  		`foo.bar-`,
   157  		`foo.bar-.baz`,
   158  		`foo.-bar`,
   159  		`foo.-bar.baz`,
   160  		`foo.bar.baz.this.should.fail.on.long.name.beause.it.is.longer.thanisshouldbethis.should.fail.on.long.name.beause.it.is.longer.thanisshouldbethis.should.fail.on.long.name.beause.it.is.longer.thanisshouldbethis.should.fail.on.long.name.beause.it.is.longer.thanisshouldbe`,
   161  	}
   162  
   163  	for _, domain := range valid {
   164  		if ret, err := ValidateDNSSearch(domain); err != nil || ret == "" {
   165  			t.Fatalf("ValidateDNSSearch(`"+domain+"`) got %s %s", ret, err)
   166  		}
   167  	}
   168  
   169  	for _, domain := range invalid {
   170  		if ret, err := ValidateDNSSearch(domain); err == nil || ret != "" {
   171  			t.Fatalf("ValidateDNSSearch(`"+domain+"`) got %s %s", ret, err)
   172  		}
   173  	}
   174  }
   175  
   176  func TestValidateLabel(t *testing.T) {
   177  	if _, err := ValidateLabel("label"); err == nil || err.Error() != "bad attribute format: label" {
   178  		t.Fatalf("Expected an error [bad attribute format: label], go %v", err)
   179  	}
   180  	if actual, err := ValidateLabel("key1=value1"); err != nil || actual != "key1=value1" {
   181  		t.Fatalf("Expected [key1=value1], got [%v,%v]", actual, err)
   182  	}
   183  	// Validate it's working with more than one =
   184  	if actual, err := ValidateLabel("key1=value1=value2"); err != nil {
   185  		t.Fatalf("Expected [key1=value1=value2], got [%v,%v]", actual, err)
   186  	}
   187  	// Validate it's working with one more
   188  	if actual, err := ValidateLabel("key1=value1=value2=value3"); err != nil {
   189  		t.Fatalf("Expected [key1=value1=value2=value2], got [%v,%v]", actual, err)
   190  	}
   191  }
   192  
   193  func logOptsValidator(val string) (string, error) {
   194  	allowedKeys := map[string]string{"max-size": "1", "max-file": "2"}
   195  	vals := strings.Split(val, "=")
   196  	if allowedKeys[vals[0]] != "" {
   197  		return val, nil
   198  	}
   199  	return "", fmt.Errorf("invalid key %s", vals[0])
   200  }
   201  
   202  func TestNamedListOpts(t *testing.T) {
   203  	var v []string
   204  	o := NewNamedListOptsRef("foo-name", &v, nil)
   205  
   206  	o.Set("foo")
   207  	if o.String() != "[foo]" {
   208  		t.Errorf("%s != [foo]", o.String())
   209  	}
   210  	if o.Name() != "foo-name" {
   211  		t.Errorf("%s != foo-name", o.Name())
   212  	}
   213  	if len(v) != 1 {
   214  		t.Errorf("expected foo to be in the values, got %v", v)
   215  	}
   216  }
   217  
   218  func TestNamedMapOpts(t *testing.T) {
   219  	tmpMap := make(map[string]string)
   220  	o := NewNamedMapOpts("max-name", tmpMap, nil)
   221  
   222  	o.Set("max-size=1")
   223  	if o.String() != "map[max-size:1]" {
   224  		t.Errorf("%s != [map[max-size:1]", o.String())
   225  	}
   226  	if o.Name() != "max-name" {
   227  		t.Errorf("%s != max-name", o.Name())
   228  	}
   229  	if _, exist := tmpMap["max-size"]; !exist {
   230  		t.Errorf("expected map-size to be in the values, got %v", tmpMap)
   231  	}
   232  }