github.com/ethersphere/bee/v2@v2.2.0/pkg/pricer/headerutils/utilities_test.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 headerutils_test
     6  
     7  import (
     8  	"reflect"
     9  	"testing"
    10  
    11  	"github.com/ethersphere/bee/v2/pkg/p2p"
    12  	"github.com/ethersphere/bee/v2/pkg/pricer/headerutils"
    13  	"github.com/ethersphere/bee/v2/pkg/swarm"
    14  )
    15  
    16  func TestMakePricingHeaders(t *testing.T) {
    17  	t.Parallel()
    18  
    19  	addr := swarm.MustParseHexAddress("010101e1010101")
    20  
    21  	makeHeaders, err := headerutils.MakePricingHeaders(uint64(5348), addr)
    22  	if err != nil {
    23  		t.Fatal(err)
    24  
    25  	}
    26  
    27  	expectedHeaders := p2p.Headers{
    28  		headerutils.PriceFieldName:  []byte{0, 0, 0, 0, 0, 0, 20, 228},
    29  		headerutils.TargetFieldName: []byte{1, 1, 1, 225, 1, 1, 1},
    30  	}
    31  
    32  	if !reflect.DeepEqual(makeHeaders, expectedHeaders) {
    33  		t.Fatalf("Made headers not as expected, got %+v, want %+v", makeHeaders, expectedHeaders)
    34  	}
    35  
    36  }
    37  
    38  func TestMakePricingResponseHeaders(t *testing.T) {
    39  	t.Parallel()
    40  
    41  	addr := swarm.MustParseHexAddress("010101e1010101")
    42  
    43  	makeHeaders, err := headerutils.MakePricingResponseHeaders(uint64(5348), addr, uint8(11))
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  
    48  	expectedHeaders := p2p.Headers{
    49  		headerutils.PriceFieldName:  []byte{0, 0, 0, 0, 0, 0, 20, 228},
    50  		headerutils.TargetFieldName: []byte{1, 1, 1, 225, 1, 1, 1},
    51  		headerutils.IndexFieldName:  []byte{11},
    52  	}
    53  
    54  	if !reflect.DeepEqual(makeHeaders, expectedHeaders) {
    55  		t.Fatalf("Made headers not as expected, got %+v, want %+v", makeHeaders, expectedHeaders)
    56  	}
    57  
    58  }
    59  
    60  func TestParsePricingHeaders(t *testing.T) {
    61  	t.Parallel()
    62  
    63  	toReadHeaders := p2p.Headers{
    64  		headerutils.PriceFieldName:  []byte{0, 0, 0, 0, 0, 0, 20, 228},
    65  		headerutils.TargetFieldName: []byte{1, 1, 1, 225, 1, 1, 1},
    66  	}
    67  
    68  	parsedTarget, parsedPrice, err := headerutils.ParsePricingHeaders(toReadHeaders)
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  
    73  	addr := swarm.MustParseHexAddress("010101e1010101")
    74  
    75  	if parsedPrice != uint64(5348) {
    76  		t.Fatalf("Price mismatch, got %v, want %v", parsedPrice, 5348)
    77  	}
    78  
    79  	if !parsedTarget.Equal(addr) {
    80  		t.Fatalf("Target mismatch, got %v, want %v", parsedTarget, addr)
    81  	}
    82  }
    83  
    84  func TestParsePricingResponseHeaders(t *testing.T) {
    85  	t.Parallel()
    86  
    87  	toReadHeaders := p2p.Headers{
    88  		headerutils.PriceFieldName:  []byte{0, 0, 0, 0, 0, 0, 20, 228},
    89  		headerutils.TargetFieldName: []byte{1, 1, 1, 225, 1, 1, 1},
    90  		headerutils.IndexFieldName:  []byte{11},
    91  	}
    92  
    93  	parsedTarget, parsedPrice, parsedIndex, err := headerutils.ParsePricingResponseHeaders(toReadHeaders)
    94  	if err != nil {
    95  		t.Fatal(err)
    96  	}
    97  
    98  	addr := swarm.MustParseHexAddress("010101e1010101")
    99  
   100  	if parsedPrice != uint64(5348) {
   101  		t.Fatalf("Price mismatch, got %v, want %v", parsedPrice, 5348)
   102  	}
   103  
   104  	if parsedIndex != uint8(11) {
   105  		t.Fatalf("Price mismatch, got %v, want %v", parsedPrice, 5348)
   106  	}
   107  
   108  	if !parsedTarget.Equal(addr) {
   109  		t.Fatalf("Target mismatch, got %v, want %v", parsedTarget, addr)
   110  	}
   111  }
   112  
   113  func TestParseIndexHeader(t *testing.T) {
   114  	t.Parallel()
   115  
   116  	toReadHeaders := p2p.Headers{
   117  		headerutils.IndexFieldName: []byte{11},
   118  	}
   119  
   120  	parsedIndex, err := headerutils.ParseIndexHeader(toReadHeaders)
   121  	if err != nil {
   122  		t.Fatal(err)
   123  	}
   124  
   125  	if parsedIndex != uint8(11) {
   126  		t.Fatalf("Index mismatch, got %v, want %v", parsedIndex, 11)
   127  	}
   128  
   129  }
   130  
   131  func TestParseTargetHeader(t *testing.T) {
   132  	t.Parallel()
   133  
   134  	toReadHeaders := p2p.Headers{
   135  		headerutils.TargetFieldName: []byte{1, 1, 1, 225, 1, 1, 1},
   136  	}
   137  
   138  	parsedTarget, err := headerutils.ParseTargetHeader(toReadHeaders)
   139  	if err != nil {
   140  		t.Fatal(err)
   141  	}
   142  
   143  	addr := swarm.MustParseHexAddress("010101e1010101")
   144  
   145  	if !parsedTarget.Equal(addr) {
   146  		t.Fatalf("Target mismatch, got %v, want %v", parsedTarget, addr)
   147  	}
   148  
   149  }
   150  
   151  func TestParsePriceHeader(t *testing.T) {
   152  	t.Parallel()
   153  
   154  	toReadHeaders := p2p.Headers{
   155  		headerutils.PriceFieldName: []byte{0, 0, 0, 0, 0, 0, 20, 228},
   156  	}
   157  
   158  	parsedPrice, err := headerutils.ParsePriceHeader(toReadHeaders)
   159  	if err != nil {
   160  		t.Fatal(err)
   161  	}
   162  
   163  	if parsedPrice != uint64(5348) {
   164  		t.Fatalf("Index mismatch, got %v, want %v", parsedPrice, 5348)
   165  	}
   166  
   167  }
   168  
   169  func TestReadMalformedHeaders(t *testing.T) {
   170  	t.Parallel()
   171  
   172  	toReadHeaders := p2p.Headers{
   173  		headerutils.IndexFieldName:  []byte{11, 0},
   174  		headerutils.TargetFieldName: []byte{1, 1, 1, 225, 1, 1, 1},
   175  		headerutils.PriceFieldName:  []byte{0, 0, 0, 0, 0, 20, 228},
   176  	}
   177  
   178  	_, err := headerutils.ParseIndexHeader(toReadHeaders)
   179  	if err == nil {
   180  		t.Fatal("Expected error from bad length of index bytes")
   181  	}
   182  
   183  	_, err = headerutils.ParsePriceHeader(toReadHeaders)
   184  	if err == nil {
   185  		t.Fatal("Expected error from bad length of price bytes")
   186  	}
   187  
   188  	// nolint:dogsled
   189  	_, _, _, err = headerutils.ParsePricingResponseHeaders(toReadHeaders)
   190  	if err == nil {
   191  		t.Fatal("Expected error caused by bad length of fields")
   192  	}
   193  
   194  	// nolint:dogsled
   195  	_, _, err = headerutils.ParsePricingHeaders(toReadHeaders)
   196  	if err == nil {
   197  		t.Fatal("Expected error caused by bad length of fields")
   198  	}
   199  
   200  }