github.com/nats-io/jwt/v2@v2.5.6/v1compat/types_test.go (about)

     1  /*
     2   * Copyright 2018 The NATS Authors
     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  
    16  package jwt
    17  
    18  import (
    19  	"os"
    20  	"regexp"
    21  	"strings"
    22  	"testing"
    23  )
    24  
    25  func TestVersion(t *testing.T) {
    26  	// Semantic versioning
    27  	verRe := regexp.MustCompile(`\d+.\d+.\d+(-\S+)?`)
    28  	if !verRe.MatchString(Version) {
    29  		t.Fatalf("Version not compatible with semantic versioning: %q", Version)
    30  	}
    31  }
    32  
    33  func TestVersionMatchesTag(t *testing.T) {
    34  	tag := os.Getenv("TRAVIS_TAG")
    35  	if tag == "" {
    36  		t.SkipNow()
    37  	}
    38  	// We expect a tag of the form vX.Y.Z. If that's not the case,
    39  	// we need someone to have a look. So fail if first letter is not
    40  	// a `v`
    41  	if len(tag) < 2 || tag[0] != 'v' {
    42  		t.Fatalf("Expect tag to start with `v`, tag is: %s", tag)
    43  	}
    44  	// Look only at tag from current 'v', that is v1 for this file.
    45  	if tag[1] != '1' {
    46  		// Ignore, it is not a v1 tag.
    47  		return
    48  	}
    49  	// Strip the `v` from the tag for the version comparison.
    50  	if Version != tag[1:] {
    51  		t.Fatalf("Version (%s) does not match tag (%s)", Version, tag[1:])
    52  	}
    53  }
    54  
    55  func TestTimeRangeValidation(t *testing.T) {
    56  	tr := TimeRange{
    57  		Start: "hello",
    58  		End:   "03:15:00",
    59  	}
    60  
    61  	vr := CreateValidationResults()
    62  	tr.Validate(vr)
    63  
    64  	if vr.IsEmpty() || len(vr.Issues) != 1 || !vr.IsBlocking(true) {
    65  		t.Error("bad start should be invalid")
    66  	}
    67  
    68  	if !strings.Contains(vr.Issues[0].Error(), tr.Start) {
    69  		t.Error("error should contain the faulty value")
    70  	}
    71  
    72  	tr = TimeRange{
    73  		Start: "15:43:22",
    74  		End:   "27:11:11",
    75  	}
    76  
    77  	vr = CreateValidationResults()
    78  	tr.Validate(vr)
    79  
    80  	if vr.IsEmpty() || len(vr.Issues) != 1 || !vr.IsBlocking(true) {
    81  		t.Error("bad end should be invalid")
    82  	}
    83  
    84  	if !strings.Contains(vr.Issues[0].Error(), tr.End) {
    85  		t.Error("error should contain the faulty value")
    86  	}
    87  
    88  	tr = TimeRange{
    89  		Start: "",
    90  		End:   "03:15:00",
    91  	}
    92  
    93  	vr = CreateValidationResults()
    94  	tr.Validate(vr)
    95  
    96  	if vr.IsEmpty() || len(vr.Issues) != 1 || !vr.IsBlocking(true) {
    97  		t.Error("bad start should be invalid")
    98  	}
    99  
   100  	tr = TimeRange{
   101  		Start: "15:43:22",
   102  		End:   "",
   103  	}
   104  
   105  	vr = CreateValidationResults()
   106  	tr.Validate(vr)
   107  
   108  	if vr.IsEmpty() || len(vr.Issues) != 1 || !vr.IsBlocking(true) {
   109  		t.Error("bad end should be invalid")
   110  	}
   111  }
   112  
   113  func TestTagList(t *testing.T) {
   114  	tags := TagList{}
   115  
   116  	tags.Add("one")
   117  
   118  	AssertEquals(true, tags.Contains("one"), t)
   119  	AssertEquals(true, tags.Contains("ONE"), t)
   120  	AssertEquals("one", tags[0], t)
   121  
   122  	tags.Add("TWO")
   123  
   124  	AssertEquals(true, tags.Contains("two"), t)
   125  	AssertEquals(true, tags.Contains("TWO"), t)
   126  	AssertEquals("two", tags[1], t)
   127  
   128  	tags.Remove("ONE")
   129  	AssertEquals("two", tags[0], t)
   130  	AssertEquals(false, tags.Contains("one"), t)
   131  	AssertEquals(false, tags.Contains("ONE"), t)
   132  }
   133  
   134  func TestStringList(t *testing.T) {
   135  	slist := StringList{}
   136  
   137  	slist.Add("one")
   138  
   139  	AssertEquals(true, slist.Contains("one"), t)
   140  	AssertEquals(false, slist.Contains("ONE"), t)
   141  	AssertEquals("one", slist[0], t)
   142  
   143  	slist.Add("TWO")
   144  
   145  	AssertEquals(false, slist.Contains("two"), t)
   146  	AssertEquals(true, slist.Contains("TWO"), t)
   147  	AssertEquals("TWO", slist[1], t)
   148  
   149  	slist.Remove("ONE")
   150  	AssertEquals("one", slist[0], t)
   151  	AssertEquals(true, slist.Contains("one"), t)
   152  	AssertEquals(false, slist.Contains("ONE"), t)
   153  
   154  	slist.Add("ONE")
   155  	AssertEquals(true, slist.Contains("one"), t)
   156  	AssertEquals(true, slist.Contains("ONE"), t)
   157  	AssertEquals(3, len(slist), t)
   158  
   159  	slist.Remove("one")
   160  	AssertEquals("TWO", slist[0], t)
   161  	AssertEquals(false, slist.Contains("one"), t)
   162  	AssertEquals(true, slist.Contains("ONE"), t)
   163  }
   164  
   165  func TestSubjectValid(t *testing.T) {
   166  	var s Subject
   167  
   168  	vr := CreateValidationResults()
   169  	s.Validate(vr)
   170  	if !vr.IsBlocking(false) {
   171  		t.Fatalf("Empty string is not a valid subjects")
   172  	}
   173  
   174  	s = "has spaces"
   175  	vr = CreateValidationResults()
   176  	s.Validate(vr)
   177  	if !vr.IsBlocking(false) {
   178  		t.Fatalf("Subjects cannot contain spaces")
   179  	}
   180  
   181  	s = "has.spa ces.and.tokens"
   182  	vr = CreateValidationResults()
   183  	s.Validate(vr)
   184  	if !vr.IsBlocking(false) {
   185  		t.Fatalf("Subjects cannot have spaces")
   186  	}
   187  
   188  	s = "one"
   189  	vr = CreateValidationResults()
   190  	s.Validate(vr)
   191  	if !vr.IsEmpty() {
   192  		t.Fatalf("%s is a valid subject", s)
   193  	}
   194  
   195  	s = "one.two.three"
   196  	vr = CreateValidationResults()
   197  	s.Validate(vr)
   198  	if !vr.IsEmpty() {
   199  		t.Fatalf("%s is a valid subject", s)
   200  	}
   201  }
   202  
   203  func TestSubjectHasWildCards(t *testing.T) {
   204  	s := Subject("one")
   205  	AssertEquals(false, s.HasWildCards(), t)
   206  
   207  	s = "one.two.three"
   208  	AssertEquals(false, s.HasWildCards(), t)
   209  
   210  	s = "*"
   211  	AssertEquals(true, s.HasWildCards(), t)
   212  
   213  	s = "one.*.three"
   214  	AssertEquals(true, s.HasWildCards(), t)
   215  
   216  	s = "*.two.three"
   217  	AssertEquals(true, s.HasWildCards(), t)
   218  
   219  	s = "one.two.*"
   220  	AssertEquals(true, s.HasWildCards(), t)
   221  
   222  	s = "one.>"
   223  	AssertEquals(true, s.HasWildCards(), t)
   224  
   225  	s = "one.two.>"
   226  	AssertEquals(true, s.HasWildCards(), t)
   227  
   228  	s = ">"
   229  	AssertEquals(true, s.HasWildCards(), t)
   230  }
   231  
   232  func TestSubjectContainment(t *testing.T) {
   233  	var s Subject
   234  	var o Subject
   235  
   236  	s = "one.two.three"
   237  	o = "one.two.three"
   238  	AssertEquals(true, s.IsContainedIn(o), t)
   239  
   240  	s = "one.two.three"
   241  	o = "one.two.*"
   242  	AssertEquals(true, s.IsContainedIn(o), t)
   243  
   244  	s = "one.two.three"
   245  	o = "one.*.three"
   246  	AssertEquals(true, s.IsContainedIn(o), t)
   247  
   248  	s = "one.two.three"
   249  	o = "*.two.three"
   250  	AssertEquals(true, s.IsContainedIn(o), t)
   251  
   252  	s = "one.two.three"
   253  	o = "one.two.>"
   254  	AssertEquals(true, s.IsContainedIn(o), t)
   255  
   256  	s = "one.two.three"
   257  	o = "one.>"
   258  	AssertEquals(true, s.IsContainedIn(o), t)
   259  
   260  	s = "one.two.three"
   261  	o = ">"
   262  	AssertEquals(true, s.IsContainedIn(o), t)
   263  
   264  	s = "one.two.three"
   265  	o = "one.two"
   266  	AssertEquals(false, s.IsContainedIn(o), t)
   267  
   268  	s = "one"
   269  	o = "one.two"
   270  	AssertEquals(false, s.IsContainedIn(o), t)
   271  }