Adapter for libpepmime, offering ObjC API.
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.
 
 

187 lines
7.5 KiB

//
// PEPMessage+MIMETest.m
// pEpMimeObjCAdapterTests_macOS
//
// Created by Andreas Buff on 07.07.21.
//
#import <XCTest/XCTest.h>
#import "PEPMessage+PEPMessage_MIME.h"
#import <PEPObjCTypes_macOS/PEPObjCTypes_macOS.h>
#import "PEPMessage+PEPTestTypes.h"
@interface PEPMessage_MIMETest : XCTestCase
@end
@implementation PEPMessage_MIMETest
- (void)testPEPMessage2MIMEString_onlyRequiredFieldsSet {
PEPMessage *testee = [PEPMessage testMessageWithOnlyRequiredFieldsSet:YES];
[self assertPEPMessageToMimeString:testee];
}
- (void)testPEPMessage2MIMEString_allFieldsSet {
PEPMessage *testee = [PEPMessage testMessageWithOnlyRequiredFieldsSet:NO];
[self assertPEPMessageToMimeString:testee];
}
- (void)testPEPMessage2MIMEData_onlyRequiredFieldsSet {
PEPMessage *testee = [PEPMessage testMessageWithOnlyRequiredFieldsSet:YES];
[self assertPEPMessageToMimeData:testee];
}
- (void)testPEPMessage2MIMEData_allFieldsSet {
PEPMessage *testee = [PEPMessage testMessageWithOnlyRequiredFieldsSet:NO];
[self assertPEPMessageToMimeData:testee];
}
- (void)testMIMEData2PEPMessage_onlyRequiredFieldsSet {
PEPMessage *pEpMessage = [PEPMessage testMessageWithOnlyRequiredFieldsSet:YES];
NSError *error = nil;
NSData *mimeData = [pEpMessage mimeDataWithError:&error];
XCTAssertNotNil(mimeData);
[self assertMimedata2PEPMessage:mimeData];
}
- (void)testMIMEData2PEPMessage_allFieldsSet {
PEPMessage *pEpMessage = [PEPMessage testMessageWithOnlyRequiredFieldsSet:NO];
NSError *error = nil;
NSData *mimeData = [pEpMessage mimeDataWithError:&error];
XCTAssertNotNil(mimeData);
[self assertMimedata2PEPMessage:mimeData];
}
- (void)testMIMEString2PEPMessage_onlyRequiredFieldsSet {
PEPMessage *pEpMessage = [PEPMessage testMessageWithOnlyRequiredFieldsSet:YES];
NSError *error = nil;
NSString *mimestring = [pEpMessage mimeStringWithError:&error];
XCTAssertNotNil(mimestring);
[self assertMimeString2PEPMessage:mimestring];
}
- (void)testMIMEString2PEPMessage_allFieldsSet {
PEPMessage *pEpMessage = [PEPMessage testMessageWithOnlyRequiredFieldsSet:NO];
NSError *error = nil;
NSString *mimestring = [pEpMessage mimeStringWithError:&error];
XCTAssertNotNil(mimestring);
[self assertMimeString2PEPMessage:mimestring];
}
- (void)testRoundtripPEPMessage2MIMEStringAndBack_allFieldsSet {
PEPMessage *pEpMessage = [PEPMessage testMessageWithOnlyRequiredFieldsSet:NO];
NSError *error = nil;
NSString *mimestring = [pEpMessage mimeStringWithError:&error];
XCTAssertNotNil(mimestring);
[self assertMimeString2PEPMessage:mimestring];
PEPMessage *backConvertedPEPMessage = [PEPMessage pEpMessageWithMimeString:mimestring
error:&error];
XCTAssertNotNil(backConvertedPEPMessage);
[pEpMessage isEqual:backConvertedPEPMessage];
}
- (void)testRoundtripPEPMessage2MIMEStringAndBack_onlyRequiredFieldsSet {
PEPMessage *pEpMessage = [PEPMessage testMessageWithOnlyRequiredFieldsSet:YES];
NSError *error = nil;
NSString *mimestring = [pEpMessage mimeStringWithError:&error];
XCTAssertNotNil(mimestring);
[self assertMimeString2PEPMessage:mimestring];
PEPMessage *backConvertedPEPMessage = [PEPMessage pEpMessageWithMimeString:mimestring
error:&error];
XCTAssertNotNil(backConvertedPEPMessage);
[pEpMessage isEqual:backConvertedPEPMessage];
}
- (void)testRoundtripPEPMessage2MIMEDataAndBack_allFieldsSet {
PEPMessage *pEpMessage = [PEPMessage testMessageWithOnlyRequiredFieldsSet:NO];
NSError *error = nil;
NSData *mimeData = [pEpMessage mimeDataWithError:&error];
XCTAssertNotNil(mimeData);
[self assertMimedata2PEPMessage:mimeData];
PEPMessage *backConvertedPEPMessage = [PEPMessage pEpMessageWithMimeData:mimeData error:&error];
XCTAssertNotNil(backConvertedPEPMessage);
[pEpMessage isEqual:backConvertedPEPMessage];
}
- (void)testRoundtripPEPMessage2MIMEDataAndBack_onlyRequiredFieldsSet {
PEPMessage *pEpMessage = [PEPMessage testMessageWithOnlyRequiredFieldsSet:YES];
NSError *error = nil;
NSData *mimeData = [pEpMessage mimeDataWithError:&error];
XCTAssertNotNil(mimeData);
[self assertMimedata2PEPMessage:mimeData];
PEPMessage *backConvertedPEPMessage = [PEPMessage pEpMessageWithMimeData:mimeData error:&error];
XCTAssertNotNil(backConvertedPEPMessage);
[pEpMessage isEqual:backConvertedPEPMessage];
}
/// If you have mails causing issue which you want to test, add them to Resoureces/test_mails group.
/// If OK for privacy reasons, feel free to check them in.
/// The must have ".eml" extension.
- (void)testRoundtripForAllMailsInTestmails {
NSBundle *bundle = [NSBundle bundleForClass:self.class];
NSArray<NSURL*> *urls = [bundle URLsForResourcesWithExtension:@"eml" subdirectory:nil];
XCTAssertTrue(urls.count > 0, @"This test does nothing if there are zero mails in test_mails directory");
for (NSURL *url in urls) {
NSError *error = nil;
NSString *mailString = [NSString stringWithContentsOfURL:url
encoding:NSUTF8StringEncoding
error:&error];
if (!mailString) {
mailString = [NSString stringWithContentsOfURL:url
encoding:NSASCIIStringEncoding
error:&error];
}
if (!mailString) {
XCTFail(@"Error lodaing url: %@", url);
continue;
}
[self assertRoundtripMIMEString2PEPMEssageAndBack:mailString];
}
}
// MARK: - HELPER
- (void)assertPEPMessageToMimeString:(PEPMessage *)pEpMEssage {
NSError *error = nil;
NSString *testee = [pEpMEssage mimeStringWithError:&error];
XCTAssertNotNil(testee);
}
- (void)assertPEPMessageToMimeData:(PEPMessage *)pEpMEssage {
NSError *error = nil;
NSData *testee = [pEpMEssage mimeDataWithError:&error];
XCTAssertNotNil(testee);
NSString *convertedTestee = [[NSString alloc] initWithData:testee encoding:NSUTF8StringEncoding];
XCTAssertNotNil(convertedTestee, @"Somthins is wrong with data.");
}
- (void)assertMimeString2PEPMessage:(NSString *)mimeString {
NSError *error = nil;
PEPMessage *testee = [PEPMessage pEpMessageWithMimeString:mimeString error:&error];
XCTAssertNotNil(testee);
}
- (void)assertMimedata2PEPMessage:(NSData *)mimeData {
NSError *error = nil;
PEPMessage *testee = [PEPMessage pEpMessageWithMimeData:mimeData error:&error];
XCTAssertNotNil(testee);
}
- (void)assertRoundtripMIMEString2PEPMEssageAndBack:(NSString *)mimeString {
NSError *error = nil;
PEPMessage *pEpMEssage = [PEPMessage pEpMessageWithMimeString:mimeString error:&error];
XCTAssertNotNil(pEpMEssage);
NSData *mimeData = [pEpMEssage mimeDataWithError:&error];
XCTAssertNotNil(mimeData);
NSString *mimeStringFromData = [[NSString alloc] initWithData:mimeData
encoding:NSUTF8StringEncoding];
XCTAssertNotNil(mimeStringFromData);
NSString *mimeStringBackConverted = [pEpMEssage mimeStringWithError:&error];
XCTAssertNotNil(mimeStringBackConverted);
PEPMessage *onceMoreMessage = [PEPMessage pEpMessageWithMimeString:mimeStringBackConverted
error:&error];
XCTAssertTrue([pEpMEssage isEqual:onceMoreMessage]);
}
@end