-
Notifications
You must be signed in to change notification settings - Fork 3
/
sample.go
322 lines (272 loc) · 8.57 KB
/
sample.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package sample
import (
"archive/zip"
"bytes"
"crypto/rand"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/liftedinit/ghostcloud/x/ghostcloud/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
sdk "github.com/cosmos/cosmos-sdk/types"
)
const HelloWorldHTMLBody = "<html><body>Hello World</body></html>"
// AccAddress returns a sample account address
func AccAddress() string {
pk := ed25519.GenPrivKey().PubKey()
addr := pk.Address()
return sdk.AccAddress(addr).String()
}
func CreateNDatasetPayloads(n int, datasetSize int) ([]*types.Meta, []*types.Payload) {
metas := make([]*types.Meta, n)
payloads := make([]*types.Payload, n)
for i := 0; i < n; i++ {
metas[i], payloads[i] = CreateDatasetPayload(i, datasetSize)
}
return metas, payloads
}
func CreateNDatasetPayloadsWithIndexHtml(n int, datasetSize int) ([]*types.Meta, []*types.Payload) {
metas := make([]*types.Meta, n)
payloads := make([]*types.Payload, n)
for i := 0; i < n; i++ {
metas[i], payloads[i] = CreateDatasetPayloadWithIndexHtml(i, datasetSize)
}
return metas, payloads
}
func CreateNArchivePayloads(n int) ([]*types.Meta, []*types.Payload) {
metas := make([]*types.Meta, n)
payloads := make([]*types.Payload, n)
for i := 0; i < n; i++ {
metas[i], payloads[i] = CreateArchivePayload(i)
}
return metas, payloads
}
func CreateNDeployments(n int, datasetSize int) []*types.Deployment {
return CreateNDeploymentsWithAddr(AccAddress(), n, datasetSize)
}
func CreateNDeploymentsWithAddr(addr string, n int, datasetSize int) []*types.Deployment {
deployments := make([]*types.Deployment, n)
for i := 0; i < n; i++ {
deployments[i] = CreateDeploymentWithAddr(addr, i, datasetSize)
}
return deployments
}
func CreateNMetaDataset(n int, datasetSize int) ([]*types.Meta, []*types.Dataset) {
metas := make([]*types.Meta, n)
datasets := make([]*types.Dataset, n)
for i := 0; i < n; i++ {
metas[i], datasets[i] = CreateMetaDataset(i, datasetSize)
}
return metas, datasets
}
func CreateNMetaDatasetWithAddr(addr string, n int, datasetSize int) ([]*types.Meta, []*types.Dataset) {
metas := make([]*types.Meta, n)
datasets := make([]*types.Dataset, n)
for i := 0; i < n; i++ {
metas[i], datasets[i] = CreateMetaDatasetWithAddr(addr, i, datasetSize)
}
return metas, datasets
}
func createDatasetPayload(addr string, i int, datasetSize int) (*types.Meta, *types.Payload) {
return CreateMetaWithAddr(addr, i), &types.Payload{
PayloadOption: &types.Payload_Dataset{Dataset: CreateDataset(datasetSize)},
}
}
func CreateDatasetPayload(i int, datasetSize int) (*types.Meta, *types.Payload) {
return createDatasetPayload(AccAddress(), i, datasetSize)
}
func CreateDatasetPayloadWithIndexHtml(i int, datasetSize int) (*types.Meta, *types.Payload) {
return CreateMeta(i), &types.Payload{
PayloadOption: &types.Payload_Dataset{Dataset: CreateDatasetWithIndexHtml(datasetSize)},
}
}
func CreateDatasetPayloadWithAddr(addr string, i int, datasetSize int) (*types.Meta, *types.Payload) {
return createDatasetPayload(addr, i, datasetSize)
}
func CreateDatasetPayloadWithAddrAndIndexHtml(addr string, i int, datasetSize int) (*types.Meta, *types.Payload) {
return CreateMetaWithAddr(addr, i), &types.Payload{
PayloadOption: &types.Payload_Dataset{Dataset: CreateDatasetWithIndexHtml(datasetSize)},
}
}
func CreateArchivePayloadWithAddrAndIndexHtml(addr string, i int) (*types.Meta, *types.Payload) {
return CreateMetaWithAddr(addr, i), &types.Payload{
PayloadOption: &types.Payload_Archive{Archive: CreateArchive()},
}
}
func CreateArchivePayload(i int) (*types.Meta, *types.Payload) {
return CreateMeta(i), &types.Payload{
PayloadOption: &types.Payload_Archive{Archive: CreateArchive()},
}
}
func CreateRandomArchivePayload(i int, size int64, name string) (*types.Meta, *types.Payload) {
body, err := generateRandomBytes(int(size))
if err != nil {
panic(err)
}
return CreateMeta(i), &types.Payload{
PayloadOption: &types.Payload_Archive{Archive: &types.Archive{
Type: types.ArchiveType_Zip,
Content: CreateZip(name, string(body)),
}},
}
}
func CreateBombArchivePayload(i int, size uint64, name string) (*types.Meta, *types.Payload) {
return CreateMeta(i), &types.Payload{
PayloadOption: &types.Payload_Archive{Archive: &types.Archive{
Type: types.ArchiveType_Zip,
Content: CreateZip(name, strings.Repeat("a", int(size))),
}},
}
}
func createDeployment(addr string, i int, datasetSize int) *types.Deployment {
return &types.Deployment{
Meta: CreateMetaWithAddr(addr, i),
Dataset: CreateDataset(datasetSize),
}
}
func CreateDeployment(i int, datasetSize int) *types.Deployment {
return createDeployment(AccAddress(), i, datasetSize)
}
func CreateDeploymentWithAddr(addr string, i int, datasetSize int) *types.Deployment {
return createDeployment(addr, i, datasetSize)
}
func CreateDeploymentWithAddrAndIndexHtml(addr string, i int, datasetSize int) *types.Deployment {
return &types.Deployment{
Meta: CreateMetaWithAddr(addr, i),
Dataset: CreateDatasetWithIndexHtml(datasetSize),
}
}
func CreateMetaDataset(i int, datasetSize int) (*types.Meta, *types.Dataset) {
return CreateMeta(i), CreateDataset(datasetSize)
}
func CreateMetaDatasetWithAddr(addr string, i int, datasetSize int) (*types.Meta, *types.Dataset) {
return CreateMetaWithAddr(addr, i), CreateDataset(datasetSize)
}
func createMeta(addr string, i int) *types.Meta {
return &types.Meta{
Creator: addr,
Name: strconv.Itoa(i),
Description: strconv.Itoa(i),
Domain: strconv.Itoa(i),
}
}
func CreateMeta(i int) *types.Meta {
return createMeta(AccAddress(), i)
}
func CreateMetaWithAddr(addr string, i int) *types.Meta {
return createMeta(addr, i)
}
func CreateMetaInvalidAddress() *types.Meta {
return &types.Meta{
Creator: "invalid_address",
Name: "name",
Description: "description",
Domain: "domain",
}
}
func CreateDataset(n int) *types.Dataset {
return &types.Dataset{Items: CreateNItems(n)}
}
func CreateDatasetWithIndexHtml(n int) *types.Dataset {
return &types.Dataset{Items: CreateNItemsWithIndexHtml(n)}
}
func CreateArchive() *types.Archive {
return &types.Archive{
Type: types.ArchiveType_Zip,
Content: CreateZip("index.html", HelloWorldHTMLBody),
}
}
func CreateZip(fileName string, body string) []byte {
return createInMemoryZip(fileName, body)
}
func CreateItem(i int) *types.Item {
return &types.Item{
Meta: &types.ItemMeta{Path: strconv.Itoa(i)},
Content: &types.ItemContent{Content: []byte{byte(i)}},
}
}
func CreateItemWithIndexHtml() *types.Item {
return &types.Item{
Meta: &types.ItemMeta{Path: "index.html"},
Content: &types.ItemContent{Content: []byte{0x00}},
}
}
func CreateNItems(n int) []*types.Item {
items := make([]*types.Item, n)
for i := 0; i < n; i++ {
items[i] = CreateItem(i)
}
return items
}
func CreateNItemsWithIndexHtml(n int) []*types.Item {
items := make([]*types.Item, n)
items[0] = CreateItemWithIndexHtml()
for i := 1; i < n; i++ {
items[i] = CreateItem(i)
}
return items
}
func createInMemoryZip(fileName string, body string) []byte {
// Step 1: Create a buffer to hold the zip archive's data in memory
var buffer bytes.Buffer
// Step 2: Create a new zip archive writing to the buffer
zipWriter := zip.NewWriter(&buffer)
// Dummy data to put into the zip file
files := []struct {
Name, Body string
}{
{fileName, body},
}
// Step 3: Add files to the archive
for _, file := range files {
f, err := zipWriter.Create(file.Name)
if err != nil {
panic(err)
}
_, err = f.Write([]byte(file.Body))
if err != nil {
panic(err)
}
}
// Step 4: Close the zip archive
err := zipWriter.Close()
if err != nil {
panic(err)
}
// Step 5: Convert buffer's contents to bytes
return buffer.Bytes()
}
func CreateTempDataset() (dir string, err error) {
dir, err = os.MkdirTemp("", "example")
if err != nil {
return dir, fmt.Errorf("error creating temporary directory: %v", err)
}
indexFilePath := filepath.Join(dir, "index.html")
_, err = os.Create(indexFilePath)
if err != nil {
return dir, fmt.Errorf("error creating index.html file: %v", err)
}
return dir, nil
}
func CreateTempArchive(fileName string, body string) (file *os.File, err error) {
file, err = os.CreateTemp("", "test-archive-*.zip")
if err != nil {
return file, fmt.Errorf("error creating temporary file: %v", err)
}
data := CreateZip(fileName, body)
_, err = file.Write(data)
if err != nil {
return file, fmt.Errorf("error writing to temporary file: %v", err)
}
return file, nil
}
func generateRandomBytes(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
if err != nil {
return nil, err
}
return b, nil
}