From 49f30b159775c9f43f832762c21607f2a3a1db11 Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Sat, 8 Nov 2014 18:45:30 -0800 Subject: [PATCH 1/5] added random to godeps --- Godeps/Godeps.json | 4 + .../jbenet/go-random/random/random.go | 88 +++++++++++++++++ .../jbenet/go-random/random/random_test.go | 96 +++++++++++++++++++ test/random-dep.go | 10 ++ 4 files changed, 198 insertions(+) create mode 100644 Godeps/_workspace/src/github.com/jbenet/go-random/random/random.go create mode 100644 Godeps/_workspace/src/github.com/jbenet/go-random/random/random_test.go create mode 100644 test/random-dep.go diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 8960a4c94..a0ea280b3 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -110,6 +110,10 @@ "Comment": "0.1.0-5-g1976046", "Rev": "1976046c2b0db0b668791b3e541d76a38b7c1af7" }, + { + "ImportPath": "github.com/jbenet/go-random/random", + "Rev": "623fff67b06299c43b5542d42c427c7c2855a362" + }, { "ImportPath": "github.com/kr/binarydist", "Rev": "9955b0ab8708602d411341e55fffd7e0700f86bd" diff --git a/Godeps/_workspace/src/github.com/jbenet/go-random/random/random.go b/Godeps/_workspace/src/github.com/jbenet/go-random/random/random.go new file mode 100644 index 000000000..7f8e1adb1 --- /dev/null +++ b/Godeps/_workspace/src/github.com/jbenet/go-random/random/random.go @@ -0,0 +1,88 @@ +package main + +import ( + "bytes" + randcrypto "crypto/rand" + "fmt" + "io" + randmath "math/rand" + "os" + "strconv" +) + +func main() { + l := len(os.Args) + if l != 2 && l != 3 { + usageError() + } + + count, err := strconv.ParseInt(os.Args[1], 10, 64) + if err != nil { + usageError() + } + + if l == 2 { + err = writeRandomBytes(count, os.Stdout) + } else { + seed, err2 := strconv.ParseInt(os.Args[2], 10, 64) + if err2 != nil { + usageError() + } + err = writePseudoRandomBytes(count, os.Stdout, seed) + } + + if err != nil { + die(err) + } +} + +func usageError() { + fmt.Fprintf(os.Stderr, "Usage: %s []\n", os.Args[0]) + fmt.Fprintf(os.Stderr, "If is given, output pseudo random bytes made from (from Go's math/rand)\n") + fmt.Fprintf(os.Stderr, "Otherwise, output random bytes (from Go's crypto/rand)\n") + os.Exit(-1) +} + +func die(err error) { + fmt.Fprintf(os.Stderr, "Error: %v", err) + os.Exit(-1) +} + +func writeRandomBytes(count int64, w io.Writer) error { + r := &io.LimitedReader{R: randcrypto.Reader, N: count} + _, err := io.Copy(w, r) + return err +} + +func writePseudoRandomBytes(count int64, w io.Writer, seed int64) error { + randmath.Seed(seed) + + // Configurable buffer size + bufsize := int64(1024 * 1024 * 4) + b := make([]byte, bufsize) + + for count > 0 { + if bufsize > count { + bufsize = count + b = b[:bufsize] + } + + var n int64 + for i := int64(0); i < bufsize; i++ { + n = randmath.Int63() + for j := 0; j < 8 && i < bufsize; j++ { + b[i] = byte(n & 0xff) + n >>= 8 + i++ + } + } + count = count - bufsize + + r := bytes.NewReader(b) + _, err := io.Copy(w, r) + if err != nil { + return err + } + } + return nil +} diff --git a/Godeps/_workspace/src/github.com/jbenet/go-random/random/random_test.go b/Godeps/_workspace/src/github.com/jbenet/go-random/random/random_test.go new file mode 100644 index 000000000..5b40eff46 --- /dev/null +++ b/Godeps/_workspace/src/github.com/jbenet/go-random/random/random_test.go @@ -0,0 +1,96 @@ +package main + +import ( + "bytes" + "io/ioutil" + "testing" + "time" +) + +func TestPseudoRandom(t *testing.T) { + + var testCases = []int{ + 1024, + 187654, + 1048576, + 4932132, + } + + for _, size := range testCases { + var buf bytes.Buffer + err := writePseudoRandomBytes(int64(size), &buf, int64(time.Now().UnixNano())) + if err != nil { + t.Fatal(err) + } + + if buf.Len() != size { + t.Fatal("buffer not of the right size: %d != %d", buf.Len(), size) + } + } +} + +func TestPseudoRandomSeed(t *testing.T) { + + var first []byte + var size = int64(1024 * 4) + seed := time.Now().UnixNano() + + for i := 0; i < 100; i++ { + var bufs bytes.Buffer + var bufr bytes.Buffer + + if err := writePseudoRandomBytes(size, &bufs, seed); err != nil { + t.Fatal(err) + } + + if err := writePseudoRandomBytes(size, &bufr, time.Now().UnixNano()); err != nil { + t.Fatal(err) + } + + if bufs.Len() != int(size) { + t.Fatal("buffer not of the right size: %d != %d", bufs.Len(), size) + } + if bufr.Len() != int(size) { + t.Fatal("buffer not of the right size: %d != %d", bufr.Len(), size) + } + + if first == nil { + first = bufs.Bytes() + } else if !bytes.Equal(first, bufs.Bytes()) { + t.Fatal("seeded constructed different bytes") + } + + if bytes.Equal(first, bufr.Bytes()) { + t.Fatal("non-seeded constructed same bytes") + } + } +} + +func TestCryptoRandom(t *testing.T) { + + var testCases = []int{ + 1024, + 187654, + 1048576, + } + + for _, size := range testCases { + var buf bytes.Buffer + err := writeRandomBytes(int64(size), &buf) + if err != nil { + t.Fatal(err) + } + + if buf.Len() != size { + t.Fatal("buffer not of the right size: %d != %d", buf.Len(), size) + } + } +} + +func BenchmarkCryptoRandom(b *testing.B) { + writeRandomBytes(int64(b.N), ioutil.Discard) +} + +func BenchmarkPseudoRandom(b *testing.B) { + writePseudoRandomBytes(int64(b.N), ioutil.Discard, time.Now().UnixNano()) +} diff --git a/test/random-dep.go b/test/random-dep.go new file mode 100644 index 000000000..7cf145161 --- /dev/null +++ b/test/random-dep.go @@ -0,0 +1,10 @@ +// package randomdep is here to introduce a dependency in random for godep to +// function properly. this way we can keep go-random vendored and not +// accidentally break our tests when we change it. +package randomdep + +import ( + random "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random/random" +) + +var _ = random From 5680c112ef25d80e211863144c616d0db94cce3a Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Sat, 8 Nov 2014 18:49:28 -0800 Subject: [PATCH 2/5] test: install random from vendored code --- test/Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/Makefile b/test/Makefile index 489beaab6..617321a7c 100644 --- a/test/Makefile +++ b/test/Makefile @@ -6,6 +6,7 @@ T = $(sort $(wildcard t[0-9][0-9][0-9][0-9]-*.sh)) SHARNESS = sharness/sharness.sh +RANDOMSRC = Godeps/_workspace/src/github.com/jbenet/go-random/random all: clean deps $(T) aggregate @@ -35,8 +36,7 @@ ipfs: random: @echo "*** installing $@ ***" - go get github.com/jbenet/go-random/random - go install github.com/jbenet/go-random/random - cp `which random` random + go build ../$(RANDOMSRC) + cp ../$(RANDOMSRC)/random random .PHONY: all clean $(T) aggregate ipfs random From 842de46ccf4aafd193ce8e655b41b04ac5adda66 Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Sat, 8 Nov 2014 18:56:43 -0800 Subject: [PATCH 3/5] test: moved installed things into own dirs I moved installed things into own dirs bin and lib. @chriscool sorry to move things around again, the top level test dir was getting a bit clutterd. --- test/.gitignore | 4 +++- test/Makefile | 13 +++++++------ test/{ => lib}/install-sharness.sh | 5 +++-- test/{ => lib}/random-dep.go | 0 test/{ => lib}/test-aggregate-results.sh | 2 +- test/{ => lib}/test-lib.sh | 6 +++--- test/t0010-basic-commands.sh | 2 +- test/t0020-init.sh | 2 +- test/t0030-mount.sh | 2 +- test/t0040-add-and-cat.sh | 2 +- 10 files changed, 21 insertions(+), 17 deletions(-) rename test/{ => lib}/install-sharness.sh (87%) rename test/{ => lib}/random-dep.go (100%) rename test/{ => lib}/test-aggregate-results.sh (86%) rename test/{ => lib}/test-lib.sh (93%) diff --git a/test/.gitignore b/test/.gitignore index 8612fc160..7c23cc5f7 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -1,3 +1,5 @@ -sharness/ +lib/sharness/ +bin/ipfs +bin/random test-results/ trash directory.*.sh/ diff --git a/test/Makefile b/test/Makefile index 617321a7c..100401f03 100644 --- a/test/Makefile +++ b/test/Makefile @@ -5,7 +5,7 @@ # T = $(sort $(wildcard t[0-9][0-9][0-9][0-9]-*.sh)) -SHARNESS = sharness/sharness.sh +SHARNESS = lib/sharness/sharness.sh RANDOMSRC = Godeps/_workspace/src/github.com/jbenet/go-random/random all: clean deps $(T) aggregate @@ -20,23 +20,24 @@ $(T): aggregate: @echo "*** $@ ***" - ./test-aggregate-results.sh + lib/test-aggregate-results.sh deps: $(SHARNESS) ipfs random $(SHARNESS): @echo "*** installing $@ ***" - ./install-sharness.sh + lib/install-sharness.sh # phony to ensure we re-build it every time we run tests ipfs: @echo "*** installing $@ ***" + mkdir -p bin cd ../cmd/ipfs && go build - cp ../cmd/ipfs/ipfs ipfs + cp ../cmd/ipfs/ipfs bin/ipfs random: @echo "*** installing $@ ***" - go build ../$(RANDOMSRC) - cp ../$(RANDOMSRC)/random random + mkdir -p bin + go build -o bin/random ../$(RANDOMSRC) .PHONY: all clean $(T) aggregate ipfs random diff --git a/test/install-sharness.sh b/test/lib/install-sharness.sh similarity index 87% rename from test/install-sharness.sh rename to test/lib/install-sharness.sh index 2caf97712..a0a74b178 100755 --- a/test/install-sharness.sh +++ b/test/lib/install-sharness.sh @@ -8,6 +8,7 @@ # settings version=50229a79ba22b2f13ccd82451d86570fecbd194c urlprefix=https://raw.githubusercontent.com/mlafeldt/sharness/$version +installpath=lib/sharness # files to download sfile=sharness.sh @@ -39,8 +40,8 @@ verified_download() { return 0 } -mkdir -p sharness || die "Could not create 'sharness' directory" -cd sharness || die "Could not cd into 'sharness' directory" +mkdir -p $installpath || die "Could not create 'sharness' directory" +cd $installpath || die "Could not cd into 'sharness' directory" verified_download "$sfile" "$shash"; sok=$? verified_download "$afile" "$ahash"; aok=$? diff --git a/test/random-dep.go b/test/lib/random-dep.go similarity index 100% rename from test/random-dep.go rename to test/lib/random-dep.go diff --git a/test/test-aggregate-results.sh b/test/lib/test-aggregate-results.sh similarity index 86% rename from test/test-aggregate-results.sh rename to test/lib/test-aggregate-results.sh index 10838815a..c2ff76ca1 100755 --- a/test/test-aggregate-results.sh +++ b/test/lib/test-aggregate-results.sh @@ -6,7 +6,7 @@ # MIT Licensed; see the LICENSE file in this repository. # -SHARNESS_AGGREGATE="sharness/aggregate-results.sh" +SHARNESS_AGGREGATE="lib/sharness/aggregate-results.sh" test -f "$SHARNESS_AGGREGATE" || { echo >&2 "Cannot find: $SHARNESS_AGGREGATE" diff --git a/test/test-lib.sh b/test/lib/test-lib.sh similarity index 93% rename from test/test-lib.sh rename to test/lib/test-lib.sh index a8877418b..87a4b1059 100644 --- a/test/test-lib.sh +++ b/test/lib/test-lib.sh @@ -9,16 +9,16 @@ # use the ipfs tool to test against # add current directory to path, for ipfs tool. -PATH=$(pwd):${PATH} +PATH=$(pwd)/bin:${PATH} # assert the `ipfs` we're using is the right one. -if test `which ipfs` != $(pwd)/ipfs; then +if test `which ipfs` != $(pwd)/bin/ipfs; then echo >&2 "Cannot find the tests' local ipfs tool." echo >&2 "Please check test and ipfs tool installation." exit 1 fi -SHARNESS_LIB="sharness/sharness.sh" +SHARNESS_LIB="lib/sharness/sharness.sh" . "$SHARNESS_LIB" || { echo >&2 "Cannot source: $SHARNESS_LIB" diff --git a/test/t0010-basic-commands.sh b/test/t0010-basic-commands.sh index dd63dd2c4..e007ae980 100755 --- a/test/t0010-basic-commands.sh +++ b/test/t0010-basic-commands.sh @@ -6,7 +6,7 @@ test_description="Test installation and some basic commands" -. ./test-lib.sh +. lib/test-lib.sh test_expect_success "current dir is writable" ' echo "It works!" >test.txt diff --git a/test/t0020-init.sh b/test/t0020-init.sh index 01601420c..7ca9d1653 100755 --- a/test/t0020-init.sh +++ b/test/t0020-init.sh @@ -6,7 +6,7 @@ test_description="Test init command" -. ./test-lib.sh +. lib/test-lib.sh test_expect_success "ipfs init succeeds" ' export IPFS_DIR="$(pwd)/.go-ipfs" && diff --git a/test/t0030-mount.sh b/test/t0030-mount.sh index ddb187f72..7cc46938d 100755 --- a/test/t0030-mount.sh +++ b/test/t0030-mount.sh @@ -6,7 +6,7 @@ test_description="Test mount command" -. ./test-lib.sh +. lib/test-lib.sh # if in travis CI, dont test mount (no fuse) if ! test_have_prereq FUSE; then diff --git a/test/t0040-add-and-cat.sh b/test/t0040-add-and-cat.sh index 3091afc2e..caa709dc5 100755 --- a/test/t0040-add-and-cat.sh +++ b/test/t0040-add-and-cat.sh @@ -6,7 +6,7 @@ test_description="Test add and cat commands" -. ./test-lib.sh +. lib/test-lib.sh test_launch_ipfs_mount From 4bf3e967d8cc61776bb028bb5103d386ee89a792 Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Sat, 8 Nov 2014 19:32:56 -0800 Subject: [PATCH 4/5] test/t0040-add-and-cat updated hashes (new random) --- test/t0040-add-and-cat.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/t0040-add-and-cat.sh b/test/t0040-add-and-cat.sh index caa709dc5..240346009 100755 --- a/test/t0040-add-and-cat.sh +++ b/test/t0040-add-and-cat.sh @@ -47,7 +47,7 @@ test_expect_success "generate 100MB file using go-random" ' ' test_expect_success "sha1 of the file looks ok" ' - echo "54dc0dbbc353b2ffb745285793f89af0c9d98449 mountdir/bigfile" >sha1_expected && + echo "ae986dd159e4f014aee7409cdc2001ea74f618d1 mountdir/bigfile" >sha1_expected && shasum mountdir/bigfile >sha1_actual && test_cmp sha1_expected sha1_actual ' @@ -57,7 +57,7 @@ test_expect_success "ipfs add bigfile succeeds" ' ' test_expect_success "ipfs add bigfile output looks good" ' - HASH="QmeZVkWkDu4W1vxWdDgUbqKYba9K3u45hJEdPA4Wr2sHZz" && + HASH="QmVm3Da371opC3hpsCLuYSozdyM6wRvu9UoUqoyW8u4LRq" && echo "added $HASH $(pwd)/mountdir/bigfile" >expected && test_cmp expected actual ' @@ -67,7 +67,7 @@ test_expect_success "ipfs cat succeeds" ' ' test_expect_success "ipfs cat output looks good" ' - echo "54dc0dbbc353b2ffb745285793f89af0c9d98449 -" >sha1_expected && + echo "ae986dd159e4f014aee7409cdc2001ea74f618d1 -" >sha1_expected && test_cmp sha1_expected sha1_actual ' From 1e434ef3c46fe263cade622558311a032aee1f3c Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Sat, 8 Nov 2014 20:01:38 -0800 Subject: [PATCH 5/5] vendor: updated random + fixed test import There is a dummy test import: test/lib/random-dep.go Because godep doen't yet vendor binaries nicely. --- Godeps/Godeps.json | 4 +- .../src/github.com/jbenet/go-random/LICENSE | 21 ++++++++ .../src/github.com/jbenet/go-random/README.md | 29 +++++++++++ .../src/github.com/jbenet/go-random/lib.go | 47 ++++++++++++++++++ .../jbenet/go-random/random/.gitignore | 1 + .../jbenet/go-random/random/random.go | 49 ++----------------- .../go-random/{random => }/random_test.go | 14 +++--- test/lib/random-dep.go | 4 +- 8 files changed, 112 insertions(+), 57 deletions(-) create mode 100644 Godeps/_workspace/src/github.com/jbenet/go-random/LICENSE create mode 100644 Godeps/_workspace/src/github.com/jbenet/go-random/README.md create mode 100644 Godeps/_workspace/src/github.com/jbenet/go-random/lib.go create mode 100644 Godeps/_workspace/src/github.com/jbenet/go-random/random/.gitignore rename Godeps/_workspace/src/github.com/jbenet/go-random/{random => }/random_test.go (80%) diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index a0ea280b3..53ce7e3c0 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -111,8 +111,8 @@ "Rev": "1976046c2b0db0b668791b3e541d76a38b7c1af7" }, { - "ImportPath": "github.com/jbenet/go-random/random", - "Rev": "623fff67b06299c43b5542d42c427c7c2855a362" + "ImportPath": "github.com/jbenet/go-random", + "Rev": "e4585173eb8c47eea36c3dbff22f26f3f94d3586" }, { "ImportPath": "github.com/kr/binarydist", diff --git a/Godeps/_workspace/src/github.com/jbenet/go-random/LICENSE b/Godeps/_workspace/src/github.com/jbenet/go-random/LICENSE new file mode 100644 index 000000000..c7386b3c9 --- /dev/null +++ b/Godeps/_workspace/src/github.com/jbenet/go-random/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 Juan Batiz-Benet + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/Godeps/_workspace/src/github.com/jbenet/go-random/README.md b/Godeps/_workspace/src/github.com/jbenet/go-random/README.md new file mode 100644 index 000000000..3b0b20ccb --- /dev/null +++ b/Godeps/_workspace/src/github.com/jbenet/go-random/README.md @@ -0,0 +1,29 @@ +# go-random outputs randomness + +This is a unix util that outputs randomness. +It is a thin wrapper around `crypto/rand`. +It aims to be portable (though it may not yet be). + +### Install + +```sh +go install github.com/jbenet/go-random/random +``` + +(The extra /random is there because go get is stupidly too proscriptive about +package/repository names and I don't yet know how to change the default binary +output name) + +### Usage: + +``` +> random +Usage: random +Print random bytes (from Go's crypto/rand) +> random 6 +2q���# +``` + +### License + +MIT diff --git a/Godeps/_workspace/src/github.com/jbenet/go-random/lib.go b/Godeps/_workspace/src/github.com/jbenet/go-random/lib.go new file mode 100644 index 000000000..54867a752 --- /dev/null +++ b/Godeps/_workspace/src/github.com/jbenet/go-random/lib.go @@ -0,0 +1,47 @@ +package random + +import ( + "bytes" + randcrypto "crypto/rand" + "io" + randmath "math/rand" +) + +func WriteRandomBytes(count int64, w io.Writer) error { + r := &io.LimitedReader{R: randcrypto.Reader, N: count} + _, err := io.Copy(w, r) + return err +} + +func WritePseudoRandomBytes(count int64, w io.Writer, seed int64) error { + randmath.Seed(seed) + + // Configurable buffer size + bufsize := int64(1024 * 1024 * 4) + b := make([]byte, bufsize) + + for count > 0 { + if bufsize > count { + bufsize = count + b = b[:bufsize] + } + + var n int64 + for i := int64(0); i < bufsize; i++ { + n = randmath.Int63() + for j := 0; j < 8 && i < bufsize; j++ { + b[i] = byte(n & 0xff) + n >>= 8 + i++ + } + } + count = count - bufsize + + r := bytes.NewReader(b) + _, err := io.Copy(w, r) + if err != nil { + return err + } + } + return nil +} diff --git a/Godeps/_workspace/src/github.com/jbenet/go-random/random/.gitignore b/Godeps/_workspace/src/github.com/jbenet/go-random/random/.gitignore new file mode 100644 index 000000000..a1e4bc59a --- /dev/null +++ b/Godeps/_workspace/src/github.com/jbenet/go-random/random/.gitignore @@ -0,0 +1 @@ +random diff --git a/Godeps/_workspace/src/github.com/jbenet/go-random/random/random.go b/Godeps/_workspace/src/github.com/jbenet/go-random/random/random.go index 7f8e1adb1..0222c59d3 100644 --- a/Godeps/_workspace/src/github.com/jbenet/go-random/random/random.go +++ b/Godeps/_workspace/src/github.com/jbenet/go-random/random/random.go @@ -1,13 +1,11 @@ package main import ( - "bytes" - randcrypto "crypto/rand" "fmt" - "io" - randmath "math/rand" "os" "strconv" + + random "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random" ) func main() { @@ -22,13 +20,13 @@ func main() { } if l == 2 { - err = writeRandomBytes(count, os.Stdout) + err = random.WriteRandomBytes(count, os.Stdout) } else { seed, err2 := strconv.ParseInt(os.Args[2], 10, 64) if err2 != nil { usageError() } - err = writePseudoRandomBytes(count, os.Stdout, seed) + err = random.WritePseudoRandomBytes(count, os.Stdout, seed) } if err != nil { @@ -47,42 +45,3 @@ func die(err error) { fmt.Fprintf(os.Stderr, "Error: %v", err) os.Exit(-1) } - -func writeRandomBytes(count int64, w io.Writer) error { - r := &io.LimitedReader{R: randcrypto.Reader, N: count} - _, err := io.Copy(w, r) - return err -} - -func writePseudoRandomBytes(count int64, w io.Writer, seed int64) error { - randmath.Seed(seed) - - // Configurable buffer size - bufsize := int64(1024 * 1024 * 4) - b := make([]byte, bufsize) - - for count > 0 { - if bufsize > count { - bufsize = count - b = b[:bufsize] - } - - var n int64 - for i := int64(0); i < bufsize; i++ { - n = randmath.Int63() - for j := 0; j < 8 && i < bufsize; j++ { - b[i] = byte(n & 0xff) - n >>= 8 - i++ - } - } - count = count - bufsize - - r := bytes.NewReader(b) - _, err := io.Copy(w, r) - if err != nil { - return err - } - } - return nil -} diff --git a/Godeps/_workspace/src/github.com/jbenet/go-random/random/random_test.go b/Godeps/_workspace/src/github.com/jbenet/go-random/random_test.go similarity index 80% rename from Godeps/_workspace/src/github.com/jbenet/go-random/random/random_test.go rename to Godeps/_workspace/src/github.com/jbenet/go-random/random_test.go index 5b40eff46..f121ecd55 100644 --- a/Godeps/_workspace/src/github.com/jbenet/go-random/random/random_test.go +++ b/Godeps/_workspace/src/github.com/jbenet/go-random/random_test.go @@ -1,4 +1,4 @@ -package main +package random import ( "bytes" @@ -18,7 +18,7 @@ func TestPseudoRandom(t *testing.T) { for _, size := range testCases { var buf bytes.Buffer - err := writePseudoRandomBytes(int64(size), &buf, int64(time.Now().UnixNano())) + err := WritePseudoRandomBytes(int64(size), &buf, int64(time.Now().UnixNano())) if err != nil { t.Fatal(err) } @@ -39,11 +39,11 @@ func TestPseudoRandomSeed(t *testing.T) { var bufs bytes.Buffer var bufr bytes.Buffer - if err := writePseudoRandomBytes(size, &bufs, seed); err != nil { + if err := WritePseudoRandomBytes(size, &bufs, seed); err != nil { t.Fatal(err) } - if err := writePseudoRandomBytes(size, &bufr, time.Now().UnixNano()); err != nil { + if err := WritePseudoRandomBytes(size, &bufr, time.Now().UnixNano()); err != nil { t.Fatal(err) } @@ -76,7 +76,7 @@ func TestCryptoRandom(t *testing.T) { for _, size := range testCases { var buf bytes.Buffer - err := writeRandomBytes(int64(size), &buf) + err := WriteRandomBytes(int64(size), &buf) if err != nil { t.Fatal(err) } @@ -88,9 +88,9 @@ func TestCryptoRandom(t *testing.T) { } func BenchmarkCryptoRandom(b *testing.B) { - writeRandomBytes(int64(b.N), ioutil.Discard) + WriteRandomBytes(int64(b.N), ioutil.Discard) } func BenchmarkPseudoRandom(b *testing.B) { - writePseudoRandomBytes(int64(b.N), ioutil.Discard, time.Now().UnixNano()) + WritePseudoRandomBytes(int64(b.N), ioutil.Discard, time.Now().UnixNano()) } diff --git a/test/lib/random-dep.go b/test/lib/random-dep.go index 7cf145161..9e38df98b 100644 --- a/test/lib/random-dep.go +++ b/test/lib/random-dep.go @@ -4,7 +4,5 @@ package randomdep import ( - random "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random/random" + _ "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random" ) - -var _ = random