Files
mesh-delivery/modules/mesh-grants/internal/grants/bigvalues_test.go
T

67 lines
2.5 KiB
Go

package grants
import (
"bytes"
"errors"
"strings"
"testing"
)
// limitStore refuses a value over the bucket's 262144 bytes, as the bus does.
type limitStore struct{ memStore }
func (l limitStore) Put(state, key string, value []byte) (uint64, error) {
if len(value) > 262144 {
return 0, errors.New("the value is over the 262144 bytes the bucket takes")
}
return l.memStore.Put(state, key, value)
}
type memObjects map[string][]byte
func (m memObjects) PutObject(name string, data []byte) error { m[name] = data; return nil }
func (m memObjects) GetObject(name string) ([]byte, error) {
if d, ok := m[name]; ok {
return d, nil
}
return nil, errors.New("no such object")
}
// Design 47 G A-373: the user list and the pass result, over 256 KB each, are written and read back through objects the keys name.
func TestAListOverTheBucketsValueIsKeptAsAnObject(t *testing.T) {
big := []byte(`{"sealed":"held: for the test","v":"` + strings.Repeat("x", 450000) + `"}`)
for _, key := range []string{KeyList, KeyPass} {
raw := limitStore{memStore{}}
if _, err := raw.Put(StateUserList, key, big); err == nil {
t.Fatal("the bucket takes a value over its limit")
}
objects := memObjects{}
s := BigValues(raw, objects)
if _, err := s.Put(StateUserList, key, big); err != nil {
t.Fatalf("%s: %v", key, err)
}
got, _, found, err := s.Get(StateUserList, key)
if err != nil || !found || !bytes.Equal(got, big) {
t.Fatalf("%s read back: %d bytes, found %v, %v", key, len(got), found, err)
}
// The tools' reader holds no objects: it is shown the small fields and where the rest is, never an empty answer.
part, _, found, err := BigValues(raw, nil).Get(StateUserList, key)
if err != nil || !found || !strings.Contains(string(part), BigObjectName(key)) || !strings.Contains(string(part), `"partial"`) || !strings.Contains(string(part), "held: for the test") {
t.Errorf("%s read with no objects: %s, %v", key, part, err)
}
// A write that cannot be made stays an error.
if _, err := BigValues(raw, failingObjects{}).Put(StateUserList, key, big); err == nil {
t.Errorf("%s: an object that could not be put is no error", key)
}
// Another key and a small value are kept as they were.
small := []byte(`{"a":1}`)
if _, err := s.Put(StateUserList, KeyAsked, small); err != nil || len(objects) != 1 {
t.Errorf("another key: %v, objects %v", err, objects)
}
}
}
type failingObjects struct{ memObjects }
func (failingObjects) PutObject(string, []byte) error { return errors.New("refused") }