builder_test.go 2.04 KB
Newer Older
acud's avatar
acud committed
1 2 3 4
// Copyright 2020 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.

5
package builder_test
acud's avatar
acud committed
6 7 8

import (
	"bytes"
acud's avatar
acud committed
9
	"context"
acud's avatar
acud committed
10 11 12 13
	"encoding/hex"
	"fmt"
	"testing"

14
	"github.com/ethersphere/bee/pkg/file/pipeline/builder"
acud's avatar
acud committed
15
	test "github.com/ethersphere/bee/pkg/file/testing"
acud's avatar
acud committed
16
	"github.com/ethersphere/bee/pkg/storage"
acud's avatar
acud committed
17 18 19 20 21 22
	"github.com/ethersphere/bee/pkg/storage/mock"
	"github.com/ethersphere/bee/pkg/swarm"
)

func TestPartialWrites(t *testing.T) {
	m := mock.NewStorer()
23
	p := builder.NewPipelineBuilder(context.Background(), m, storage.ModePutUpload, false)
acud's avatar
acud committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
	_, _ = p.Write([]byte("hello "))
	_, _ = p.Write([]byte("world"))

	sum, err := p.Sum()
	if err != nil {
		t.Fatal(err)
	}
	exp := swarm.MustParseHexAddress("92672a471f4419b255d7cb0cf313474a6f5856fb347c5ece85fb706d644b630f")
	if !bytes.Equal(exp.Bytes(), sum) {
		t.Fatalf("expected %s got %s", exp.String(), hex.EncodeToString(sum))
	}
}

func TestHelloWorld(t *testing.T) {
	m := mock.NewStorer()
39
	p := builder.NewPipelineBuilder(context.Background(), m, storage.ModePutUpload, false)
acud's avatar
acud committed
40

acud's avatar
acud committed
41
	data := []byte("hello world")
acud's avatar
acud committed
42 43 44 45 46
	_, err := p.Write(data)
	if err != nil {
		t.Fatal(err)
	}

acud's avatar
acud committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
	sum, err := p.Sum()
	if err != nil {
		t.Fatal(err)
	}
	exp := swarm.MustParseHexAddress("92672a471f4419b255d7cb0cf313474a6f5856fb347c5ece85fb706d644b630f")
	if !bytes.Equal(exp.Bytes(), sum) {
		t.Fatalf("expected %s got %s", exp.String(), hex.EncodeToString(sum))
	}
}

func TestAllVectors(t *testing.T) {
	for i := 1; i <= 20; i++ {
		data, expect := test.GetVector(t, i)
		t.Run(fmt.Sprintf("data length %d, vector %d", len(data), i), func(t *testing.T) {
			m := mock.NewStorer()
62
			p := builder.NewPipelineBuilder(context.Background(), m, storage.ModePutUpload, false)
acud's avatar
acud committed
63

acud's avatar
acud committed
64 65 66 67
			_, err := p.Write(data)
			if err != nil {
				t.Fatal(err)
			}
acud's avatar
acud committed
68 69 70 71 72 73 74 75 76 77 78
			sum, err := p.Sum()
			if err != nil {
				t.Fatal(err)
			}
			a := swarm.NewAddress(sum)
			if !a.Equal(expect) {
				t.Fatalf("failed run %d, expected address %s but got %s", i, expect.String(), a.String())
			}
		})
	}
}