-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathparamfetch_test.go
More file actions
141 lines (118 loc) · 3.97 KB
/
paramfetch_test.go
File metadata and controls
141 lines (118 loc) · 3.97 KB
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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package paramfetch
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
logging "github.com/ipfs/go-log/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
)
func init() {
logging.SetAllLoggers(logging.LevelDebug)
}
// small files only
const params = `{
"v28-proof-of-spacetime-fallback-merkletree-poseidon_hasher-8-0-0-0170db1f394b35d995252228ee359194b13199d259380541dc529fb0099096b0.vk": {
"cid": "QmcS5JZs8X3TdtkEBpHAdUYjdNDqcL7fWQFtQz69mpnu2X",
"digest": "0e0958009936b9d5e515ec97b8cb792d",
"sector_size": 2048
},
"v28-proof-of-spacetime-fallback-merkletree-poseidon_hasher-8-0-0-0cfb4f178bbb71cf2ecfcd42accce558b27199ab4fb59cb78f2483fe21ef36d9.vk": {
"cid": "QmfCeddjFpWtavzfEzZpJfzSajGNwfL4RjFXWAvA9TSnTV",
"digest": "4dae975de4f011f101f5a2f86d1daaba",
"sector_size": 536870912
},
"v28-proof-of-spacetime-fallback-merkletree-poseidon_hasher-8-0-0-3ea05428c9d11689f23529cde32fd30aabd50f7d2c93657c1d3650bca3e8ea9e.vk": {
"cid": "QmSTCXF2ipGA3f6muVo6kHc2URSx6PzZxGUqu7uykaH5KU",
"digest": "ffd79788d614d27919ae5bd2d94eacb6",
"sector_size": 2048
},
"v28-proof-of-spacetime-fallback-merkletree-poseidon_hasher-8-0-0-50c7368dea9593ed0989e70974d28024efa9d156d585b7eea1be22b2e753f331.vk": {
"cid": "QmbmUMa3TbbW3X5kFhExs6WgC4KeWT18YivaVmXDkB6ANG",
"digest": "79ebb55f56fda427743e35053edad8fc",
"sector_size": 8388608
}
}
`
const srs = `{}`
func TestGetParams(t *testing.T) {
pd := t.TempDir()
require.NoError(t, os.Setenv(dirEnv, pd))
err := GetParams(context.Background(), []byte(params), []byte(srs), 0)
require.NoError(t, err)
}
func TestGetParamsParallel(t *testing.T) {
pd := t.TempDir()
require.NoError(t, os.Setenv(dirEnv, pd))
eg, ctx := errgroup.WithContext(context.Background())
for i := 0; i < 4; i++ {
eg.Go(func() error { return GetParams(ctx, []byte(params), []byte(srs), 0) })
}
require.NoError(t, eg.Wait())
}
func TestCheckFileIgnoresUntrustableExtension(t *testing.T) {
const mockParamInfoBytes = `{
"cid": "Qmxxxxxdoesntexist",
"digest": "0e0958009936b9d5e515ec97b8cb792d",
"sector_size": 2048
}`
var mockParamInfo paramFile
if err := json.Unmarshal([]byte(mockParamInfoBytes), &mockParamInfo); err != nil {
require.NoError(t, err)
}
ft := &fetch{}
err := os.Setenv("TRUST_PARAMS", "1")
require.NoError(t, err)
defer func() {
err := os.Unsetenv("TRUST_PARAMS")
assert.NoError(t, err)
}()
err = ft.checkFile(filepath.Join(".", "should_ignore.params"), mockParamInfo)
assert.NoError(t, err)
err = ft.checkFile(filepath.Join(".", "should_check_and_fail.vk"), mockParamInfo)
assert.Error(t, err)
err = ft.checkFile(filepath.Join(".", "also_check_and_fail.srs"), mockParamInfo)
assert.Error(t, err)
}
func TestDoFetchBadStatusCode(t *testing.T) {
cases := []struct {
name string
code int
}{
{"BadRequest", http.StatusBadRequest},
{"Forbidden", http.StatusForbidden},
{"NotFound", http.StatusNotFound},
{"InternalServerError", http.StatusInternalServerError},
{"BadGateway", http.StatusBadGateway},
{"ServiceUnavailable", http.StatusServiceUnavailable},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(tc.code)
}))
defer ts.Close()
t.Setenv("IPFS_GATEWAY", ts.URL+"/ipfs/")
tmpDir := t.TempDir()
outPath := filepath.Join(tmpDir, "test-param-file")
info := paramFile{
Cid: "QmFakeCid",
Digest: "0000000000000000",
}
err := doFetch(context.Background(), outPath, info)
require.Error(t, err)
errMsg := fmt.Sprintf("%v", err)
assert.True(t, strings.Contains(errMsg, fmt.Sprintf("%d", tc.code)),
"error should contain HTTP status code %d, got: %s", tc.code, errMsg)
assert.False(t, strings.Contains(errMsg, "checksum"),
"error should not mention checksum mismatch, got: %s", errMsg)
})
}
}