diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 8960a4c94..53ce7e3c0 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", + "Rev": "e4585173eb8c47eea36c3dbff22f26f3f94d3586" + }, { "ImportPath": "github.com/kr/binarydist", "Rev": "9955b0ab8708602d411341e55fffd7e0700f86bd" 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 <int> +Print <int> 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 new file mode 100644 index 000000000..0222c59d3 --- /dev/null +++ b/Godeps/_workspace/src/github.com/jbenet/go-random/random/random.go @@ -0,0 +1,47 @@ +package main + +import ( + "fmt" + "os" + "strconv" + + random "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random" +) + +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 = random.WriteRandomBytes(count, os.Stdout) + } else { + seed, err2 := strconv.ParseInt(os.Args[2], 10, 64) + if err2 != nil { + usageError() + } + err = random.WritePseudoRandomBytes(count, os.Stdout, seed) + } + + if err != nil { + die(err) + } +} + +func usageError() { + fmt.Fprintf(os.Stderr, "Usage: %s <count> [<seed>]\n", os.Args[0]) + fmt.Fprintf(os.Stderr, "If <seed> is given, output <count> pseudo random bytes made from <seed> (from Go's math/rand)\n") + fmt.Fprintf(os.Stderr, "Otherwise, output <count> 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) +} diff --git a/Godeps/_workspace/src/github.com/jbenet/go-random/random_test.go b/Godeps/_workspace/src/github.com/jbenet/go-random/random_test.go new file mode 100644 index 000000000..f121ecd55 --- /dev/null +++ b/Godeps/_workspace/src/github.com/jbenet/go-random/random_test.go @@ -0,0 +1,96 @@ +package random + +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/.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 489beaab6..100401f03 100644 --- a/test/Makefile +++ b/test/Makefile @@ -5,7 +5,8 @@ # 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 @@ -19,24 +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 get github.com/jbenet/go-random/random - go install github.com/jbenet/go-random/random - cp `which 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/lib/random-dep.go b/test/lib/random-dep.go new file mode 100644 index 000000000..9e38df98b --- /dev/null +++ b/test/lib/random-dep.go @@ -0,0 +1,8 @@ +// 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 ( + _ "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random" +) 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..240346009 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 @@ -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 '