validator_test.go 1.23 KB
Newer Older
1 2 3
// 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.
lash's avatar
lash committed
4
package content_test
5 6 7 8 9

import (
	"encoding/binary"
	"testing"

lash's avatar
lash committed
10
	"github.com/ethersphere/bee/pkg/content"
11 12 13
	"github.com/ethersphere/bee/pkg/swarm"
)

14
// TestValidator checks that the validator evaluates correctly
15
// on valid and invalid input
16
func TestValidator(t *testing.T) {
17
	// generate address from pre-generated hex of 'foo' from legacy bmt
18
	bmtHashOfFoo := "2387e8e7d8a48c2a9339c97c1dc3461a9a7aa07e994c5cb8b38fd7c1b3e6ea48"
19 20 21 22 23 24 25 26
	address := swarm.MustParseHexAddress(bmtHashOfFoo)

	// set up a chunk object with correct expected length prefix
	// and test validation result
	foo := "foo"
	fooLength := len(foo)
	fooBytes := make([]byte, 8+fooLength)
	binary.LittleEndian.PutUint64(fooBytes, uint64(fooLength))
27
	copy(fooBytes[8:], foo)
28
	ch := swarm.NewChunk(address, fooBytes)
acud's avatar
acud committed
29
	if !content.Valid(ch) {
30
		t.Fatalf("data '%s' should have validated to hash '%s'", ch.Data(), ch.Address())
31 32 33 34
	}

	// now test with incorrect data
	ch = swarm.NewChunk(address, fooBytes[:len(fooBytes)-1])
acud's avatar
acud committed
35
	if content.Valid(ch) {
36
		t.Fatalf("data '%s' should not have validated to hash '%s'", ch.Data(), ch.Address())
37 38
	}
}