/
basic_mock.go
63 lines (50 loc) · 1.12 KB
/
basic_mock.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
//go:build !wasi && !wasm
// +build !wasi,!wasm
package storageSym
import (
"unsafe"
"github.com/ipfs/go-cid"
"github.com/taubyte/go-sdk/errno"
)
func MockNew(testId uint32, testName string) {
StorageNew = func(storageName string, idPtr *uint32) (error errno.Error) {
if testName != storageName {
return 1
}
*idPtr = uint32(testId)
return 0
}
}
func MockCreate(testId uint32) {
StorageNewContent = func(contentIdPtr *uint32) (error errno.Error) {
*contentIdPtr = testId
return 0
}
}
func MockGet(testData map[string]uint32, expectedCid uint32) {
StorageGet = func(storageName string, idPtr *uint32) (error errno.Error) {
var ok bool
*idPtr, ok = testData[storageName]
if ok == false {
return 1
}
if *idPtr != expectedCid {
return 1
}
return 0
}
}
func MockOpen(testId uint32, expectedCid string) {
StorageOpenCid = func(contentIdPtr *uint32, _cid *byte) (error errno.Error) {
cidBytes := unsafe.Slice(_cid, 64)
_, testCid, err := cid.CidFromBytes(cidBytes)
if err != nil {
return 1
}
if testCid.String() != expectedCid {
return 1
}
*contentIdPtr = testId
return 0
}
}