diff --git a/Makefile b/Makefile index 3b89b73..600abc3 100644 --- a/Makefile +++ b/Makefile @@ -28,6 +28,8 @@ clean: $Q rm -rf bin .GOPATH test: .GOPATH/.ok + $Q ./bin/crux --url=http://127.0.0.1:9020/ --port=9020 --workdir=test/test1 --publickeys=testdata/key.pub --privatekeys=testdata/key & + $Q ./bin/crux --url=http://127.0.0.1:9025/ --port=9025 --workdir=test/test2 --publickeys=testdata/rcpt1.pub --privatekeys=testdata/rcpt1 & $Q go test $(if $V,-v) -i -race $(allpackages) # install -race libs to speed up next run ifndef CI $Q go vet $(allpackages) @@ -38,6 +40,7 @@ else $Q ( GODEBUG=cgocheck=2 go test -v -race $(allpackages); echo $$? ) | \ tee .GOPATH/test/output.txt | sed '$$ d'; exit $$(tail -1 .GOPATH/test/output.txt) endif + $Q pkill crux list: .GOPATH/.ok @echo $(allpackages) diff --git a/test/client_test.go b/test/client_test.go new file mode 100644 index 0000000..32d1319 --- /dev/null +++ b/test/client_test.go @@ -0,0 +1,82 @@ +package client + +import ( + "golang.org/x/net/context" + "google.golang.org/grpc" + "github.com/blk-io/chimera-api/chimera" + "encoding/base64" + "reflect" + "testing" + ) + +const sender = "zSifTnkv5r4K67Dq304eVcM4FpxGfHLe1yTCBm0/7wg=" +const receiver = "I/EbshW61ykJ+qTivXPaKyQ5WmQDUFfMNGEBj2E2uUs=" +var payload = []byte("payload") + + +func TestIntegration(t *testing.T) { + var conn1 *grpc.ClientConn + var conn2 *grpc.ClientConn + // Initiate a connection with the first server + conn1, err := grpc.Dial(":9020", grpc.WithInsecure()) + if err != nil { + t.Fatalf("did not connect: %s", err) + } + defer conn1.Close() + c1 := chimera.NewClientClient(conn1) + // Initiate a connection with the second server + conn2, err = grpc.Dial(":9025", grpc.WithInsecure()) + if err != nil { + t.Fatalf("did not connect: %s", err) + } + defer conn2.Close() + c2 := chimera.NewClientClient(conn2) + + Upcheckresponse1, err := c1.Upcheck(context.Background(), &chimera.UpCheckResponse{Message: "foo"}) + if err != nil { + t.Fatalf("error when calling Upcheck: %s", err) + } + t.Logf("Response from server: %s", Upcheckresponse1.Message) + + Upcheckresponse2, err := c2.Upcheck(context.Background(), &chimera.UpCheckResponse{Message: "foo"}) + if err != nil { + t.Fatalf("error when calling Upcheck: %s", err) + } + t.Logf("Response from server: %s", Upcheckresponse2.Message) + + sendReqs := []chimera.SendRequest{ + { + Payload: []byte("payload"), + From: sender, + To: []string{receiver}, + }, + { + Payload: []byte("test"), + To: []string{}, + }, + { + Payload: []byte("blk-io crux"), + }, + } + + sendResponse := chimera.SendResponse{} + for _, sendReq := range sendReqs { + sendResp, err:= c1.Send(context.Background(), &sendReq) + if err != nil { + t.Fatalf("gRPC send failed with %s", err) + } + sendResponse = chimera.SendResponse{Key:sendResp.Key} + t.Logf("The response for Send request is: %s", base64.StdEncoding.EncodeToString(sendResponse.Key)) + + recResp, err:= c1.Receive(context.Background(), &chimera.ReceiveRequest{Key:sendResponse.Key, To:receiver}) + if err != nil { + t.Fatalf("gRPC receive failed with %s", err) + } + receiveResponse := chimera.ReceiveResponse{Payload:recResp.Payload} + if !reflect.DeepEqual(receiveResponse.Payload, sendReq.Payload) { + t.Fatalf("handler returned unexpected response: %v, expected: %v\n", receiveResponse.Payload, sendReq.Payload) + } else { + t.Logf("The payload return is %v", receiveResponse.Payload) + } + } +} diff --git a/test/test1/testdata/key b/test/test1/testdata/key new file mode 100644 index 0000000..fdb841b --- /dev/null +++ b/test/test1/testdata/key @@ -0,0 +1 @@ +{"data":{"bytes":"W1n0C+NfjcU/cUBXsP5FQ/frU+qpvKQ7Pi/Mu5Hf/Ic="},"type":"unlocked"} \ No newline at end of file diff --git a/test/test1/testdata/key.pub b/test/test1/testdata/key.pub new file mode 100644 index 0000000..e8d6224 --- /dev/null +++ b/test/test1/testdata/key.pub @@ -0,0 +1 @@ +zSifTnkv5r4K67Dq304eVcM4FpxGfHLe1yTCBm0/7wg= \ No newline at end of file diff --git a/test/test2/testdata/rcpt1 b/test/test2/testdata/rcpt1 new file mode 100644 index 0000000..6d0736c --- /dev/null +++ b/test/test2/testdata/rcpt1 @@ -0,0 +1 @@ +{"data":{"bytes":"9e0UrkhdfGY0kBYuk3Nv3g4FlYXjTHpXORWO2r1An/A="},"type":"unlocked"} \ No newline at end of file diff --git a/test/test2/testdata/rcpt1.pub b/test/test2/testdata/rcpt1.pub new file mode 100644 index 0000000..6599ad6 --- /dev/null +++ b/test/test2/testdata/rcpt1.pub @@ -0,0 +1 @@ +I/EbshW61ykJ+qTivXPaKyQ5WmQDUFfMNGEBj2E2uUs= \ No newline at end of file