github.com/ethersphere/bee/v2@v2.2.0/pkg/soc/testing/soc.go (about)

     1  // Copyright 2021 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package testing
     6  
     7  import (
     8  	"crypto/ecdsa"
     9  	"testing"
    10  
    11  	"github.com/ethersphere/bee/v2/pkg/cac"
    12  	"github.com/ethersphere/bee/v2/pkg/crypto"
    13  	"github.com/ethersphere/bee/v2/pkg/soc"
    14  	"github.com/ethersphere/bee/v2/pkg/swarm"
    15  )
    16  
    17  // MockSOC defines a mocked SOC with exported fields for easy testing.
    18  type MockSOC struct {
    19  	ID           soc.ID
    20  	Owner        []byte
    21  	Signature    []byte
    22  	WrappedChunk swarm.Chunk
    23  }
    24  
    25  // Address returns the SOC address of the mocked SOC.
    26  func (ms MockSOC) Address() swarm.Address {
    27  	addr, _ := soc.CreateAddress(ms.ID, ms.Owner)
    28  	return addr
    29  }
    30  
    31  // Chunk returns the SOC chunk of the mocked SOC.
    32  func (ms MockSOC) Chunk() swarm.Chunk {
    33  	return swarm.NewChunk(ms.Address(), append(ms.ID, append(ms.Signature, ms.WrappedChunk.Data()...)...))
    34  }
    35  
    36  // GenerateMockSocWithSigner generates a valid mocked SOC from given data and signer.
    37  func GenerateMockSocWithSigner(t *testing.T, data []byte, signer crypto.Signer) *MockSOC {
    38  	t.Helper()
    39  
    40  	owner, err := signer.EthereumAddress()
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  	ch, err := cac.New(data)
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  
    49  	id := make([]byte, swarm.HashSize)
    50  	hasher := swarm.NewHasher()
    51  	_, err = hasher.Write(append(id, ch.Address().Bytes()...))
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  
    56  	signature, err := signer.Sign(hasher.Sum(nil))
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  
    61  	return &MockSOC{
    62  		ID:           id,
    63  		Owner:        owner.Bytes(),
    64  		Signature:    signature,
    65  		WrappedChunk: ch,
    66  	}
    67  }
    68  
    69  // GenerateMockSOC generates a valid mocked SOC from given data.
    70  func GenerateMockSOC(t *testing.T, data []byte) *MockSOC {
    71  	t.Helper()
    72  
    73  	privKey, err := crypto.GenerateSecp256k1Key()
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  	signer := crypto.NewDefaultSigner(privKey)
    78  	owner, err := signer.EthereumAddress()
    79  	if err != nil {
    80  		t.Fatal(err)
    81  	}
    82  
    83  	ch, err := cac.New(data)
    84  	if err != nil {
    85  		t.Fatal(err)
    86  	}
    87  
    88  	id := make([]byte, swarm.HashSize)
    89  	hasher := swarm.NewHasher()
    90  	_, err = hasher.Write(append(id, ch.Address().Bytes()...))
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  
    95  	signature, err := signer.Sign(hasher.Sum(nil))
    96  	if err != nil {
    97  		t.Fatal(err)
    98  	}
    99  
   100  	return &MockSOC{
   101  		ID:           id,
   102  		Owner:        owner.Bytes(),
   103  		Signature:    signature,
   104  		WrappedChunk: ch,
   105  	}
   106  }
   107  
   108  // GenerateMockSOCWithKey generates a valid mocked SOC from given data and key.
   109  func GenerateMockSOCWithKey(t *testing.T, data []byte, privKey *ecdsa.PrivateKey) *MockSOC {
   110  	t.Helper()
   111  
   112  	signer := crypto.NewDefaultSigner(privKey)
   113  	owner, err := signer.EthereumAddress()
   114  	if err != nil {
   115  		t.Fatal(err)
   116  	}
   117  
   118  	ch, err := cac.New(data)
   119  	if err != nil {
   120  		t.Fatal(err)
   121  	}
   122  
   123  	id := make([]byte, swarm.HashSize)
   124  	hasher := swarm.NewHasher()
   125  	_, err = hasher.Write(append(id, ch.Address().Bytes()...))
   126  	if err != nil {
   127  		t.Fatal(err)
   128  	}
   129  
   130  	signature, err := signer.Sign(hasher.Sum(nil))
   131  	if err != nil {
   132  		t.Fatal(err)
   133  	}
   134  
   135  	return &MockSOC{
   136  		ID:           id,
   137  		Owner:        owner.Bytes(),
   138  		Signature:    signature,
   139  		WrappedChunk: ch,
   140  	}
   141  }