-
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
-
323
-
324
-
325
-
326
-
327
-
328
-
329
-
330
-
331
-
332
-
333
-
334
-
335
-
336
-
337
-
338
-
339
-
340
-
341
-
342
-
343
-
344
-
345
-
346
-
347
-
348
-
349
-
350
-
351
-
352
-
353
-
354
-
355
-
356
-
357
-
358
-
359
-
360
-
361
-
362
-
363
-
364
-
365
-
366
-
367
-
368
-
369
-
370
-
371
-
372
-
373
-
374
-
375
-
376
-
377
-
378
-
379
-
380
-
381
-
382
-
383
-
384
-
385
-
386
-
387
-
388
-
389
-
390
-
391
-
392
-
393
-
394
-
395
-
396
-
397
-
398
-
399
-
400
-
401
-
402
-
403
-
404
-
405
-
406
-
407
-
408
-
409
-
410
-
411
-
412
-
413
-
414
-
415
-
416
-
417
-
418
-
419
-
420
-
421
-
422
-
423
-
424
-
425
-
426
-
427
-
428
-
429
-
430
-
431
-
432
-
433
-
434
-
435
-
436
-
437
-
438
-
439
-
440
-
441
-
442
-
443
-
444
-
445
-
446
-
447
-
448
-
449
-
450
-
451
-
452
-
453
-
454
-
455
-
456
-
457
-
458
-
459
-
460
-
461
-
462
-
463
-
464
-
465
-
466
-
467
-
468
-
469
-
470
-
471
-
472
-
473
-
474
-
475
-
476
-
477
-
478
-
479
-
480
-
481
-
482
-
483
-
484
-
485
-
486
-
487
-
488
-
489
-
490
-
491
-
492
-
493
-
494
-
495
-
496
-
497
-
498
-
499
-
500
-
501
-
502
-
503
-
504
-
505
-
506
-
507
-
508
-
509
-
510
-
511
-
512
-
513
-
514
-
515
-
516
-
517
-
518
-
519
-
520
-
521
-
522
-
523
-
524
-
525
-
526
-
527
-
528
-
529
-
530
-
531
-
532
-
533
-
534
-
535
-
536
-
537
-
538
-
539
-
540
-
541
-
542
-
543
-
544
-
545
-
546
-
547
-
548
-
549
-
550
-
551
-
552
-
553
-
554
-
555
-
556
-
557
-
558
-
559
-
560
-
561
-
562
-
563
-
564
-
565
-
566
-
567
-
568
-
569
-
570
-
571
-
572
-
573
-
574
-
575
-
576
-
577
-
578
-
579
package tests
import (
"bytes"
"fmt"
pub "github.com/go-ap/activitypub"
"io"
"os"
"path/filepath"
"reflect"
"testing"
"time"
"unsafe"
j "github.com/go-ap/jsonld"
)
const dir = "./mocks"
var stopOnFailure = false
type testPair struct {
expected bool
blank interface{}
result interface{}
}
type testMaps map[string]testPair
type visit struct {
a1 unsafe.Pointer
a2 unsafe.Pointer
typ reflect.Type
}
type canErrorFunc func(format string, args ...interface{})
// See reflect.DeepEqual
func assertDeepEquals(t canErrorFunc, x, y interface{}) bool {
if x == nil || y == nil {
return x == y
}
v1 := reflect.ValueOf(x)
v2 := reflect.ValueOf(y)
if v1.Type() != v2.Type() {
t("%T != %T", x, y)
return false
}
return deepValueEqual(t, v1, v2, make(map[visit]bool), 0)
}
// See reflect.deepValueEqual
func deepValueEqual(t canErrorFunc, v1, v2 reflect.Value, visited map[visit]bool, depth int) bool {
if !v1.IsValid() || !v2.IsValid() {
return v1.IsValid() == v2.IsValid()
}
if v1.Type() != v2.Type() {
t("types differ %s != %s", v1.Type().Name(), v2.Type().Name())
return false
}
hard := func(v1, v2 reflect.Value) bool {
switch v1.Kind() {
case reflect.Ptr:
return false
case reflect.Map, reflect.Slice, reflect.Interface:
// Nil pointers cannot be cyclic. Avoid putting them in the visited map.
return !v1.IsNil() && !v2.IsNil()
}
return false
}
if hard(v1, v2) {
var addr1, addr2 unsafe.Pointer
if v1.CanAddr() {
addr1 = unsafe.Pointer(v1.UnsafeAddr())
} else {
addr1 = unsafe.Pointer(v1.Pointer())
}
if v2.CanAddr() {
addr2 = unsafe.Pointer(v2.UnsafeAddr())
} else {
addr2 = unsafe.Pointer(v2.Pointer())
}
if uintptr(addr1) > uintptr(addr2) {
// Canonicalize order to reduce number of entries in visited.
// Assumes non-moving garbage collector.
addr1, addr2 = addr2, addr1
}
// Short circuit if references are already seen.
typ := v1.Type()
v := visit{addr1, addr2, typ}
if visited[v] {
return true
}
// Remember for later.
visited[v] = true
}
switch v1.Kind() {
case reflect.Array:
for i := 0; i < v1.Len(); i++ {
if !deepValueEqual(t, v1.Index(i), v2.Index(i), visited, depth+1) {
t("Arrays not equal at index %d %s %s", i, v1.Index(i), v2.Index(i))
return false
}
}
return true
case reflect.Slice:
if v1.IsNil() != v2.IsNil() {
t("One of the slices is not nil %s[%d] vs %s[%d]", v1.Type().Name(), v1.Len(), v2.Type().Name(), v2.Len())
return false
}
if v1.Len() != v2.Len() {
t("Slices lengths are different %s[%d] vs %s[%d]", v1.Type().Name(), v1.Len(), v2.Type().Name(), v2.Len())
return false
}
if v1.Pointer() == v2.Pointer() {
return true
}
for i := 0; i < v1.Len(); i++ {
if !deepValueEqual(t, v1.Index(i), v2.Index(i), visited, depth+1) {
t("Slices elements at pos %d are not equal %#v vs %#v", i, v1.Index(i), v2.Index(i))
return false
}
}
return true
case reflect.Interface:
if v1.IsNil() || v2.IsNil() {
if v1.IsNil() == v2.IsNil() {
return true
}
var isNil1, isNil2 string
if v1.IsNil() {
isNil1 = "is"
} else {
isNil1 = "is not"
}
if v2.IsNil() {
isNil2 = "is"
} else {
isNil2 = "is not"
}
t("Interface '%s' %s nil and '%s' %s nil", v1.Type().Name(), isNil1, v2.Type().Name(), isNil2)
return false
}
return deepValueEqual(t, v1.Elem(), v2.Elem(), visited, depth+1)
case reflect.Ptr:
if v1.Pointer() == v2.Pointer() {
return true
}
return deepValueEqual(t, v1.Elem(), v2.Elem(), visited, depth+1)
case reflect.Struct:
for i, n := 0, v1.NumField(); i < n; i++ {
var (
f1 = v1.Field(i)
f2 = v2.Field(i)
n1 = v1.Type().Field(i).Name
n2 = v2.Type().Field(i).Name
t1 = f1.Type().Name()
t2 = f2.Type().Name()
)
if !deepValueEqual(t, v1.Field(i), v2.Field(i), visited, depth+1) {
t("Struct fields at pos %d %s[%s] and %s[%s] are not deeply equal", i, n1, t1, n2, t2)
if f1.CanInterface() && f2.CanInterface() {
t(" Values: %#v - %#v", v1.Field(i).Interface(), v2.Field(i).Interface())
}
return false
}
}
return true
case reflect.Map:
if v1.IsNil() != v2.IsNil() {
t("Maps are not nil", v1.Type().Name(), v2.Type().Name())
return false
}
if v1.Len() != v2.Len() {
t("Maps don't have the same length %d vs %d", v1.Len(), v2.Len())
return false
}
if v1.Pointer() == v2.Pointer() {
return true
}
for _, k := range v1.MapKeys() {
val1 := v1.MapIndex(k)
val2 := v2.MapIndex(k)
if !val1.IsValid() || !val2.IsValid() || !deepValueEqual(t, v1.MapIndex(k), v2.MapIndex(k), visited, depth+1) {
t("Maps values at index %s are not equal", k.String())
return false
}
}
return true
case reflect.Func:
if v1.IsNil() && v2.IsNil() {
return true
}
// Can't do better than this:
return false
case reflect.String:
return v1.String() == v2.String()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v1.Int() == v2.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return v1.Uint() == v2.Uint()
case reflect.Float32, reflect.Float64:
return v1.Float() == v2.Float()
case reflect.Bool:
return v1.Bool() == v2.Bool()
case reflect.Complex64, reflect.Complex128:
return v1.Complex() == v2.Complex()
}
return false
}
var zLoc, _ = time.LoadLocation("UTC")
var allTests = testMaps{
//"empty": testPair{
// expected: true,
// blank: &pub.Object{},
// result: &pub.Object{},
//},
//"link_simple": testPair{
// expected: true,
// blank: &pub.Link{},
// result: &pub.Link{
// Type: pub.LinkType,
// Href: pub.IRI("http://example.org/abc"),
// HrefLang: pub.LangRef("en"),
// MediaType: pub.MimeType("text/html"),
// Name: pub.NaturalLanguageValues{{
// pub.NilLangRef, pub.Content("An example link"),
// }},
// },
//},
//"object_with_url": testPair{
// expected: true,
// blank: &pub.Object{},
// result: &pub.Object{
// URL: pub.IRI("http://littr.git/api/accounts/system"),
// },
//},
//"object_with_url_collection": testPair{
// expected: true,
// blank: &pub.Object{},
// result: &pub.Object{
// URL: pub.ItemCollection{
// pub.IRI("http://littr.git/api/accounts/system"),
// pub.IRI("http://littr.git/~system"),
// },
// },
//},
//"object_simple": testPair{
// expected: true,
// blank: &pub.Object{},
// result: &pub.Object{
// Type: pub.ObjectType,
// ID: pub.ID("http://www.test.example/object/1"),
// Name: pub.NaturalLanguageValues{{
// pub.NilLangRef, pub.Content("A Simple, non-specific object"),
// }},
// },
//},
//"object_no_type": testPair{
// expected: true,
// blank: &pub.Object{},
// result: &pub.Object{
// ID: pub.ID("http://www.test.example/object/1"),
// Name: pub.NaturalLanguageValues{{
// pub.NilLangRef, pub.Content("A Simple, non-specific object without a type"),
// }},
// },
//},
//"object_with_tags": testPair{
// expected: true,
// blank: &pub.Object{},
// result: &pub.Object{
// Type: pub.ObjectType,
// ID: pub.ID("http://www.test.example/object/1"),
// Name: pub.NaturalLanguageValues{{
// pub.NilLangRef, pub.Content("A Simple, non-specific object"),
// }},
// Tag: pub.ItemCollection{
// &pub.Mention{
// Name: pub.NaturalLanguageValues{{
// pub.NilLangRef, pub.Content("#my_tag"),
// }},
// Type: pub.MentionType,
// ID: pub.ID("http://example.com/tag/my_tag"),
// },
// &pub.Mention{
// Name: pub.NaturalLanguageValues{{
// pub.NilLangRef, pub.Content("@ana"),
// }},
// Type: pub.MentionType,
// ID: pub.ID("http://example.com/users/ana"),
// },
// },
// },
//},
//"object_with_replies": testPair{
// expected: true,
// blank: &pub.Object{},
// result: &pub.Object{
// Type: pub.ObjectType,
// ID: pub.ID("http://www.test.example/object/1"),
// Replies: &pub.Collection{
// ID: pub.ID("http://www.test.example/object/1/replies"),
// Type: pub.CollectionType,
// TotalItems: 1,
// Items: pub.ItemCollection{
// &pub.Object{
// ID: pub.ID("http://www.test.example/object/1/replies/2"),
// Type: pub.ArticleType,
// Name: pub.NaturalLanguageValues{{
// pub.NilLangRef, pub.Content("Example title"),
// }},
// },
// },
// },
// },
//},
//"activity_simple": testPair{
// expected: true,
// blank: &pub.Activity{
// Actor: &pub.Person{},
// },
// result: &pub.Activity{
// Type: pub.ActivityType,
// Summary: pub.NaturalLanguageValues{{pub.NilLangRef, pub.Content("Sally did something to a note")}},
// Actor: &pub.Person{
// Type: pub.PersonType,
// Name: pub.NaturalLanguageValues{{pub.NilLangRef, pub.Content("Sally")}},
// },
// Object: &pub.Object{
// Type: pub.NoteType,
// Name: pub.NaturalLanguageValues{{pub.NilLangRef, pub.Content("A Note")}},
// },
// },
//},
//"person_with_outbox": testPair{
// expected: true,
// blank: &pub.Person{},
// result: &pub.Person{
// ID: pub.ID("http://example.com/accounts/ana"),
// Type: pub.PersonType,
// Name: pub.NaturalLanguageValues{{pub.NilLangRef, pub.Content("ana")}},
// PreferredUsername: pub.NaturalLanguageValues{{pub.NilLangRef, pub.Content("Ana")}},
// URL: pub.IRI("http://example.com/accounts/ana"),
// Outbox: &pub.OrderedCollection{
// ID: "http://example.com/accounts/ana/outbox",
// Type: pub.OrderedCollectionType,
// URL: pub.IRI("http://example.com/outbox"),
// },
// },
//},
//"ordered_collection": testPair{
// expected: true,
// blank: &pub.OrderedCollection{},
// result: &pub.OrderedCollection{
// ID: pub.ID("http://example.com/outbox"),
// Type: pub.OrderedCollectionType,
// URL: pub.IRI("http://example.com/outbox"),
// TotalItems: 1,
// OrderedItems: pub.ItemCollection{
// &pub.Object{
// ID: pub.ID("http://example.com/outbox/53c6fb47"),
// Type: pub.ArticleType,
// Name: pub.NaturalLanguageValues{{pub.NilLangRef, pub.Content("Example title")}},
// Content: pub.NaturalLanguageValues{{pub.NilLangRef, pub.Content("Example content!")}},
// URL: pub.IRI("http://example.com/53c6fb47"),
// MediaType: pub.MimeType("text/markdown"),
// Published: time.Date(2018, time.July, 5, 16, 46, 44, 0, zLoc),
// Generator: pub.IRI("http://example.com"),
// AttributedTo: pub.IRI("http://example.com/accounts/alice"),
// },
// },
// },
//},
"ordered_collection_page": testPair{
expected: true,
blank: &pub.OrderedCollectionPage{},
result: &pub.OrderedCollectionPage{
PartOf: pub.IRI("http://example.com/outbox"),
Next: pub.IRI("http://example.com/outbox?page=3"),
Prev: pub.IRI("http://example.com/outbox?page=1"),
ID: pub.ID("http://example.com/outbox?page=2"),
Type: pub.OrderedCollectionPageType,
URL: pub.IRI("http://example.com/outbox?page=2"),
Current: pub.IRI("http://example.com/outbox?page=2"),
TotalItems: 1,
StartIndex: 100,
OrderedItems: pub.ItemCollection{
&pub.Object{
ID: pub.ID("http://example.com/outbox/53c6fb47"),
Type: pub.ArticleType,
Name: pub.NaturalLanguageValues{{pub.NilLangRef, pub.Content("Example title")}},
Content: pub.NaturalLanguageValues{{pub.NilLangRef, pub.Content("Example content!")}},
URL: pub.IRI("http://example.com/53c6fb47"),
MediaType: pub.MimeType("text/markdown"),
Published: time.Date(2018, time.July, 5, 16, 46, 44, 0, zLoc),
Generator: pub.IRI("http://example.com"),
AttributedTo: pub.IRI("http://example.com/accounts/alice"),
},
},
},
},
//"natural_language_values": {
// expected: true,
// blank: &pub.NaturalLanguageValues{},
// result: &pub.NaturalLanguageValues{
// {
// pub.NilLangRef, pub.Content([]byte{'\n','\t', '\t', '\n'}),
// },
// {pub.LangRef("en"), pub.Content("Ana got apples ⓐ")},
// {pub.LangRef("fr"), pub.Content("Aná a des pommes ⒜")},
// {pub.LangRef("ro"), pub.Content("Ana are mere")},
// },
//},
//"activity_create_simple": {
// expected: true,
// blank: &pub.Create{},
// result: &pub.Create{
// Type: pub.CreateType,
// Actor: pub.IRI("https://littr.git/api/accounts/anonymous"),
// Object: &pub.Object{
// Type: pub.NoteType,
// AttributedTo: pub.IRI("https://littr.git/api/accounts/anonymous"),
// InReplyTo: pub.IRI("https://littr.git/api/accounts/system/outbox/7ca154ff"),
// Content: pub.NaturalLanguageValues{{pub.NilLangRef, pub.Content("<p>Hello world</p>")}},
// To: pub.ItemCollection{pub.IRI("https://www.w3.org/ns/activitystreams#Public")},
// },
// },
//},
//"activity_create_multiple_objects": {
// expected: true,
// blank: &pub.Create{},
// result: &pub.Create{
// Type: pub.CreateType,
// Actor: pub.IRI("https://littr.git/api/accounts/anonymous"),
// Object: pub.ItemCollection{
// &pub.Object{
// Type: pub.NoteType,
// AttributedTo: pub.IRI("https://littr.git/api/accounts/anonymous"),
// InReplyTo: pub.IRI("https://littr.git/api/accounts/system/outbox/7ca154ff"),
// Content: pub.NaturalLanguageValues{{pub.NilLangRef, pub.Content("<p>Hello world</p>")}},
// To: pub.ItemCollection{pub.IRI("https://www.w3.org/ns/activitystreams#Public")},
// },
// &pub.Article{
// Type: pub.ArticleType,
// ID: pub.ID("http://www.test.example/article/1"),
// Name: pub.NaturalLanguageValues{
// {
// pub.NilLangRef,
// pub.Content("This someday will grow up to be an article"),
// },
// },
// InReplyTo: pub.ItemCollection{
// pub.IRI("http://www.test.example/object/1"),
// pub.IRI("http://www.test.example/object/778"),
// },
// },
// },
// },
//},
//"object_with_audience": testPair{
// expected: true,
// blank: &pub.Object{},
// result: &pub.Object{
// Type: pub.ObjectType,
// ID: pub.ID("http://www.test.example/object/1"),
// To: pub.ItemCollection{
// pub.IRI("https://www.w3.org/ns/activitystreams#Public"),
// },
// Bto: pub.ItemCollection{
// pub.IRI("http://example.com/sharedInbox"),
// },
// CC: pub.ItemCollection{
// pub.IRI("https://example.com/actors/ana"),
// pub.IRI("https://example.com/actors/bob"),
// },
// BCC: pub.ItemCollection{
// pub.IRI("https://darkside.cookie/actors/darthvader"),
// },
// },
//},
//"article_with_multiple_inreplyto": {
// expected: true,
// blank: &pub.Article{},
// result: &pub.Article{
// Type: pub.ArticleType,
// ID: pub.ID("http://www.test.example/article/1"),
// Name: pub.NaturalLanguageValues{
// {
// pub.NilLangRef,
// pub.Content("This someday will grow up to be an article"),
// },
// },
// InReplyTo: pub.ItemCollection{
// pub.IRI("http://www.test.example/object/1"),
// pub.IRI("http://www.test.example/object/778"),
// },
// },
//},
}
func getFileContents(path string) ([]byte, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
st, err := f.Stat()
if err != nil {
return nil, err
}
data := make([]byte, st.Size())
io.ReadFull(f, data)
data = bytes.Trim(data, "\x00")
return data, nil
}
func TestUnmarshal(t *testing.T) {
var err error
var f = t.Errorf
if len(allTests) == 0 {
t.Skip("No tests found")
}
for k, pair := range allTests {
path := filepath.Join(dir, fmt.Sprintf("%s.json", k))
t.Run(path, func(t *testing.T) {
var data []byte
data, err = getFileContents(path)
if err != nil {
f("Error: %s for %s", err, path)
return
}
object := pair.blank
err = j.Unmarshal(data, object)
if err != nil {
f("Error: %s for %s", err, data)
return
}
expLbl := ""
if !pair.expected {
expLbl = "not be "
}
status := assertDeepEquals(f, object, pair.result)
if pair.expected != status {
if stopOnFailure {
f = t.Fatalf
}
f("Mock: %s: %s\n%#v\n should %sequal to expected\n%#v", k, path, object, expLbl, pair.result)
return
}
if !status {
oj, err := j.Marshal(object)
if err != nil {
f(err.Error())
}
tj, err := j.Marshal(pair.result)
if err != nil {
f(err.Error())
}
f("Mock: %s: %s\n%s\n should %sequal to expected\n%s", k, path, oj, expLbl, tj)
}
//if err == nil {
// fmt.Printf(" --- %s: %s\n %s\n", "PASS", k, path)
//}
})
}
}