Skip to content

Commit

Permalink
Merge pull request #1437 from m-herold/CATTY-382
Browse files Browse the repository at this point in the history
CATTY-382 Fix md5 for large NSData
  • Loading branch information
m-herold authored Jul 22, 2020
2 parents 2018e8c + eddbb4d commit bdde576
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

@objc extension NSData {
func md5() -> String {
let data = Data(self)
let data = Data(referencing: self)
return data.md5()
}
}
92 changes: 33 additions & 59 deletions src/CattyTests/Extensions/NSDataExtensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,64 +25,38 @@ import XCTest
@testable import Pocket_Code

final class NSDataExtensionTests: XCTestCase {
func testMD5() {

guard let data1 = "".data(using: .ascii) else {
XCTFail("Failed to get string into data using ASCII encoding")
return
}
let testData1 = NSData(data: data1)
let correctOutput1 = "d41d8cd98f00b204e9800998ecf8427e"

guard let data2 = "a".data(using: .ascii) else {
XCTFail("Failed to get string into data using ASCII encoding")
return
}
let testData2 = NSData(data: data2)
let correctOutput2 = "0cc175b9c0f1b6a831c399e269772661"

guard let data3 = "abc".data(using: .ascii) else {
XCTFail("Failed to get string into data using ASCII encoding")
return
}
let testData3 = NSData(data: data3)
let correctOutput3 = "900150983cd24fb0d6963f7d28e17f72"

guard let data4 = "message digest".data(using: .ascii) else {
XCTFail("Failed to get string into data using ASCII encoding")
return
}
let testData4 = NSData(data: data4)
let correctOutput4 = "f96b697d7cb7938d525a2f31aaf161d0"

guard let data5 = "abcdefghijklmnopqrstuvwxyz".data(using: .ascii) else {
XCTFail("Failed to get string into data using ASCII encoding")
return
}
let testData5 = NSData(data: data5)
let correctOutput5 = "c3fcd3d76192e4007dfb496cca67e13b"

guard let data6 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".data(using: .ascii) else {
XCTFail("Failed to get string into data using ASCII encoding")
return
}
let testData6 = NSData(data: data6)
let correctOutput6 = "d174ab98d277d9f5a5611c2c9f419d9f"

guard let data7 = "12345678901234567890123456789012345678901234567890123456789012345678901234567890".data(using: .ascii) else {
XCTFail("Failed to get string into data using ASCII encoding")
return
}
let testData7 = NSData(data: data7)
let correctOutput7 = "57edf4a22be3c955ac49da2e2107b67a"

XCTAssertEqual(testData1.md5(), correctOutput1)
XCTAssertEqual(testData2.md5(), correctOutput2)
XCTAssertEqual(testData3.md5(), correctOutput3)
XCTAssertEqual(testData4.md5(), correctOutput4)
XCTAssertEqual(testData5.md5(), correctOutput5)
XCTAssertEqual(testData6.md5(), correctOutput6)
XCTAssertEqual(testData7.md5(), correctOutput7)

}
func testMD5() {
guard let data1 = "".data(using: .ascii) else {
XCTFail("Failed to get string into data using ASCII encoding")
return
}
let testData1 = NSData(data: data1)
let correctOutput1 = "d41d8cd98f00b204e9800998ecf8427e"

guard let data2 = "a".data(using: .ascii) else {
XCTFail("Failed to get string into data using ASCII encoding")
return
}
let testData2 = NSData(data: data2)
let correctOutput2 = "0cc175b9c0f1b6a831c399e269772661"

XCTAssertEqual(testData1.md5(), correctOutput1)
XCTAssertEqual(testData2.md5(), correctOutput2)
}

func testMD5WithLargeData() {
let fiveHundredMegabyteData = NSData(data: Data(count: 524288000))
let expectation = self.expectation(description: "md5 calculated")

DispatchQueue.global(qos: .userInitiated).async {
let md5 = fiveHundredMegabyteData.md5()
XCTAssertNotNil(md5)
expectation.fulfill()
}

waitForExpectations(timeout: 10) { error in
XCTAssertNil(error)
}
}
}

0 comments on commit bdde576

Please sign in to comment.