Skip to content

Commit

Permalink
move MakeRandomString to strutil
Browse files Browse the repository at this point in the history
  • Loading branch information
mvo5 committed Mar 1, 2016
1 parent 7c18dba commit 18be8ef
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 43 deletions.
4 changes: 2 additions & 2 deletions classic/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
23 changes: 0 additions & 23 deletions helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
12 changes: 0 additions & 12 deletions helpers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package helpers
import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions oauth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"fmt"
"time"

"github.com/ubuntu-core/snappy/helpers"
"github.com/ubuntu-core/snappy/strutil"
)

// Token contains the sso token
Expand Down Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions osutil/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions osutil/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"os"
"path/filepath"

"github.com/ubuntu-core/snappy/helpers"
"github.com/ubuntu-core/snappy/strutil"

. "gopkg.in/check.v1"
)
Expand Down Expand Up @@ -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)

Expand Down
46 changes: 46 additions & 0 deletions strutil/strutil.go
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*
*/

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
}
44 changes: 44 additions & 0 deletions strutil/strutil_test.go
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*
*/

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")
}

0 comments on commit 18be8ef

Please sign in to comment.