sum7
/
yaja
Archived
1
0
Fork 0

[TASK] add jid

This commit is contained in:
Martin Geno 2017-10-02 14:38:52 +02:00
parent 4b002015b6
commit 54f67245d6
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
6 changed files with 254 additions and 24 deletions

View File

@ -1,19 +0,0 @@
package cmd
import (
log "github.com/sirupsen/logrus"
"dev.sum7.eu/genofire/yaja/model/config"
)
var (
configPath string
)
func loadConfig() *config.Config {
config, err := config.ReadConfigFile(configPath)
if err != nil {
log.Fatal("unable to load config file:", err)
}
return config
}

View File

@ -5,18 +5,27 @@ import (
"os/signal"
"syscall"
"dev.sum7.eu/genofire/yaja/model/config"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var configPath string
// serveCmd represents the serve command
var serveCmd = &cobra.Command{
Use: "serve",
Short: "Runs the yaja server",
Example: "yaja serve -config /etc/yaja.toml",
Example: "yaja serve -c /etc/yaja.conf",
Run: func(cmd *cobra.Command, args []string) {
loadConfig()
_, err := config.ReadConfigFile(configPath)
if err != nil {
log.Fatal("unable to load config file:", err)
}
log.Infoln("yaja started ")
// Wait for INT/TERM
sigs := make(chan os.Signal, 1)
@ -29,5 +38,5 @@ var serveCmd = &cobra.Command{
func init() {
RootCmd.AddCommand(serveCmd)
serveCmd.Flags().StringVarP(&configPath, "config", "c", "config.toml", "Path to configuration file")
serveCmd.Flags().StringVarP(&configPath, "config", "c", "yaja.conf", "Path to configuration file")
}

View File

@ -6,7 +6,6 @@ import (
"github.com/stretchr/testify/assert"
)
// Function to test the configuration of the microservice
func TestReadConfig(t *testing.T) {
assert := assert.New(t)

View File

@ -1,5 +1,19 @@
package config
import "dev.sum7.eu/genofire/yaja/model"
type Config struct {
TLSDir string `toml:"tlsdir"`
PortClient int `toml:"port_client"`
PortServer int `toml:"port_server"`
Domain []*Domain `toml:"domain"`
}
type Domain struct {
FQDN string `toml:"fqdn"`
Admins []*model.JID `toml:"admins"`
TLSDisable bool `toml:"tls_disable"`
TLSPrivate string `toml:"tls_private"`
TLSPublic string `toml:"tls_public"`
}

68
model/jid.go Normal file
View File

@ -0,0 +1,68 @@
package model
import (
"errors"
"regexp"
)
var jidRegex *regexp.Regexp
func init() {
jidRegex = regexp.MustCompile(`^(?:([^@/<>'\" ]+)@)?([^@/<>'\"]+)(?:/([^<>'\" ][^<>'\"]*))?$`)
}
// JID struct
type JID struct {
Local string
Domain string
Resource string
}
// NewJID get JID from string
func NewJID(jidString string) *JID {
jidSplitTmp := jidRegex.FindAllStringSubmatch(jidString, -1)
if len(jidSplitTmp) != 1 {
return nil
}
jidSplit := jidSplitTmp[0]
return &JID{
Local: jidSplit[1],
Domain: jidSplit[2],
Resource: jidSplit[3],
}
}
// Bare get the "bare" jid
func (jid *JID) Bare() string {
if jid.Local != "" {
return jid.Local + "@" + jid.Domain
}
return jid.Domain
}
// Full get the "full" jid as string
func (jid *JID) Full() string {
if jid.Resource != "" {
return jid.Bare() + "/" + jid.Resource
}
return jid.Bare()
}
//MarshalJSON to bytearray
func (jid JID) MarshalJSON() ([]byte, error) {
return []byte(jid.Full()), nil
}
// UnmarshalJSON from bytearray
func (jid *JID) UnmarshalJSON(data []byte) (err error) {
newJID := NewJID(string(data))
if newJID == nil {
return errors.New("not a valid jid")
}
jid.Local = newJID.Local
jid.Domain = newJID.Domain
jid.Resource = newJID.Resource
return nil
}

159
model/jid_test.go Normal file
View File

@ -0,0 +1,159 @@
package model
import (
"testing"
"github.com/stretchr/testify/assert"
)
// Test Values for NewJID from RFC7622
// https://tools.ietf.org/html/rfc7622
func TestNewJID(t *testing.T) {
assert := assert.New(t)
checkList := map[string]*JID{
"juliet@example.com": &JID{
Local: "juliet",
Domain: "example.com",
},
"juliet@example.com/foo": &JID{
Local: "juliet",
Domain: "example.com",
Resource: "foo",
},
"juliet@example.com/foo bar": &JID{
Local: "juliet",
Domain: "example.com",
Resource: "foo bar",
},
"juliet@example.com/foo@bar": &JID{
Local: "juliet",
Domain: "example.com",
Resource: "foo@bar",
},
"foo\\20bar@example.com": &JID{
Local: "foo\\20bar",
Domain: "example.com",
},
"fussball@example.com": &JID{
Local: "fussball",
Domain: "example.com",
},
"fu&#xDF;ball@example.com": &JID{
Local: "fu&#xDF;ball",
Domain: "example.com",
},
"&#x3C0;@example.com": &JID{
Local: "&#x3C0;",
Domain: "example.com",
},
"&#x3A3;@example.com/foo": &JID{
Local: "&#x3A3;",
Domain: "example.com",
Resource: "foo",
},
"&#x3C3;@example.com/foo": &JID{
Local: "&#x3C3;",
Domain: "example.com",
Resource: "foo",
},
"&#x3C2;@example.com/foo": &JID{
Local: "&#x3C2;",
Domain: "example.com",
Resource: "foo",
},
"king@example.com/&#x265A;": &JID{
Local: "king",
Domain: "example.com",
Resource: "&#x265A;",
},
"example.com": &JID{
Domain: "example.com",
},
"example.com/foobar": &JID{
Domain: "example.com",
Resource: "foobar",
},
"a.example.com/b@example.net": &JID{
Domain: "a.example.com",
Resource: "b@example.net",
},
"\"juliet\"@example.com": nil,
"foo bar@example.com": nil,
"juliet@example.com/ foo": nil,
"@example.com/": nil,
// "henry&#x2163;@example.com": nil, -- ignore for easier implementation
// "&#x265A;@example.com": nil,
"juliet@": nil,
"/foobar": nil,
}
for jidString, jidValid := range checkList {
jid := NewJID(jidString)
if jidValid != nil {
assert.NotNil(jid, "this should be a valid JID:"+jidString)
if jid == nil {
continue
}
assert.Equal(jidValid.Local, jid.Local, "the local part was not right detectet:"+jidString)
assert.Equal(jidValid.Domain, jid.Domain, "the domain part was not right detectet:"+jidString)
assert.Equal(jidValid.Resource, jid.Resource, "the resource part was not right detectet:"+jidString)
assert.Equal(jidValid.Full(), jidString, "the function full of jid did not work")
} else {
assert.Nil(jid, "this should not be a valid JID:"+jidString)
}
}
}
func TestJIDBare(t *testing.T) {
assert := assert.New(t)
checkList := map[string]*JID{
"aaa@example.com": &JID{
Local: "aaa",
Domain: "example.com",
},
"aab@example.com": &JID{
Local: "aab",
Domain: "example.com",
Resource: "foo",
},
"example.com": &JID{
Domain: "example.com",
Resource: "foo",
},
}
for jidValid, jid := range checkList {
jidBase := jid.Bare()
assert.Equal(jidValid, jidBase)
}
}
func TestJSONMarshal(t *testing.T) {
assert := assert.New(t)
jid := &JID{}
err := jid.UnmarshalJSON([]byte("juliet@example.com/foo"))
assert.NoError(err)
assert.Equal(jid.Local, "juliet")
assert.Equal(jid.Domain, "example.com")
assert.Equal(jid.Resource, "foo")
err = jid.UnmarshalJSON([]byte("juliet@example.com/ foo"))
assert.Error(err)
jid = &JID{
Local: "romeo",
Domain: "example.com",
Resource: "bar",
}
jidString, err := jid.MarshalJSON()
assert.NoError(err)
assert.Equal("romeo@example.com/bar", string(jidString))
}