mirror of
https://dev.narayana.im/narayana/telegabber.git
synced 2026-08-05 12:17:06 +00:00
179 lines
5.7 KiB
Go
179 lines
5.7 KiB
Go
package gateway
|
|
|
|
import (
|
|
"crypto/sha1"
|
|
"encoding/xml"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gosrc.io/xmpp/stanza"
|
|
)
|
|
|
|
func testPresence(t *testing.T, presence stanza.Presence, reference string) {
|
|
byteXML, err := xml.Marshal(presence)
|
|
if err != nil {
|
|
t.Errorf("XML parse error: %v", err)
|
|
}
|
|
xmlText := string(byteXML)
|
|
if xmlText != reference {
|
|
t.Errorf("%v does not match %v", xmlText, reference)
|
|
}
|
|
}
|
|
|
|
func TestPresenceFrom(t *testing.T) {
|
|
presence := newPresence("from@test", "to@test", SPFrom("test"))
|
|
testPresence(t, presence, "<presence from=\"test@from@test\" to=\"to@test\"></presence>")
|
|
}
|
|
|
|
func TestPresenceNoFrom(t *testing.T) {
|
|
presence := newPresence("from@test", "to@test")
|
|
testPresence(t, presence, "<presence from=\"from@test\" to=\"to@test\"></presence>")
|
|
}
|
|
|
|
func TestPresenceType(t *testing.T) {
|
|
presence := newPresence("from@test", "to@test", SPType("subscribe"))
|
|
testPresence(t, presence, "<presence type=\"subscribe\" from=\"from@test\" to=\"to@test\"></presence>")
|
|
}
|
|
|
|
func TestPresenceShow(t *testing.T) {
|
|
presence := newPresence("from@test", "to@test", SPShow("dnd"))
|
|
testPresence(t, presence, "<presence from=\"from@test\" to=\"to@test\"><show>dnd</show></presence>")
|
|
}
|
|
|
|
func TestPresenceStatus(t *testing.T) {
|
|
presence := newPresence("from@test", "to@test", SPStatus("cooking"))
|
|
testPresence(t, presence, "<presence from=\"from@test\" to=\"to@test\"><status>cooking</status></presence>")
|
|
}
|
|
|
|
func TestPresenceNickname(t *testing.T) {
|
|
presence := newPresence("from@test", "to@test", SPNickname("Ishmael"))
|
|
testPresence(t, presence, "<presence from=\"from@test\" to=\"to@test\"><nick xmlns=\"http://jabber.org/protocol/nick\">Ishmael</nick></presence>")
|
|
}
|
|
|
|
func TestPresencePhoto(t *testing.T) {
|
|
presence := newPresence("from@test", "to@test", SPPhoto("01b87fcd030b72895ff8e88db57ec525450f000d"))
|
|
testPresence(t, presence, "<presence from=\"from@test\" to=\"to@test\"><x xmlns=\"vcard-temp:x:update\"><photo>01b87fcd030b72895ff8e88db57ec525450f000d</photo></x></presence>")
|
|
}
|
|
|
|
func TestPresenceCaps(t *testing.T) {
|
|
presence := newPresence("from@test", "to@test", SPCaps("QgayPKawpkPSDYmwT/WM94uAlu0="))
|
|
testPresence(t, presence, "<presence from=\"from@test\" to=\"to@test\"><c xmlns=\"http://jabber.org/protocol/caps\" hash=\"sha-1\" node=\"https://dev.narayana.im/narayana/telegabber/\" ver=\"QgayPKawpkPSDYmwT/WM94uAlu0=\"></c></presence>")
|
|
}
|
|
|
|
func testCapsHash(t *testing.T, disco *stanza.DiscoInfo, reference string) {
|
|
b64 := discoToCapsHash(disco)
|
|
if b64 != reference {
|
|
hash := sha1.New()
|
|
discoToCaps(disco, hash)
|
|
sha1Hash := hash.Sum(nil)
|
|
|
|
var sb strings.Builder
|
|
discoToCaps(disco, &sb)
|
|
|
|
t.Errorf("%v does not match %v\nRaw hash: %v\nRaw string: %v", b64, reference, sha1Hash, sb.String())
|
|
}
|
|
}
|
|
|
|
func TestDiscoCapsHash1(t *testing.T) {
|
|
disco := stanza.DiscoInfo{
|
|
Identity: []stanza.Identity{
|
|
stanza.Identity{
|
|
Name: "Exodus 0.9.1",
|
|
Category: "client",
|
|
Type: "pc",
|
|
},
|
|
},
|
|
Features: []stanza.Feature{
|
|
stanza.Feature{ Var: "http://jabber.org/protocol/disco#info" },
|
|
stanza.Feature{ Var: "http://jabber.org/protocol/disco#items" },
|
|
stanza.Feature{ Var: "http://jabber.org/protocol/muc" },
|
|
stanza.Feature{ Var: "http://jabber.org/protocol/caps" },
|
|
},
|
|
}
|
|
testCapsHash(t, &disco, "QgayPKawpkPSDYmwT/WM94uAlu0=")
|
|
}
|
|
|
|
func TestDiscoCapsHash2(t *testing.T) {
|
|
disco := stanza.DiscoInfo{
|
|
Identity: []stanza.Identity{
|
|
stanza.Identity{
|
|
Name: "Psi 0.11",
|
|
Category: "client",
|
|
Type: "pc",
|
|
},
|
|
stanza.Identity{
|
|
Name: "Ψ 0.11",
|
|
Category: "client",
|
|
Type: "pc",
|
|
},
|
|
},
|
|
Features: []stanza.Feature{
|
|
stanza.Feature{ Var: "http://jabber.org/protocol/disco#info" },
|
|
stanza.Feature{ Var: "http://jabber.org/protocol/disco#items" },
|
|
stanza.Feature{ Var: "http://jabber.org/protocol/muc" },
|
|
stanza.Feature{ Var: "http://jabber.org/protocol/caps" },
|
|
},
|
|
Form: &stanza.Form{
|
|
Type: stanza.FormTypeResult,
|
|
Fields: []*stanza.Field{
|
|
&stanza.Field{
|
|
Var: "FORM_TYPE",
|
|
Type: stanza.FieldTypeHidden,
|
|
ValuesList: []string{"urn:xmpp:dataforms:softwareinfo"},
|
|
},
|
|
&stanza.Field{
|
|
Var: "ip_version",
|
|
Type: stanza.FieldTypeTextMulti,
|
|
ValuesList: []string{"ipv4", "ipv6"},
|
|
},
|
|
&stanza.Field{
|
|
Var: "os",
|
|
ValuesList: []string{"Mac"},
|
|
},
|
|
&stanza.Field{
|
|
Var: "os_version",
|
|
ValuesList: []string{"10.5.1"},
|
|
},
|
|
&stanza.Field{
|
|
Var: "software",
|
|
ValuesList: []string{"Psi"},
|
|
},
|
|
&stanza.Field{
|
|
Var: "software_version",
|
|
ValuesList: []string{"0.11"},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
testCapsHash(t, &disco, "MxdZjNKNku1+SiM9N92yqIK2HTQ=")
|
|
}
|
|
|
|
func TestDiscoCapsHash3(t *testing.T) {
|
|
disco := stanza.DiscoInfo{
|
|
Identity: []stanza.Identity{
|
|
stanza.Identity{
|
|
Name: "BombusMod",
|
|
Category: "client",
|
|
Type: "mobile",
|
|
},
|
|
},
|
|
Features: []stanza.Feature{
|
|
stanza.Feature{ Var: "http://jabber.org/protocol/activity" },
|
|
stanza.Feature{ Var: "http://jabber.org/protocol/activity+notify" },
|
|
stanza.Feature{ Var: "http://jabber.org/protocol/caps" },
|
|
stanza.Feature{ Var: "http://jabber.org/protocol/commands" },
|
|
stanza.Feature{ Var: "http://jabber.org/protocol/disco#info" },
|
|
stanza.Feature{ Var: "http://jabber.org/protocol/disco#items" },
|
|
stanza.Feature{ Var: "http://jabber.org/protocol/rosterx" },
|
|
stanza.Feature{ Var: "jabber:iq:last" },
|
|
stanza.Feature{ Var: "jabber:iq:privacy" },
|
|
stanza.Feature{ Var: "jabber:iq:roster" },
|
|
stanza.Feature{ Var: "jabber:iq:time" },
|
|
stanza.Feature{ Var: "jabber:iq:version" },
|
|
stanza.Feature{ Var: "jabber:x:oob" },
|
|
stanza.Feature{ Var: "urn:xmpp:ping" },
|
|
stanza.Feature{ Var: "urn:xmpp:time" },
|
|
},
|
|
}
|
|
testCapsHash(t, &disco, "7Awj7pIUiI5UW/L2fdtzXZFQHsw=")
|
|
}
|