Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
go-libp2p
Commits
59bb732c
Commit
59bb732c
authored
Jan 05, 2015
by
Juan Batiz-Benet
Browse files
p2p/test: bogus key pair for faster tests
parent
3b0da8a3
Changes
2
Hide whitespace changes
Inline
Side-by-side
net/mock/mock_net.go
View file @
59bb732c
...
@@ -4,13 +4,13 @@ import (
...
@@ -4,13 +4,13 @@ import (
"fmt"
"fmt"
"sort"
"sort"
"sync"
"sync"
"time"
ic
"github.com/jbenet/go-ipfs/p2p/crypto"
ic
"github.com/jbenet/go-ipfs/p2p/crypto"
host
"github.com/jbenet/go-ipfs/p2p/host"
host
"github.com/jbenet/go-ipfs/p2p/host"
bhost
"github.com/jbenet/go-ipfs/p2p/host/basic"
bhost
"github.com/jbenet/go-ipfs/p2p/host/basic"
inet
"github.com/jbenet/go-ipfs/p2p/net"
inet
"github.com/jbenet/go-ipfs/p2p/net"
peer
"github.com/jbenet/go-ipfs/p2p/peer"
peer
"github.com/jbenet/go-ipfs/p2p/peer"
p2putil
"github.com/jbenet/go-ipfs/p2p/test/util"
testutil
"github.com/jbenet/go-ipfs/util/testutil"
testutil
"github.com/jbenet/go-ipfs/util/testutil"
context
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
context
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
...
@@ -45,7 +45,7 @@ func New(ctx context.Context) Mocknet {
...
@@ -45,7 +45,7 @@ func New(ctx context.Context) Mocknet {
}
}
func
(
mn
*
mocknet
)
GenPeer
()
(
host
.
Host
,
error
)
{
func
(
mn
*
mocknet
)
GenPeer
()
(
host
.
Host
,
error
)
{
sk
,
_
,
err
:=
test
util
.
SeededTestKeyPair
(
time
.
Now
()
.
UnixNano
()
)
sk
,
err
:=
p2p
util
.
RandTestBogusPrivateKey
()
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
...
test/util/key.go
0 → 100644
View file @
59bb732c
package
testutil
import
(
"bytes"
"io"
"testing"
u
"github.com/jbenet/go-ipfs/util"
eventlog
"github.com/jbenet/go-ipfs/util/eventlog"
testutil
"github.com/jbenet/go-ipfs/util/testutil"
ic
"github.com/jbenet/go-ipfs/p2p/crypto"
peer
"github.com/jbenet/go-ipfs/p2p/peer"
ma
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
)
var
log
=
eventlog
.
Logger
(
"boguskey"
)
// TestBogusPrivateKey is a key used for testing (to avoid expensive keygen)
type
TestBogusPrivateKey
[]
byte
// TestBogusPublicKey is a key used for testing (to avoid expensive keygen)
type
TestBogusPublicKey
[]
byte
func
(
pk
TestBogusPublicKey
)
Verify
(
data
,
sig
[]
byte
)
(
bool
,
error
)
{
log
.
Criticalf
(
"TestBogusPublicKey.Verify -- this better be a test!"
)
return
bytes
.
Equal
(
data
,
reverse
(
sig
)),
nil
}
func
(
pk
TestBogusPublicKey
)
Bytes
()
([]
byte
,
error
)
{
return
[]
byte
(
pk
),
nil
}
func
(
pk
TestBogusPublicKey
)
Encrypt
(
b
[]
byte
)
([]
byte
,
error
)
{
log
.
Criticalf
(
"TestBogusPublicKey.Encrypt -- this better be a test!"
)
return
reverse
(
b
),
nil
}
// Equals checks whether this key is equal to another
func
(
pk
TestBogusPublicKey
)
Equals
(
k
ic
.
Key
)
bool
{
return
ic
.
KeyEqual
(
pk
,
k
)
}
func
(
pk
TestBogusPublicKey
)
Hash
()
([]
byte
,
error
)
{
return
ic
.
KeyHash
(
pk
)
}
func
(
sk
TestBogusPrivateKey
)
GenSecret
()
[]
byte
{
return
[]
byte
(
sk
)
}
func
(
sk
TestBogusPrivateKey
)
Sign
(
message
[]
byte
)
([]
byte
,
error
)
{
log
.
Criticalf
(
"TestBogusPrivateKey.Sign -- this better be a test!"
)
return
reverse
(
message
),
nil
}
func
(
sk
TestBogusPrivateKey
)
GetPublic
()
ic
.
PubKey
{
return
TestBogusPublicKey
(
sk
)
}
func
(
sk
TestBogusPrivateKey
)
Decrypt
(
b
[]
byte
)
([]
byte
,
error
)
{
log
.
Criticalf
(
"TestBogusPrivateKey.Decrypt -- this better be a test!"
)
return
reverse
(
b
),
nil
}
func
(
sk
TestBogusPrivateKey
)
Bytes
()
([]
byte
,
error
)
{
return
[]
byte
(
sk
),
nil
}
// Equals checks whether this key is equal to another
func
(
sk
TestBogusPrivateKey
)
Equals
(
k
ic
.
Key
)
bool
{
return
ic
.
KeyEqual
(
sk
,
k
)
}
func
(
sk
TestBogusPrivateKey
)
Hash
()
([]
byte
,
error
)
{
return
ic
.
KeyHash
(
sk
)
}
func
RandTestBogusPrivateKey
()
(
TestBogusPrivateKey
,
error
)
{
r
:=
u
.
NewTimeSeededRand
()
k
:=
make
([]
byte
,
5
)
if
_
,
err
:=
io
.
ReadFull
(
r
,
k
);
err
!=
nil
{
return
nil
,
err
}
return
TestBogusPrivateKey
(
k
),
nil
}
func
RandTestBogusPublicKey
()
(
TestBogusPublicKey
,
error
)
{
k
,
err
:=
RandTestBogusPrivateKey
()
return
TestBogusPublicKey
(
k
),
err
}
func
RandTestBogusPrivateKeyOrFatal
(
t
*
testing
.
T
)
TestBogusPrivateKey
{
k
,
err
:=
RandTestBogusPrivateKey
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
return
k
}
func
RandTestBogusPublicKeyOrFatal
(
t
*
testing
.
T
)
TestBogusPublicKey
{
k
,
err
:=
RandTestBogusPublicKey
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
return
k
}
func
RandTestBogusIdentity
()
(
testutil
.
Identity
,
error
)
{
k
,
err
:=
RandTestBogusPrivateKey
()
if
err
!=
nil
{
return
nil
,
err
}
id
,
err
:=
peer
.
IDFromPrivateKey
(
k
)
if
err
!=
nil
{
return
nil
,
err
}
return
&
identity
{
k
:
k
,
id
:
id
,
a
:
testutil
.
RandLocalTCPAddress
(),
},
nil
}
func
RandTestBogusIdentityOrFatal
(
t
*
testing
.
T
)
testutil
.
Identity
{
k
,
err
:=
RandTestBogusIdentity
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
return
k
}
// identity is a temporary shim to delay binding of PeerNetParams.
type
identity
struct
{
k
TestBogusPrivateKey
id
peer
.
ID
a
ma
.
Multiaddr
}
func
(
p
*
identity
)
ID
()
peer
.
ID
{
return
p
.
id
}
func
(
p
*
identity
)
Address
()
ma
.
Multiaddr
{
return
p
.
a
}
func
(
p
*
identity
)
PrivateKey
()
ic
.
PrivKey
{
return
p
.
k
}
func
(
p
*
identity
)
PublicKey
()
ic
.
PubKey
{
return
p
.
k
.
GetPublic
()
}
func
reverse
(
a
[]
byte
)
[]
byte
{
b
:=
make
([]
byte
,
len
(
a
))
for
i
:=
0
;
i
<
len
(
a
);
i
++
{
b
[
i
]
=
a
[
len
(
a
)
-
1
-
i
]
}
return
b
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment