Commit 7ba3cadb authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

Merge pull request #289 from jbenet/update-random-sharness-tests

Update random for test/
parents 7ee0c6dc 1e434ef3
......@@ -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"
......
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.
# 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
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
}
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)
}
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())
}
sharness/
lib/sharness/
bin/ipfs
bin/random
test-results/
trash directory.*.sh/
......@@ -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
......@@ -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=$?
......
// 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"
)
......@@ -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"
......
......@@ -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"
......
......@@ -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
......
......@@ -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" &&
......
......@@ -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
......
......@@ -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
'
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment