mirror of
https://github.com/FlipsideCrypto/dcrd.git
synced 2026-02-06 10:56:47 +00:00
rpcserver: Upstream sync to add basic RPC tests.
Upstream commit 2d86fbface
This commit is contained in:
commit
2da4b5f15f
134
rpcserver_test.go
Normal file
134
rpcserver_test.go
Normal file
@ -0,0 +1,134 @@
|
||||
// Copyright (c) 2016 The btcsuite developers
|
||||
// Copyright (c) 2017 The Decred developers
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/decred/dcrd/chaincfg"
|
||||
"github.com/decred/dcrd/rpctest"
|
||||
)
|
||||
|
||||
func testGetBestBlock(r *rpctest.Harness, t *testing.T) {
|
||||
_, prevbestHeight, err := r.Node.GetBestBlock()
|
||||
if err != nil {
|
||||
t.Fatalf("Call to `getbestblock` failed: %v", err)
|
||||
}
|
||||
|
||||
// Create a new block connecting to the current tip.
|
||||
generatedBlockHashes, err := r.Node.Generate(1)
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to generate block: %v", err)
|
||||
}
|
||||
|
||||
bestHash, bestHeight, err := r.Node.GetBestBlock()
|
||||
if err != nil {
|
||||
t.Fatalf("Call to `getbestblock` failed: %v", err)
|
||||
}
|
||||
|
||||
// Hash should be the same as the newly submitted block.
|
||||
if !bytes.Equal(bestHash[:], generatedBlockHashes[0][:]) {
|
||||
t.Fatalf("Block hashes do not match. Returned hash %v, wanted "+
|
||||
"hash %v", bestHash, generatedBlockHashes[0][:])
|
||||
}
|
||||
|
||||
// Block height should now reflect newest height.
|
||||
if bestHeight != prevbestHeight+1 {
|
||||
t.Fatalf("Block heights do not match. Got %v, wanted %v",
|
||||
bestHeight, prevbestHeight+1)
|
||||
}
|
||||
}
|
||||
|
||||
func testGetBlockCount(r *rpctest.Harness, t *testing.T) {
|
||||
// Save the current count.
|
||||
currentCount, err := r.Node.GetBlockCount()
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to get block count: %v", err)
|
||||
}
|
||||
|
||||
if _, err := r.Node.Generate(1); err != nil {
|
||||
t.Fatalf("Unable to generate block: %v", err)
|
||||
}
|
||||
|
||||
// Count should have increased by one.
|
||||
newCount, err := r.Node.GetBlockCount()
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to get block count: %v", err)
|
||||
}
|
||||
if newCount != currentCount+1 {
|
||||
t.Fatalf("Block count incorrect. Got %v should be %v",
|
||||
newCount, currentCount+1)
|
||||
}
|
||||
}
|
||||
|
||||
func testGetBlockHash(r *rpctest.Harness, t *testing.T) {
|
||||
// Create a new block connecting to the current tip.
|
||||
generatedBlockHashes, err := r.Node.Generate(1)
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to generate block: %v", err)
|
||||
}
|
||||
|
||||
info, err := r.Node.GetInfo()
|
||||
if err != nil {
|
||||
t.Fatalf("call to getinfo cailed: %v", err)
|
||||
}
|
||||
|
||||
blockHash, err := r.Node.GetBlockHash(int64(info.Blocks))
|
||||
if err != nil {
|
||||
t.Fatalf("Call to `getblockhash` failed: %v", err)
|
||||
}
|
||||
|
||||
// Block hashes should match newly created block.
|
||||
if !bytes.Equal(generatedBlockHashes[0][:], blockHash[:]) {
|
||||
t.Fatalf("Block hashes do not match. Returned hash %v, wanted "+
|
||||
"hash %v", blockHash, generatedBlockHashes[0][:])
|
||||
}
|
||||
}
|
||||
|
||||
var rpcTestCases = []rpctest.HarnessTestCase{
|
||||
testGetBestBlock,
|
||||
testGetBlockCount,
|
||||
testGetBlockHash,
|
||||
}
|
||||
|
||||
var primaryHarness *rpctest.Harness
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
var err error
|
||||
primaryHarness, err = rpctest.New(&chaincfg.SimNetParams, nil, nil)
|
||||
if err != nil {
|
||||
fmt.Println("unable to create primary harness: ", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Initialize the primary mining node with a chain of length 125,
|
||||
// providing 25 mature coinbases to allow spending from for testing
|
||||
// purposes.
|
||||
if err = primaryHarness.SetUp(true, 25); err != nil {
|
||||
fmt.Println("unable to setup test chain: ", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
exitCode := m.Run()
|
||||
|
||||
// Clean up the primary harness created above. This includes removing
|
||||
// all temporary directories, and shutting down any created processes.
|
||||
if err := primaryHarness.TearDown(); err != nil {
|
||||
fmt.Println("unable to setup test chain: ", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
os.Exit(exitCode)
|
||||
}
|
||||
|
||||
func TestRpcServer(t *testing.T) {
|
||||
for _, testCase := range rpcTestCases {
|
||||
testCase(primaryHarness, t)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user