registry_test.go (4895B)
1 // Copyright 2026 Shivakumar (Certichain) 2 // 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 package registry 16 17 import ( 18 "bytes" 19 "crypto/ed25519" 20 "errors" 21 "slices" 22 "testing" 23 24 "certichain/internal/crypto" 25 "certichain/internal/storage" 26 ) 27 28 func mustPublicKey(t *testing.T) ed25519.PublicKey { 29 t.Helper() 30 31 pubKey, _, err := crypto.GenerateKeyPair() 32 if err != nil { 33 t.Fatal(err) 34 } 35 return pubKey 36 } 37 38 func TestRegister_Behaviour(t *testing.T) { 39 t.Parallel() 40 41 pubKey := mustPublicKey(t) 42 store := storage.NewMemoryStore() 43 registry := New(store) 44 45 tests := []struct { 46 name string 47 pubKey ed25519.PublicKey 48 wantErr error 49 }{ 50 { 51 "Valid Public Key", 52 pubKey, 53 nil, 54 }, 55 { 56 "Invalid Key Size", 57 make([]byte, 42), 58 ErrInvalidKey, 59 }, 60 { 61 "nil Key", 62 nil, 63 ErrInvalidKey, 64 }, 65 } 66 67 for _, tt := range tests { 68 t.Run(tt.name, func(t *testing.T) { 69 t.Parallel() 70 _, err := registry.Register(tt.pubKey) 71 72 if !errors.Is(err, tt.wantErr) { 73 t.Fatal(err) 74 } 75 }) 76 } 77 } 78 79 func TestRegister_Duplicate(t *testing.T) { 80 t.Parallel() 81 82 store := storage.NewMemoryStore() 83 registry := New(store) 84 pubKey := mustPublicKey(t) 85 86 if _, err := registry.Register(pubKey); err != nil { 87 t.Fatalf("Register Unexpected Error: %v", err) 88 } 89 if _, err := registry.Register(pubKey); !errors.Is(err, ErrAlreadyExists) { 90 t.Fatalf("Register Error: want = %v, got = %v", ErrAlreadyExists, err) 91 } 92 } 93 94 func TestRegister_DifferentKeys(t *testing.T) { 95 t.Parallel() 96 97 store := storage.NewMemoryStore() 98 registry := New(store) 99 100 allDIDs := make([]string, 0, 5) 101 102 for range 5 { 103 pubKey := mustPublicKey(t) 104 did, err := registry.Register(pubKey) 105 if err != nil { 106 t.Fatal(err) 107 } 108 109 if !crypto.IsDID(did) { 110 t.Fatal(err) 111 } 112 113 allDIDs = append(allDIDs, did) 114 } 115 116 slices.Sort(allDIDs) 117 for i := 1; i < len(allDIDs); i++ { 118 if allDIDs[i] == allDIDs[i-1] { 119 t.Errorf("Register Error: duplicate DID %s", allDIDs[i]) 120 } 121 } 122 } 123 124 func TestGet_Behaviour(t *testing.T) { 125 t.Parallel() 126 127 store := storage.NewMemoryStore() 128 registry := New(store) 129 130 pubKey := mustPublicKey(t) 131 did, err := registry.Register(pubKey) 132 if err != nil { 133 t.Fatal(err) 134 } 135 136 unregistedPub := mustPublicKey(t) 137 unregistedDID, err := crypto.NewDID(unregistedPub) 138 if err != nil { 139 t.Fatal(err) 140 } 141 142 tests := []struct { 143 name string 144 did string 145 wantErr error 146 }{ 147 { 148 "Registred DID", 149 did, 150 nil, 151 }, 152 { 153 "Unregistred DID", 154 unregistedDID, 155 storage.ErrNotFound, 156 }, 157 { 158 "Malformed DID", 159 did[:len(did)-1], 160 ErrInvalidDID, 161 }, 162 } 163 164 for _, tt := range tests { 165 t.Run(tt.name, func(t *testing.T) { 166 t.Parallel() 167 168 retrivedPubKey, err := registry.Get(tt.did) 169 if !errors.Is(err, tt.wantErr) { 170 t.Fatalf("Get Error: want = %v, got = %v", tt.wantErr, err) 171 } 172 if tt.wantErr == nil && !bytes.Equal(pubKey, retrivedPubKey) { 173 t.Errorf("Get Error: mismatch from original public key:\nwant = %x,\ngot = %x", pubKey, retrivedPubKey) 174 } 175 }) 176 } 177 } 178 179 func TestHas(t *testing.T) { 180 t.Parallel() 181 182 store := storage.NewMemoryStore() 183 registry := New(store) 184 185 pubKey := mustPublicKey(t) 186 did, err := crypto.NewDID(pubKey) 187 if err != nil { 188 t.Fatal(err) 189 } 190 191 exists, err := registry.Has(did) 192 if err != nil { 193 t.Fatal(err) 194 } 195 if exists { 196 t.Fatalf("Has Error: DID already exists in registry: %v", err) 197 } 198 199 if _, err := registry.Register(pubKey); err != nil { 200 t.Fatal(err) 201 } 202 203 exists, err = registry.Has(did) 204 if err != nil { 205 t.Fatal(err) 206 } 207 if !exists { 208 t.Fatal("Has Error: DID missing after Register") 209 } 210 } 211 212 func TestRevoke(t *testing.T) { 213 t.Parallel() 214 215 store := storage.NewMemoryStore() 216 registry := New(store) 217 pubKey := mustPublicKey(t) 218 219 did, err := registry.Register(pubKey) 220 if err != nil { 221 t.Fatal(err) 222 } 223 224 if err := registry.Revoke(did); err != nil { 225 t.Errorf("Revoke Unexpected Error: %v", err) 226 } 227 228 exists, err := registry.Has(did) 229 if err != nil { 230 t.Fatal(err) 231 } 232 if exists { 233 t.Errorf("Revoke Error: DID exists after revoke: %v", err) 234 } 235 } 236 237 func TestRevoke_NeverRegistered(t *testing.T) { 238 t.Parallel() 239 store := storage.NewMemoryStore() 240 registry := New(store) 241 pubKey := mustPublicKey(t) 242 243 did, err := crypto.NewDID(pubKey) 244 if err != nil { 245 t.Fatal(err) 246 } 247 248 if err := registry.Revoke(did); !errors.Is(err, storage.ErrNotFound) { 249 t.Errorf("Revoke Error: Unregistred DID: want = %v, got = %v", storage.ErrNotFound, err) 250 251 } 252 }