DbLoadTests.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2019-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import XCTest
  21. @testable import Threema
  22. class DbLoadTests: XCTestCase {
  23. override func setUp() {
  24. // necessary for ValidationLogger
  25. AppGroup.setGroupId("group.ch.threema") //THREEMA_GROUP_IDENTIFIER @"group.ch.threema"
  26. }
  27. /**
  28. Print out shell commands to copy database and external data form simulator. To prepare database before, run methods in this file like 'testDbLoad'.
  29. */
  30. func testCopyOldVersionOfDatabase() {
  31. let databasePath = DocumentManager.databaseDirectory()?.path
  32. if databasePath != nil {
  33. print("\nCopy 'old' version of database for testing DB migration:")
  34. print("""
  35. \n
  36. cd \(databasePath!)
  37. tar cf ThreemaDataOldVersion.tar.gz .ThreemaData_SUPPORT ThreemaData.sqlite
  38. mv ThreemaDataOldVersion.tar.gz ~/Documents/
  39. cd ~/Documents
  40. mkdir ThreemaDataOldVersion
  41. tar xf ThreemaDataOldVersion.tar.gz -C ./ThreemaDataOldVersion
  42. \n
  43. """)
  44. print("As last step copy the directory 'ThreemaDataOldVersion' via iTunes into 'applicationDocuments' of Threema.\n")
  45. }
  46. XCTAssertNotNil(databasePath)
  47. }
  48. /**
  49. Adding conversation with ECHOECHO and adding 400 messages like images (./Resources/Bild-1.jpg 3.5MB), audio (./Resources/SmallVoice.mp3), videos (./Resources/Video-1.mp4), files (./Resources/Test.pdf) and texts. Resulting in
  50. */
  51. func testDbLoad() {
  52. print(MyIdentityStore.shared().identity)
  53. var conversation: Conversation? = nil
  54. let entityManager = EntityManager(forBackgroundProcess: false)
  55. entityManager?.performSyncBlockAndSafe({
  56. if let contact = entityManager?.entityFetcher?.contact(forId: "ECHOECHO") {
  57. conversation = (entityManager?.conversation(for: contact, createIfNotExisting: true))!
  58. }
  59. })
  60. let testBundle: Bundle = Bundle(for: DbLoadTests.self)
  61. let testImageUrl = testBundle.url(forResource: "Bild-1-0", withExtension: "jpg")
  62. let testImageData = try? Data(contentsOf: testImageUrl!)
  63. let testAudioUrl = testBundle.url(forResource: "SmallVoice", withExtension: "mp3")
  64. let testAudioData = try? Data(contentsOf: testAudioUrl!)
  65. let testVideoUrl = testBundle.url(forResource: "Video-1", withExtension: "mp4")
  66. let testVideoData = try? Data(contentsOf: testVideoUrl!)
  67. let testVideoThumbnailUrl = testBundle.url(forResource: "Video-1-Thumbnail", withExtension: "png")
  68. let testVideoThumbnailData = try? Data(contentsOf: testVideoThumbnailUrl!)
  69. let testFileUrl = testBundle.url(forResource: "Test", withExtension: "pdf")
  70. let testFileData = try? Data(contentsOf: testFileUrl!)
  71. for ii in 1...20 {
  72. entityManager?.performSyncBlockAndSafe({
  73. for i in 1...20 {
  74. let calendar = Calendar.current
  75. let date = calendar.date(byAdding: .day, value: -10 * (i + ii), to: Date())
  76. if i % 5 == 0 {
  77. let message: TextMessage = (entityManager?.entityCreator.textMessage(for: conversation))!
  78. message.text = "test \(ii)-\(i)"
  79. message.date = date
  80. message.sender = conversation?.contact
  81. message.sent = true
  82. message.delivered = true
  83. message.read = true
  84. message.remoteSentDate = Date()
  85. }
  86. else if i % 4 == 0 {
  87. let dbAudio: AudioData = (entityManager?.entityCreator.audioData())!
  88. dbAudio.data = testAudioData
  89. let message: AudioMessage = (entityManager?.entityCreator.audioMessage(for: conversation))!
  90. message.audio = dbAudio
  91. message.duration = NSNumber(integerLiteral: 2)
  92. message.date = date
  93. message.sender = conversation?.contact
  94. message.sent = true
  95. message.delivered = true
  96. message.read = true
  97. message.remoteSentDate = Date()
  98. }
  99. else if i % 3 == 0 {
  100. let dbVideo: VideoData = (entityManager?.entityCreator.videoData())!
  101. dbVideo.data = testVideoData
  102. let thumbnail: UIImage = UIImage(data: testVideoThumbnailData!)!
  103. let thumbnailData: Data = thumbnail.jpegData(compressionQuality: CGFloat.init(90.0))!
  104. let dbThumbnail: ImageData = (entityManager?.entityCreator.imageData())!
  105. dbThumbnail.data = thumbnailData;
  106. dbThumbnail.width = NSNumber(value: Float(thumbnail.size.width))
  107. dbThumbnail.height = NSNumber(value: Float(thumbnail.size.height))
  108. let message: VideoMessage = (entityManager?.entityCreator.videoMessage(for: conversation))!
  109. message.video = dbVideo
  110. message.thumbnail = dbThumbnail
  111. message.duration = NSNumber(integerLiteral: 5)
  112. message.date = date
  113. message.sender = conversation?.contact
  114. message.sent = true
  115. message.delivered = true
  116. message.read = true
  117. message.remoteSentDate = Date()
  118. }
  119. else if i % 2 == 0 {
  120. let dbFile: FileData = (entityManager?.entityCreator.fileData())!
  121. dbFile.data = testFileData
  122. let message: FileMessage = (entityManager?.entityCreator.fileMessage(for: conversation))!
  123. message.data = dbFile
  124. message.fileName = "Test.pdf"
  125. message.fileSize = NSNumber(integerLiteral: dbFile.data.count)
  126. message.mimeType = "application/pdf"
  127. message.type = NSNumber(integerLiteral: 0);
  128. message.date = date
  129. message.sender = conversation?.contact
  130. message.sent = true
  131. message.delivered = true
  132. message.read = true
  133. message.remoteSentDate = Date()
  134. }
  135. else {
  136. let dbImage: ImageData = (entityManager?.entityCreator.imageData())!
  137. dbImage.data = testImageData
  138. let image: UIImage = UIImage(data: testImageData!)!
  139. let thumbnail: UIImage = MediaConverter.getThumbnailFor(image)
  140. let thumbnailData: Data = thumbnail.jpegData(compressionQuality: CGFloat.init(90.0))!
  141. let dbThumbnail: ImageData = (entityManager?.entityCreator.imageData())!
  142. dbThumbnail.data = thumbnailData;
  143. dbThumbnail.width = NSNumber(value: Float(image.size.width))
  144. dbThumbnail.height = NSNumber(value: Float(image.size.height))
  145. let message: ImageMessage = (entityManager?.entityCreator.imageMessage(for: conversation))!
  146. message.thumbnail = dbThumbnail;
  147. message.image = dbImage;
  148. message.date = date
  149. message.sender = conversation?.contact
  150. message.sent = true
  151. message.delivered = true
  152. message.read = true
  153. message.remoteSentDate = Date()
  154. }
  155. }
  156. })
  157. }
  158. }
  159. /**
  160. Adding 160 contacts with Threema ID's (./Resources/test_ids.txt) for testing.
  161. */
  162. func testLoadContacts() {
  163. let testBundle: Bundle = Bundle(for: DbLoadTests.self)
  164. if let filePath = testBundle.path(forResource: "test_ids", ofType: "txt") {
  165. let queue = DispatchQueue.global()
  166. let semaphore = DispatchSemaphore(value: 0)
  167. var pks = [String: Data]()
  168. do {
  169. var fetchIdentities = [String]()
  170. let ids = try String(contentsOfFile: filePath, encoding: .utf8)
  171. for id in ids.components(separatedBy: .newlines) {
  172. if id.count > 0 {
  173. fetchIdentities.append(id)
  174. }
  175. }
  176. queue.async {
  177. let api = ServerAPIConnector()
  178. api.fetchBulkIdentityInfo(fetchIdentities, onCompletion: { (identities, publicKeys, featureMasks, states, types) in
  179. for index in 0..<(identities!.count-1) {
  180. pks[identities![index] as! String] = Data(base64Encoded: publicKeys![index] as! String)
  181. }
  182. semaphore.signal()
  183. }) { (error) in
  184. print(error?.localizedDescription)
  185. semaphore.signal()
  186. }
  187. }
  188. }
  189. catch {
  190. print(error)
  191. }
  192. semaphore.wait()
  193. let entityManager = EntityManager(forBackgroundProcess: false)
  194. for pk in pks {
  195. print("add id: \(pk.key)")
  196. entityManager?.performSyncBlockAndSafe({
  197. if let contact = entityManager?.entityCreator.contact() {
  198. contact.identity = pk.key
  199. contact.verificationLevel = 0
  200. contact.publicNickname = pk.key
  201. contact.hidden = 0
  202. contact.workContact = 0
  203. contact.publicKey = pk.value
  204. }
  205. })
  206. }
  207. }
  208. }
  209. func testPerformanceExample() {
  210. // This is an example of a performance test case.
  211. self.measure {
  212. // Put the code you want to measure the time of here.
  213. }
  214. }
  215. }