github.com/nats-io/jwt/v2@v2.5.6/test/util_test.go (about)

     1  /*
     2   * Copyright 2018-2021 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  	"errors"
    20  	"fmt"
    21  	"runtime"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/nats-io/nkeys"
    26  
    27  	. "github.com/nats-io/jwt/v2"
    28  )
    29  
    30  func Trace(message string) string {
    31  	lines := make([]string, 0, 32)
    32  	err := errors.New(message)
    33  	msg := err.Error()
    34  	lines = append(lines, msg)
    35  
    36  	for i := 2; true; i++ {
    37  		_, file, line, ok := runtime.Caller(i)
    38  		if !ok {
    39  			break
    40  		}
    41  		msg := fmt.Sprintf("%s:%d", file, line)
    42  		lines = append(lines, msg)
    43  	}
    44  	return strings.Join(lines, "\n")
    45  }
    46  
    47  func AssertEquals(expected, v interface{}, t *testing.T) {
    48  	if expected != v {
    49  		t.Fatalf("%v", Trace(fmt.Sprintf("The expected value %v != %v", expected, v)))
    50  	}
    51  }
    52  
    53  func AssertNil(v interface{}, t *testing.T) {
    54  	if v != nil {
    55  		t.FailNow()
    56  	}
    57  }
    58  
    59  func AssertNoError(err error, t *testing.T) {
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  }
    64  
    65  func AssertTrue(condition bool, t *testing.T) {
    66  	if !condition {
    67  		t.FailNow()
    68  	}
    69  }
    70  
    71  func AssertFalse(condition bool, t *testing.T) {
    72  	if condition {
    73  		t.FailNow()
    74  	}
    75  }
    76  
    77  func createAccountNKey(t *testing.T) nkeys.KeyPair {
    78  	kp, err := nkeys.CreateAccount()
    79  	if err != nil {
    80  		t.Fatal("error creating account kp", err)
    81  	}
    82  	return kp
    83  }
    84  
    85  func createOperatorNKey(t *testing.T) nkeys.KeyPair {
    86  	kp, err := nkeys.CreateOperator()
    87  	if err != nil {
    88  		t.Fatal("error creating operator kp", err)
    89  	}
    90  	return kp
    91  }
    92  
    93  func publicKey(kp nkeys.KeyPair, t *testing.T) string {
    94  	pk, err := kp.PublicKey()
    95  	if err != nil {
    96  		t.Fatal("error reading public key", err)
    97  	}
    98  	return pk
    99  }
   100  
   101  func encode(c Claims, kp nkeys.KeyPair, t *testing.T) string {
   102  	s, err := c.Encode(kp)
   103  	if err != nil {
   104  		t.Fatal("error encoding claim", err)
   105  	}
   106  	return s
   107  }