github.com/nats-io/jwt/v2@v2.5.6/revocation_list_test.go (about) 1 /* 2 * Copyright 2020 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 "sort" 20 "testing" 21 "time" 22 ) 23 24 func TestRevocationCompact(t *testing.T) { 25 a := NewAccountClaims(publicKey(createAccountNKey(t), t)) 26 27 now := time.Now() 28 var keys []string 29 keys = append(keys, publicKey(createUserNKey(t), t)) 30 keys = append(keys, publicKey(createUserNKey(t), t)) 31 keys = append(keys, publicKey(createUserNKey(t), t)) 32 sort.Strings(keys) 33 a.Revocations = make(RevocationList) 34 a.Revocations.Revoke(keys[0], now.Add(-time.Hour)) 35 a.Revocations.Revoke(keys[1], now.Add(-time.Minute)) 36 a.Revocations.Revoke(keys[2], now.Add(-time.Second)) 37 // no change expected - there's no 38 deleted := a.Revocations.MaybeCompact() 39 if len(a.Revocations) != 3 || deleted != nil { 40 t.Error("expected 3 revocations") 41 } 42 // should delete the first key 43 a.Revocations.Revoke(All, now.Add(-time.Minute*30)) 44 deleted = a.Revocations.MaybeCompact() 45 if len(a.Revocations) != 3 && len(deleted) != 1 && deleted[0].PublicKey != keys[0] { 46 t.Error("expected 3 revocations") 47 } 48 // should delete the 2 remaining keys, only All remains 49 a.Revocations.Revoke(All, now.Add(-time.Second)) 50 deleted = a.Revocations.MaybeCompact() 51 if len(a.Revocations) != 1 && len(deleted) != 2 && deleted[0].PublicKey != keys[1] && deleted[1].PublicKey != keys[2] { 52 t.Error("didn't revoke expected entries") 53 } 54 }