Skip to content

Commit

Permalink
Update TestKeys generator and include cert files
Browse files Browse the repository at this point in the history
  • Loading branch information
armstrongnate committed Jun 12, 2018
1 parent d5ff00d commit 1e19905
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 29 deletions.
35 changes: 29 additions & 6 deletions Sources/RootsEncoder/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,45 @@ s += "// GENERATED: DO NOT EDIT\n"

if CommandLine.arguments.contains("test") {
s += "//\n"
s += "// This file contain a function that returns the contents of Tests/ssl.crt and Tests/ssl.key.\n"
s += "// This file contain a function that returns the contents of certificates and keys in Tests/.\n"
s += "//\n"
s += "import Foundation\n"
s += "\n"

s += "let certificateForTests = "
let certificateURL = URL(fileURLWithPath: "Tests/ssl.crt")
// server.crt
s += "let serverCertificate = "
let certificateURL = URL(fileURLWithPath: "Tests/server.crt")
let certificateData = try Data(contentsOf: certificateURL)
let encodedCertificate = certificateData.base64EncodedString()
s += "Data(base64Encoded: \"\(encodedCertificate)\", options:[])!\n"

s += "let keyForTests = "
let keyURL = URL(fileURLWithPath: "Tests/ssl.key")
// server.pem
s += "let serverKey = "
let keyURL = URL(fileURLWithPath: "Tests/server.pem")
let keyData = try Data(contentsOf: keyURL)
let encodedKey = keyData.base64EncodedString()
s += "Data(base64Encoded: \"\(encodedKey)\", options:[])!"
s += "Data(base64Encoded: \"\(encodedKey)\", options:[])!\n"

// ca.crt
s += "let trustCollectionCertificate = "
let caURL = URL(fileURLWithPath: "Tests/ca.crt")
let caData = try Data(contentsOf: caURL)
let caEncoded = caData.base64EncodedString()
s += "Data(base64Encoded: \"\(caEncoded)\", options:[])!\n"

// client.crt
s += "let clientCertificate = "
let clientCertURL = URL(fileURLWithPath: "Tests/client.crt")
let clientCertData = try Data(contentsOf: clientCertURL)
let clientCertEncoded = clientCertData.base64EncodedString()
s += "Data(base64Encoded: \"\(clientCertEncoded)\", options:[])!\n"

// client.pem
s += "let clientKey = "
let clientKeyURL = URL(fileURLWithPath: "Tests/client.pem")
let clientKeyData = try Data(contentsOf: clientKeyURL)
let clientKeyEncoded = clientKeyData.base64EncodedString()
s += "Data(base64Encoded: \"\(clientKeyEncoded)\", options:[])!"
} else {
s += "//\n"
s += "// This file contain a function that returns the default roots.pem.\n"
Expand Down
16 changes: 8 additions & 8 deletions Tests/SwiftGRPCTests/BasicEchoTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,23 @@ class BasicEchoTestCase: XCTestCase {

provider = makeProvider()

let certificateString = String(data: certificateForTests, encoding: .utf8)!
let keyString = String(data: keyForTests, encoding: .utf8)!
let rootCerts = String(data: trustCollectionCertificateForTests, encoding: .utf8)!
let clientCertificateString = String(data: clientCertificateForTests, encoding: .utf8)!
let clientKeyString = String(data: clientKeyForTests, encoding: .utf8)!
let serverCertificateString = String(data: serverCertificate, encoding: .utf8)!
let serverKeyString = String(data: serverKey, encoding: .utf8)!
let rootCerts = String(data: trustCollectionCertificate, encoding: .utf8)!
let clientCertificateString = String(data: clientCertificate, encoding: .utf8)!
let clientKeyString = String(data: clientKey, encoding: .utf8)!

switch security {
case .ssl:
server = Echo_EchoServer(address: address,
certificateString: certificateString,
keyString: keyString,
certificateString: serverCertificateString,
keyString: serverKeyString,
provider: provider)
server.start()
client = Echo_EchoServiceClient(address: address, certificates: rootCerts, arguments: [.sslTargetNameOverride("example.com")])
client.host = "example.com"
case .tlsMutualAuth:
server = Echo_EchoServer(address: address, certificateString: certificateString, keyString: keyString, rootCerts: rootCerts, provider: provider)
server = Echo_EchoServer(address: address, certificateString: serverCertificateString, keyString: serverKeyString, rootCerts: rootCerts, provider: provider)
server.start()
client = Echo_EchoServiceClient(address: address, certificates: rootCerts, clientCertificates: clientCertificateString, clientKey: clientKeyString, arguments: [.sslTargetNameOverride("example.com")])
client.host = "example.com"
Expand Down
6 changes: 3 additions & 3 deletions Tests/SwiftGRPCTests/GRPCTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ func runTest(useSSL: Bool) {
let server: Server
if useSSL {
server = Server(address: address,
key: String(data: keyForTests, encoding: .utf8)!,
certs: String(data: certificateForTests, encoding: .utf8)!)
key: String(data: serverKey, encoding: .utf8)!,
certs: String(data: serverCertificate, encoding: .utf8)!)
} else {
server = Server(address: address)
}
Expand Down Expand Up @@ -145,7 +145,7 @@ func runClient(useSSL: Bool) throws {

if useSSL {
channel = Channel(address: address,
certificates: String(data: trustCollectionCertificateForTests, encoding: .utf8)!,
certificates: String(data: trustCollectionCertificate, encoding: .utf8)!,
arguments: [.sslTargetNameOverride(host)])
} else {
channel = Channel(address: address, secure: false)
Expand Down
Loading

0 comments on commit 1e19905

Please sign in to comment.