-
Notifications
You must be signed in to change notification settings - Fork 15
/
main_test.go
69 lines (53 loc) · 3.36 KB
/
main_test.go
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
package main_test
import (
"bufio"
"io"
"os/exec"
"strings"
"testing"
)
const executable = "./trustedcoin"
const getManifestRequest = `{"jsonrpc":"2.0","id":"getmanifest","method":"getmanifest","params":{}}`
const getManifestExpectedResponse = `{"jsonrpc":"2.0","id":"getmanifest","result":{"options":[{"name":"bitcoin-rpcconnect","type":"string","default":"","description":"Hostname (IP) to bitcoind RPC (optional)."},{"name":"bitcoin-rpcport","type":"string","default":"","description":"Port to bitcoind RPC (optional)."},{"name":"bitcoin-rpcuser","type":"string","default":"","description":"Username to bitcoind RPC (optional)."},{"name":"bitcoin-rpcpassword","type":"string","default":"","description":"Password to bitcoind RPC (optional)."},{"name":"bitcoin-datadir","type":"string","default":"","description":"-datadir arg for bitcoin-cli. For compatibility with bcli, not actually used."}],"rpcmethods":[{"name":"getrawblockbyheight","usage":"height","description":"Get the bitcoin block at a given height","long_description":""},{"name":"getchaininfo","usage":"","description":"Get the chain id, the header count, the block count and whether this is IBD.","long_description":""},{"name":"estimatefees","usage":"","description":"Get the Bitcoin feerate in sat/kilo-vbyte.","long_description":""},{"name":"sendrawtransaction","usage":"tx","description":"Send a raw transaction to the Bitcoin network.","long_description":""},{"name":"getutxout","usage":"txid vout","description":"Get informations about an output, identified by a {txid} an a {vout}","long_description":""}],"subscriptions":[],"hooks":[],"featurebits":{"features":"","channel":"","init":"","invoice":""},"dynamic":false,"notifications":[]}}`
const initRequest = `{"jsonrpc":"2.0","id":"init","method":"init","params":{"options":{},"configuration":{"network":"bitcoin","lightning-dir":"/tmp","rpc-file":"foo"}}}`
const initExpectedResponse = `{"jsonrpc":"2.0","id":"init"}`
const shutdownNotification = `{"jsonrpc":"2.0","method":"shutdown","params":{}}`
func TestInitAndShutdown(t *testing.T) {
cmd, stdin, stdout, stderr := start(t)
stop(t, cmd, stdin, stdout, stderr)
}
func start(t *testing.T) (*exec.Cmd, io.WriteCloser, io.ReadCloser, io.ReadCloser) {
cmd := exec.Command(executable)
stdin, _ := cmd.StdinPipe()
stdout, _ := cmd.StdoutPipe()
stderr, _ := cmd.StderrPipe()
err := cmd.Start()
if err != nil {
t.Fatalf("expected trustedcoin to start, got %v", err)
}
_, _ = io.WriteString(stdin, getManifestRequest)
if response := readline(stdout); response != getManifestExpectedResponse {
t.Fatalf("unexpected manifest response: %s", response)
}
_, _ = io.WriteString(stdin, initRequest)
if response := readline(stdout); response != initExpectedResponse {
t.Fatalf("unexpected init response: %s", response)
}
if response := readline(stderr); !strings.Contains(response, "initialized plugin") {
t.Fatalf("unexpected output in stderr: %s", response)
}
return cmd, stdin, stdout, stderr
}
func stop(t *testing.T, cmd *exec.Cmd, stdin io.WriteCloser, stdout, stderr io.ReadCloser) {
_, _ = io.WriteString(stdin, shutdownNotification)
_ = stdin.Close()
_ = stdout.Close()
_ = stderr.Close()
if err := cmd.Wait(); err != nil {
t.Fatalf("expected process to exit cleanly, got %v", err)
}
}
func readline(r io.Reader) string {
line, _ := bufio.NewReader(r).ReadString('\n')
return strings.TrimSuffix(line, "\n")
}