PendingMessageTests.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 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 PendingMessageTests: XCTestCase {
  23. func testArchivePendingMessage() throws {
  24. // Inputs
  25. let senderId = "ABCDEFGH"
  26. let messageId = "0123456789abcdef"
  27. let threemaDict = [
  28. "cmd": "newmsg",
  29. "from": senderId,
  30. "nick": "Hansmuster",
  31. "messageId": messageId,
  32. "voip": "true",
  33. ]
  34. // Expected data
  35. let threemPushNotification = try ThreemaPushNotification(from: threemaDict)
  36. // Processing
  37. let pendingMessage = PendingMessage(senderIdentity: senderId, messageIdentity: messageId, pushPayload: threemaDict)
  38. let archivedPendingMessage = NSKeyedArchiver.archivedData(withRootObject: pendingMessage)
  39. let unarchivedMessage = NSKeyedUnarchiver.unarchiveObject(with: archivedPendingMessage) as? PendingMessage
  40. // Validation
  41. let message = try XCTUnwrap(unarchivedMessage)
  42. let actualThreemaPush = try XCTUnwrap(message.threemaPushNotification)
  43. XCTAssertEqual(message.senderId, senderId)
  44. XCTAssertEqual(message.messageId, messageId)
  45. verifyThreemPushNotifiaction(actual: actualThreemaPush, expected: threemPushNotification)
  46. }
  47. func testArchivePendingMessageNoVoip() throws {
  48. // Inputs
  49. let senderId = "ABCDEFGH"
  50. let messageId = "0123456789abcdef"
  51. let threemaDict = [
  52. "cmd": "newmsg",
  53. "from": senderId,
  54. "nick": "Hansmuster",
  55. "messageId": messageId,
  56. ]
  57. // Expected data
  58. let threemPushNotification = try ThreemaPushNotification(from: threemaDict)
  59. // Processing
  60. let pendingMessage = PendingMessage(senderIdentity: senderId, messageIdentity: messageId, pushPayload: threemaDict)
  61. let archivedPendingMessage = NSKeyedArchiver.archivedData(withRootObject: pendingMessage)
  62. let unarchivedMessage = NSKeyedUnarchiver.unarchiveObject(with: archivedPendingMessage) as? PendingMessage
  63. // Validation
  64. let message = try XCTUnwrap(unarchivedMessage)
  65. let actualThreemaPush = try XCTUnwrap(message.threemaPushNotification)
  66. XCTAssertEqual(message.senderId, senderId)
  67. XCTAssertEqual(message.messageId, messageId)
  68. verifyThreemPushNotifiaction(actual: actualThreemaPush, expected: threemPushNotification)
  69. }
  70. func testUnarchivingOldThreemaPushNotificationDictionary() throws {
  71. // Expected data
  72. let senderId = "ABCDEFGH"
  73. let messageId = "0123456789abcdef"
  74. let threemaDict = [
  75. "cmd": "newmsg",
  76. "from": senderId,
  77. "nick": "Hansmuster",
  78. "messageId": messageId,
  79. "voip": "true",
  80. ]
  81. let threemPushNotification = try ThreemaPushNotification(from: threemaDict)
  82. // Processing
  83. let testBundle = Bundle(for: PendingMessageTests.self)
  84. let tempArchivePath = testBundle.path(forResource: "PendingMessage", ofType: "plist", inDirectory: nil)
  85. let archivePath = try XCTUnwrap(tempArchivePath)
  86. let unarchivedMessage = NSKeyedUnarchiver.unarchiveObject(withFile: archivePath) as? PendingMessage
  87. // Validation
  88. let message = try XCTUnwrap(unarchivedMessage)
  89. let actualThreemaPush = try XCTUnwrap(message.threemaPushNotification)
  90. XCTAssertEqual(message.senderId, senderId)
  91. XCTAssertEqual(message.messageId, messageId)
  92. verifyThreemPushNotifiaction(actual: actualThreemaPush, expected: threemPushNotification)
  93. }
  94. func testUnarchivingOldThreemaPushNotificationDictionaryNoVoip() throws {
  95. // Expected data
  96. let senderId = "ABCDEFGH"
  97. let messageId = "0123456789abcdef"
  98. let threemaDict = [
  99. "cmd": "newmsg",
  100. "from": senderId,
  101. "nick": "Hansmuster",
  102. "messageId": messageId,
  103. ]
  104. let threemPushNotification = try ThreemaPushNotification(from: threemaDict)
  105. // Processing
  106. let testBundle = Bundle(for: PendingMessageTests.self)
  107. let tempArchivePath = testBundle.path(forResource: "PendingMessageNoVoip", ofType: "plist", inDirectory: nil)
  108. let archivePath = try XCTUnwrap(tempArchivePath)
  109. let unarchivedMessage = NSKeyedUnarchiver.unarchiveObject(withFile: archivePath) as? PendingMessage
  110. // Validation
  111. let message = try XCTUnwrap(unarchivedMessage)
  112. let actualThreemaPush = try XCTUnwrap(message.threemaPushNotification)
  113. XCTAssertEqual(message.senderId, senderId)
  114. XCTAssertEqual(message.messageId, messageId)
  115. verifyThreemPushNotifiaction(actual: actualThreemaPush, expected: threemPushNotification)
  116. }
  117. }
  118. // MARK: - Helper functions
  119. private extension PendingMessageTests {
  120. func verifyThreemPushNotifiaction(actual: ThreemaPushNotification, expected: ThreemaPushNotification) {
  121. XCTAssertEqual(actual.command, expected.command)
  122. XCTAssertEqual(actual.from, expected.from)
  123. XCTAssertEqual(actual.nickname, expected.nickname)
  124. XCTAssertEqual(actual.messageId, expected.messageId)
  125. XCTAssertEqual(actual.voip, expected.voip)
  126. }
  127. }