Skip to content

Commit

Permalink
Add tests for client bindings (#9036)
Browse files Browse the repository at this point in the history
* Add tests for client bindings

changelog_begin
changelog_end

Closes #8636

* Address #9036 (comment)
  • Loading branch information
stefanobaghino-da committed Mar 8, 2021
1 parent 27fd932 commit 1bec211
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 0 deletions.
29 changes: 29 additions & 0 deletions runtime-components/non-repudiation-client/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# SPDX-License-Identifier: Apache-2.0

load("//bazel_tools:java.bzl", "da_java_library")
load("//bazel_tools:scala.bzl", "da_scala_test")

da_java_library(
name = "non-repudiation-client",
Expand All @@ -20,3 +21,31 @@ da_java_library(
"@maven//:io_grpc_grpc_stub",
],
)

da_scala_test(
name = "test",
srcs = glob(["src/test/scala/**/*.scala"]),
resources = [
"src/test/resources/logback-test.xml",
],
scala_deps = [
"@maven//:org_scala_lang_modules_scala_collection_compat",
],
runtime_deps = [
"@maven//:ch_qos_logback_logback_classic",
],
deps = [
"//language-support/java/bindings:bindings-java",
"//language-support/java/bindings-rxjava",
"//ledger-api/grpc-definitions:ledger_api_proto_scala",
"//libs-scala/resources",
"//libs-scala/resources-grpc",
"//runtime-components/non-repudiation",
"//runtime-components/non-repudiation-client",
"//runtime-components/non-repudiation-testing",
"@maven//:io_grpc_grpc_api",
"@maven//:io_grpc_grpc_core",
"@maven//:io_grpc_grpc_netty",
"@maven//:io_reactivex_rxjava2_rxjava",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="stderr-appender" class="ch.qos.logback.core.ConsoleAppender">
<target>System.err</target>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>trace</level>
</filter>
<encoder>
<pattern>%date{"yyyy-MM-dd'T'HH:mm:ss.SSSXXX", UTC} %-5level %logger{5}@[%-4.30thread] - %msg%n</pattern>
</encoder>
</appender>

<root level="${LOGLEVEL:-WARN}">
<appender-ref ref="stderr-appender"/>
</root>

</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.nonrepudiation.client

import java.time.{Clock, Instant, ZoneId}
import java.util.Collections

import com.daml.ledger.api.v1.CommandServiceOuterClass.SubmitAndWaitRequest
import com.daml.ledger.javaapi.data.Command
import com.daml.ledger.api.v1.CommandsOuterClass.{Command => ProtoCommand}
import com.daml.ledger.api.v1.command_service.CommandServiceGrpc.CommandService
import com.daml.ledger.api.v1.command_submission_service.CommandSubmissionServiceGrpc.CommandSubmissionService
import com.daml.ledger.rxjava.DamlLedgerClient
import com.daml.nonrepudiation.testing._
import com.daml.nonrepudiation.{
AlgorithmString,
CommandIdString,
NonRepudiationProxy,
SignatureBytes,
SignedPayload,
}
import com.daml.resources.grpc.{GrpcResourceOwnerFactories => Resources}
import io.grpc.netty.NettyChannelBuilder
import org.scalatest.Inside
import org.scalatest.flatspec.AsyncFlatSpec
import org.scalatest.matchers.should.Matchers

import scala.concurrent.ExecutionContext
import scala.concurrent.duration.DurationInt

final class SigningInterceptorSpec extends AsyncFlatSpec with Matchers with Inside {

behavior of "SigningInterceptor"

it should "be possible to use it with the Java bindings" in {
val (key, certificate) = generateKeyAndCertificate()
val builders =
DummyTestSetup.Builders(
useNetworkStack = true,
serviceExecutionContext = ExecutionContext.global,
)

val certificates = new Certificates
val signedPayloads = new SignedPayloads[CommandIdString]

val expectedAlgorithm = AlgorithmString.SHA256withRSA
val expectedFingerprint = certificates.put(certificate)
val expectedTimestamp = Instant.now()

val proxy =
for {
_ <- Resources.forServer(builders.participantServer, 5.seconds)
participant <- Resources.forChannel(builders.participantChannel, 5.seconds)
proxy <- NonRepudiationProxy.owner(
participant,
builders.proxyServer,
certificates,
signedPayloads,
Clock.fixed(expectedTimestamp, ZoneId.systemDefault()),
CommandService.scalaDescriptor.fullName,
CommandSubmissionService.scalaDescriptor.fullName,
)
} yield proxy

proxy.use { _ =>
val clientChannelBuilder =
builders.proxyChannel
.asInstanceOf[NettyChannelBuilder]
.intercept(SigningInterceptor.signCommands(key, certificate))
val client = DamlLedgerClient.newBuilder(clientChannelBuilder).build()
client.connect()
val command = ProtoCommand.parseFrom(generateCommand().getCommands.commands.head.toByteArray)
val expectedWorkflowId = "workflow-id"
val expectedApplicationId = "application-id"
val expectedCommandId = "command-id"
val expectedParty = "party-1"
client.getCommandClient
.submitAndWait(
expectedWorkflowId,
expectedApplicationId,
expectedCommandId,
expectedParty,
Collections.singletonList(Command.fromProtoCommand(command)),
)
.blockingGet()
inside(signedPayloads.get(CommandIdString.wrap("command-id"))) {
case Seq(SignedPayload(algorithm, fingerprint, payload, signature, timestamp)) =>
val commands = SubmitAndWaitRequest.parseFrom(payload.unsafeArray).getCommands
algorithm shouldBe expectedAlgorithm
fingerprint shouldBe expectedFingerprint
commands.getWorkflowId shouldBe expectedWorkflowId
commands.getApplicationId shouldBe expectedApplicationId
commands.getCommandId shouldBe expectedCommandId
commands.getParty shouldBe expectedParty
commands.getCommandsCount shouldBe 1
signature shouldBe SignatureBytes.sign(expectedAlgorithm, key, payload)
timestamp shouldBe expectedTimestamp
}
}
}

}

0 comments on commit 1bec211

Please sign in to comment.