Skip to content

Commit

Permalink
Merge pull request #1225 from ably/1201-unroll-reusable-tests-statements
Browse files Browse the repository at this point in the history
[Groundwork for #1201] Unroll test-defining loops in test files into `reusableTests` function calls
  • Loading branch information
lawrence-forooghian authored Nov 25, 2021
2 parents f31eb55 + b3c606e commit 5093927
Show file tree
Hide file tree
Showing 6 changed files with 335 additions and 323 deletions.
99 changes: 47 additions & 52 deletions Spec/Auth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3969,58 +3969,53 @@ class Auth : QuickSpec {
describe("TokenRequest") {
// TE6
describe("fromJson") {
let cases = [
"with TTL": (
"{" +
" \"clientId\":\"myClientId\"," +
" \"mac\":\"4rr4J+JzjiCL1DoS8wq7k11Z4oTGCb1PoeN+yGjkaH4=\"," +
" \"capability\":\"{\\\"test\\\":[\\\"publish\\\"]}\"," +
" \"ttl\":42000," +
" \"timestamp\":1479087321934," +
" \"keyName\":\"xxxxxx.yyyyyy\"," +
" \"nonce\":\"7830658976108826\"" +
"}",
{ (_ request: ARTTokenRequest) in
expect(request.clientId).to(equal("myClientId"))
expect(request.mac).to(equal("4rr4J+JzjiCL1DoS8wq7k11Z4oTGCb1PoeN+yGjkaH4="))
expect(request.capability).to(equal("{\"test\":[\"publish\"]}"))
expect(request.ttl as? TimeInterval).to(equal(TimeInterval(42)))
expect(request.timestamp).to(equal(Date(timeIntervalSince1970: 1479087321.934)))
expect(request.keyName).to(equal("xxxxxx.yyyyyy"))
expect(request.nonce).to(equal("7830658976108826"))
}
),
"without TTL": (
"{" +
" \"mac\":\"4rr4J+JzjiCL1DoS8wq7k11Z4oTGCb1PoeN+yGjkaH4=\"," +
" \"capability\":\"{\\\"test\\\":[\\\"publish\\\"]}\"," +
" \"timestamp\":1479087321934," +
" \"keyName\":\"xxxxxx.yyyyyy\"," +
" \"nonce\":\"7830658976108826\"" +
"}",
{ (_ request: ARTTokenRequest) in
expect(request.clientId).to(beNil())
expect(request.mac).to(equal("4rr4J+JzjiCL1DoS8wq7k11Z4oTGCb1PoeN+yGjkaH4="))
expect(request.capability).to(equal("{\"test\":[\"publish\"]}"))
expect(request.ttl).to(beNil())
expect(request.timestamp).to(equal(Date(timeIntervalSince1970: 1479087321.934)))
expect(request.keyName).to(equal("xxxxxx.yyyyyy"))
expect(request.nonce).to(equal("7830658976108826"))
}
)
]

for (caseName, (json, check)) in cases {
context(caseName) {
it("accepts a string, which should be interpreted as JSON") {
check(try! ARTTokenRequest.fromJson(json as ARTJsonCompatible))
}

it("accepts a NSDictionary") {
let data = json.data(using: String.Encoding.utf8)!
let dict = try! JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as! NSDictionary
check(try! ARTTokenRequest.fromJson(dict))
}
func reusableTestsTestTokenRequestFromJson(_ json: String, check: @escaping (_ request: ARTTokenRequest) -> Void) {
it("accepts a string, which should be interpreted as JSON") {
check(try! ARTTokenRequest.fromJson(json as ARTJsonCompatible))
}

it("accepts a NSDictionary") {
let data = json.data(using: String.Encoding.utf8)!
let dict = try! JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as! NSDictionary
check(try! ARTTokenRequest.fromJson(dict))
}
}

context("with TTL") {
reusableTestsTestTokenRequestFromJson("{" +
" \"clientId\":\"myClientId\"," +
" \"mac\":\"4rr4J+JzjiCL1DoS8wq7k11Z4oTGCb1PoeN+yGjkaH4=\"," +
" \"capability\":\"{\\\"test\\\":[\\\"publish\\\"]}\"," +
" \"ttl\":42000," +
" \"timestamp\":1479087321934," +
" \"keyName\":\"xxxxxx.yyyyyy\"," +
" \"nonce\":\"7830658976108826\"" +
"}") { request in
expect(request.clientId).to(equal("myClientId"))
expect(request.mac).to(equal("4rr4J+JzjiCL1DoS8wq7k11Z4oTGCb1PoeN+yGjkaH4="))
expect(request.capability).to(equal("{\"test\":[\"publish\"]}"))
expect(request.ttl as? TimeInterval).to(equal(TimeInterval(42)))
expect(request.timestamp).to(equal(Date(timeIntervalSince1970: 1479087321.934)))
expect(request.keyName).to(equal("xxxxxx.yyyyyy"))
expect(request.nonce).to(equal("7830658976108826"))
}
}

context("without TTL") {
reusableTestsTestTokenRequestFromJson("{" +
" \"mac\":\"4rr4J+JzjiCL1DoS8wq7k11Z4oTGCb1PoeN+yGjkaH4=\"," +
" \"capability\":\"{\\\"test\\\":[\\\"publish\\\"]}\"," +
" \"timestamp\":1479087321934," +
" \"keyName\":\"xxxxxx.yyyyyy\"," +
" \"nonce\":\"7830658976108826\"" +
"}") { request in
expect(request.clientId).to(beNil())
expect(request.mac).to(equal("4rr4J+JzjiCL1DoS8wq7k11Z4oTGCb1PoeN+yGjkaH4="))
expect(request.capability).to(equal("{\"test\":[\"publish\"]}"))
expect(request.ttl).to(beNil())
expect(request.timestamp).to(equal(Date(timeIntervalSince1970: 1479087321.934)))
expect(request.keyName).to(equal("xxxxxx.yyyyyy"))
expect(request.nonce).to(equal("7830658976108826"))
}
}

Expand Down
102 changes: 54 additions & 48 deletions Spec/Crypto.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,58 +125,64 @@ class Crypto : QuickSpec {
}
}

for cryptoFixture in CryptoTest.fixtures {
context("with fixtures from \(cryptoFixture.fileName).json") {
let (key, iv, items) = AblyTests.loadCryptoTestData(cryptoFixture.fileName)
let decoder = ARTDataEncoder.init(cipherParams: nil, error: nil)
let cipherParams = ARTCipherParams(algorithm: "aes", key: key as ARTCipherKeyCompatible, iv: iv)
let encrypter = ARTDataEncoder.init(cipherParams: cipherParams, error: nil)

func extractMessage(_ fixture: AblyTests.CryptoTestItem.TestMessage) -> ARTMessage {
let msg = ARTMessage(name: fixture.name, data: fixture.data)
msg.encoding = fixture.encoding
return msg
}

it("should encrypt messages as expected in the fixtures") {
for item in items {
let fixture = extractMessage(item.encoded)
let encryptedFixture = extractMessage(item.encrypted)
expect(encryptedFixture.encoding).to(endWith("\(cryptoFixture.expectedEncryptedEncoding)/base64"))

var error: NSError?
let decoded = fixture.decode(with: decoder, error: &error) as! ARTMessage
expect(error).to(beNil())
expect(decoded).notTo(beNil())

let encrypted = decoded.encode(with: encrypter, error: &error)
expect(error).to(beNil())
expect(encrypted).notTo(beNil())

expect((encrypted as! ARTMessage)).to(equal(encryptedFixture))
}
func reusableTestsTestFixture(_ cryptoFixture: ( fileName: String, expectedEncryptedEncoding: String, keyLength: UInt)) {
let (key, iv, items) = AblyTests.loadCryptoTestData(cryptoFixture.fileName)
let decoder = ARTDataEncoder.init(cipherParams: nil, error: nil)
let cipherParams = ARTCipherParams(algorithm: "aes", key: key as ARTCipherKeyCompatible, iv: iv)
let encrypter = ARTDataEncoder.init(cipherParams: cipherParams, error: nil)

func extractMessage(_ fixture: AblyTests.CryptoTestItem.TestMessage) -> ARTMessage {
let msg = ARTMessage(name: fixture.name, data: fixture.data)
msg.encoding = fixture.encoding
return msg
}

it("should encrypt messages as expected in the fixtures") {
for item in items {
let fixture = extractMessage(item.encoded)
let encryptedFixture = extractMessage(item.encrypted)
expect(encryptedFixture.encoding).to(endWith("\(cryptoFixture.expectedEncryptedEncoding)/base64"))

var error: NSError?
let decoded = fixture.decode(with: decoder, error: &error) as! ARTMessage
expect(error).to(beNil())
expect(decoded).notTo(beNil())

let encrypted = decoded.encode(with: encrypter, error: &error)
expect(error).to(beNil())
expect(encrypted).notTo(beNil())

expect((encrypted as! ARTMessage)).to(equal(encryptedFixture))
}

it("should decrypt messages as expected in the fixtures") {
for item in items {
let fixture = extractMessage(item.encoded)
let encryptedFixture = extractMessage(item.encrypted)
expect(encryptedFixture.encoding).to(endWith("\(cryptoFixture.expectedEncryptedEncoding)/base64"))

var error: NSError?
let decoded = fixture.decode(with: decoder, error: &error) as! ARTMessage
expect(error).to(beNil())
expect(decoded).notTo(beNil())

let decrypted = encryptedFixture.decode(with: encrypter, error: &error)
expect(error).to(beNil())
expect(decrypted).notTo(beNil())
expect((decrypted as! ARTMessage)).to(equal(decoded))
}
}

it("should decrypt messages as expected in the fixtures") {
for item in items {
let fixture = extractMessage(item.encoded)
let encryptedFixture = extractMessage(item.encrypted)
expect(encryptedFixture.encoding).to(endWith("\(cryptoFixture.expectedEncryptedEncoding)/base64"))

var error: NSError?
let decoded = fixture.decode(with: decoder, error: &error) as! ARTMessage
expect(error).to(beNil())
expect(decoded).notTo(beNil())

let decrypted = encryptedFixture.decode(with: encrypter, error: &error)
expect(error).to(beNil())
expect(decrypted).notTo(beNil())

expect((decrypted as! ARTMessage)).to(equal(decoded))
}
}
}

context("with fixtures from crypto-data-128.json") {
reusableTestsTestFixture(("crypto-data-128", "cipher+aes-128-cbc", 128))
}

context("with fixtures from crypto-data-256.json") {
reusableTestsTestFixture(("crypto-data-256", "cipher+aes-256-cbc", 256))
}
}
}
}
Loading

0 comments on commit 5093927

Please sign in to comment.