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

package soc_test

import (
8
	"strings"
lash's avatar
lash committed
9 10 11 12 13 14
	"testing"

	"github.com/ethersphere/bee/pkg/soc"
	"github.com/ethersphere/bee/pkg/swarm"
)

15 16 17 18 19 20 21 22 23 24
// TestValid verifies that the validator can detect
// valid soc chunks.
func TestValid(t *testing.T) {
	socAddress := swarm.MustParseHexAddress("9d453ebb73b2fedaaf44ceddcf7a0aa37f3e3d6453fea5841c31f0ea6d61dc85")

	// signed soc chunk of:
	// id: 0
	// wrapped chunk of: `foo`
	// owner: 0x8d3766440f0d7b949a5e32995d09619a7f86e632
	sch := swarm.NewChunk(socAddress, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 90, 205, 56, 79, 235, 193, 51, 183, 178, 69, 229, 221, 198, 45, 130, 210, 205, 237, 145, 130, 210, 113, 97, 38, 205, 136, 68, 80, 154, 246, 90, 5, 61, 235, 65, 130, 8, 2, 127, 84, 142, 62, 136, 52, 58, 246, 248, 74, 135, 114, 251, 60, 235, 192, 161, 131, 58, 14, 167, 236, 12, 19, 72, 49, 27, 3, 0, 0, 0, 0, 0, 0, 0, 102, 111, 111})
lash's avatar
lash committed
25 26

	// check valid chunk
acud's avatar
acud committed
27
	if !soc.Valid(sch) {
lash's avatar
lash committed
28 29
		t.Fatal("valid chunk evaluates to invalid")
	}
30
}
lash's avatar
lash committed
31

32 33 34 35 36 37 38 39 40 41
// TestInvalid verifies that the validator can detect chunks
// with invalid data and invalid address.
func TestInvalid(t *testing.T) {
	socAddress := swarm.MustParseHexAddress("9d453ebb73b2fedaaf44ceddcf7a0aa37f3e3d6453fea5841c31f0ea6d61dc85")

	// signed soc chunk of:
	// id: 0
	// wrapped chunk of: `foo`
	// owner: 0x8d3766440f0d7b949a5e32995d09619a7f86e632
	sch := swarm.NewChunk(socAddress, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 90, 205, 56, 79, 235, 193, 51, 183, 178, 69, 229, 221, 198, 45, 130, 210, 205, 237, 145, 130, 210, 113, 97, 38, 205, 136, 68, 80, 154, 246, 90, 5, 61, 235, 65, 130, 8, 2, 127, 84, 142, 62, 136, 52, 58, 246, 248, 74, 135, 114, 251, 60, 235, 192, 161, 131, 58, 14, 167, 236, 12, 19, 72, 49, 27, 3, 0, 0, 0, 0, 0, 0, 0, 102, 111, 111})
lash's avatar
lash committed
42

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	for _, c := range []struct {
		name  string
		chunk func() swarm.Chunk
	}{
		{
			name: "wrong soc address",
			chunk: func() swarm.Chunk {
				wrongAddressBytes := sch.Address().Bytes()
				wrongAddressBytes[0] = 255 - wrongAddressBytes[0]
				wrongAddress := swarm.NewAddress(wrongAddressBytes)
				return swarm.NewChunk(wrongAddress, sch.Data())
			},
		},
		{
			name: "invalid data",
			chunk: func() swarm.Chunk {
				data := make([]byte, len(sch.Data()))
				copy(data, sch.Data())
				cursor := soc.IdSize + soc.SignatureSize
				chunkData := data[cursor:]
				chunkData[0] = 0x01
				return swarm.NewChunk(socAddress, data)
			},
		},
		{
			name: "invalid id",
			chunk: func() swarm.Chunk {
				data := make([]byte, len(sch.Data()))
				copy(data, sch.Data())
				id := data[:soc.IdSize]
				id[0] = 0x01
				return swarm.NewChunk(socAddress, data)
			},
		},
		{
			name: "invalid signature",
			chunk: func() swarm.Chunk {
				data := make([]byte, len(sch.Data()))
				copy(data, sch.Data())
				// modify signature
				cursor := soc.IdSize + soc.SignatureSize
				sig := data[soc.IdSize:cursor]
				sig[0] = 0x01
				return swarm.NewChunk(socAddress, data)
			},
		},
		{
			name: "nil data",
			chunk: func() swarm.Chunk {
				return swarm.NewChunk(socAddress, nil)
			},
		},
		{
			name: "small data",
			chunk: func() swarm.Chunk {
				return swarm.NewChunk(socAddress, []byte("small"))
			},
		},
		{
			name: "large data",
			chunk: func() swarm.Chunk {
				return swarm.NewChunk(socAddress, []byte(strings.Repeat("a", swarm.ChunkSize+swarm.SpanSize+1)))
			},
		},
	} {
		t.Run(c.name, func(t *testing.T) {
			if soc.Valid(c.chunk()) {
				t.Fatal("chunk with invalid data evaluates to valid")
			}
		})
lash's avatar
lash committed
113 114
	}
}