-
Notifications
You must be signed in to change notification settings - Fork 14
/
integration_test.go
190 lines (179 loc) · 6.29 KB
/
integration_test.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
package main_test
import (
gnarkLogger "github.com/consensys/gnark/logger"
"io"
"net/http"
"strings"
"testing"
"worldcoin/gnark-mbu/logging"
"worldcoin/gnark-mbu/prover"
"worldcoin/gnark-mbu/server"
)
const ProverAddress = "localhost:8080"
const MetricsAddress = "localhost:9999"
var mode string
func TestMain(m *testing.M) {
gnarkLogger.Set(*logging.Logger())
logging.Logger().Info().Msg("Setting up the prover")
ps, err := prover.SetupInsertion(3, 2)
if err != nil {
panic(err)
}
cfg := server.Config{
ProverAddress: ProverAddress,
MetricsAddress: MetricsAddress,
Mode: server.InsertionMode,
}
logging.Logger().Info().Msg("Starting the insertion server")
instance := server.Run(&cfg, ps)
logging.Logger().Info().Msg("Running the insertion tests")
mode = server.InsertionMode
m.Run()
instance.RequestStop()
instance.AwaitStop()
cfg.Mode = server.DeletionMode
ps, err = prover.SetupDeletion(3, 2)
if err != nil {
panic(err)
}
logging.Logger().Info().Msg("Starting the deletion server")
instance = server.Run(&cfg, ps)
logging.Logger().Info().Msg("Running the deletion tests")
mode = server.DeletionMode
m.Run()
instance.RequestStop()
instance.AwaitStop()
}
func TestWrongMethod(t *testing.T) {
response, err := http.Get("http://localhost:8080/prove")
if err != nil {
t.Fatal(err)
}
if response.StatusCode != http.StatusMethodNotAllowed {
t.Fatalf("Expected status code %d, got %d", http.StatusMethodNotAllowed, response.StatusCode)
}
}
func TestInsertionHappyPath(t *testing.T) {
if mode != server.InsertionMode {
return
}
body := `{
"inputHash":"0x5057a31740d54d42ac70c05e0768fb770c682cb2c559bdd03fe4099f7e584e4f",
"startIndex":0,
"preRoot":"0x18f43331537ee2af2e3d758d50f72106467c6eea50371dd528d57eb2b856d238",
"postRoot":"0x2267bee7aae8ed55eb9aecff101145335ed1dd0a5a276a2b7eb3ae7d20e232d8",
"identityCommitments":["0x1","0x2"],
"merkleProofs": [
["0x0","0x2098f5fb9e239eab3ceac3f27b81e481dc3124d55ffed523a839ee8446b64864","0x1069673dcdb12263df301a6ff584a7ec261a44cb9dc68df067a4774460b1f1e1"],
["0x1","0x2098f5fb9e239eab3ceac3f27b81e481dc3124d55ffed523a839ee8446b64864","0x1069673dcdb12263df301a6ff584a7ec261a44cb9dc68df067a4774460b1f1e1"]
]}`
response, err := http.Post("http://localhost:8080/prove", "application/json", strings.NewReader(body))
if err != nil {
t.Fatal(err)
}
if response.StatusCode != http.StatusOK {
t.Fatalf("Expected status code %d, got %d", http.StatusOK, response.StatusCode)
}
}
func TestDeletionHappyPath(t *testing.T) {
if mode != server.DeletionMode {
return
}
body := `{
"inputHash":"0xdcd389a94b549222fadc9e335c358a3fe4d534155182f46927f82ea8491c7480",
"deletionIndices":[0,2],
"preRoot":"0xd11eefe87b985333c0d327b0cdd39a9641b5ac32c35c2bda84301ef3231a8ac",
"postRoot":"0x1912415186579e1d9ff6282b76d081f0acd527d8549ea803385b1382d9498f35",
"identityCommitments":["0x1","0x3"],
"merkleProofs":[
["0x2","0x20a3af0435914ccd84b806164531b0cd36e37d4efb93efab76913a93e1f30996","0x1069673dcdb12263df301a6ff584a7ec261a44cb9dc68df067a4774460b1f1e1"],
["0x4","0x65e2c6cc08a36c4a943286bc91c216054a1981eb4f7570f67394ef8937a21b8","0x1069673dcdb12263df301a6ff584a7ec261a44cb9dc68df067a4774460b1f1e1"]
]}`
response, err := http.Post("http://localhost:8080/prove", "application/json", strings.NewReader(body))
if err != nil {
t.Fatal(err)
}
if response.StatusCode != http.StatusOK {
t.Fatalf("Expected status code %d, got %d", http.StatusOK, response.StatusCode)
}
}
func TestInsertionWrongInput(t *testing.T) {
if mode != server.InsertionMode {
return
}
body := `{
"inputHash":"0x5057a31740d54d42ac70c05e0768fb770c682cb2c559bdd03fe4099f7e584e4f",
"startIndex":0,
"preRoot":"0x18f43331537ee2af2e3d758d50f72106467c6eea50371dd528d57eb2b856d238",
"postRoot":"0x2267bee7aae8ed55eb9aecff101145335ed1dd0a5a276a2b7eb3ae7d20e232d8",
"identityCommitments":["0x1","0x2"],
"merkleProofs": [
["0x0","0x0","0x0"],
["0x1","0x0","0x0"]
]}`
response, err := http.Post("http://localhost:8080/prove", "application/json", strings.NewReader(body))
if err != nil {
t.Fatal(err)
}
if response.StatusCode != http.StatusBadRequest {
t.Fatalf("Expected status code %d, got %d", http.StatusBadRequest, response.StatusCode)
}
responseBody, err := io.ReadAll(response.Body)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(responseBody), "proving_error") {
t.Fatalf("Expected error message to be tagged with 'proving_error', got %s", string(responseBody))
}
}
func TestDeletionWrongInput(t *testing.T) {
if mode != server.DeletionMode {
return
}
body := `{
"inputHash":"0xdcd389a94b549222fadc9e335c358a3fe4d534155182f46927f82ea8491c7480",
"deletionIndices":[0,2],
"preRoot":"0xd11eefe87b985333c0d327b0cdd39a9641b5ac32c35c2bda84301ef3231a8ac",
"postRoot":"0x1912415186579e1d9ff6282b76d081f0acd527d8549ea803385b1382d9498f35",
"identityCommitments":["0x1","0x3"],
"merkleProofs":[
["0x2","0xD","0xD"],
["0x4","0xD","0xD"]
]}`
response, err := http.Post("http://localhost:8080/prove", "application/json", strings.NewReader(body))
if err != nil {
t.Fatal(err)
}
if response.StatusCode != http.StatusBadRequest {
t.Fatalf("Expected status code %d, got %d", http.StatusBadRequest, response.StatusCode)
}
responseBody, err := io.ReadAll(response.Body)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(responseBody), "proving_error") {
t.Fatalf("Expected error message to be tagged with 'proving_error', got %s", string(responseBody))
}
}
func TestDeletionBatchPadding(t *testing.T) {
if mode != server.DeletionMode {
return
}
body := `{
"inputHash":"0x509d6e4ca8a621713cc5feb95de95cb4eed3c1127176d93da653fd3cc55db537",
"deletionIndices":[0,8],
"preRoot":"0xd11eefe87b985333c0d327b0cdd39a9641b5ac32c35c2bda84301ef3231a8ac",
"postRoot":"0x22c58cf24838c2eb1701f2aa6e6a867e10237590dbdb423e4d3e053b121c44cb",
"identityCommitments":["0x1","0x0"],
"merkleProofs":[
["0x2","0x20a3af0435914ccd84b806164531b0cd36e37d4efb93efab76913a93e1f30996","0x1069673dcdb12263df301a6ff584a7ec261a44cb9dc68df067a4774460b1f1e1"],
["0x0","0x0","0x0"]
]}`
response, err := http.Post("http://localhost:8080/prove", "application/json", strings.NewReader(body))
if err != nil {
t.Fatal(err)
}
if response.StatusCode != http.StatusOK {
t.Fatalf("Expected status code %d, got %d", http.StatusOK, response.StatusCode)
}
}