validator.go 1.17 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.
4

5 6 7 8 9 10 11 12 13 14 15
package validator

import (
	"bytes"

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

// MockValidator returns true if the data and address passed in the Validate method
// are a byte-wise match to the data and address passed to the constructor
type MockValidator struct {
16
	addressDataPair map[string][]byte // Make validator accept more than one address/data pair
17 18 19 20
}

// NewMockValidator constructs a new MockValidator
func NewMockValidator(address swarm.Address, data []byte) *MockValidator {
21 22
	mp := &MockValidator{
		addressDataPair: make(map[string][]byte),
23
	}
24 25
	mp.addressDataPair[address.String()] = data
	return mp
26 27
}

28 29 30 31 32 33
// Add a new address/data pair which can be validated
func (v *MockValidator) AddPair(address swarm.Address, data []byte) {
	v.addressDataPair[address.String()] = data
}

// Validate checks the passed chunk for validity
34
func (v *MockValidator) Validate(ch swarm.Chunk) (valid bool) {
35 36 37 38
	if data, ok := v.addressDataPair[ch.Address().String()]; ok {
		if bytes.Equal(data, ch.Data()) {
			return true
		}
39
	}
40
	return false
41
}