diff --git a/models/models.go b/models/models.go index ac69a778e7..f9ce67d935 100644 --- a/models/models.go +++ b/models/models.go @@ -97,6 +97,21 @@ func LoadConfigs() { DbCfg.Path = sec.Key("PATH").MustString("data/gogs.db") } +// parsePostgreSQLHostPort parses given input in various forms defined in +// https://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-CONNSTRING +// and returns proper host and port number. +func parsePostgreSQLHostPort(info string) (string, string) { + host, port := "127.0.0.1", "5432" + if strings.Contains(info, ":") && !strings.HasSuffix(info, "]") { + idx := strings.LastIndex(info, ":") + host = info[:idx] + port = info[idx+1:] + } else if len(info) > 0 { + host = info + } + return host, port +} + func getEngine() (*xorm.Engine, error) { connStr := "" var Param string = "?" diff --git a/models/models_test.go b/models/models_test.go new file mode 100644 index 0000000000..f68590c509 --- /dev/null +++ b/models/models_test.go @@ -0,0 +1,33 @@ +// Copyright 2016 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package models + +import ( + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func Test_parsePostgreSQLHostPort(t *testing.T) { + testSuites := []struct { + input string + host, port string + }{ + {"127.0.0.1:1234", "127.0.0.1", "1234"}, + {"127.0.0.1", "127.0.0.1", "5432"}, + {"[::1]:1234", "[::1]", "1234"}, + {"[::1]", "[::1]", "5432"}, + {"/tmp/pg.sock:1234", "/tmp/pg.sock", "1234"}, + {"/tmp/pg.sock", "/tmp/pg.sock", "5432"}, + } + + Convey("Parse PostgreSQL host and port", t, func() { + for _, suite := range testSuites { + host, port := parsePostgreSQLHostPort(suite.input) + So(host, ShouldEqual, suite.host) + So(port, ShouldEqual, suite.port) + } + }) +} diff --git a/models/ssh_key_test.go b/models/ssh_key_test.go index 61f2e18be4..2f7a572c44 100644 --- a/models/ssh_key_test.go +++ b/models/ssh_key_test.go @@ -1,7 +1,12 @@ +// Copyright 2016 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + package models import ( "fmt" + "strings" "testing" . "github.com/smartystreets/goconvey/convey" @@ -37,7 +42,13 @@ func Test_SSHParsePublicKey(t *testing.T) { So(lengthN, ShouldEqual, key.length) keyTypeK, lengthK, errK := SSHKeyGenParsePublicKey(key.content) - So(errK, ShouldBeNil) + if errK != nil { + // Some server just does not support ecdsa format. + if strings.Contains(errK.Error(), "line 1 too long:") { + continue + } + So(errK, ShouldBeNil) + } So(keyTypeK, ShouldEqual, key.typeName) So(lengthK, ShouldEqual, key.length) }