Skip to content

Commit

Permalink
test: identity address unit
Browse files Browse the repository at this point in the history
  • Loading branch information
nugaon committed Nov 5, 2024
1 parent 7432ed3 commit 0ce6407
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions pkg/soc/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2024 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package soc_test

import (
"encoding/hex"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/v2/pkg/cac"
"github.com/ethersphere/bee/v2/pkg/soc"
"github.com/ethersphere/bee/v2/pkg/swarm"
)

// TestIdentityAddress tests the IdentityAddress function.
func TestIdentityAddress(t *testing.T) {
t.Run("single owner chunk", func(t *testing.T) {
// Create a single owner chunk (SOC)
owner := common.HexToAddress("8d3766440f0d7b949a5e32995d09619a7f86e632")
// signature of hash(id + chunk address of foo)
sig, err := hex.DecodeString("5acd384febc133b7b245e5ddc62d82d2cded9182d2716126cd8844509af65a053deb418208027f548e3e88343af6f84a8772fb3cebc0a1833a0ea7ec0c1348311b")
if err != nil {
t.Fatal(err)
}
id := make([]byte, swarm.HashSize)
copy(id, []byte("id"))
payload := []byte("foo")
ch, err := cac.New(payload)
if err != nil {
t.Fatal(err)
}
sch, err := soc.NewSigned(id, ch, owner.Bytes(), sig)
if err != nil {
t.Fatal(err)
}
schChunk, err := sch.Chunk()
if err != nil {
t.Fatal(err)
}
schAddress, err := sch.Address()
if err != nil {
t.Fatal(err)
}

idAddr, err := soc.IdentityAddress(schChunk)
if err != nil {
t.Fatalf("IdentityAddress returned error: %v", err)
}

if idAddr.IsZero() {
t.Fatalf("expected non-zero address, got zero address")
}

if idAddr.Equal(schAddress) {
t.Fatalf("expected identity address to be different from SOC address")
}
})

t.Run("content addressed chunk", func(t *testing.T) {
// Create a content addressed chunk (CAC)
data := []byte("data")
cacChunk, err := cac.New(data)
if err != nil {
t.Fatalf("failed to create content addressed chunk: %v", err)
}

// Call IdentityAddress with the CAC
addr, err := soc.IdentityAddress(cacChunk)
if err != nil {
t.Fatalf("IdentityAddress returned error: %v", err)
}

// Verify the address matches the CAC address
if !addr.Equal(cacChunk.Address()) {
t.Fatalf("expected address %s, got %s", cacChunk.Address(), addr)
}
})
}

0 comments on commit 0ce6407

Please sign in to comment.