From 54f67245d68b93613dc0c6eed3009d95a1e2c9ba Mon Sep 17 00:00:00 2001 From: Martin Geno Date: Mon, 2 Oct 2017 14:38:52 +0200 Subject: [PATCH] [TASK] add jid --- cmd/config.go | 19 ----- cmd/serve.go | 15 +++- model/config/file_test.go | 1 - model/config/struct.go | 16 +++- model/jid.go | 68 ++++++++++++++++ model/jid_test.go | 159 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 254 insertions(+), 24 deletions(-) delete mode 100644 cmd/config.go create mode 100644 model/jid.go create mode 100644 model/jid_test.go diff --git a/cmd/config.go b/cmd/config.go deleted file mode 100644 index 511e8a0..0000000 --- a/cmd/config.go +++ /dev/null @@ -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 -} diff --git a/cmd/serve.go b/cmd/serve.go index dd68217..009213b 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -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") } diff --git a/model/config/file_test.go b/model/config/file_test.go index 109e132..8b3b892 100644 --- a/model/config/file_test.go +++ b/model/config/file_test.go @@ -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) diff --git a/model/config/struct.go b/model/config/struct.go index a5a66d3..3eafb60 100644 --- a/model/config/struct.go +++ b/model/config/struct.go @@ -1,5 +1,19 @@ package config +import "dev.sum7.eu/genofire/yaja/model" + type Config struct { - TLSDir string `toml:"tlsdir"` + 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"` } diff --git a/model/jid.go b/model/jid.go new file mode 100644 index 0000000..4a8d1b9 --- /dev/null +++ b/model/jid.go @@ -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 +} diff --git a/model/jid_test.go b/model/jid_test.go new file mode 100644 index 0000000..3d05224 --- /dev/null +++ b/model/jid_test.go @@ -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ßball@example.com": &JID{ + Local: "fußball", + Domain: "example.com", + }, + "π@example.com": &JID{ + Local: "π", + Domain: "example.com", + }, + "Σ@example.com/foo": &JID{ + Local: "Σ", + Domain: "example.com", + Resource: "foo", + }, + "σ@example.com/foo": &JID{ + Local: "σ", + Domain: "example.com", + Resource: "foo", + }, + "ς@example.com/foo": &JID{ + Local: "ς", + Domain: "example.com", + Resource: "foo", + }, + "king@example.com/♚": &JID{ + Local: "king", + Domain: "example.com", + Resource: "♚", + }, + "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Ⅳ@example.com": nil, -- ignore for easier implementation + // "♚@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)) +}