Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
ad43e1d4
Commit
ad43e1d4
authored
10 years ago
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
testutil: peer gen with addrs
parent
b573d662
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
0 deletions
+42
-0
util/testutil/gen.go
util/testutil/gen.go
+42
-0
No files found.
util/testutil/gen.go
View file @
ad43e1d4
...
...
@@ -2,9 +2,14 @@ package testutil
import
(
crand
"crypto/rand"
"fmt"
"math/rand"
ci
"github.com/jbenet/go-ipfs/crypto"
peer
"github.com/jbenet/go-ipfs/peer"
u
"github.com/jbenet/go-ipfs/util"
ma
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
)
func
RandPeer
()
peer
.
Peer
{
...
...
@@ -22,3 +27,40 @@ func PeerWithNewKeys() (peer.Peer, error) {
return
NewPeerWithKeyPair
(
sk
,
pk
)
}
func
PeerWithKeysAndStringAddress
(
addr
string
)
(
peer
.
Peer
,
error
)
{
maddr
,
err
:=
ma
.
NewMultiaddr
(
addr
)
if
err
!=
nil
{
return
nil
,
err
}
return
PeerWithKeysAndAddress
(
maddr
)
}
func
PeerWithKeysAndAddress
(
maddr
ma
.
Multiaddr
)
(
peer
.
Peer
,
error
)
{
sk
,
pk
,
err
:=
ci
.
GenerateKeyPair
(
ci
.
RSA
,
512
)
if
err
!=
nil
{
return
nil
,
err
}
p
,
err
:=
NewPeerWithKeyPair
(
sk
,
pk
)
if
err
!=
nil
{
return
nil
,
err
}
p
.
AddAddress
(
maddr
)
return
p
,
nil
}
// RandLocalTCPAddress returns a random multiaddr. it suppresses errors
// for nice composability-- do check the address isn't nil.
func
RandLocalTCPAddress
()
ma
.
Multiaddr
{
// chances are it will work out, but it **might** fail if the port is in use
// most ports above 10000 aren't in use by long running processes, so yay.
// (maybe there should be a range of "loopback" ports that are guaranteed
// to be open for the process, but naturally can only talk to self.)
addr
:=
fmt
.
Sprintf
(
"/ip4/127.0.0.1/tcp/%d"
,
10000
+
rand
.
Intn
(
50000
))
maddr
,
_
:=
ma
.
NewMultiaddr
(
addr
)
return
maddr
}
This diff is collapsed.
Click to expand it.
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