aboutsummaryrefslogtreecommitdiffstats
path: root/whisper/common_test.go
blob: 76e6463e6896bd19acff5364977bf266742b3c5b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Contains some common utility functions for testing.

package whisper

import (
    "bytes"
    "fmt"
    "io/ioutil"
    "math/rand"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/p2p"
    "github.com/ethereum/go-ethereum/p2p/discover"
)

// randomNodeID generates and returns a random P2P discovery node id for the
// whisper tests.
func randomNodeID() (id discover.NodeID) {
    for i := range id {
        id[i] = byte(rand.Intn(255))
    }
    return id
}

// randomNodeName generates and returns a random P2P node name for the whisper
// tests.
func randomNodeName() string {
    return common.MakeName(fmt.Sprintf("whisper-go-test-%3d", rand.Intn(999)), "1.0")
}

// whisperCaps returns the node capabilities for running the whisper sub-protocol.
func whisperCaps() []p2p.Cap {
    return []p2p.Cap{
        p2p.Cap{
            Name:    protocolName,
            Version: uint(protocolVersion),
        },
    }
}

// bufMsgPipe creates a buffered message pipe between two endpoints.
func bufMsgPipe() (*p2p.MsgPipeRW, *p2p.MsgPipeRW) {
    A, midA := p2p.MsgPipe()
    midB, B := p2p.MsgPipe()

    go copyMsgPipe(midA, midB)
    go copyMsgPipe(midB, midA)

    return A, B
}

// copyMsgPipe copies messages from the src pipe to the dest.
func copyMsgPipe(dst, src *p2p.MsgPipeRW) {
    defer dst.Close()
    for {
        msg, err := src.ReadMsg()
        if err != nil {
            return
        }
        data, err := ioutil.ReadAll(msg.Payload)
        if err != nil {
            return
        }
        msg.Payload = bytes.NewReader(data)
        if err := dst.WriteMsg(msg); err != nil {
            return
        }
    }
}