forked from nbd-wtf/go-nostr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvelopes_test.go
184 lines (161 loc) · 6.71 KB
/
envelopes_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
package nostr
import (
"encoding/json"
"testing"
)
func TestEventEnvelopeEncodingAndDecoding(t *testing.T) {
eventEnvelopes := []string{
`["EVENT","_",{"id":"dc90c95f09947507c1044e8f48bcf6350aa6bff1507dd4acfc755b9239b5c962","pubkey":"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d","created_at":1644271588,"kind":1,"tags":[],"content":"now that https://blueskyweb.org/blog/2-7-2022-overview was announced we can stop working on nostr?","sig":"230e9d8f0ddaf7eb70b5f7741ccfa37e87a455c9a469282e3464e2052d3192cd63a167e196e381ef9d7e69e9ea43af2443b839974dc85d8aaab9efe1d9296524"}]`,
`["EVENT",{"id":"9e662bdd7d8abc40b5b15ee1ff5e9320efc87e9274d8d440c58e6eed2dddfbe2","pubkey":"373ebe3d45ec91977296a178d9f19f326c70631d2a1b0bbba5c5ecc2eb53b9e7","created_at":1644844224,"kind":3,"tags":[["p","3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"],["p","75fc5ac2487363293bd27fb0d14fb966477d0f1dbc6361d37806a6a740eda91e"],["p","46d0dfd3a724a302ca9175163bdf788f3606b3fd1bb12d5fe055d1e418cb60ea"]],"content":"{\"wss://nostr-pub.wellorder.net\":{\"read\":true,\"write\":true},\"wss://nostr.bitcoiner.social\":{\"read\":false,\"write\":true},\"wss://expensive-relay.fiatjaf.com\":{\"read\":true,\"write\":true},\"wss://relayer.fiatjaf.com\":{\"read\":true,\"write\":true},\"wss://relay.bitid.nz\":{\"read\":true,\"write\":true},\"wss://nostr.rocks\":{\"read\":true,\"write\":true}}","sig":"811355d3484d375df47581cb5d66bed05002c2978894098304f20b595e571b7e01b2efd906c5650080ffe49cf1c62b36715698e9d88b9e8be43029a2f3fa66be"}]`,
}
for _, raw := range eventEnvelopes {
var env EventEnvelope
if err := json.Unmarshal([]byte(raw), &env); err != nil {
t.Errorf("failed to parse event envelope json: %v", err)
}
if env.GetID() != env.ID {
t.Errorf("error serializing event id: %s != %s", env.GetID(), env.ID)
}
if ok, _ := env.CheckSignature(); !ok {
t.Error("signature verification failed when it should have succeeded")
}
asjson, err := json.Marshal(env)
if err != nil {
t.Errorf("failed to re marshal event as json: %v", err)
}
if string(asjson) != raw {
t.Log(string(asjson))
t.Error("json serialization broken")
}
}
}
func TestNoticeEnvelopeEncodingAndDecoding(t *testing.T) {
src := `["NOTICE","kjasbdlasvdluiasvd\"kjasbdksab\\d"]`
var env NoticeEnvelope
json.Unmarshal([]byte(src), &env)
if env != "kjasbdlasvdluiasvd\"kjasbdksab\\d" {
t.Error("failed to decode NOTICE")
}
if res, _ := json.Marshal(env); string(res) != src {
t.Errorf("failed to encode NOTICE: expected '%s', got '%s'", src, string(res))
}
}
func TestEoseEnvelopeEncodingAndDecoding(t *testing.T) {
src := `["EOSE","kjasbdlasvdluiasvd\"kjasbdksab\\d"]`
var env EOSEEnvelope
json.Unmarshal([]byte(src), &env)
if env != "kjasbdlasvdluiasvd\"kjasbdksab\\d" {
t.Error("failed to decode EOSE")
}
if res, _ := json.Marshal(env); string(res) != src {
t.Errorf("failed to encode EOSE: expected '%s', got '%s'", src, string(res))
}
}
func TestOKEnvelopeEncodingAndDecoding(t *testing.T) {
okEnvelopes := []string{
`["OK","3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefaaaaa",false,"error: could not connect to the database"]`,
`["OK","3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefaaaaa",true,""]`,
}
for _, raw := range okEnvelopes {
var env OKEnvelope
if err := json.Unmarshal([]byte(raw), &env); err != nil {
t.Errorf("failed to parse ok envelope json: %v", err)
}
asjson, err := json.Marshal(env)
if err != nil {
t.Errorf("failed to re marshal ok as json: %v", err)
}
if string(asjson) != raw {
t.Log(string(asjson))
t.Error("json serialization broken")
}
}
}
func TestClosedEnvelopeEncodingAndDecoding(t *testing.T) {
for _, src := range []string{
`["CLOSED","_","error: something went wrong"]`,
`["CLOSED",":1","auth-required: take a selfie and send it to the CIA"]`,
} {
var env ClosedEnvelope
json.Unmarshal([]byte(src), &env)
if env.SubscriptionID != "_" && env.SubscriptionID != ":1" {
t.Error("failed to decode CLOSED")
}
if res, _ := json.Marshal(env); string(res) != src {
t.Errorf("failed to encode CLOSED: expected '%s', got '%s'", src, string(res))
}
}
}
func TestAuthEnvelopeEncodingAndDecoding(t *testing.T) {
authEnvelopes := []string{
`["AUTH","kjsabdlasb aslkd kasndkad \"as.kdnbskadb"]`,
`["AUTH",{"id":"ae1fc7154296569d87ca4663f6bdf448c217d1590d28c85d158557b8b43b4d69","pubkey":"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798","created_at":1683660344,"kind":1,"tags":[],"content":"hello world","sig":"94e10947814b1ebe38af42300ecd90c7642763896c4f69506ae97bfdf54eec3c0c21df96b7d95daa74ff3d414b1d758ee95fc258125deebc31df0c6ba9396a51"}]`,
}
for _, raw := range authEnvelopes {
var env AuthEnvelope
if err := json.Unmarshal([]byte(raw), &env); err != nil {
t.Errorf("failed to parse auth envelope json: %v", err)
}
asjson, err := json.Marshal(env)
if err != nil {
t.Errorf("failed to re marshal auth as json: %v", err)
}
if string(asjson) != raw {
t.Log(string(asjson))
t.Error("json serialization broken")
}
}
}
func TestParseMessage(t *testing.T) {
testCases := []struct {
Name string
Message []byte
ExpectedEnvelope Envelope
}{
{
Name: "nil",
Message: nil,
ExpectedEnvelope: nil,
},
{
Name: "invalid string",
Message: []byte("invalid input"),
ExpectedEnvelope: nil,
},
{
Name: "invalid string with a comma",
Message: []byte("invalid, input"),
ExpectedEnvelope: nil,
},
{
Name: "CLOSED envelope",
Message: []byte(`["CLOSED",":1","error: we are broken"]`),
ExpectedEnvelope: &ClosedEnvelope{SubscriptionID: ":1", Reason: "error: we are broken"},
},
{
Name: "AUTH envelope",
Message: []byte(`["AUTH","bisteka"]`),
ExpectedEnvelope: &AuthEnvelope{Challenge: ptr("bisteka")},
},
{
Name: "REQ envelope",
Message: []byte(`["REQ","million", {"kinds": [1]}, {"kinds": [30023 ], "#d": ["buteko", "batuke"]}]`),
ExpectedEnvelope: &ReqEnvelope{SubscriptionID: "million", Filters: Filters{{Kinds: []int{1}}, {Kinds: []int{30023}, Tags: TagMap{"d": []string{"buteko", "batuke"}}}}},
},
}
for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
envelope := ParseMessage(testCase.Message)
if testCase.ExpectedEnvelope == nil && envelope == nil {
return
}
if testCase.ExpectedEnvelope == nil && envelope != nil {
t.Fatalf("expected nil but got %v\n", envelope)
}
if testCase.ExpectedEnvelope.String() != envelope.String() {
t.Fatalf("unexpected output:\n %s\n != %s", testCase.ExpectedEnvelope, envelope)
}
})
}
}
func ptr[S any](s S) *S { return &s }