-
Notifications
You must be signed in to change notification settings - Fork 0
/
corrupt_memorystore.go
71 lines (61 loc) · 2.02 KB
/
corrupt_memorystore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
package testutils
import (
"github.com/docker/notary/tuf/store"
)
// CorruptingMemoryStore corrupts all data returned by GetMeta
type CorruptingMemoryStore struct {
store.MemoryStore
}
// NewCorruptingMemoryStore returns a new instance of memory store that
// corrupts all data requested from it.
func NewCorruptingMemoryStore(meta map[string][]byte) *CorruptingMemoryStore {
s := store.NewMemoryStore(meta)
return &CorruptingMemoryStore{MemoryStore: *s}
}
// GetMeta returns up to size bytes of meta identified by string. It will
// always be corrupted by setting the first character to }
func (cm CorruptingMemoryStore) GetMeta(name string, size int64) ([]byte, error) {
d, err := cm.MemoryStore.GetMeta(name, size)
if err != nil {
return nil, err
}
d[0] = '}' // all our content is JSON so must start with {
return d, err
}
// LongMemoryStore corrupts all data returned by GetMeta
type LongMemoryStore struct {
store.MemoryStore
}
// NewLongMemoryStore returns a new instance of memory store that
// returns one byte too much data on any request to GetMeta
func NewLongMemoryStore(meta map[string][]byte) *LongMemoryStore {
s := store.NewMemoryStore(meta)
return &LongMemoryStore{MemoryStore: *s}
}
// GetMeta returns one byte too much
func (lm LongMemoryStore) GetMeta(name string, size int64) ([]byte, error) {
d, err := lm.MemoryStore.GetMeta(name, size)
if err != nil {
return nil, err
}
d = append(d, ' ')
return d, err
}
// ShortMemoryStore corrupts all data returned by GetMeta
type ShortMemoryStore struct {
store.MemoryStore
}
// NewShortMemoryStore returns a new instance of memory store that
// returns one byte too little data on any request to GetMeta
func NewShortMemoryStore(meta map[string][]byte) *ShortMemoryStore {
s := store.NewMemoryStore(meta)
return &ShortMemoryStore{MemoryStore: *s}
}
// GetMeta returns one byte too few
func (sm ShortMemoryStore) GetMeta(name string, size int64) ([]byte, error) {
d, err := sm.MemoryStore.GetMeta(name, size)
if err != nil {
return nil, err
}
return d[1:], err
}