mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-23 21:47:52 +08:00
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.
This commit is contained in:
4
Godeps/Godeps.json
generated
4
Godeps/Godeps.json
generated
@ -111,8 +111,8 @@
|
|||||||
"Rev": "1976046c2b0db0b668791b3e541d76a38b7c1af7"
|
"Rev": "1976046c2b0db0b668791b3e541d76a38b7c1af7"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/jbenet/go-random/random",
|
"ImportPath": "github.com/jbenet/go-random",
|
||||||
"Rev": "623fff67b06299c43b5542d42c427c7c2855a362"
|
"Rev": "e4585173eb8c47eea36c3dbff22f26f3f94d3586"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/kr/binarydist",
|
"ImportPath": "github.com/kr/binarydist",
|
||||||
|
21
Godeps/_workspace/src/github.com/jbenet/go-random/LICENSE
generated
vendored
Normal file
21
Godeps/_workspace/src/github.com/jbenet/go-random/LICENSE
generated
vendored
Normal file
@ -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.
|
29
Godeps/_workspace/src/github.com/jbenet/go-random/README.md
generated
vendored
Normal file
29
Godeps/_workspace/src/github.com/jbenet/go-random/README.md
generated
vendored
Normal file
@ -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<EFBFBD><EFBFBD><EFBFBD>#
|
||||||
|
```
|
||||||
|
|
||||||
|
### License
|
||||||
|
|
||||||
|
MIT
|
47
Godeps/_workspace/src/github.com/jbenet/go-random/lib.go
generated
vendored
Normal file
47
Godeps/_workspace/src/github.com/jbenet/go-random/lib.go
generated
vendored
Normal file
@ -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
|
||||||
|
}
|
1
Godeps/_workspace/src/github.com/jbenet/go-random/random/.gitignore
generated
vendored
Normal file
1
Godeps/_workspace/src/github.com/jbenet/go-random/random/.gitignore
generated
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
random
|
49
Godeps/_workspace/src/github.com/jbenet/go-random/random/random.go
generated
vendored
49
Godeps/_workspace/src/github.com/jbenet/go-random/random/random.go
generated
vendored
@ -1,13 +1,11 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
randcrypto "crypto/rand"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
randmath "math/rand"
|
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
random "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -22,13 +20,13 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if l == 2 {
|
if l == 2 {
|
||||||
err = writeRandomBytes(count, os.Stdout)
|
err = random.WriteRandomBytes(count, os.Stdout)
|
||||||
} else {
|
} else {
|
||||||
seed, err2 := strconv.ParseInt(os.Args[2], 10, 64)
|
seed, err2 := strconv.ParseInt(os.Args[2], 10, 64)
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
usageError()
|
usageError()
|
||||||
}
|
}
|
||||||
err = writePseudoRandomBytes(count, os.Stdout, seed)
|
err = random.WritePseudoRandomBytes(count, os.Stdout, seed)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -47,42 +45,3 @@ func die(err error) {
|
|||||||
fmt.Fprintf(os.Stderr, "Error: %v", err)
|
fmt.Fprintf(os.Stderr, "Error: %v", err)
|
||||||
os.Exit(-1)
|
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
|
|
||||||
}
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package random
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@ -18,7 +18,7 @@ func TestPseudoRandom(t *testing.T) {
|
|||||||
|
|
||||||
for _, size := range testCases {
|
for _, size := range testCases {
|
||||||
var buf bytes.Buffer
|
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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -39,11 +39,11 @@ func TestPseudoRandomSeed(t *testing.T) {
|
|||||||
var bufs bytes.Buffer
|
var bufs bytes.Buffer
|
||||||
var bufr 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)
|
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)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,7 +76,7 @@ func TestCryptoRandom(t *testing.T) {
|
|||||||
|
|
||||||
for _, size := range testCases {
|
for _, size := range testCases {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
err := writeRandomBytes(int64(size), &buf)
|
err := WriteRandomBytes(int64(size), &buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -88,9 +88,9 @@ func TestCryptoRandom(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkCryptoRandom(b *testing.B) {
|
func BenchmarkCryptoRandom(b *testing.B) {
|
||||||
writeRandomBytes(int64(b.N), ioutil.Discard)
|
WriteRandomBytes(int64(b.N), ioutil.Discard)
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkPseudoRandom(b *testing.B) {
|
func BenchmarkPseudoRandom(b *testing.B) {
|
||||||
writePseudoRandomBytes(int64(b.N), ioutil.Discard, time.Now().UnixNano())
|
WritePseudoRandomBytes(int64(b.N), ioutil.Discard, time.Now().UnixNano())
|
||||||
}
|
}
|
@ -4,7 +4,5 @@
|
|||||||
package randomdep
|
package randomdep
|
||||||
|
|
||||||
import (
|
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
|
|
||||||
|
Reference in New Issue
Block a user