2
0
Эх сурвалжийг харах

Do not run performance tests on CI. at all.

Marcin Krzyżanowski 8 жил өмнө
parent
commit
4eea49c46a

+ 52 - 39
Tests/CryptoSwiftTests/AESTests.swift

@@ -294,8 +294,23 @@ final class AESTests: XCTestCase {
         }
     }
 
-    func testAES_encrypt_performance() {
-        #if !CI
+    func testAESWithWrongKey() {
+        let key:Array<UInt8> = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
+        let key2:Array<UInt8> = [0x22,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x33];
+        let iv:Array<UInt8> = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F]
+        let plaintext:Array<UInt8> = [49, 46, 50, 50, 50, 51, 51, 51, 51]
+
+        let aes = try! AES(key: key, iv:iv, blockMode: .CBC, padding: PKCS7())
+        let aes2 = try! AES(key: key2, iv:iv, blockMode: .CBC, padding: PKCS7())
+        let encrypted = try! aes.encrypt(plaintext)
+        let decrypted = try? aes2.decrypt(encrypted)
+        XCTAssertTrue(decrypted! != plaintext, "failed")
+    }
+}
+
+#if !CI
+extension AESTests {
+    func testAESEncryptPerformance() {
         let key:Array<UInt8> = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
         let iv:Array<UInt8> = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F]
         let message = Array<UInt8>(repeating: 7, count: 1024 * 1024)
@@ -303,11 +318,9 @@ final class AESTests: XCTestCase {
         measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: true, for: { () -> Void in
             _ = try! aes.encrypt(message)
         })
-        #endif
     }
 
-    func testAES_decrypt_performance() {
-        #if !CI
+    func testAESDecryptPerformance() {
         let key:Array<UInt8> = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
         let iv:Array<UInt8> = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F]
         let message = Array<UInt8>(repeating: 7, count: 1024 * 1024)
@@ -315,41 +328,41 @@ final class AESTests: XCTestCase {
         measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: true, for: { () -> Void in
             _ = try! aes.decrypt(message)
         })
-        #endif
     }
