github.com/ethersphere/bee/v2@v2.2.0/pkg/postage/batch_test.go (about)

     1  // Copyright 2020 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 postage_test
     6  
     7  import (
     8  	"bytes"
     9  	"testing"
    10  
    11  	"github.com/ethersphere/bee/v2/pkg/postage"
    12  	postagetesting "github.com/ethersphere/bee/v2/pkg/postage/testing"
    13  )
    14  
    15  // TestBatchMarshalling tests the idempotence  of binary marshal/unmarshal for a
    16  // Batch.
    17  func TestBatchMarshalling(t *testing.T) {
    18  	a := postagetesting.MustNewBatch()
    19  	buf, err := a.MarshalBinary()
    20  	if err != nil {
    21  		t.Fatal(err)
    22  	}
    23  	if len(buf) != 95 {
    24  		t.Fatalf("invalid length for serialised batch. expected 95, got %d", len(buf))
    25  	}
    26  	b := &postage.Batch{}
    27  	if err := b.UnmarshalBinary(buf); err != nil {
    28  		t.Fatalf("unexpected error unmarshalling batch: %v", err)
    29  	}
    30  	if !bytes.Equal(b.ID, a.ID) {
    31  		t.Fatalf("id mismatch, expected %x, got %x", a.ID, b.ID)
    32  	}
    33  	if !bytes.Equal(b.Owner, a.Owner) {
    34  		t.Fatalf("owner mismatch, expected %x, got %x", a.Owner, b.Owner)
    35  	}
    36  	if a.Value.Uint64() != b.Value.Uint64() {
    37  		t.Fatalf("value mismatch, expected %d, got %d", a.Value.Uint64(), b.Value.Uint64())
    38  	}
    39  	if a.Start != b.Start {
    40  		t.Fatalf("start mismatch, expected %d, got %d", a.Start, b.Start)
    41  	}
    42  	if a.Depth != b.Depth {
    43  		t.Fatalf("depth mismatch, expected %d, got %d", a.Depth, b.Depth)
    44  	}
    45  	if a.BucketDepth != b.BucketDepth {
    46  		t.Fatalf("bucket depth mismatch, expected %d, got %d", a.BucketDepth, b.BucketDepth)
    47  	}
    48  	if a.Immutable != b.Immutable {
    49  		t.Fatalf("depth mismatch, expected %v, got %v", a.Immutable, b.Immutable)
    50  	}
    51  }