github.com/nats-io/jwt/v2@v2.5.6/util_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 "errors" 20 "fmt" 21 "runtime" 22 "strings" 23 "testing" 24 25 "github.com/nats-io/nkeys" 26 ) 27 28 func Trace(message string) string { 29 lines := make([]string, 0, 32) 30 err := errors.New(message) 31 msg := err.Error() 32 lines = append(lines, msg) 33 34 for i := 2; true; i++ { 35 _, file, line, ok := runtime.Caller(i) 36 if !ok { 37 break 38 } 39 msg := fmt.Sprintf("%s:%d", file, line) 40 lines = append(lines, msg) 41 } 42 return strings.Join(lines, "\n") 43 } 44 45 func AssertEquals(expected, v interface{}, t *testing.T) { 46 if expected != v { 47 t.Fatalf("%v", Trace(fmt.Sprintf("The expected value %v != %v", expected, v))) 48 } 49 } 50 51 func AssertNil(v interface{}, t *testing.T) { 52 if v != nil { 53 t.FailNow() 54 } 55 } 56 57 func AssertNoError(err error, t *testing.T) { 58 if err != nil { 59 t.Fatal(err) 60 } 61 } 62 63 func AssertTrue(condition bool, t *testing.T) { 64 if !condition { 65 t.FailNow() 66 } 67 } 68 69 func AssertFalse(condition bool, t *testing.T) { 70 if condition { 71 t.FailNow() 72 } 73 } 74 75 func createAccountNKey(t *testing.T) nkeys.KeyPair { 76 kp, err := nkeys.CreateAccount() 77 if err != nil { 78 t.Fatal("error creating account kp", err) 79 } 80 return kp 81 } 82 83 func createUserNKey(t *testing.T) nkeys.KeyPair { 84 kp, err := nkeys.CreateUser() 85 if err != nil { 86 t.Fatal("error creating account kp", err) 87 } 88 return kp 89 } 90 91 func createOperatorNKey(t *testing.T) nkeys.KeyPair { 92 kp, err := nkeys.CreateOperator() 93 if err != nil { 94 t.Fatal("error creating operator kp", err) 95 } 96 return kp 97 } 98 99 func createServerNKey(t *testing.T) nkeys.KeyPair { 100 kp, err := nkeys.CreateServer() 101 if err != nil { 102 t.Fatal("error creating server kp", err) 103 } 104 return kp 105 } 106 107 func createClusterNKey(t *testing.T) nkeys.KeyPair { 108 kp, err := nkeys.CreateCluster() 109 if err != nil { 110 t.Fatal("error creating cluster kp", err) 111 } 112 return kp 113 } 114 115 func publicKey(kp nkeys.KeyPair, t *testing.T) string { 116 pk, err := kp.PublicKey() 117 if err != nil { 118 t.Fatal("error reading public key", err) 119 } 120 return pk 121 } 122 123 func seedKey(kp nkeys.KeyPair, t *testing.T) []byte { 124 sk, err := kp.Seed() 125 if err != nil { 126 t.Fatal("error reading seed", err) 127 } 128 return sk 129 } 130 131 func encode(c Claims, kp nkeys.KeyPair, t *testing.T) string { 132 s, err := c.Encode(kp) 133 if err != nil { 134 t.Fatal("error encoding claim", err) 135 } 136 return s 137 }