+}
+#endif
+
+
+extension AESTests {
+    static func allTests() -> [(String, (AESTests) -> () -> Void)] {
+        var tests = [
+            ("testAESEncrypt2", testAESEncrypt2),
+            ("testAESEncrypt3", testAESEncrypt3),
+            ("testAESEncrypt", testAESEncrypt),
+            ("testAESEncryptCBCNoPadding", testAESEncryptCBCNoPadding),
+            ("testAESEncryptCBCWithPadding", testAESEncryptCBCWithPadding),
+            ("testAESEncryptCBCWithPaddingPartial", testAESEncryptCBCWithPaddingPartial),
+            ("testAESEncryptIncremental", testAESEncryptIncremental),
+            ("testAESDecryptCBCWithPaddingPartial", testAESDecryptCBCWithPaddingPartial),
+            ("testAESEncryptCFB", testAESEncryptCFB),
+            ("testAESEncryptCFBLong", testAESEncryptCFBLong),
+            ("testAESEncryptOFB128", testAESEncryptOFB128),
+            ("testAESEncryptOFB256", testAESEncryptOFB256),
+            ("testAESEncryptPCBC256", testAESEncryptPCBC256),
+            ("testAESEncryptCTR", testAESEncryptCTR),
+            ("testAESEncryptCTRIrregularLength", testAESEncryptCTRIrregularLength),
+            ("testAESDecryptCTRSeek", testAESDecryptCTRSeek),
+            ("testAESEncryptCTRIrregularLengthIncrementalUpdate", testAESEncryptCTRIrregularLengthIncrementalUpdate),
+            ("testIssue298", testIssue298),
+            ("testAESWithWrongKey", testAESWithWrongKey)
+        ]
 
-    func testAESWithWrongKey() {
-        let key:Array<UInt8> = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
-        let key2:Array<UInt8> = [0x22,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x33];
-        let iv:Array<UInt8> = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F]
-        let plaintext:Array<UInt8> = [49, 46, 50, 50, 50, 51, 51, 51, 51]
-
-        let aes = try! AES(key: key, iv:iv, blockMode: .CBC, padding: PKCS7())
-        let aes2 = try! AES(key: key2, iv:iv, blockMode: .CBC, padding: PKCS7())
-        let encrypted = try! aes.encrypt(plaintext)
-        let decrypted = try? aes2.decrypt(encrypted)
-        XCTAssertTrue(decrypted! != plaintext, "failed")
+        #if !CI
+            tests += [
+                ("testAESEncryptPerformance", testAESEncryptPerformance),
+                ("testAESDecryptPerformance", testAESDecryptPerformance)
+            ]
+        #endif
+        return tests
     }
-
-    static let allTests =  [
-        ("testAESEncrypt2", testAESEncrypt2),
-        ("testAESEncrypt3", testAESEncrypt3),
-        ("testAESEncrypt", testAESEncrypt),
-        ("testAESEncryptCBCNoPadding", testAESEncryptCBCNoPadding),
-        ("testAESEncryptCBCWithPadding", testAESEncryptCBCWithPadding),
-        ("testAESEncryptCBCWithPaddingPartial", testAESEncryptCBCWithPaddingPartial),
-        ("testAESEncryptIncremental", testAESEncryptIncremental),
-        ("testAESDecryptCBCWithPaddingPartial", testAESDecryptCBCWithPaddingPartial),
-        ("testAESEncryptCFB", testAESEncryptCFB),
-        ("testAESEncryptCFBLong", testAESEncryptCFBLong),
-        ("testAESEncryptOFB128", testAESEncryptOFB128),
-        ("testAESEncryptOFB256", testAESEncryptOFB256),
-        ("testAESEncryptPCBC256", testAESEncryptPCBC256),
-        ("testAESEncryptCTR", testAESEncryptCTR),
-        ("testAESEncryptCTRIrregularLength", testAESEncryptCTRIrregularLength),
-        ("testAESDecryptCTRSeek", testAESDecryptCTRSeek),
-        ("testAESEncryptCTRIrregularLengthIncrementalUpdate", testAESEncryptCTRIrregularLengthIncrementalUpdate),
-        ("testIssue298", testIssue298),
-        ("testAESWithWrongKey", testAESWithWrongKey)
-    ]
 }

+ 22 - 8
Tests/CryptoSwiftTests/ChaCha20Tests.swift

@@ -93,9 +93,11 @@ final class ChaCha20Tests: XCTestCase {
             XCTFail()
         }
     }
+}
 
+#if !CI
+extension ChaCha20Tests {
     func testChaCha20Performance() {
-        #if !CI
         let key:Array<UInt8> = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
         let iv:Array<UInt8> = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F]
         let message = Array<UInt8>(repeating: 7, count: (1024 * 1024) * 1)
@@ -107,13 +109,25 @@ final class ChaCha20Tests: XCTestCase {
             }
             self.stopMeasuring()
         })
+    }
+}
+#endif
+
+
+extension ChaCha20Tests {
+    static func allTests() -> [(String, (ChaCha20Tests) -> () -> Void)] {
+        var tests = [
+            ("testChaCha20", testChaCha20),
+            ("testVector1Py", testVector1Py),
+            ("testChaCha20EncryptPartial", testChaCha20EncryptPartial)
+            ]
+
+        #if !CI
+            tests += [
+                ("testChaCha20Performance", testChaCha20Performance)
+            ]
         #endif
+
+        return tests
     }
-    
-    static let allTests =  [
-        ("testChaCha20", testChaCha20),
-        ("testVector1Py", testVector1Py),
-        ("testChaCha20EncryptPartial", testChaCha20EncryptPartial),
-        ("testChaCha20Performance", testChaCha20Performance)
-    ]
 }

+ 49 - 38
Tests/CryptoSwiftTests/DigestTests.swift

@@ -42,27 +42,6 @@ final class DigestTests: XCTestCase {
         }
     }
 
