Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Commit

Permalink
Util tweaks
Browse files Browse the repository at this point in the history
- Move rand util into rand.go
- RandString changed to RandomID and uses 256 bits
- Move string util into strings.go
  • Loading branch information
gabriel committed May 9, 2016
1 parent 72317fc commit e222a80
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 46 deletions.
4 changes: 2 additions & 2 deletions util/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func RemoveFileAtPath(path string) {
// openTempFile(path.Join(os.TempDir(), "foo"), "", 0600) => "/tmp/foo.RCG2KUSCGYOO3PCKNWQHBOXBKACOPIKL"
//
func openTempFile(prefix string, suffix string, mode os.FileMode) (string, *os.File, error) {
filename, err := RandString(prefix, 20)
filename, err := RandomID(prefix)
if err != nil {
return "", nil, err
}
Expand Down Expand Up @@ -171,7 +171,7 @@ func TempPath(tempDir string, prefix string) string {
if tempDir == "" {
tempDir = os.TempDir()
}
filename, err := RandString(prefix, 20)
filename, err := RandomID(prefix)
if err != nil {
// We had an error getting random bytes, we'll use current nanoseconds
filename = fmt.Sprintf("%s%d", prefix, time.Now().UnixNano())
Expand Down
2 changes: 1 addition & 1 deletion util/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestTempPathValid(t *testing.T) {
tempPath := TempPath("", "TempPrefix.")
t.Logf("Temp path: %s", tempPath)
assert.True(t, strings.HasPrefix(filepath.Base(tempPath), "TempPrefix."))
assert.Equal(t, len(filepath.Base(tempPath)), 43)
assert.Equal(t, len(filepath.Base(tempPath)), 63)
}

func TestTempPathRandFail(t *testing.T) {
Expand Down
21 changes: 6 additions & 15 deletions util/util.go → util/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,20 @@ import (
"strings"
)

// JoinPredicate joins strings with predicate
func JoinPredicate(arr []string, delimeter string, f func(s string) bool) string {
arrNew := make([]string, 0, len(arr))
for _, s := range arr {
if f(s) {
arrNew = append(arrNew, s)
}
}
return strings.Join(arrNew, delimeter)
}
var randRead = rand.Read

// RandString returns random (base32) string with prefix.
func RandString(prefix string, numbytes int) (string, error) {
buf, err := RandBytes(numbytes)
// RandomID returns random ID (base32) string with prefix, using 256 bits as
// recommended by tptacek: https://gist.github.com/tqbf/be58d2d39690c3b366ad
func RandomID(prefix string) (string, error) {
buf, err := RandBytes(32)
if err != nil {
return "", err
}
str := prefix + base32.StdEncoding.EncodeToString(buf)
str = strings.Replace(str, "=", "", -1)
return str, nil
}

var randRead = rand.Read

// RandBytes returns random bytes of length
func RandBytes(length int) ([]byte, error) {
buf := make([]byte, length)
Expand Down
23 changes: 23 additions & 0 deletions util/rand_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.

package util

import (
"strings"
"testing"
)

func TestRandString(t *testing.T) {
s, err := RandomID("prefix.")
t.Logf("Rand string: %s", s)
if err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(s, "prefix.") {
t.Errorf("Invalid prefix: %s", s)
}
if len(s)-len("prefix.") != 52 {
t.Errorf("Invalid length: %s (%d)", s, len(s))
}
}
17 changes: 17 additions & 0 deletions util/strings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.

package util

import "strings"

// JoinPredicate joins strings with predicate
func JoinPredicate(arr []string, delimeter string, f func(s string) bool) string {
arrNew := make([]string, 0, len(arr))
for _, s := range arr {
if f(s) {
arrNew = append(arrNew, s)
}
}
return strings.Join(arrNew, delimeter)
}
17 changes: 17 additions & 0 deletions util/strings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.

package util

import (
"strings"
"testing"
)

func TestJoinPredicate(t *testing.T) {
f := func(s string) bool { return strings.HasPrefix(s, "f") }
s := JoinPredicate([]string{"foo", "bar", "faa"}, "-", f)
if s != "foo-faa" {
t.Errorf("Unexpected output: %s", s)
}
}
29 changes: 1 addition & 28 deletions util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,6 @@

package util

import (
"strings"
"testing"

"github.com/keybase/go-logging"
)
import "github.com/keybase/go-logging"

var testLog = logging.Logger{Module: "test"}

func TestJoinPredicate(t *testing.T) {
f := func(s string) bool { return strings.HasPrefix(s, "f") }
s := JoinPredicate([]string{"foo", "bar", "faa"}, "-", f)
if s != "foo-faa" {
t.Errorf("Unexpected output: %s", s)
}
}

func TestRandString(t *testing.T) {
s, err := RandString("prefix", 20)
t.Logf("Rand string: %s", s)
if err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(s, "prefix") {
t.Errorf("Invalid prefix: %s", s)
}
if len(s) != 38 {
t.Errorf("Invalid length: %s (%d)", s, len(s))
}
}

0 comments on commit e222a80

Please sign in to comment.