2018-08-01 22:08:44 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2018-08-18 03:34:56 +00:00
|
|
|
"io/ioutil"
|
2018-08-01 22:08:44 +00:00
|
|
|
"testing"
|
|
|
|
|
2018-10-03 01:49:01 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/event"
|
2018-08-01 22:08:44 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2018-08-18 03:34:56 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-08-01 22:08:44 +00:00
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
|
|
)
|
|
|
|
|
2018-08-18 03:34:56 +00:00
|
|
|
func init() {
|
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
|
|
logrus.SetOutput(ioutil.Discard)
|
|
|
|
}
|
|
|
|
|
2018-10-05 17:14:50 +00:00
|
|
|
type mockAttestationService struct{}
|
2018-09-21 14:32:20 +00:00
|
|
|
|
2018-10-05 17:14:50 +00:00
|
|
|
func (m *mockAttestationService) IncomingAttestationFeed() *event.Feed {
|
|
|
|
return new(event.Feed)
|
2018-10-02 20:07:33 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 14:32:20 +00:00
|
|
|
type mockChainService struct {
|
|
|
|
blockFeed *event.Feed
|
|
|
|
stateFeed *event.Feed
|
|
|
|
attestationFeed *event.Feed
|
2018-09-05 03:35:32 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 14:32:20 +00:00
|
|
|
func (m *mockChainService) IncomingBlockFeed() *event.Feed {
|
|
|
|
return new(event.Feed)
|
2018-08-18 03:34:56 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 14:32:20 +00:00
|
|
|
func (m *mockChainService) CanonicalBlockFeed() *event.Feed {
|
2018-08-24 04:09:59 +00:00
|
|
|
return m.blockFeed
|
2018-08-18 03:34:56 +00:00
|
|
|
}
|
|
|
|
|
2018-12-01 22:09:12 +00:00
|
|
|
func (m *mockChainService) CanonicalStateFeed() *event.Feed {
|
2018-08-24 04:09:59 +00:00
|
|
|
return m.stateFeed
|
2018-08-18 03:34:56 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 14:32:20 +00:00
|
|
|
func newMockChainService() *mockChainService {
|
|
|
|
return &mockChainService{
|
|
|
|
blockFeed: new(event.Feed),
|
|
|
|
stateFeed: new(event.Feed),
|
|
|
|
attestationFeed: new(event.Feed),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 22:08:44 +00:00
|
|
|
func TestLifecycle(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
2018-09-05 03:35:32 +00:00
|
|
|
rpcService := NewRPCService(context.Background(), &Config{
|
2018-10-05 17:14:50 +00:00
|
|
|
Port: "7348",
|
|
|
|
CertFlag: "alice.crt",
|
|
|
|
KeyFlag: "alice.key",
|
2018-09-05 03:35:32 +00:00
|
|
|
})
|
2018-08-01 22:08:44 +00:00
|
|
|
|
|
|
|
rpcService.Start()
|
|
|
|
|
|
|
|
testutil.AssertLogsContain(t, hook, "Starting service")
|
|
|
|
testutil.AssertLogsContain(t, hook, fmt.Sprintf("RPC server listening on port :%s", rpcService.port))
|
|
|
|
|
|
|
|
rpcService.Stop()
|
|
|
|
testutil.AssertLogsContain(t, hook, "Stopping service")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBadEndpoint(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
2018-09-21 14:32:20 +00:00
|
|
|
rpcService := NewRPCService(context.Background(), &Config{
|
2018-10-05 17:14:50 +00:00
|
|
|
Port: "ralph merkle!!!",
|
2018-09-21 14:32:20 +00:00
|
|
|
})
|
2018-08-01 22:08:44 +00:00
|
|
|
|
|
|
|
rpcService.Start()
|
|
|
|
|
|
|
|
testutil.AssertLogsContain(t, hook, "Starting service")
|
|
|
|
testutil.AssertLogsContain(t, hook, fmt.Sprintf("Could not listen to port :%s", rpcService.port))
|
|
|
|
|
|
|
|
rpcService.Stop()
|
|
|
|
testutil.AssertLogsContain(t, hook, "Stopping service")
|
|
|
|
}
|
2018-08-09 22:54:59 +00:00
|
|
|
|
|
|
|
func TestInsecureEndpoint(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
2018-09-21 14:32:20 +00:00
|
|
|
rpcService := NewRPCService(context.Background(), &Config{
|
2018-10-05 17:14:50 +00:00
|
|
|
Port: "7777",
|
2018-09-21 14:32:20 +00:00
|
|
|
})
|
2018-08-09 22:54:59 +00:00
|
|
|
|
|
|
|
rpcService.Start()
|
|
|
|
|
|
|
|
testutil.AssertLogsContain(t, hook, "Starting service")
|
|
|
|
testutil.AssertLogsContain(t, hook, fmt.Sprintf("RPC server listening on port :%s", rpcService.port))
|
|
|
|
testutil.AssertLogsContain(t, hook, "You are using an insecure gRPC connection")
|
|
|
|
|
|
|
|
rpcService.Stop()
|
|
|
|
testutil.AssertLogsContain(t, hook, "Stopping service")
|
|
|
|
}
|