This repository has been archived on 2022-11-27. You can view files and clone it, but cannot push or open issues or pull requests.
activitypub/unmarshalling_with_mocks_test.go
Marius Orcsik c3a38f35e7
Holy shit! Merged the activitystreams repo
Only 3 tests failing
2019-12-03 17:23:59 +01:00

538 lines
14 KiB
Go

package activitypub
import (
"bytes"
"fmt"
"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{})
func assertDeepEquals(t canErrorFunc, x, y interface{}) bool {
if x == nil || y == nil {
return x == y
}
v1 := reflect.ValueOf(x)
//if v1.CanAddr() {
// v1 = v1.Addr()
//}
v2 := reflect.ValueOf(y)
//if v2.CanAddr() {
// v2 = v2.Addr()
//}
if v1.Type() != v2.Type() {
t("%T != %T", x, y)
return false
}
return deepValueEqual(t, v1, v2, make(map[visit]bool), 0)
}
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
}
// We want to avoid putting more in the visited map than we need to.
// For any possible reference cycle that might be encountered,
// hard(t) needs to return true for at least one of the types in the cycle.
hard := func(k reflect.Kind) bool {
switch k {
case reflect.Map, reflect.Slice, reflect.Ptr, reflect.Interface:
return true
}
//t("Invalid type for %s", k)
return false
}
if v1.CanAddr() && v2.CanAddr() && hard(v1.Kind()) {
addr1 := unsafe.Pointer(v1.UnsafeAddr())
addr2 := unsafe.Pointer(v2.UnsafeAddr())
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++ {
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, v1.Type().Field(i).Name, v1.Field(i).Type().Name(), v2.Type().Field(i).Name, v2.Field(i).Type().Name())
if v1.Field(i).CanAddr() && v2.Field(i).CanAddr() {
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
}
return true // i guess?
}
var zLoc, _ = time.LoadLocation("UTC")
var allTests = testMaps{
"empty": testPair{
expected: true,
blank: &Object{},
result: &Object{},
},
"link_simple": testPair{
expected: true,
blank: &Link{},
result: &Link{
Type: LinkType,
Href: IRI("http://example.org/abc"),
HrefLang: LangRef("en"),
MediaType: MimeType("text/html"),
Name: NaturalLanguageValues{{
NilLangRef, "An example link",
}},
},
},
"object_with_url": testPair{
expected: true,
blank: &Object{},
result: &Object{
URL: IRI("http://littr.git/api/accounts/system"),
},
},
"object_with_url_collection": testPair{
expected: true,
blank: &Object{},
result: &Object{
URL: ItemCollection{
IRI("http://littr.git/api/accounts/system"),
IRI("http://littr.git/~system"),
},
},
},
"object_simple": testPair{
expected: true,
blank: &Object{},
result: &Object{
Type: ObjectType,
ID: ObjectID("http://www.test.example/object/1"),
Name: NaturalLanguageValues{{
NilLangRef, "A Simple, non-specific object",
}},
},
},
"object_with_tags": testPair{
expected: true,
blank: &Object{},
result: &Object{
Type: ObjectType,
ID: ObjectID("http://www.test.example/object/1"),
Name: NaturalLanguageValues{{
NilLangRef, "A Simple, non-specific object",
}},
Tag: ItemCollection{
&Mention{
Name: NaturalLanguageValues{{
NilLangRef, "#my_tag",
}},
Type: MentionType,
ID: ObjectID("http://example.com/tag/my_tag"),
},
&Mention{
Name: NaturalLanguageValues{{
NilLangRef, "@ana",
}},
Type: MentionType,
ID: ObjectID("http://example.com/users/ana"),
},
},
},
},
"object_with_replies": testPair{
expected: true,
blank: &Object{},
result: &Object{
Type: ObjectType,
ID: ObjectID("http://www.test.example/object/1"),
Replies: &Collection{
ID: ObjectID("http://www.test.example/object/1/replies"),
Type: CollectionType,
TotalItems: 1,
Items: ItemCollection{
&Object{
ID: ObjectID("http://www.test.example/object/1/replies/2"),
Type: ArticleType,
Name: NaturalLanguageValues{{NilLangRef, "Example title"}},
},
},
},
},
},
"activity_simple": testPair{
expected: true,
blank: &Activity{
Actor: &Person{},
},
result: &Activity{
Type: ActivityType,
Summary: NaturalLanguageValues{{NilLangRef, "Sally did something to a note"}},
Actor: &Person{
Type: PersonType,
Name: NaturalLanguageValues{{NilLangRef, "Sally"}},
},
Object: &Object{
Type: NoteType,
Name: NaturalLanguageValues{{NilLangRef, "A Note"}},
},
},
},
"person_with_outbox": testPair{
expected: true,
blank: &Person{},
result: &Person{
ID: ObjectID("http://example.com/accounts/ana"),
Type: PersonType,
Name: NaturalLanguageValues{{NilLangRef, "ana"}},
URL: IRI("http://example.com/accounts/ana"),
},
},
"ordered_collection": testPair{
expected: true,
blank: &OrderedCollection{},
result: &OrderedCollection{
ID: ObjectID("http://example.com/outbox"),
Type: OrderedCollectionType,
URL: IRI("http://example.com/outbox"),
TotalItems: 1,
OrderedItems: ItemCollection{
&Object{
ID: ObjectID("http://example.com/outbox/53c6fb47"),
Type: ArticleType,
Name: NaturalLanguageValues{{NilLangRef, "Example title"}},
Content: NaturalLanguageValues{{NilLangRef, "Example content!"}},
URL: IRI("http://example.com/53c6fb47"),
MediaType: MimeType("text/markdown"),
Published: time.Date(2018, time.July, 5, 16, 46, 44, 0, zLoc),
Generator: IRI("http://example.com"),
AttributedTo: IRI("http://example.com/accounts/alice"),
},
},
},
},
"ordered_collection_page": testPair{
expected: true,
blank: &OrderedCollectionPage{},
result: &OrderedCollectionPage{
PartOf: IRI("http://example.com/outbox"),
Next: IRI("http://example.com/outbox?page=3"),
Prev: IRI("http://example.com/outbox?page=1"),
ID: ObjectID("http://example.com/outbox?page=2"),
Type: OrderedCollectionPageType,
URL: IRI("http://example.com/outbox?page=2"),
Current: IRI("http://example.com/outbox?page=2"),
TotalItems: 1,
OrderedItems: ItemCollection{
&Object{
ID: ObjectID("http://example.com/outbox/53c6fb47"),
Type: ArticleType,
Name: NaturalLanguageValues{{NilLangRef, "Example title"}},
Content: NaturalLanguageValues{{NilLangRef, "Example content!"}},
URL: IRI("http://example.com/53c6fb47"),
MediaType: MimeType("text/markdown"),
Published: time.Date(2018, time.July, 5, 16, 46, 44, 0, zLoc),
Generator: IRI("http://example.com"),
AttributedTo: IRI("http://example.com/accounts/alice"),
},
},
},
},
"natural_language_values": {
expected: true,
blank: &NaturalLanguageValues{},
result: &NaturalLanguageValues{
{
NilLangRef, `
`},
{LangRef("en"), "Ana got apples ⓐ"},
{LangRef("fr"), "Aná a des pommes ⒜"},
{LangRef("ro"), "Ana are mere"},
},
},
"activity_create_simple": {
expected: true,
blank: &Create{},
result: &Create{
Type: CreateType,
Actor: IRI("https://littr.git/api/accounts/anonymous"),
Object: &Object{
Type: NoteType,
AttributedTo: IRI("https://littr.git/api/accounts/anonymous"),
InReplyTo: ItemCollection{IRI("https://littr.git/api/accounts/system/outbox/7ca154ff")},
Content: NaturalLanguageValues{{NilLangRef, "<p>Hello world</p>"}},
To: ItemCollection{IRI("https://www.w3.org/ns/activitystreams#Public")},
},
},
},
"activity_create_multiple_objects": {
expected: true,
blank: &Create{},
result: &Create{
Type: CreateType,
Actor: IRI("https://littr.git/api/accounts/anonymous"),
Object: ItemCollection{
&Object{
Type: NoteType,
AttributedTo: IRI("https://littr.git/api/accounts/anonymous"),
InReplyTo: ItemCollection{IRI("https://littr.git/api/accounts/system/outbox/7ca154ff")},
Content: NaturalLanguageValues{{NilLangRef, "<p>Hello world</p>"}},
To: ItemCollection{IRI("https://www.w3.org/ns/activitystreams#Public")},
},
&Article{
Type: ArticleType,
ID: ObjectID("http://www.test.example/article/1"),
Name: NaturalLanguageValues{
{
NilLangRef,
"This someday will grow up to be an article",
},
},
InReplyTo: ItemCollection{
IRI("http://www.test.example/object/1"),
IRI("http://www.test.example/object/778"),
},
},
},
},
},
"object_with_audience": testPair{
expected: true,
blank: &Object{},
result: &Object{
Type: ObjectType,
ID: ObjectID("http://www.test.example/object/1"),
To: ItemCollection{
IRI("https://www.w3.org/ns/activitystreams#Public"),
},
Bto: ItemCollection{
IRI("http://example.com/sharedInbox"),
},
CC: ItemCollection{
IRI("https://example.com/actors/ana"),
IRI("https://example.com/actors/bob"),
},
BCC: ItemCollection{
IRI("https://darkside.cookie/actors/darthvader"),
},
},
},
"article_with_multiple_inreplyto": {
expected: true,
blank: &Article{},
result: &Article{
Type: ArticleType,
ID: ObjectID("http://www.test.example/article/1"),
Name: NaturalLanguageValues{
{
NilLangRef,
"This someday will grow up to be an article",
},
},
InReplyTo: ItemCollection{
IRI("http://www.test.example/object/1"),
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)
//}
})
}
}