diff --git a/classic/run.go b/classic/run.go index 2b9b09b9a9d..4cacc31db5c 100644 --- a/classic/run.go +++ b/classic/run.go @@ -27,7 +27,7 @@ import ( "time" "github.com/ubuntu-core/snappy/dirs" - "github.com/ubuntu-core/snappy/helpers" + "github.com/ubuntu-core/snappy/strutil" ) type bindMount struct { @@ -51,7 +51,7 @@ var bindMountDirs = []bindMount{ func genClassicScopeName() string { now := time.Now() ti := fmt.Sprintf("%4d-%02d-%02d_%02d:%02d:%02d", now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second()) - return fmt.Sprintf("snappy-classic_%s_%s.scope", ti, helpers.MakeRandomString(5)) + return fmt.Sprintf("snappy-classic_%s_%s.scope", ti, strutil.MakeRandomString(5)) } func runInClassicEnv(cmdStr ...string) error { diff --git a/helpers/helpers.go b/helpers/helpers.go index 0a6adc2c392..e8bc3db1244 100644 --- a/helpers/helpers.go +++ b/helpers/helpers.go @@ -21,20 +21,13 @@ package helpers import ( "bytes" - "math/rand" "reflect" "strings" "text/template" - "time" "github.com/ubuntu-core/snappy/logger" ) -func init() { - // golang does not init Seed() itself - rand.Seed(time.Now().UTC().UnixNano()) -} - // MakeMapFromEnvList takes a string list of the form "key=value" // and returns a map[string]string from that list // This is useful for os.Environ() manipulation @@ -50,22 +43,6 @@ func MakeMapFromEnvList(env []string) map[string]string { return envMap } -const letters = "BCDFGHJKLMNPQRSTVWXYbcdfghjklmnpqrstvwxy0123456789" - -// MakeRandomString returns a random string of length length -// -// The vowels are omited to avoid that words are created by pure -// chance. Numbers are included. -func MakeRandomString(length int) string { - - out := "" - for i := 0; i < length; i++ { - out += string(letters[rand.Intn(len(letters))]) - } - - return out -} - // Getattr get the attribute of the given name func Getattr(i interface{}, name string) interface{} { v := reflect.ValueOf(i) diff --git a/helpers/helpers_test.go b/helpers/helpers_test.go index 8e630284559..0a95f447230 100644 --- a/helpers/helpers_test.go +++ b/helpers/helpers_test.go @@ -22,7 +22,6 @@ package helpers import ( "fmt" "io/ioutil" - "math/rand" "os" "os/exec" "path/filepath" @@ -57,17 +56,6 @@ func (ts *HTestSuite) TestMakeMapFromEnvListInvalidInput(c *C) { c.Assert(envMap, DeepEquals, map[string]string(nil)) } -func (ts *HTestSuite) TestMakeRandomString(c *C) { - // for our tests - rand.Seed(1) - - s1 := MakeRandomString(10) - c.Assert(s1, Equals, "pw7MpXh0JB") - - s2 := MakeRandomString(5) - c.Assert(s2, Equals, "4PQyl") -} - func skipOnMissingDevKmsg(c *C) { _, err := os.Stat("/dev/kmsg") if err != nil { diff --git a/oauth/oauth.go b/oauth/oauth.go index 6d53eaa5018..7c663de0065 100644 --- a/oauth/oauth.go +++ b/oauth/oauth.go @@ -24,7 +24,7 @@ import ( "fmt" "time" - "github.com/ubuntu-core/snappy/helpers" + "github.com/ubuntu-core/snappy/strutil" ) // Token contains the sso token @@ -72,7 +72,7 @@ func quote(s string) string { func MakePlaintextSignature(token *Token) string { // hrm, rfc5849 says that nonce, timestamp are not used for PLAINTEXT // but our sso server is unhappy without, so - nonce := helpers.MakeRandomString(60) + nonce := strutil.MakeRandomString(60) timestamp := time.Now().Unix() s := fmt.Sprintf(`OAuth oauth_nonce="%s", oauth_timestamp="%v", oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="%s", oauth_token="%s", oauth_signature="%s%%26%s"`, nonce, timestamp, quote(token.ConsumerKey), quote(token.TokenKey), quote(token.ConsumerSecret), quote(token.TokenSecret)) diff --git a/osutil/io.go b/osutil/io.go index 346881bcbc3..b0f500515cd 100644 --- a/osutil/io.go +++ b/osutil/io.go @@ -23,7 +23,7 @@ import ( "os" "path/filepath" - "github.com/ubuntu-core/snappy/helpers" + "github.com/ubuntu-core/snappy/strutil" ) // AtomicWriteFlags are a bitfield of flags for AtomicWriteFile @@ -49,7 +49,7 @@ func AtomicWriteFile(filename string, data []byte, perm os.FileMode, flags Atomi } } } - tmp := filename + "." + helpers.MakeRandomString(12) + tmp := filename + "." + strutil.MakeRandomString(12) // XXX: if go switches to use aio_fsync, we need to open the dir for writing dir, err := os.Open(filepath.Dir(filename)) diff --git a/osutil/io_test.go b/osutil/io_test.go index 537bea2c1d9..5c403aced16 100644 --- a/osutil/io_test.go +++ b/osutil/io_test.go @@ -25,7 +25,7 @@ import ( "os" "path/filepath" - "github.com/ubuntu-core/snappy/helpers" + "github.com/ubuntu-core/snappy/strutil" . "gopkg.in/check.v1" ) @@ -163,7 +163,7 @@ func (ts *AtomicWriteTestSuite) TestAtomicWriteFileNoOverwriteTmpExisting(c *C) tmpdir := c.MkDir() // ensure we always get the same result rand.Seed(1) - expectedRandomness := helpers.MakeRandomString(12) + expectedRandomness := strutil.MakeRandomString(12) // ensure we always get the same result rand.Seed(1) diff --git a/strutil/strutil.go b/strutil/strutil.go new file mode 100644 index 00000000000..d7a95708e2c --- /dev/null +++ b/strutil/strutil.go @@ -0,0 +1,46 @@ +// -*- Mode: Go; indent-tabs-mode: t -*- + +/* + * Copyright (C) 2014-2015 Canonical Ltd + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 3 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package strutil + +import ( + "math/rand" + "time" +) + +func init() { + // golang does not init Seed() itself + rand.Seed(time.Now().UTC().UnixNano()) +} + +const letters = "BCDFGHJKLMNPQRSTVWXYbcdfghjklmnpqrstvwxy0123456789" + +// MakeRandomString returns a random string of length length +// +// The vowels are omited to avoid that words are created by pure +// chance. Numbers are included. +func MakeRandomString(length int) string { + + out := "" + for i := 0; i < length; i++ { + out += string(letters[rand.Intn(len(letters))]) + } + + return out +} diff --git a/strutil/strutil_test.go b/strutil/strutil_test.go new file mode 100644 index 00000000000..bda68f5581e --- /dev/null +++ b/strutil/strutil_test.go @@ -0,0 +1,44 @@ +// -*- Mode: Go; indent-tabs-mode: t -*- + +/* + * Copyright (C) 2014-2015 Canonical Ltd + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 3 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package strutil + +import ( + "math/rand" + "testing" + + . "gopkg.in/check.v1" +) + +func Test(t *testing.T) { TestingT(t) } + +type MakeRandomStringTestSuite struct{} + +var _ = Suite(&MakeRandomStringTestSuite{}) + +func (ts *MakeRandomStringTestSuite) TestMakeRandomString(c *C) { + // for our tests + rand.Seed(1) + + s1 := MakeRandomString(10) + c.Assert(s1, Equals, "pw7MpXh0JB") + + s2 := MakeRandomString(5) + c.Assert(s2, Equals, "4PQyl") +}