-    func testMD5PerformanceSwift() {
-        #if !CI
-        self.measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: false, for: { () -> Void in
-            let arr = Array<UInt8>(repeating: 200, count: 1024 * 1024)
-            self.startMeasuring()
-            _ = Digest.md5(arr)
-            self.stopMeasuring()
-        })
-        #endif
-    }
-
-    func testSHA1Performance() {
-        #if !CI
-        self.measure {
-            for _ in 0..<10_000 {
-                let _ = "".sha1()
-            }
-        }
-        #endif
-    }
-
     func testSHA1() {
         let data:Data = Data(bytes: UnsafePointer<UInt8>([0x31, 0x32, 0x33] as Array<UInt8>), count: 3)
         XCTAssertEqual(data.sha1().toHexString(), "40bd001563085fc35165329ea1ff5c5ecbdbbeef", "SHA1 calculation failed");
@@ -154,22 +133,54 @@ final class DigestTests: XCTestCase {
         let data:Data = Data(bytes: UnsafePointer<UInt8>([49, 50, 51] as Array<UInt8>), count: 3)
         XCTAssert(data.checksum() == 0x96, "Invalid checksum")
     }
+}
+
+#if !CI
+extension DigestTests {
+    func testMD5Performance() {
+        self.measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: false, for: { () -> Void in
+            let arr = Array<UInt8>(repeating: 200, count: 1024 * 1024)
+            self.startMeasuring()
+            _ = Digest.md5(arr)
+            self.stopMeasuring()
+        })
+    }
 
-    static let allTests =  [
-        ("testMD5Data", testMD5Data),
-        ("testMD5String", testMD5String),
-        ("testMD5Updates", testMD5Updates),
-        ("testMD5PerformanceSwift", testMD5PerformanceSwift),
-        ("testSHA1Performance", testSHA1Performance),
-        ("testSHA1", testSHA1),
-        ("testSHA224", testSHA224),
-        ("testSHA256", testSHA256),
-        ("testSHA384", testSHA384),
-        ("testSHA512", testSHA512),
-        ("testSHA3", testSHA3),
-        ("testCRC32", testCRC32),
-        ("testCRC32NotReflected", testCRC32NotReflected),
-        ("testCRC15", testCRC16),
-        ("testChecksum", testChecksum)
-    ]
+    func testSHA1Performance() {
+        self.measure {
+            for _ in 0..<10_000 {
+                let _ = "".sha1()
+            }
+        }
+    }
+}
+#endif
+
+extension DigestTests {
+    static func allTests() -> [(String, (DigestTests) -> () -> Void)] {
+        var tests = [
+            ("testMD5Data", testMD5Data),
+            ("testMD5String", testMD5String),
+            ("testMD5Updates", testMD5Updates),
+            ("testSHA1", testSHA1),
+            ("testSHA224", testSHA224),
+            ("testSHA256", testSHA256),
+            ("testSHA384", testSHA384),
+            ("testSHA512", testSHA512),
+            ("testSHA3", testSHA3),
+            ("testCRC32", testCRC32),
+            ("testCRC32NotReflected", testCRC32NotReflected),
+            ("testCRC15", testCRC16),
+            ("testChecksum", testChecksum)
+        ]
+
+        #if !CI
+            tests += [
+                ("testMD5Performance", testMD5Performance),
+                ("testSHA1Performance", testSHA1Performance)
+            ]
+        #endif
+
+        return tests
+    }
 }

+ 32 - 20
Tests/CryptoSwiftTests/ExtensionsTest.swift

