You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1890 lines
69 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. //
  2. // PEPInternalSessionTest.m
  3. // pEpObjCAdapterTests
  4. //
  5. // Created by Andreas Buff on 18.01.18.
  6. // Copyright © 2018 pp. All rights reserved.
  7. //
  8. #import <XCTest/XCTest.h>
  9. #import "PEPObjCAdapterTypes_iOS.h"
  10. #import "PEPObjCAdapter_iOS.h"
  11. #import "NSNumber+PEPRating.h"
  12. #import "PEPObjCAdapter.h"
  13. #import "PEPMessage.h"
  14. #import "PEPAttachment.h"
  15. #import "PEPTestUtils.h"
  16. #import "PEPSync.h"
  17. #import "PEPSendMessageDelegate.h"
  18. #import "PEPInternalSessionTestNotifyHandshakeDelegate.h"
  19. #import "PEPInternalSessionTestSendMessageDelegate.h"
  20. #import "PEPPassphraseCache+Reset.h"
  21. #import "PEPPassphraseProviderMock.h"
  22. #import "PEPSessionProvider.h"
  23. #import "PEPInternalSession.h"
  24. #import "PEPIdentity+isPEPUser.h"
  25. #import "PEPInternalSession+SetIdentity.h"
  26. @interface PEPInternalSessionTest : XCTestCase
  27. @property (nonatomic) PEPSync *sync;
  28. @property (nonatomic) PEPInternalSessionTestSendMessageDelegate *sendMessageDelegate;
  29. @property (nonatomic) PEPInternalSessionTestNotifyHandshakeDelegate *notifyHandshakeDelegate;
  30. @end
  31. @implementation PEPInternalSessionTest
  32. - (void)setUp
  33. {
  34. [super setUp];
  35. [self pEpCleanUp];
  36. [PEPObjCAdapter setUnEncryptedSubjectEnabled:NO];
  37. NSError *error = nil;
  38. XCTAssertTrue([PEPObjCAdapter configurePassphraseForNewKeys:nil error:&error]);
  39. XCTAssertNil(error);
  40. [PEPPassphraseCache reset];
  41. }
  42. - (void)tearDown
  43. {
  44. [self shutdownSync];
  45. [self pEpCleanUp];
  46. [super tearDown];
  47. }
  48. - (void)testTrustWords
  49. {
  50. PEPInternalSession *session = [PEPSessionProvider session];
  51. NSError *error = nil;
  52. NSArray *trustwords = [session
  53. trustwordsForFingerprint:@"DB47DB47DB47DB47DB47DB47DB47DB47DB47DB47"
  54. languageID:@"en"
  55. shortened:false
  56. error:&error];
  57. XCTAssertNil(error);
  58. XCTAssertEqual([trustwords count], 10);
  59. for(id word in trustwords) {
  60. XCTAssertEqualObjects(word, @"BAPTISMAL");
  61. }
  62. }
  63. - (void)testGenKey
  64. {
  65. PEPInternalSession *session = [PEPSessionProvider session];
  66. PEPIdentity *identMe = [[PEPIdentity alloc]
  67. initWithAddress:@"pep.test.iosgenkey@pep-project.org"
  68. userID:@"Me"
  69. userName:@"pEp Test iOS GenKey"
  70. isOwn:YES];
  71. NSError *error = nil;
  72. XCTAssertTrue([session mySelf:identMe error:&error]);
  73. XCTAssertNil(error);
  74. XCTAssertNotNil(identMe.fingerPrint);
  75. XCTAssertNotEqual(identMe.commType, PEPCommTypeUnknown);
  76. XCTAssertTrue([identMe isPEPUser:session error:&error]);
  77. }
  78. - (void)testMySelfCommType
  79. {
  80. PEPInternalSession *session = [PEPSessionProvider session];
  81. PEPIdentity *identMe = [[PEPIdentity alloc]
  82. initWithAddress:@"pep.test.iosgenkey@pep-project.org"
  83. userID:@"pep.test.iosgenkey@pep-project.org_userID"
  84. userName:@"pEp Test iOS GenKey"
  85. isOwn:YES];
  86. NSError *error = nil;
  87. XCTAssertTrue([session mySelf:identMe error:&error]);
  88. XCTAssertNil(error);
  89. XCTAssertNotNil(identMe.fingerPrint);
  90. XCTAssertNotEqual(identMe.commType, PEPCommTypeUnknown);
  91. XCTAssertTrue([identMe isPEPUser:session error:&error]);
  92. dispatch_queue_t queue = dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0);
  93. dispatch_sync(queue, ^{
  94. NSError *innerError = nil;
  95. PEPInternalSession *session2 = [PEPSessionProvider session];
  96. // Now simulate an update from the app, which usually only caches
  97. // kPepUsername, kPepAddress and optionally kPepUserID.
  98. PEPIdentity *identMe2 = [[PEPIdentity alloc]
  99. initWithAddress:identMe.address
  100. userID:identMe.userID
  101. userName:identMe.userName
  102. isOwn:NO];
  103. XCTAssertTrue([session2 mySelf:identMe2 error:&innerError]);
  104. XCTAssertNil(innerError);
  105. XCTAssertNotNil(identMe2.fingerPrint);
  106. XCTAssertTrue([identMe2 isPEPUser:session error:&innerError]);
  107. XCTAssertEqualObjects(identMe2.fingerPrint, identMe.fingerPrint);
  108. // Now pretend the app only knows kPepUsername and kPepAddress
  109. PEPIdentity *identMe3 = [PEPTestUtils foreignPepIdentityWithAddress:identMe.address
  110. userName:identMe.userName];
  111. XCTAssertTrue([session2 mySelf:identMe3 error:&innerError]);
  112. XCTAssertNil(innerError);
  113. XCTAssertNotNil(identMe3.fingerPrint);
  114. XCTAssertTrue([identMe3 isPEPUser:session error:&innerError]);
  115. XCTAssertEqualObjects(identMe3.fingerPrint, identMe.fingerPrint);
  116. XCTAssertEqualObjects(identMe.address, identMe2.address);
  117. XCTAssertEqualObjects(identMe.address, identMe3.address);
  118. XCTAssertEqual(identMe.commType, identMe2.commType);
  119. XCTAssertEqual(identMe.commType, identMe3.commType);
  120. });
  121. }
  122. - (void)testPartnerWithoutFingerPrint
  123. {
  124. PEPInternalSession *session = [PEPSessionProvider session];
  125. PEPIdentity *identRandom = [[PEPIdentity alloc]
  126. initWithAddress:@"does_not_exist@example.com"
  127. userID:@"got_out"
  128. userName:@"No Way Not Even Alice"
  129. isOwn:NO];
  130. NSError *error = nil;
  131. XCTAssertTrue([session updateIdentity:identRandom error:&error]);
  132. XCTAssertNil(error);
  133. XCTAssertNil(identRandom.fingerPrint);
  134. }
  135. - (void)testImportPartnerKeys
  136. {
  137. XCTAssertNotNil([self checkImportingKeyFilePath:@"6FF00E97_sec.asc"
  138. address:@"pep.test.alice@pep-project.org"
  139. userID:@"This Is Alice"
  140. fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
  141. session: nil]);
  142. XCTAssertNotNil([self checkImportingKeyFilePath:@"0xC9C2EE39.asc"
  143. address:@"pep.test.bob@pep-project.org"
  144. userID:@"This Is Bob"
  145. fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"
  146. session: nil]);
  147. }
  148. - (void)testIdentityRating
  149. {
  150. PEPInternalSession *session = [PEPSessionProvider session];
  151. PEPIdentity *me = [self
  152. checkMySelfImportingKeyFilePath:@"6FF00E97_sec.asc"
  153. address:@"pep.test.alice@pep-project.org"
  154. userID:@"Alice_User_ID"
  155. fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
  156. session:session];
  157. XCTAssertEqual([self ratingForIdentity:me session:session], PEPRatingTrustedAndAnonymized);
  158. PEPIdentity *alice = [self
  159. checkImportingKeyFilePath:@"6FF00E97_sec.asc"
  160. address:@"pep.test.alice@pep-project.org"
  161. userID:@"This Is Alice"
  162. fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
  163. session: session];
  164. XCTAssertNotNil(alice);
  165. XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
  166. }
  167. /** ENGINE-409 */
  168. - (void)testIdentityRatingMistrustReset
  169. {
  170. PEPInternalSession *session = [PEPSessionProvider session];
  171. PEPIdentity *me = [[PEPIdentity alloc]
  172. initWithAddress:@"me@example.org"
  173. userID:@"me_myself"
  174. userName:@"Me Me"
  175. isOwn:YES];
  176. NSError *error = nil;
  177. XCTAssertTrue([session mySelf:me error:&error]);
  178. XCTAssertNil(error);
  179. XCTAssertNotNil(me.fingerPrint);
  180. XCTAssertEqual([self ratingForIdentity:me session:session], PEPRatingTrustedAndAnonymized);
  181. PEPIdentity *alice = [self
  182. checkImportingKeyFilePath:@"6FF00E97_sec.asc"
  183. address:@"pep.test.alice@pep-project.org"
  184. userID:@"This Is Alice"
  185. fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
  186. session: session];
  187. XCTAssertNotNil(alice);
  188. XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
  189. XCTAssertTrue([session keyMistrusted:alice error:&error]);
  190. XCTAssertNil(error);
  191. XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingHaveNoKey);
  192. }
  193. - (void)testIdentityRatingTrustResetMistrustUndo
  194. {
  195. PEPInternalSession *session = [PEPSessionProvider session];
  196. PEPIdentity *me = [[PEPIdentity alloc]
  197. initWithAddress:@"me@example.org"
  198. userID:@"me_myself"
  199. userName:@"Me Me"
  200. isOwn:YES];
  201. NSError *error = nil;
  202. XCTAssertTrue([session mySelf:me error:&error]);
  203. XCTAssertNil(error);
  204. XCTAssertNotNil(me.fingerPrint);
  205. XCTAssertEqual([self ratingForIdentity:me session:session], PEPRatingTrustedAndAnonymized);
  206. PEPIdentity *alice = [self
  207. checkImportingKeyFilePath:@"6FF00E97_sec.asc"
  208. address:@"pep.test.alice@pep-project.org"
  209. userID:@"This Is Alice"
  210. fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
  211. session: session];
  212. XCTAssertNotNil(alice);
  213. XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
  214. XCTAssertTrue([session trustPersonalKey:alice error:&error]);
  215. XCTAssertNil(error);
  216. XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingTrusted);
  217. XCTAssertTrue([session keyResetTrust:alice error:&error]);
  218. XCTAssertNil(error);
  219. XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
  220. XCTAssertTrue([session keyMistrusted:alice error:&error]);
  221. XCTAssertNil(error);
  222. XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingHaveNoKey);
  223. }
  224. /** ENGINE-384 */
  225. - (void)testIdentityRatingCrash
  226. {
  227. PEPInternalSession *session = [PEPSessionProvider session];
  228. PEPIdentity *me = [[PEPIdentity alloc]
  229. initWithAddress:@"me@example.org"
  230. userID:@"me_myself"
  231. userName:@"Me Me"
  232. isOwn:YES];
  233. NSError *error = nil;
  234. XCTAssertTrue([session mySelf:me error:&error]);
  235. XCTAssertNil(error);
  236. XCTAssertNotNil(me.fingerPrint);
  237. XCTAssertEqual([self ratingForIdentity:me session:session], PEPRatingTrustedAndAnonymized);
  238. PEPIdentity *alice = [self
  239. checkImportingKeyFilePath:@"6FF00E97_sec.asc"
  240. address:@"pep.test.alice@pep-project.org"
  241. userID:@"This Is Alice"
  242. fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
  243. session: session];
  244. XCTAssertNotNil(alice);
  245. XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
  246. XCTAssertTrue([session trustPersonalKey:alice error:&error]);
  247. XCTAssertNil(error);
  248. XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingTrusted);
  249. XCTAssertTrue([session keyResetTrust:alice error:&error]);
  250. XCTAssertNil(error);
  251. XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
  252. XCTAssertTrue([session keyMistrusted:alice error:&error]);
  253. XCTAssertNil(error);
  254. XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingHaveNoKey);
  255. }
  256. - (void)testOutgoingColors
  257. {
  258. PEPInternalSession *session = [PEPSessionProvider session];
  259. // Our test user :
  260. // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
  261. // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
  262. XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
  263. // Our test user :
  264. PEPIdentity *identAlice = [self
  265. checkMySelfImportingKeyFilePath:@"6FF00E97_sec.asc"
  266. address:@"pep.test.alice@pep-project.org"
  267. userID:@"Alice_User_ID"
  268. fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
  269. session:session];
  270. //Message
  271. {
  272. PEPIdentity *identUnknownBob = [[PEPIdentity alloc]
  273. initWithAddress:@"pep.test.unknown.bob@pep-project.org"
  274. userID:@"4242" userName:@"pEp Test Bob Unknown"
  275. isOwn:NO];
  276. PEPMessage *msgGray = [PEPMessage new];
  277. msgGray.from = identAlice;
  278. msgGray.to = @[identUnknownBob];
  279. msgGray.shortMessage = @"All Gray Test";
  280. msgGray.longMessage = @"This is a text content";
  281. msgGray.direction = PEPMsgDirectionOutgoing;
  282. NSError *error = nil;
  283. // Test with unknown Bob
  284. NSNumber *numRating = [self
  285. testOutgoingRatingForMessage:msgGray
  286. session:session
  287. error:&error];
  288. XCTAssertNotNil(numRating);
  289. XCTAssertNil(error);
  290. XCTAssertEqual(numRating.pEpRating, PEPRatingUnencrypted);
  291. }
  292. PEPIdentity *identBob = [self
  293. checkImportingKeyFilePath:@"0xC9C2EE39.asc"
  294. address:@"pep.test.bob@pep-project.org"
  295. userID:@"42"
  296. fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"
  297. session: session];
  298. XCTAssertNotNil(identBob);
  299. PEPMessage *msg = [PEPMessage new];
  300. msg.from = identAlice;
  301. msg.to = @[identBob];
  302. msg.shortMessage = @"All Gray Test";
  303. msg.longMessage = @"This is a text content";
  304. msg.direction = PEPMsgDirectionOutgoing;
  305. NSError *error = nil;
  306. // Should be yellow, since no handshake happened.
  307. NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
  308. XCTAssertNotNil(numRating);
  309. XCTAssertNil(error);
  310. XCTAssertEqual(numRating.pEpRating, PEPRatingReliable);
  311. PEPRating rating = [self ratingForIdentity:identBob session:session];
  312. XCTAssertEqual(rating, PEPRatingReliable);
  313. // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity
  314. XCTAssertTrue([session trustPersonalKey:identBob error:&error]);
  315. XCTAssertNil(error);
  316. // This time it should be green
  317. numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
  318. XCTAssertNotNil(numRating);
  319. XCTAssertNil(error);
  320. XCTAssertEqual(numRating.pEpRating, PEPRatingTrusted);
  321. rating = [self ratingForIdentity:identBob session:session];
  322. XCTAssertEqual(rating, PEPRatingTrusted);
  323. // Let' say we undo handshake
  324. XCTAssertTrue([session keyResetTrust:identBob error:&error]);
  325. XCTAssertNil(error);
  326. // Yellow ?
  327. numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
  328. XCTAssertNotNil(numRating);
  329. XCTAssertNil(error);
  330. XCTAssertEqual(numRating.pEpRating, PEPRatingReliable);
  331. // mistrust Bob
  332. XCTAssertTrue([session keyMistrusted:identBob error:&error]);
  333. XCTAssertNil(error);
  334. identBob.fingerPrint = nil;
  335. XCTAssertFalse([session updateIdentity:identBob error:&error]);
  336. XCTAssertNotNil(error);
  337. XCTAssertEqual(error.code, PEP_KEY_UNSUITABLE);
  338. XCTAssertNil(identBob.fingerPrint);
  339. // Gray == PEPRatingUnencrypted
  340. numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
  341. XCTAssertNotNil(numRating);
  342. XCTAssertNil(error);
  343. XCTAssertEqual(numRating.pEpRating, PEPRatingUnencrypted);
  344. }
  345. - (void)testOutgoingBccColors
  346. {
  347. PEPInternalSession *session = [PEPSessionProvider session];
  348. // Our test user :
  349. // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
  350. // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
  351. XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
  352. PEPIdentity *identAlice = [[PEPIdentity alloc]
  353. initWithAddress:@"pep.test.alice@pep-project.org"
  354. userID:ownUserId
  355. userName:@"pEp Test Alice"
  356. isOwn:YES
  357. fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
  358. NSError *error = nil;
  359. XCTAssertTrue([session mySelf:identAlice error:&error]);
  360. XCTAssertNil(error);
  361. PEPMessage *msg = [PEPMessage new];
  362. msg.from = identAlice;
  363. msg.to = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.bob@pep-project.org"
  364. userID:@"42" userName:@"pEp Test Bob" isOwn:NO]];
  365. msg.shortMessage = @"All Green Test";
  366. msg.longMessage = @"This is a text content";
  367. msg.direction = PEPMsgDirectionOutgoing;
  368. // Test with unknown Bob
  369. PEPRating rating;
  370. NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
  371. XCTAssertNotNil(numRating);
  372. XCTAssertNil(error);
  373. XCTAssertEqual(numRating.pEpRating, PEPRatingUnencrypted);
  374. // Now let see with bob's pubkey already known
  375. // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
  376. // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39
  377. PEPIdentity *identBob = [self checkImportingKeyFilePath:@"0xC9C2EE39.asc"
  378. address:@"pep.test.bob@pep-project.org"
  379. userID:@"42"
  380. fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"
  381. session:session];
  382. XCTAssertNotNil(identBob);
  383. XCTAssertTrue([session updateIdentity:identBob error:&error]);
  384. XCTAssertNil(error);
  385. // No key election, outgoing messages are unencrypted (after setIdentity)
  386. numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
  387. XCTAssertNotNil(numRating);
  388. XCTAssertNil(error);
  389. XCTAssertEqual(numRating.pEpRating, PEPRatingUnencrypted);
  390. // No key election, there is no key (after setIdentity)
  391. rating = [self ratingForIdentity:identBob session:session];
  392. XCTAssertEqual(rating, PEPRatingHaveNoKey);
  393. // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity
  394. XCTAssertTrue([session trustPersonalKey:identBob error:&error]);
  395. XCTAssertNil(error);
  396. // This time it should be green
  397. numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
  398. XCTAssertNotNil(numRating);
  399. XCTAssertNil(error);
  400. XCTAssertEqual(numRating.pEpRating, PEPRatingTrusted);
  401. rating = [self ratingForIdentity:identBob session:session];
  402. XCTAssertEqual(rating, PEPRatingTrusted);
  403. // Now let see if it turns back yellow if we add an unconfirmed folk.
  404. // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
  405. // AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575
  406. XCTAssertTrue([PEPTestUtils importBundledKey:@"0x70DCF575.asc" session:session]);
  407. PEPIdentity *identJohn = [[PEPIdentity alloc]
  408. initWithAddress:@"pep.test.john@pep-project.org"
  409. userID:@"101" userName:@"pEp Test John"
  410. isOwn:NO
  411. fingerPrint:@"AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575"];
  412. XCTAssertTrue([session updateIdentity:identJohn error:&error]);
  413. XCTAssertNil(error);
  414. msg.bcc = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.john@pep-project.org"
  415. userID:@"101" userName:@"pEp Test John" isOwn:NO]];
  416. // Yellow ?
  417. numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
  418. XCTAssertNotNil(numRating);
  419. XCTAssertNil(error);
  420. XCTAssertEqual(numRating.pEpRating, PEPRatingReliable);
  421. XCTAssertTrue([session trustPersonalKey:identJohn error:&error]);
  422. XCTAssertNil(error);
  423. // This time it should be green
  424. numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
  425. XCTAssertNotNil(numRating);
  426. XCTAssertNil(error);
  427. XCTAssertEqual(numRating.pEpRating, PEPRatingTrusted);
  428. rating = [self ratingForIdentity:identJohn session:session];
  429. XCTAssertEqual(rating, PEPRatingTrusted);
  430. }
  431. - (void)testDontEncryptForMistrusted
  432. {
  433. PEPInternalSession *session = [PEPSessionProvider session];
  434. // Our test user :
  435. // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
  436. // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
  437. XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
  438. PEPIdentity *identAlice = [[PEPIdentity alloc]
  439. initWithAddress:@"pep.test.alice@pep-project.org"
  440. userID:ownUserId
  441. userName:@"pEp Test Alice"
  442. isOwn:YES
  443. fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
  444. NSError *error = nil;
  445. XCTAssertTrue([session mySelf:identAlice error:&error]);
  446. XCTAssertNil(error);
  447. // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
  448. // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39
  449. XCTAssertTrue([PEPTestUtils importBundledKey:@"0xC9C2EE39.asc" session:session]);
  450. PEPIdentity *identBob = [[PEPIdentity alloc]
  451. initWithAddress:@"pep.test.bob@pep-project.org"
  452. userID:@"42" userName:@"pEp Test Bob"
  453. isOwn:NO
  454. fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"];
  455. XCTAssertTrue([session updateIdentity:identBob error:&error]);
  456. XCTAssertNil(error);
  457. // mistrust Bob
  458. XCTAssertTrue([session keyMistrusted:identBob error:&error]);
  459. XCTAssertNil(error);
  460. PEPMessage *msg = [PEPMessage new];
  461. msg.from = identAlice;
  462. msg.to = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.bob@pep-project.org" userID:@"42"
  463. userName:@"pEp Test Bob" isOwn:NO]];
  464. msg.shortMessage = @"All Green Test";
  465. msg.longMessage = @"This is a text content";
  466. msg.direction = PEPMsgDirectionOutgoing;
  467. // Gray == PEPRatingUnencrypted
  468. NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
  469. XCTAssertNotNil(numRating);
  470. XCTAssertNil(error);
  471. XCTAssertEqual(numRating.pEpRating, PEPRatingUnencrypted);
  472. PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
  473. XCTAssertNotNil(encMsg);
  474. XCTAssertNil(error);
  475. XCTAssertNotEqualObjects(encMsg.attachments[0].mimeType, @"application/pgp-encrypted");
  476. [self pEpCleanUp];
  477. }
  478. - (void)testRevoke
  479. {
  480. PEPInternalSession *session = [PEPSessionProvider session];
  481. // Our test user :
  482. // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
  483. // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
  484. XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
  485. NSString *fpr = @"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
  486. PEPIdentity *identAlice = [[PEPIdentity alloc]
  487. initWithAddress:@"pep.test.alice@pep-project.org"
  488. userID:ownUserId
  489. userName:@"pEp Test Alice"
  490. isOwn:YES
  491. fingerPrint:fpr];
  492. NSError *error = nil;
  493. XCTAssertTrue([session mySelf:identAlice error:&error]);
  494. XCTAssertNil(error);
  495. PEPIdentity *identAlice2 = [identAlice mutableCopy];
  496. // This will revoke key
  497. XCTAssertTrue([session keyMistrusted:identAlice2 error:&error]);
  498. XCTAssertNil(error);
  499. identAlice2.fingerPrint = nil;
  500. XCTAssertTrue([session mySelf:identAlice error:&error]);
  501. XCTAssertNil(error);
  502. // Check fingerprint is different
  503. XCTAssertNotEqualObjects(identAlice2.fingerPrint, fpr);
  504. }
  505. - (void)testMailToMyself
  506. {
  507. PEPInternalSession *session = [PEPSessionProvider session];
  508. // Our test user :
  509. // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
  510. // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
  511. XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
  512. PEPIdentity *identAlice = [[PEPIdentity alloc]
  513. initWithAddress:@"pep.test.alice@pep-project.org"
  514. userID:ownUserId
  515. userName:@"pEp Test Alice"
  516. isOwn:YES
  517. fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
  518. NSError *error = nil;
  519. XCTAssertTrue([session mySelf:identAlice error:&error]);
  520. XCTAssertNil(error);
  521. PEPMessage *msg = [PEPMessage new];
  522. msg.from = identAlice;
  523. msg.to = @[identAlice];
  524. msg.shortMessage = @"Mail to Myself";
  525. msg.longMessage = @"This is a text content";
  526. msg.direction = PEPMsgDirectionOutgoing;
  527. msg.attachments = @[];
  528. NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
  529. XCTAssertNotNil(numRating);
  530. XCTAssertNil(error);
  531. XCTAssertEqual(numRating.pEpRating, PEPRatingTrustedAndAnonymized);
  532. PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
  533. XCTAssertNotNil(encMsg);
  534. XCTAssertNil(error);
  535. NSArray *keys;
  536. error = nil;
  537. PEPRating rating = PEPRatingUndefined;
  538. PEPMessage *decmsg = [session
  539. decryptMessage:encMsg
  540. flags:nil
  541. rating:&rating
  542. extraKeys:&keys
  543. status:nil
  544. error:&error];
  545. XCTAssertNotNil(decmsg);
  546. XCTAssertNil(error);
  547. XCTAssertEqual(rating, PEPRatingTrustedAndAnonymized);
  548. // There shouldn't be any attachments
  549. XCTAssertEqual(decmsg.attachments.count, 0);
  550. }
  551. - (void)testOutgoingContactColor
  552. {
  553. PEPInternalSession *session = [PEPSessionProvider session];
  554. PEPIdentity *partner1Orig = [PEPTestUtils foreignPepIdentityWithAddress:@"partner1@dontcare.me"
  555. userName:@"Partner 1"];
  556. NSString *pubKeyPartner1 = [PEPTestUtils loadResourceByName:@"partner1_F2D281C2789DD7F6_pub.asc"];
  557. XCTAssertNotNil(pubKeyPartner1);
  558. NSError *error = nil;
  559. XCTAssertTrue([session importKey:pubKeyPartner1 error:&error]);
  560. XCTAssertNil(error);
  561. PEPRating color = [self ratingForIdentity:partner1Orig session:session];
  562. XCTAssertEqual(color, PEPRatingReliable);
  563. }
  564. - (void)testGetTrustwords
  565. {
  566. PEPInternalSession *session = [PEPSessionProvider session];
  567. PEPIdentity *partner1Orig = [[PEPIdentity alloc]
  568. initWithAddress:@"partner1@dontcare.me" userID:@"partner1"
  569. userName:@"partner1"
  570. isOwn:NO fingerPrint:@"F0CD3F7B422E5D587ABD885BF2D281C2789DD7F6"];
  571. PEPIdentity *meOrig = [[PEPIdentity alloc]
  572. initWithAddress:@"me@dontcare.me" userID:@"me"
  573. userName:@"me"
  574. isOwn:NO fingerPrint:@"CC1F73F6FB774BF08B197691E3BFBCA9248FC681"];
  575. NSError *error = nil;
  576. NSString *trustwordsFull = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
  577. language:@"en" full:YES error:&error];
  578. XCTAssertNil(error);
  579. XCTAssertEqualObjects(trustwordsFull,
  580. @"EMERSON GASPER TOKENISM BOLUS COLLAGE DESPISE BEDDED ENCRYPTION IMAGINE BEDFORD");
  581. NSString *trustwordsUndefined = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
  582. language:@"ZZ" full:YES error:&error];
  583. XCTAssertNotNil(error);
  584. XCTAssertNil(trustwordsUndefined);
  585. }
  586. - (void)testStringToRating
  587. {
  588. PEPInternalSession *session = [PEPSessionProvider session];
  589. XCTAssertEqual([session ratingFromString:@"cannot_decrypt"], PEPRatingCannotDecrypt);
  590. XCTAssertEqual([session ratingFromString:@"have_no_key"], PEPRatingHaveNoKey);
  591. XCTAssertEqual([session ratingFromString:@"unencrypted"], PEPRatingUnencrypted);
  592. XCTAssertEqual([session ratingFromString:@"unreliable"], PEPRatingUnreliable);
  593. XCTAssertEqual([session ratingFromString:@"reliable"], PEPRatingReliable);
  594. XCTAssertEqual([session ratingFromString:@"trusted"], PEPRatingTrusted);
  595. XCTAssertEqual([session ratingFromString:@"trusted_and_anonymized"],
  596. PEPRatingTrustedAndAnonymized);
  597. XCTAssertEqual([session ratingFromString:@"fully_anonymous"], PEPRatingFullyAnonymous);
  598. XCTAssertEqual([session ratingFromString:@"mistrust"], PEPRatingMistrust);
  599. XCTAssertEqual([session ratingFromString:@"b0rken"], PEPRatingB0rken);
  600. XCTAssertEqual([session ratingFromString:@"under_attack"], PEPRatingUnderAttack);
  601. XCTAssertEqual([session ratingFromString:@"undefined"], PEPRatingUndefined);
  602. XCTAssertEqual([session ratingFromString:@"does not exist111"], PEPRatingUndefined);
  603. }
  604. - (void)testRatingToString
  605. {
  606. PEPInternalSession *session = [PEPSessionProvider session];
  607. XCTAssertEqualObjects([session stringFromRating:PEPRatingCannotDecrypt], @"cannot_decrypt");
  608. XCTAssertEqualObjects([session stringFromRating:PEPRatingHaveNoKey], @"have_no_key");
  609. XCTAssertEqualObjects([session stringFromRating:PEPRatingUnencrypted], @"unencrypted");
  610. XCTAssertEqualObjects([session stringFromRating:PEPRatingUnreliable], @"unreliable");
  611. XCTAssertEqualObjects([session stringFromRating:PEPRatingReliable], @"reliable");
  612. XCTAssertEqualObjects([session stringFromRating:PEPRatingTrusted], @"trusted");
  613. XCTAssertEqualObjects([session stringFromRating:PEPRatingTrustedAndAnonymized],
  614. @"trusted_and_anonymized");
  615. XCTAssertEqualObjects([session stringFromRating:PEPRatingFullyAnonymous],
  616. @"fully_anonymous");
  617. XCTAssertEqualObjects([session stringFromRating:PEPRatingMistrust], @"mistrust");
  618. XCTAssertEqualObjects([session stringFromRating:PEPRatingB0rken], @"b0rken");
  619. XCTAssertEqualObjects([session stringFromRating:PEPRatingUnderAttack], @"under_attack");
  620. XCTAssertEqualObjects([session stringFromRating:PEPRatingUndefined], @"undefined");
  621. XCTAssertEqualObjects([session stringFromRating:500], @"undefined");
  622. }
  623. - (void)testIsPEPUser
  624. {
  625. PEPInternalSession *session = [PEPSessionProvider session];
  626. PEPIdentity *identMe = [[PEPIdentity alloc]
  627. initWithAddress:@"me-myself-and-i@pep-project.org"
  628. userID:@"me-myself-and-i"
  629. userName:@"pEp Me"
  630. isOwn:YES];
  631. NSError *error = nil;
  632. XCTAssertTrue([session mySelf:identMe error:&error]);
  633. XCTAssertNil(error);
  634. XCTAssertNotNil(identMe.fingerPrint);
  635. // PEP_CANNOT_FIND_PERSON == 902
  636. XCTAssertTrue([session isPEPUser:identMe error:&error]);
  637. }
  638. - (void)testXEncStatusForOutgoingEncryptedMail
  639. {
  640. [self helperXEncStatusForOutgoingEncryptdMailToSelf:NO expectedRating:PEPRatingReliable];
  641. }
  642. - (void)testXEncStatusForOutgoingSelfEncryptedMail
  643. {
  644. [self helperXEncStatusForOutgoingEncryptdMailToSelf:YES
  645. expectedRating:PEPRatingTrustedAndAnonymized];
  646. }
  647. /**
  648. ENGINE-364. Tries to invoke trustPersonalKey on an identity without key,
  649. giving it a fake fingerprint.
  650. */
  651. - (void)testTrustPersonalKey
  652. {
  653. PEPInternalSession *session = [PEPSessionProvider session];
  654. PEPIdentity *identMe = [[PEPIdentity alloc]
  655. initWithAddress:@"me-myself-and-i@pep-project.org"
  656. userID:@"me-myself-and-i"
  657. userName:@"pEp Me"
  658. isOwn:YES];
  659. NSError *error = nil;
  660. XCTAssertTrue([session mySelf:identMe error:&error]);
  661. XCTAssertNil(error);
  662. XCTAssertNotNil(identMe.fingerPrint);
  663. // The fingerprint is definitely wrong, we don't have a key
  664. PEPIdentity *identAlice = [[PEPIdentity alloc]
  665. initWithAddress:@"alice@pep-project.org"
  666. userID:@"alice"
  667. userName:@"pEp Test Alice"
  668. isOwn:NO
  669. fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
  670. XCTAssertFalse([session trustPersonalKey:identAlice error:&error]);
  671. XCTAssertNotNil(error);
  672. }
  673. /**
  674. ENGINE-381
  675. */
  676. - (void)testVolatileIdentityRating
  677. {
  678. PEPInternalSession *session = [PEPSessionProvider session];
  679. PEPIdentity *identMe = [[PEPIdentity alloc]
  680. initWithAddress:@"me-myself-and-i@pep-project.org"
  681. userID:@"me-myself-and-i"
  682. userName:@"pEp Me"
  683. isOwn:YES];
  684. NSError *error = nil;
  685. XCTAssertTrue([session mySelf:identMe error:&error]);
  686. XCTAssertNil(error);
  687. XCTAssertNotNil(identMe.fingerPrint);
  688. PEPIdentity *identAlice = [self
  689. checkImportingKeyFilePath:@"6FF00E97_sec.asc"
  690. address:@"pep.test.alice@pep-project.org"
  691. userID:@"alice_user_id"
  692. fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
  693. session: session];
  694. XCTAssertNotNil(identAlice);
  695. dispatch_group_t identityRatingGroup = dispatch_group_create();
  696. void (^ratingBlock)(void) = ^{
  697. PEPInternalSession *innerSession = [PEPSessionProvider session];
  698. PEPRating rating = [self ratingForIdentity:identAlice session:innerSession];
  699. XCTAssertEqual(rating, PEPRatingReliable);
  700. };
  701. for (int i = 0; i < 4; ++i) {
  702. dispatch_group_async(identityRatingGroup,
  703. dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0),
  704. ratingBlock);
  705. }
  706. for (int i = 0; i < 4; ++i) {
  707. ratingBlock();
  708. }
  709. dispatch_group_wait(identityRatingGroup, DISPATCH_TIME_FOREVER);
  710. }
  711. /**
  712. IOSAD-93, testing for easy error case.
  713. */
  714. - (void)testEncryptAndAttachPrivateKeyIllegalValue
  715. {
  716. PEPInternalSession *session = [PEPSessionProvider session];
  717. PEPIdentity *identMe = [[PEPIdentity alloc]
  718. initWithAddress:@"me-myself-and-i@pep-project.org"
  719. userID:@"me-myself-and-i"
  720. userName:@"pEp Me"
  721. isOwn:YES];
  722. NSError *error = nil;
  723. XCTAssertTrue([session mySelf:identMe error:&error]);
  724. XCTAssertNil(error);
  725. XCTAssertNotNil(identMe.fingerPrint);
  726. NSString *fprAlice = @"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
  727. PEPIdentity *identAlice = [self
  728. checkImportingKeyFilePath:@"6FF00E97_sec.asc"
  729. address:@"pep.test.alice@pep-project.org"
  730. userID:@"alice_user_id"
  731. fingerPrint:fprAlice
  732. session: session];
  733. XCTAssertNotNil(identAlice);
  734. NSString *shortMessage = @"whatever it may be";
  735. NSString *longMessage = [NSString stringWithFormat:@"%@ %@", shortMessage, shortMessage];
  736. PEPMessage *message = [PEPMessage new];
  737. message.from = identMe;
  738. message.to = @[identAlice];
  739. message.shortMessage = shortMessage;
  740. message.longMessage = longMessage;
  741. PEPStatus status = PEPStatusKeyNotFound;
  742. error = nil;
  743. PEPMessage *encrypted = [session
  744. encryptMessage:message
  745. toFpr:fprAlice
  746. encFormat:PEPEncFormatPEP
  747. flags:0
  748. status:&status error:&error];
  749. XCTAssertEqual(status, PEPStatusIllegalValue);
  750. XCTAssertNotNil(error);
  751. XCTAssertNil(encrypted);
  752. }
  753. - (void)testSetIdentityFlags
  754. {
  755. PEPInternalSession *session = [PEPSessionProvider session];
  756. PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  757. userName:@"userName"];
  758. NSError *error = nil;
  759. XCTAssertTrue([session mySelf:me error:&error]);
  760. XCTAssertNil(error);
  761. PEPIdentityFlags theFlags[] = {
  762. PEPIdentityFlagsNotForSync,
  763. PEPIdentityFlagsList,
  764. PEPIdentityFlagsDeviceGroup,
  765. 0
  766. };
  767. for (int i = 0;; ++i) {
  768. PEPIdentityFlags aFlag = theFlags[i];
  769. if (aFlag == 0) {
  770. break;
  771. }
  772. error = nil;
  773. XCTAssertTrue([session setFlags:(PEPIdentityFlags) aFlag forIdentity:me error:&error]);
  774. XCTAssertNil(error);
  775. XCTAssertTrue(me.flags & theFlags[i]);
  776. }
  777. }
  778. - (void)testTrustOwnKey
  779. {
  780. PEPInternalSession *session = [PEPSessionProvider session];
  781. PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  782. userName:@"userName"];
  783. NSError *error = nil;
  784. XCTAssertTrue([session mySelf:me error:&error]);
  785. XCTAssertNil(error);
  786. XCTAssertTrue([session trustOwnKeyIdentity:me error:&error]);
  787. XCTAssertNil(error);
  788. }
  789. #pragma mark - configUnencryptedSubject
  790. - (void)testConfigUnencryptedSubject
  791. {
  792. // Setup Config to encrypt subject
  793. [PEPObjCAdapter setUnEncryptedSubjectEnabled:NO];
  794. // Write mail to yourself ...
  795. PEPMessage *encMessage = [self mailWrittenToMySelf];
  796. // ... and assert subject is encrypted
  797. XCTAssertEqualObjects(encMessage.shortMessage, @"pp", @"Subject should be encrypted");
  798. }
  799. - (void)testConfigUnencryptedSubjectEncryptedSubjectDisabled
  800. {
  801. // Setup Config to not encrypt subject
  802. [PEPObjCAdapter setUnEncryptedSubjectEnabled:YES];
  803. // Write mail to yourself ...
  804. PEPMessage *encMessage = [self mailWrittenToMySelf];
  805. // pEp to pEp uses message 2.0, which always encrypts subjects (ENGINE-429)
  806. XCTAssertEqualObjects(encMessage.shortMessage, @"pp", @"Subject should be encrypted");
  807. }
  808. #pragma mark - Passive mode
  809. - (void)testPassiveMode
  810. {
  811. [self testPassiveModeEnabled:NO];
  812. [self testPassiveModeEnabled:YES];
  813. }
  814. #pragma mark - Decryption
  815. - (void)testDecryptionOfUnencryptedMessageWithOdtAttachmentContainingSpace
  816. {
  817. PEPInternalSession *session = [PEPSessionProvider session];
  818. PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  819. userName:@"userName"];
  820. NSError *error = nil;
  821. XCTAssertTrue([session mySelf:me error:&error]);
  822. XCTAssertNil(error);
  823. const char *dataString = "blahblah";
  824. const size_t dataSize = strlen(dataString);
  825. char *rawData = strndup(dataString, dataSize);
  826. PEPAttachment *attachment = [[PEPAttachment alloc]
  827. initWithData:[NSData
  828. dataWithBytesNoCopy:rawData length:dataSize]];
  829. attachment.filename = @"Someone andTextIncludingTheSpace.odt";
  830. attachment.mimeType = @"application/vnd.oasis.opendocument.text";
  831. NSString *shortMessage = @"Subject";
  832. NSString *longMessage = @"Oh, this is a long body text!";
  833. PEPMessage *mail = [PEPTestUtils mailFrom:me
  834. toIdent:me
  835. shortMessage:shortMessage
  836. longMessage:longMessage
  837. outgoing:YES];
  838. mail.attachments = @[attachment];
  839. error = nil;
  840. PEPStringList *keys;
  841. PEPRating rating = PEPRatingUndefined;
  842. PEPMessage *decmsg = [session
  843. decryptMessage:mail
  844. flags:nil
  845. rating:&rating
  846. extraKeys:&keys
  847. status:nil
  848. error:&error];
  849. XCTAssertNotNil(decmsg);
  850. XCTAssertNil(error);
  851. XCTAssertEqual(rating, PEPRatingUnencrypted);
  852. PEPAttachment *decryptedAttachment = [decmsg.attachments objectAtIndex:0];
  853. XCTAssertEqualObjects(decryptedAttachment.mimeType, attachment.mimeType);
  854. XCTAssertEqualObjects(decryptedAttachment.filename, attachment.filename);
  855. }
  856. #pragma mark - Sync
  857. /// Prove that mySelf triggers a message to be sent.
  858. - (void)testBasicSendMessage
  859. {
  860. PEPInternalSession *session = [PEPSessionProvider session];
  861. [self testSendMessageOnSession:session];
  862. }
  863. - (void)testDeliverHandshakeResult
  864. {
  865. PEPInternalSession *session = [PEPSessionProvider session];
  866. [self testSendMessageOnSession:session];
  867. PEPSyncHandshakeResult handshakeResults[] = { PEPSyncHandshakeResultCancel,
  868. PEPSyncHandshakeResultAccepted, PEPSyncHandshakeResultRejected };
  869. PEPIdentity *forSureNotMe = [[PEPIdentity alloc]
  870. initWithAddress:@"someoneelseentirely@pep-project.org"
  871. userID:@"that_someone_else"
  872. userName:@"other"
  873. isOwn:NO];
  874. for (int i = 0;; ++i) {
  875. NSError *error = nil;
  876. XCTAssertFalse([session
  877. deliverHandshakeResult:handshakeResults[i]
  878. identitiesSharing:@[forSureNotMe]
  879. error:&error]);
  880. XCTAssertNotNil(error);
  881. XCTAssertEqual([error code], PEPStatusIllegalValue);
  882. if (handshakeResults[i] == PEPSyncHandshakeResultRejected) {
  883. break;
  884. }
  885. }
  886. }
  887. /// Test creating a new own identity with pEp sync disabled.
  888. - (void)testNoBeaconOnMyself
  889. {
  890. PEPInternalSession *session = [PEPSessionProvider session];
  891. XCTAssertEqual(self.sendMessageDelegate.messages.count, 0);
  892. XCTAssertNil(self.sendMessageDelegate.lastMessage);
  893. PEPIdentity *identMe = [[PEPIdentity alloc]
  894. initWithAddress:@"me-myself-and-i@pep-project.org"
  895. userID:@"me-myself-and-i"
  896. userName:@"pEp Me"
  897. isOwn:YES];
  898. identMe.flags |= PEPIdentityFlagsNotForSync;
  899. NSError *error = nil;
  900. XCTAssertTrue([session mySelf:identMe error:&error]);
  901. XCTAssertNil(error);
  902. XCTAssertTrue([session disableSyncForIdentity:identMe error:&error]);
  903. XCTAssertNil(error);
  904. [self startSync];
  905. [NSThread sleepForTimeInterval:1];
  906. XCTAssertNil(self.sendMessageDelegate.lastMessage);
  907. XCTAssertEqual(self.sendMessageDelegate.messages.count, 0);
  908. [self shutdownSync];
  909. }
  910. #pragma mark - key_reset_user
  911. - (void)testKeyResetIdentityOnOwnKeyIsIllegal
  912. {
  913. PEPInternalSession *session = [PEPSessionProvider session];
  914. PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  915. userName:@"userName"];
  916. NSError *error = nil;
  917. XCTAssertTrue([session mySelf:me error:&error]);
  918. XCTAssertNil(error);
  919. NSString *fprOriginal = me.fingerPrint;
  920. XCTAssertNotNil(fprOriginal);
  921. // Cannot reset all _own_ keys with this method, as documented
  922. XCTAssertFalse([session keyReset:me fingerprint:nil error:&error]);
  923. XCTAssertNotNil(error);
  924. XCTAssertTrue([session mySelf:me error:&error]);
  925. XCTAssertNil(error);
  926. NSString *fprAfterReset = me.fingerPrint;
  927. XCTAssertNotNil(fprAfterReset);
  928. XCTAssertNotEqual(fprOriginal, fprAfterReset);
  929. }
  930. #pragma mark - leave_device_group
  931. /** Leaving a device group is successful even though none exists. */
  932. - (void)testSuccessfulLeaveDeviceGroup
  933. {
  934. PEPInternalSession *session = [PEPSessionProvider session];
  935. PEPIdentity *identMe = [[PEPIdentity alloc]
  936. initWithAddress:@"me-myself-and-i@pep-project.org"
  937. userID:@"me-myself-and-i"
  938. userName:@"pEp Me"
  939. isOwn:YES];
  940. NSError *error = nil;
  941. XCTAssertTrue([session mySelf:identMe error:&error]);
  942. XCTAssertNil(error);
  943. [self startSync];
  944. error = nil;
  945. XCTAssertTrue([session leaveDeviceGroup:&error]);
  946. XCTAssertNil(error);
  947. // leaving a device group should disable sync
  948. XCTAssertTrue(self.notifyHandshakeDelegate.engineDidShutdownKeySync);
  949. [self shutdownSync];
  950. }
  951. #pragma mark - enable/disable sync
  952. - (void)testEnableDisableFailForSyncOnPartnerIdentity
  953. {
  954. PEPInternalSession *session = [PEPSessionProvider session];
  955. PEPIdentity *notMe = [[PEPIdentity alloc]
  956. initWithAddress:@"notme@pep-project.org"
  957. userID:@"notme_ID"
  958. userName:@"notme"
  959. isOwn:NO];
  960. NSError *error = nil;
  961. XCTAssertFalse([session enableSyncForIdentity:notMe error:&error]);
  962. XCTAssertNotNil(error);
  963. error = nil;
  964. XCTAssertFalse([session disableSyncForIdentity:notMe error:&error]);
  965. XCTAssertNotNil(error);
  966. }
  967. #pragma mark - Basic Passphrases
  968. - (void)testOwnKeyWithPasswordAndEncryptToSelf
  969. {
  970. NSString *correctPassphrase = @"passphrase_testOwnKeyWithPasswordAndEncryptToSelf";
  971. NSError *error = nil;
  972. XCTAssertTrue([PEPObjCAdapter configurePassphraseForNewKeys:correctPassphrase error:&error]);
  973. XCTAssertNil(error);
  974. PEPInternalSession *session = [PEPSessionProvider session];
  975. PEPIdentity *identMeWithPassphrase = [[PEPIdentity alloc]
  976. initWithAddress:@"me-myself-and-i@pep-project.org"
  977. userID:@"me-myself-and-i"
  978. userName:@"pEp Me"
  979. isOwn:YES];
  980. XCTAssertTrue([session mySelf:identMeWithPassphrase error:&error]);
  981. XCTAssertNil(error);
  982. PEPIdentity *receiver1 = [[PEPIdentity alloc]
  983. initWithAddress:@"partner1@example.com"
  984. userID:@"partner1"
  985. userName:@"Partner 1"
  986. isOwn:NO];
  987. PEPMessage *draftMail = [PEPTestUtils
  988. mailFrom:identMeWithPassphrase
  989. toIdent:receiver1
  990. shortMessage:@"hey"
  991. longMessage:@"hey hey"
  992. outgoing:YES];
  993. error = nil;
  994. PEPStatus status = PEPStatusOutOfMemory;
  995. XCTAssertTrue([session
  996. encryptMessage:draftMail
  997. forSelf:identMeWithPassphrase
  998. extraKeys:nil
  999. status:&status
  1000. error:&error]);
  1001. XCTAssertNil(error);
  1002. }
  1003. - (void)testNotifyHandshakePassphraseNotRequired
  1004. {
  1005. NSString *correctPassphrase = @"passphrase_testOwnKeyWithPasswordSendMessage";
  1006. XCTAssertEqual(self.sendMessageDelegate.messages.count, 0);
  1007. XCTAssertNil(self.sendMessageDelegate.lastMessage);
  1008. NSError *error = nil;
  1009. XCTAssertTrue([PEPObjCAdapter configurePassphraseForNewKeys:correctPassphrase error:&error]);
  1010. XCTAssertNil(error);
  1011. error = nil;
  1012. PEPInternalSession *session = [PEPSessionProvider session];
  1013. PEPIdentity *identMe = [[PEPIdentity alloc]
  1014. initWithAddress:@"me-myself-and-i@pep-project.org"
  1015. userID:@"me-myself-and-i"
  1016. userName:@"pEp Me"
  1017. isOwn:YES];
  1018. XCTAssertTrue([session mySelf:identMe error:&error]);
  1019. XCTAssertNil(error);
  1020. [self startSync];
  1021. XCTKVOExpectation *expHaveMessage1 = [[XCTKVOExpectation alloc]
  1022. initWithKeyPath:@"lastMessage"
  1023. object:self.sendMessageDelegate];
  1024. [self waitForExpectations:@[expHaveMessage1] timeout:PEPTestInternalSyncTimeout];
  1025. XCTAssertNotNil(self.sendMessageDelegate.lastMessage);
  1026. XCTAssertGreaterThan(self.sendMessageDelegate.messages.count, 0);
  1027. XCTAssertEqualObjects(self.sendMessageDelegate.lastMessage.from.address, identMe.address);
  1028. [self shutdownSync];
  1029. }
  1030. #pragma mark - Passphrases
  1031. /// Use case: No passphrase provider
  1032. - (void)testPassphraseProviderNone
  1033. {
  1034. PEPMessage *draftMail = nil;
  1035. PEPInternalSession *session = nil;
  1036. PEPIdentity *identMe = nil;
  1037. [self setupEncryptWithImportedKeySession:&session
  1038. ownIdentity:&identMe
  1039. messageToEncrypt:&draftMail];
  1040. NSError *error = nil;
  1041. PEPStatus status = PEPStatusOutOfMemory;
  1042. XCTAssertFalse([session
  1043. encryptMessage:draftMail
  1044. forSelf:identMe
  1045. extraKeys:nil
  1046. status:&status
  1047. error:&error]);
  1048. XCTAssertNotNil(error);
  1049. XCTAssertEqualObjects(error.domain, PEPObjCAdapterEngineStatusErrorDomain);
  1050. XCTAssertEqual(error.code, PEPStatusPassphraseRequired);
  1051. }
  1052. /// Use case: Passphrase provider set, but never delivers passphrases
  1053. - (void)testPassphraseProviderEmpty
  1054. {
  1055. PEPMessage *draftMail = nil;
  1056. PEPInternalSession *session = nil;
  1057. PEPIdentity *identMe = nil;
  1058. [self setupEncryptWithImportedKeySession:&session
  1059. ownIdentity:&identMe
  1060. messageToEncrypt:&draftMail];
  1061. NSError *error = nil;
  1062. PEPStatus status = PEPStatusOutOfMemory;
  1063. [PEPObjCAdapter setPassphraseProvider:[[PEPPassphraseProviderMock alloc]
  1064. initWithPassphrases:@[]]];
  1065. XCTAssertFalse([session
  1066. encryptMessage:draftMail
  1067. forSelf:identMe
  1068. extraKeys:nil
  1069. status:&status
  1070. error:&error]);
  1071. XCTAssertNotNil(error);
  1072. XCTAssertEqualObjects(error.domain, PEPObjCAdapterEngineStatusErrorDomain);
  1073. XCTAssertEqual(error.code, PEPStatusPassphraseRequired);
  1074. }
  1075. /// Use case: Passphrase provider set, only delivers incorrect passphrases
  1076. - (void)testPassphraseProviderWrongPassphrases
  1077. {
  1078. PEPMessage *draftMail = nil;
  1079. PEPInternalSession *session = nil;
  1080. PEPIdentity *identMe = nil;
  1081. [self setupEncryptWithImportedKeySession:&session
  1082. ownIdentity:&identMe
  1083. messageToEncrypt:&draftMail];
  1084. NSError *error = nil;
  1085. PEPStatus status = PEPStatusOutOfMemory;
  1086. NSArray *nonsensePassphrases = @[@"blah1", @"blah2", @"blah3"];
  1087. [PEPObjCAdapter setPassphraseProvider:[[PEPPassphraseProviderMock alloc]
  1088. initWithPassphrases:nonsensePassphrases]];
  1089. XCTAssertFalse([session
  1090. encryptMessage:draftMail
  1091. forSelf:identMe
  1092. extraKeys:nil
  1093. status:&status
  1094. error:&error]);
  1095. XCTAssertNotNil(error);
  1096. XCTAssertEqualObjects(error.domain, PEPObjCAdapterEngineStatusErrorDomain);
  1097. XCTAssertEqual(error.code, PEPStatusWrongPassphrase);
  1098. }
  1099. /// Use case: 1 Passphrase, but too long
  1100. - (void)testPassphraseProviderPassphraseTooLong
  1101. {
  1102. PEPMessage *draftMail = nil;
  1103. PEPInternalSession *session = nil;
  1104. PEPIdentity *identMe = nil;
  1105. [self setupEncryptWithImportedKeySession:&session
  1106. ownIdentity:&identMe
  1107. messageToEncrypt:&draftMail];
  1108. NSError *error = nil;
  1109. PEPStatus status = PEPStatusOutOfMemory;
  1110. NSString *passphraseBase = @"base";
  1111. NSString *passphraseTooLong = passphraseBase;
  1112. for (NSUInteger i = 0; i < 250; ++i) {
  1113. passphraseTooLong = [passphraseTooLong stringByAppendingString:passphraseBase];
  1114. }
  1115. NSArray *onePassphraseThatIsTooLong = @[passphraseTooLong];
  1116. PEPPassphraseProviderMock *passphraseProviderMock1 = [[PEPPassphraseProviderMock
  1117. alloc]
  1118. initWithPassphrases:onePassphraseThatIsTooLong];
  1119. [PEPObjCAdapter setPassphraseProvider:passphraseProviderMock1];
  1120. XCTAssertFalse([session
  1121. encryptMessage:draftMail
  1122. forSelf:identMe
  1123. extraKeys:nil
  1124. status:&status
  1125. error:&error]);
  1126. XCTAssertNotNil(error);
  1127. XCTAssertEqualObjects(error.domain, PEPObjCAdapterEngineStatusErrorDomain);
  1128. XCTAssertEqual(error.code, PEPStatusWrongPassphrase);
  1129. XCTAssertTrue(passphraseProviderMock1.passphraseTooLongWasCalled);
  1130. }
  1131. /// Use case: Passphrase provider set, has correct passphrase after 2 unsuccessful attempts
  1132. - (void)testPassphraseProviderCorrectPassphrase
  1133. {
  1134. PEPMessage *draftMail = nil;
  1135. PEPInternalSession *session = nil;
  1136. PEPIdentity *identMe = nil;
  1137. [self setupEncryptWithImportedKeySession:&session
  1138. ownIdentity:&identMe
  1139. messageToEncrypt:&draftMail];
  1140. NSError *error = nil;
  1141. PEPStatus status = PEPStatusOutOfMemory;
  1142. NSString *correctPassphrase = @"uiae";
  1143. NSArray *passphrases = @[@"blah1", @"blah2", correctPassphrase];
  1144. [PEPObjCAdapter setPassphraseProvider:[[PEPPassphraseProviderMock alloc]
  1145. initWithPassphrases:passphrases]];
  1146. XCTAssertTrue([session
  1147. encryptMessage:draftMail
  1148. forSelf:identMe
  1149. extraKeys:nil
  1150. status:&status
  1151. error:&error]);
  1152. XCTAssertNil(error);
  1153. }
  1154. /// @Note This test *assumes* that a key reset on an own key (with set passphrase)
  1155. /// will call ensure_passphrase if that passphrase has been "forgotten",
  1156. /// which in turn invokes the currently set passphrase provider.
  1157. /// That the passphrase provider was invoked
  1158. /// by ensure_passphrase (and not by the earlier passphrase handling in
  1159. /// `[PEPInternalSession keyResetAllOwnKeysError]` *cannot be easily proven*.
  1160. - (void)testEnsurePassphrase
  1161. {
  1162. NSString *passphrase = @"a";
  1163. NSError *error = nil;
  1164. XCTAssertTrue([PEPObjCAdapter configurePassphraseForNewKeys:passphrase error:&error]);
  1165. XCTAssertNil(error);
  1166. PEPInternalSession *session = [PEPSessionProvider session];
  1167. PEPIdentity *identMe = [[PEPIdentity alloc]
  1168. initWithAddress:@"me-myself-and-i@pep-project.org"
  1169. userID:@"me-myself-and-i"
  1170. userName:@"pEp Me"
  1171. isOwn:YES];
  1172. error = nil;
  1173. XCTAssertTrue([session mySelf:identMe error:&error]);
  1174. XCTAssertNil(error);
  1175. [[PEPPassphraseCache sharedInstance] setStoredPassphrase:nil];
  1176. PEPPassphraseProviderMock *mock = [[PEPPassphraseProviderMock alloc] initWithPassphrases:@[]];
  1177. [PEPObjCAdapter setPassphraseProvider:mock];
  1178. error = nil;
  1179. XCTAssertFalse([session keyResetAllOwnKeysError:&error]);
  1180. XCTAssertNotNil(error);
  1181. XCTAssertTrue(mock.passphraseRequiredWasCalled);
  1182. }
  1183. #pragma mark - Helpers
  1184. - (void)setupEncryptWithImportedKeySession:(PEPInternalSession **)session
  1185. ownIdentity:(PEPIdentity **)ownIdentity
  1186. messageToEncrypt:(PEPMessage **)messageToEncrypt
  1187. {
  1188. *session = [PEPSessionProvider session];
  1189. NSString *fingerprint = [@"9DD8 3053 3B93 988A 9777  52CA 4802 9ADE 43F2 70EC"
  1190. stringByReplacingOccurrencesOfString:@" " withString:@""];
  1191. fingerprint = [fingerprint stringByReplacingOccurrencesOfString:@" " withString:@""];
  1192. *ownIdentity = [self
  1193. checkMySelfImportingKeyFilePath:@"Rick Deckard (43F270EC)Secret.asc"
  1194. address:@"deckard@example.com"
  1195. userID:@"deckard_user_id"
  1196. fingerPrint:fingerprint
  1197. session:*session];
  1198. XCTAssertNotNil(*ownIdentity);
  1199. PEPIdentity *dummyReceiver = [[PEPIdentity alloc]
  1200. initWithAddress:@"partner1@example.com"
  1201. userID:@"partner1"
  1202. userName:@"Partner 1"
  1203. isOwn:NO];
  1204. *messageToEncrypt = [PEPTestUtils
  1205. mailFrom:*ownIdentity
  1206. toIdent:dummyReceiver
  1207. shortMessage:@"hey"
  1208. longMessage:@"hey hey"
  1209. outgoing:YES];
  1210. }
  1211. - (void)testSendMessageOnSession:(PEPInternalSession *)session
  1212. {
  1213. XCTAssertEqual(self.sendMessageDelegate.messages.count, 0);
  1214. XCTAssertNil(self.sendMessageDelegate.lastMessage);
  1215. PEPIdentity *identMe = [[PEPIdentity alloc]
  1216. initWithAddress:@"me-myself-and-i@pep-project.org"
  1217. userID:@"me-myself-and-i"
  1218. userName:@"pEp Me"
  1219. isOwn:YES];
  1220. NSError *error = nil;
  1221. XCTAssertTrue([session mySelf:identMe error:&error]);
  1222. XCTAssertNil(error);
  1223. [self startSync];
  1224. XCTKVOExpectation *expHaveMessage = [[XCTKVOExpectation alloc]
  1225. initWithKeyPath:@"lastMessage"
  1226. object:self.sendMessageDelegate];
  1227. XCTAssertNotNil(identMe.fingerPrint);
  1228. [self waitForExpectations:@[expHaveMessage] timeout:PEPTestInternalSyncTimeout];
  1229. XCTAssertNotNil(self.sendMessageDelegate.lastMessage);
  1230. XCTAssertEqual(self.sendMessageDelegate.messages.count, 1);
  1231. [self shutdownSync];
  1232. }
  1233. - (void)startSync
  1234. {
  1235. self.sendMessageDelegate = [PEPInternalSessionTestSendMessageDelegate new];
  1236. self.notifyHandshakeDelegate = [PEPInternalSessionTestNotifyHandshakeDelegate new];
  1237. self.sync = [[PEPSync alloc]
  1238. initWithSendMessageDelegate:self.sendMessageDelegate
  1239. notifyHandshakeDelegate:self.notifyHandshakeDelegate];
  1240. [self.sync startup];
  1241. }
  1242. - (void)shutdownSync
  1243. {
  1244. [self.sync shutdown];
  1245. }
  1246. - (NSNumber * _Nullable)testOutgoingRatingForMessage:(PEPMessage * _Nonnull)theMessage
  1247. session:(PEPInternalSession *)session
  1248. error:(NSError * _Nullable * _Nullable)error
  1249. {
  1250. NSNumber *ratingOriginal = [session outgoingRatingForMessage:theMessage error:error];
  1251. NSNumber *ratingPreview = [session outgoingRatingPreviewForMessage:theMessage error:nil];
  1252. XCTAssertEqual(ratingOriginal, ratingPreview);
  1253. return ratingOriginal;
  1254. }
  1255. - (void)testPassiveModeEnabled:(BOOL)passiveModeEnabled
  1256. {
  1257. [PEPObjCAdapter setPassiveModeEnabled:passiveModeEnabled];
  1258. PEPInternalSession *session = [PEPSessionProvider session];
  1259. PEPIdentity *identMe = [[PEPIdentity alloc]
  1260. initWithAddress:@"me-myself-and-i@pep-project.org"
  1261. userID:@"me-myself-and-i"
  1262. userName:@"pEp Me"
  1263. isOwn:YES];
  1264. NSError *error = nil;
  1265. XCTAssertTrue([session mySelf:identMe error:&error]);
  1266. XCTAssertNil(error);
  1267. XCTAssertNotNil(identMe.fingerPrint);
  1268. PEPIdentity *identAlice = [[PEPIdentity alloc]
  1269. initWithAddress:@"pep.test.alice@pep-project.org"
  1270. userID:@"alice_user_id"
  1271. userName:@"Alice"
  1272. isOwn:NO];
  1273. NSString *shortMessage = @"whatever it may be";
  1274. NSString *longMessage = [NSString stringWithFormat:@"%@ %@", shortMessage, shortMessage];
  1275. PEPMessage *message = [PEPMessage new];
  1276. message.direction = PEPMsgDirectionOutgoing;
  1277. message.from = identMe;
  1278. message.to = @[identAlice];
  1279. message.shortMessage = shortMessage;
  1280. message.longMessage = longMessage;
  1281. PEPStatus status = PEPStatusKeyNotFound;
  1282. PEPMessage *encryptedMessage = [session encryptMessage:message
  1283. extraKeys:@[]
  1284. status:&status
  1285. error:&error];
  1286. XCTAssertNil(error);
  1287. XCTAssertEqual(status, PEPStatusUnencrypted);
  1288. if (passiveModeEnabled) {
  1289. XCTAssertNil(encryptedMessage.attachments);
  1290. } else {
  1291. XCTAssertEqual(encryptedMessage.attachments.count, 1);
  1292. }
  1293. }
  1294. /**
  1295. Determines the rating for the given identity.
  1296. @return PEPRatingUndefined on error
  1297. */
  1298. - (PEPRating)ratingForIdentity:(PEPIdentity *)identity session:(PEPInternalSession *)session
  1299. {
  1300. NSError *error;
  1301. NSNumber *numRating = [session ratingForIdentity:identity error:&error];
  1302. XCTAssertNil(error);
  1303. return numRating.pEpRating;
  1304. }
  1305. - (PEPIdentity *)checkImportingKeyFilePath:(NSString *)filePath address:(NSString *)address
  1306. userID:(NSString *)userID
  1307. fingerPrint:(NSString *)fingerPrint
  1308. session:(PEPInternalSession *)session
  1309. {
  1310. if (!session) {
  1311. session = [PEPSessionProvider session];
  1312. }
  1313. BOOL success = [PEPTestUtils importBundledKey:filePath session:session];
  1314. XCTAssertTrue(success);
  1315. if (success) {
  1316. // Our test user:
  1317. PEPIdentity *identTest = [[PEPIdentity alloc]
  1318. initWithAddress:address
  1319. userID:userID
  1320. userName:[NSString stringWithFormat:@"Some User Name %@", userID]
  1321. isOwn:NO];
  1322. NSError *error = nil;
  1323. XCTAssertTrue([session updateIdentity:identTest error:&error]);
  1324. XCTAssertNil(error);
  1325. XCTAssertNil(identTest.fingerPrint); // should be nil before setIdentity
  1326. error = nil;
  1327. identTest.fingerPrint = fingerPrint;
  1328. XCTAssertTrue([session setIdentity:identTest error:&error]);
  1329. XCTAssertNil(error);
  1330. // forget the fingerprint
  1331. identTest.fingerPrint = nil;
  1332. error = nil;
  1333. XCTAssertTrue([session updateIdentity:identTest error:&error]);
  1334. XCTAssertNil(error);
  1335. XCTAssertNotNil(identTest.fingerPrint);
  1336. XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
  1337. return identTest;
  1338. } else {
  1339. return nil;
  1340. }
  1341. }
  1342. - (PEPIdentity *)checkMySelfImportingKeyFilePath:(NSString *)filePath
  1343. address:(NSString *)address
  1344. userID:(NSString *)userID
  1345. fingerPrint:(NSString *)fingerPrint
  1346. session:(PEPInternalSession *)session
  1347. {
  1348. XCTAssertTrue([PEPTestUtils importBundledKey:filePath session:session]);
  1349. // Our test user:
  1350. PEPIdentity *identTest = [[PEPIdentity alloc]
  1351. initWithAddress:address
  1352. userID:userID
  1353. userName:[NSString stringWithFormat:@"Some User Name %@", userID]
  1354. isOwn:YES
  1355. fingerPrint: fingerPrint];
  1356. NSError *error;
  1357. XCTAssertTrue([session setOwnKey:identTest fingerprint:fingerPrint error:&error]);
  1358. XCTAssertNil(error);
  1359. return identTest;
  1360. }
  1361. /**
  1362. Verifies that a partner ID is really a correct Identity.
  1363. Usually used on identities imported as keys, since the engine has problems with them.
  1364. */
  1365. - (void)updateAndVerifyPartnerIdentity:(PEPIdentity *)partnerIdentity session:(PEPInternalSession *)session
  1366. {
  1367. NSError *error = nil;
  1368. XCTAssertNotNil(partnerIdentity.fingerPrint);
  1369. XCTAssertTrue([session updateIdentity:partnerIdentity error:&error]);
  1370. XCTAssertNil(error);
  1371. XCTAssertNotNil(partnerIdentity.fingerPrint);
  1372. NSString *fingerprint = partnerIdentity.fingerPrint;
  1373. partnerIdentity.fingerPrint = nil;
  1374. XCTAssertTrue([session updateIdentity:partnerIdentity error:&error]);
  1375. XCTAssertNil(error);
  1376. XCTAssertNotNil(partnerIdentity.fingerPrint);
  1377. XCTAssertEqualObjects(partnerIdentity.fingerPrint, fingerprint);
  1378. }
  1379. - (PEPMessage *)mailWrittenToMySelf
  1380. {
  1381. PEPInternalSession *session = [PEPSessionProvider session];
  1382. // Write a e-mail to yourself ...
  1383. PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1384. userName:@"userName"];
  1385. NSError *error = nil;
  1386. XCTAssertTrue([session mySelf:me error:&error]);
  1387. XCTAssertNil(error);
  1388. NSString *shortMessage = @"Subject";
  1389. NSString *longMessage = @"Oh, this is a long body text!";
  1390. PEPMessage *mail = [PEPTestUtils mailFrom:me
  1391. toIdent:me
  1392. shortMessage:shortMessage
  1393. longMessage:longMessage
  1394. outgoing:YES];
  1395. PEPStatus status = PEPStatusUnknownError;
  1396. PEPMessage *encMessage = [session
  1397. encryptMessage:mail
  1398. forSelf:me
  1399. extraKeys:nil
  1400. status:&status
  1401. error:&error];
  1402. XCTAssertNil(error);
  1403. return encMessage;
  1404. }
  1405. - (PEPMessage *)internalEncryptToMySelfKeys:(PEPStringList **)keys
  1406. {
  1407. PEPInternalSession *session = [PEPSessionProvider session];
  1408. PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1409. userName:@"userName"];
  1410. NSError *error = nil;
  1411. XCTAssertTrue([session mySelf:me error:&error]);
  1412. XCTAssertNil(error);
  1413. XCTAssertNotNil(me.fingerPrint);
  1414. // Create draft
  1415. NSString *shortMessage = @"Subject";
  1416. NSString *longMessage = @"Oh, this is a long body text!";
  1417. PEPMessage *mail = [PEPTestUtils mailFrom:me toIdent:me shortMessage:shortMessage longMessage:longMessage outgoing:YES];
  1418. PEPStatus status;
  1419. PEPMessage *encMessage = [session
  1420. encryptMessage:mail
  1421. forSelf:me
  1422. extraKeys:nil
  1423. status:&status
  1424. error:&error];
  1425. XCTAssertEqual(status, 0);
  1426. XCTAssertEqualObjects(encMessage.shortMessage, @"pp");
  1427. PEPRating rating;
  1428. error = nil;
  1429. PEPMessage *unencDict = [session
  1430. decryptMessage:encMessage
  1431. flags:nil
  1432. rating:&rating
  1433. extraKeys:keys
  1434. status:nil
  1435. error:&error];
  1436. XCTAssertNotNil(unencDict);
  1437. XCTAssertNil(error);
  1438. XCTAssertGreaterThanOrEqual(rating, PEPRatingReliable);
  1439. XCTAssertEqualObjects(unencDict.shortMessage, shortMessage);
  1440. XCTAssertEqualObjects(unencDict.longMessage, longMessage);
  1441. return unencDict;
  1442. }
  1443. - (void)pEpCleanUp
  1444. {
  1445. [PEPTestUtils cleanUp];
  1446. }
  1447. - (void)helperXEncStatusForOutgoingEncryptdMailToSelf:(BOOL)toSelf
  1448. expectedRating:(PEPRating)expectedRating
  1449. {
  1450. PEPInternalSession *session = [PEPSessionProvider session];
  1451. // Partner pubkey for the test:
  1452. // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
  1453. // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
  1454. XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97.asc" session:session]);
  1455. PEPIdentity *identAlice = [[PEPIdentity alloc]
  1456. initWithAddress:@"pep.test.alice@pep-project.org"
  1457. userID:ownUserId
  1458. userName:@"pEp Test Alice"
  1459. isOwn:NO
  1460. fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
  1461. [self updateAndVerifyPartnerIdentity:identAlice session:session];
  1462. PEPIdentity *identMe = [[PEPIdentity alloc]
  1463. initWithAddress:@"me-myself-and-i@pep-project.org"
  1464. userID:@"me-myself-and-i"
  1465. userName:@"pEp Me"
  1466. isOwn:YES];
  1467. NSError *error = nil;
  1468. XCTAssertTrue([session mySelf:identMe error:&error]);
  1469. XCTAssertNil(error);
  1470. XCTAssertNotNil(identMe.fingerPrint);
  1471. PEPMessage *msg = [PEPMessage new];
  1472. msg.from = identMe;
  1473. msg.to = @[identAlice];
  1474. msg.shortMessage = @"Mail to Alice";
  1475. msg.longMessage = @"Alice?";
  1476. msg.direction = PEPMsgDirectionOutgoing;
  1477. NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
  1478. XCTAssertNotNil(numRating);
  1479. XCTAssertNil(error);
  1480. XCTAssertEqual(numRating.pEpRating, PEPRatingReliable);
  1481. PEPMessage *encMsg;
  1482. PEPStatus statusEnc = PEPStatusVersionMismatch;
  1483. if (toSelf) {
  1484. encMsg = [session
  1485. encryptMessage:msg
  1486. forSelf:identMe
  1487. extraKeys:nil
  1488. status:&statusEnc
  1489. error:&error];
  1490. XCTAssertEqual(statusEnc, PEPStatusOK);
  1491. } else {
  1492. encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
  1493. XCTAssertNotNil(encMsg);
  1494. XCTAssertNil(error);
  1495. }
  1496. XCTAssertNotNil(encMsg);
  1497. PEPStringList *keys;
  1498. PEPRating pEpRating;
  1499. error = nil;
  1500. PEPMessage *decMsg = [session
  1501. decryptMessage:encMsg
  1502. flags:nil
  1503. rating:&pEpRating
  1504. extraKeys:&keys
  1505. status:nil
  1506. error:&error];
  1507. XCTAssertNil(error);
  1508. XCTAssertNotNil(decMsg);
  1509. if (!toSelf) {
  1510. // Only check this for outgoing mails. For drafts etc. this rating looks incorrect
  1511. // and the x-encstatus is the relevant one.
  1512. XCTAssertEqual(pEpRating, expectedRating);
  1513. }
  1514. NSArray * encStatusField = nil;
  1515. for (NSArray *field in decMsg.optionalFields) {
  1516. NSString *header = [field[0] lowercaseString];
  1517. if ([header isEqualToString:@"x-encstatus"]) {
  1518. encStatusField = field;
  1519. }
  1520. }
  1521. XCTAssertNotNil(encStatusField);
  1522. if (encStatusField) {
  1523. PEPRating outgoingRating = [session ratingFromString:encStatusField[1]];
  1524. XCTAssertEqual(outgoingRating, expectedRating);
  1525. }
  1526. }
  1527. @end