certichain

Blockchain built from scratch for verifying document's legitimacy.
Log | Files | Refs | README | LICENSE

commit 44dbe539047889aa4fde9c1e657257d56ee212bc
parent 6c1d4ca7adca4b34b07263e1c4802866c106ea29
Author: Shivakumar <[email protected]>
Date:   Sat,  1 Aug 2026 03:58:41 +0530

feat(registry): add tests to registry

Signed-off-by: Shivakumar <[email protected]>

Diffstat:
Ainternal/registry/registry_test.go | 252+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 252 insertions(+), 0 deletions(-)

diff --git a/internal/registry/registry_test.go b/internal/registry/registry_test.go @@ -0,0 +1,252 @@ +// Copyright 2026 Shivakumar (Certichain) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package registry + +import ( + "bytes" + "crypto/ed25519" + "errors" + "slices" + "testing" + + "certichain/internal/crypto" + "certichain/internal/storage" +) + +func mustPublicKey(t *testing.T) ed25519.PublicKey { + t.Helper() + + pubKey, _, err := crypto.GenerateKeyPair() + if err != nil { + t.Fatal(err) + } + return pubKey +} + +func TestRegister_Behaviour(t *testing.T) { + t.Parallel() + + pubKey := mustPublicKey(t) + store := storage.NewMemoryStore() + registry := New(store) + + tests := []struct { + name string + pubKey ed25519.PublicKey + wantErr error + }{ + { + "Valid Public Key", + pubKey, + nil, + }, + { + "Invalid Key Size", + make([]byte, 42), + ErrInvalidKey, + }, + { + "nil Key", + nil, + ErrInvalidKey, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + _, err := registry.Register(tt.pubKey) + + if !errors.Is(err, tt.wantErr) { + t.Fatal(err) + } + }) + } +} + +func TestRegister_Duplicate(t *testing.T) { + t.Parallel() + + store := storage.NewMemoryStore() + registry := New(store) + pubKey := mustPublicKey(t) + + if _, err := registry.Register(pubKey); err != nil { + t.Fatalf("Register Unexpected Error: %v", err) + } + if _, err := registry.Register(pubKey); !errors.Is(err, ErrAlreadyExists) { + t.Fatalf("Register Error: want = %v, got = %v", ErrAlreadyExists, err) + } +} + +func TestRegister_DifferentKeys(t *testing.T) { + t.Parallel() + + store := storage.NewMemoryStore() + registry := New(store) + + allDIDs := make([]string, 0, 5) + + for range 5 { + pubKey := mustPublicKey(t) + did, err := registry.Register(pubKey) + if err != nil { + t.Fatal(err) + } + + if !crypto.IsDID(did) { + t.Fatal(err) + } + + allDIDs = append(allDIDs, did) + } + + slices.Sort(allDIDs) + for i := 1; i < len(allDIDs); i++ { + if allDIDs[i] == allDIDs[i-1] { + t.Errorf("Register Error: duplicate DID %s", allDIDs[i]) + } + } +} + +func TestGet_Behaviour(t *testing.T) { + t.Parallel() + + store := storage.NewMemoryStore() + registry := New(store) + + pubKey := mustPublicKey(t) + did, err := registry.Register(pubKey) + if err != nil { + t.Fatal(err) + } + + unregistedPub := mustPublicKey(t) + unregistedDID, err := crypto.NewDID(unregistedPub) + if err != nil { + t.Fatal(err) + } + + tests := []struct { + name string + did string + wantErr error + }{ + { + "Registred DID", + did, + nil, + }, + { + "Unregistred DID", + unregistedDID, + storage.ErrNotFound, + }, + { + "Malformed DID", + did[:len(did)-1], + ErrInvalidDID, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + retrivedPubKey, err := registry.Get(tt.did) + if !errors.Is(err, tt.wantErr) { + t.Fatalf("Get Error: want = %v, got = %v", tt.wantErr, err) + } + if tt.wantErr == nil && !bytes.Equal(pubKey, retrivedPubKey) { + t.Errorf("Get Error: mismatch from original public key:\nwant = %x,\ngot = %x", pubKey, retrivedPubKey) + } + }) + } +} + +func TestHas(t *testing.T) { + t.Parallel() + + store := storage.NewMemoryStore() + registry := New(store) + + pubKey := mustPublicKey(t) + did, err := crypto.NewDID(pubKey) + if err != nil { + t.Fatal(err) + } + + exists, err := registry.Has(did) + if err != nil { + t.Fatal(err) + } + if exists { + t.Fatalf("Has Error: DID already exists in registry: %v", err) + } + + if _, err := registry.Register(pubKey); err != nil { + t.Fatal(err) + } + + exists, err = registry.Has(did) + if err != nil { + t.Fatal(err) + } + if !exists { + t.Fatal("Has Error: DID missing after Register") + } +} + +func TestRevoke(t *testing.T) { + t.Parallel() + + store := storage.NewMemoryStore() + registry := New(store) + pubKey := mustPublicKey(t) + + did, err := registry.Register(pubKey) + if err != nil { + t.Fatal(err) + } + + if err := registry.Revoke(did); err != nil { + t.Errorf("Revoke Unexpected Error: %v", err) + } + + exists, err := registry.Has(did) + if err != nil { + t.Fatal(err) + } + if exists { + t.Errorf("Revoke Error: DID exists after revoke: %v", err) + } +} + +func TestRevoke_NeverRegistered(t *testing.T) { + t.Parallel() + store := storage.NewMemoryStore() + registry := New(store) + pubKey := mustPublicKey(t) + + did, err := crypto.NewDID(pubKey) + if err != nil { + t.Fatal(err) + } + + if err := registry.Revoke(did); !errors.Is(err, storage.ErrNotFound) { + t.Errorf("Revoke Error: Unregistred DID: want = %v, got = %v", storage.ErrNotFound, err) + + } +}