@@ -11,17 +11,6 @@ import Foundation
 
 final class ExtensionsTest: XCTestCase {
 
-    func testArrayChunksPerformance() {
-        #if !CI
-        measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: false, for: { () -> Void in
-            let message = Array<UInt8>(repeating: 7, count: 1024 * 1024)
-            self.startMeasuring()
-            _ = message.chunks(size: AES.blockSize)
-            self.stopMeasuring()
-        })
-        #endif
-    }
-    
     func testBytes() {
         let size = MemoryLayout<UInt32>.size // 32 or 64  bit
         
@@ -73,14 +62,37 @@ final class ExtensionsTest: XCTestCase {
         let hex = array.toHexString()
         XCTAssertEqual(str, hex)
     }
+}
+
+#if !CI
+extension ExtensionsTest {
+    func testArrayChunksPerformance() {
+        measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: false, for: { () -> Void in
+            let message = Array<UInt8>(repeating: 7, count: 1024 * 1024)
+            self.startMeasuring()
+            _ = message.chunks(size: AES.blockSize)
+            self.stopMeasuring()
+        })
+    }
+}
+#endif
+
+extension ExtensionsTest {
+    static func allTests() -> [(String, (ExtensionsTest) -> () -> Void)] {
+        var tests = [
+            ("testBytes", testBytes),
+            ("testToUInt32Array", testToUInt32Array),
+            ("testDataInit", testDataInit),
+            ("testStringEncrypt", testStringEncrypt),
+            ("testStringDecryptBase64", testStringDecryptBase64),
+            ("testArrayInitHex", testArrayInitHex)
+        ]
 
-    static let allTests =  [
-        ("testArrayChunksPerformance", testArrayChunksPerformance),
-        ("testBytes", testBytes),
-        ("testToUInt32Array", testToUInt32Array),
-        ("testDataInit", testDataInit),
-        ("testStringEncrypt", testStringEncrypt),
-        ("testStringDecryptBase64", testStringDecryptBase64),
-        ("testArrayInitHex", testArrayInitHex)
-    ]
+        #if !CI
+            tests += [
+                ("testArrayChunksPerformance", testArrayChunksPerformance)
+            ]
+        #endif
+        return tests
+    }
 }

+ 21 - 9
Tests/CryptoSwiftTests/RabbitTests.swift

@@ -108,9 +108,11 @@ class RabbitTests: XCTestCase {
             XCTAssertEqual(rabbit.decrypt(cipherText), plainText)
         }
     }
-    
+}
+
+#if !CI
+extension RabbitTests {
     func testRabbitPerformance() {
-        #if !CI
         let key: Array<UInt8> = Array<UInt8>(repeating: 0, count: Rabbit.keySize)
         let iv: Array<UInt8> = Array<UInt8>(repeating: 0, count: Rabbit.ivSize)
         let message = Array<UInt8>(repeating: 7, count: (1024 * 1024) * 1)
@@ -119,13 +121,23 @@ class RabbitTests: XCTestCase {
             self.stopMeasuring()
             XCTAssert(!encrypted.isEmpty, "not encrypted")
         })
+    }
+}
+#endif
+
+extension RabbitTests {
+    static func allTests() -> [(String, (RabbitTests) -> () -> Void)] {
+        var tests = [
+            ("testInitialization", testInitialization),
+            ("testRabbitWithoutIV", testRabbitWithoutIV),
+            ("testRabbitWithIV", testRabbitWithIV)
+        ]
+
+        #if !CI
+            tests += [
+                ("testRabbitPerformance", testRabbitPerformance)
+            ]
         #endif
+        return tests
     }
-    
-    static let allTests =  [
-        ("testInitialization", testInitialization),
-        ("testRabbitWithoutIV", testRabbitWithoutIV),
-        ("testRabbitWithIV", testRabbitWithIV),
-        ("testRabbitPerformance", testRabbitPerformance)
-    ]
 }

+ 5 - 5
Tests/LinuxMain.swift

@@ -2,13 +2,13 @@ import XCTest
 @testable import CryptoSwiftTests
 
 XCTMain([
-    testCase(DigestTests.allTests),
+    testCase(DigestTests.allTests()),
     testCase(Poly1305Tests.allTests),
     testCase(HMACTests.allTests),
-    testCase(AESTests.allTests),
-    testCase(ChaCha20Tests.allTests),
-    testCase(RabbitTests.allTests),
-    testCase(ExtensionsTest.allTests),
+    testCase(AESTests.allTests()),
+    testCase(ChaCha20Tests.allTests()),
+    testCase(RabbitTests.allTests()),
+    testCase(ExtensionsTest.allTests()),
     testCase(PaddingTests.allTests),
     testCase(PBKDF.allTests),
     testCase(RandomBytesSequenceTests.allTests),