7 #import <XCTest/XCTest.h>
9 #import <Foundation/Foundation.h>
10 #import <KeychainCircle/KCDer.h>
12 @interface KCDerTest : XCTestCase
16 @implementation KCDerTest
20 // Put setup code here. This method is called before the invocation of each test method in the class.
24 // Put teardown code here. This method is called after the invocation of each test method in the class.
28 - (void) roundTripData: (NSData*) data {
30 size_t size = kcder_sizeof_data(data, &error);
32 XCTAssert(size != 0, @"Bad size: %@", data);
37 NSMutableData *buffer = [NSMutableData dataWithLength:size];
39 uint8_t* beginning = kcder_encode_data(data, &error, [buffer mutableBytes], [buffer mutableBytes] + size);
41 XCTAssert(beginning != NULL, "Error encoding: %@", error);
43 if (beginning == NULL)
46 XCTAssertEqual(beginning, [buffer mutableBytes], @"Size != buffer use");
48 NSData* recovered = nil;
51 const uint8_t* end = kcder_decode_data(&recovered, &error, [buffer mutableBytes], [buffer mutableBytes] + size);
53 XCTAssert(end != NULL, "Error decoding: %@", error);
58 XCTAssertEqual(end, [buffer mutableBytes] + size, @"readback didn't use all the buffer");
60 XCTAssertEqualObjects(data, recovered, @"Didn't get equal object");
65 [self roundTripData: [NSData data]];
67 uint8_t bytes[] = { 1, 2, 3, 0xFF, 4, 0x0, 0xA };
68 [self roundTripData: [NSData dataWithBytes:bytes length:sizeof(bytes)]];
71 - (void) roundTripString: (NSString*) string {
74 size_t size = kcder_sizeof_string(string, &error);
76 XCTAssert(size != 0, @"Bad size: %@", string);
81 NSMutableData *buffer = [NSMutableData dataWithLength:size];
83 uint8_t* beginning = kcder_encode_string(string, &error, [buffer mutableBytes], [buffer mutableBytes] + size);
85 XCTAssert(beginning != NULL, "Error encoding: %@", error);
87 if (beginning == NULL)
90 XCTAssertEqual(beginning, [buffer mutableBytes], @"Size != buffer use");
92 NSString* recovered = nil;
95 const uint8_t* end = kcder_decode_string(&recovered, &error, [buffer mutableBytes], [buffer mutableBytes] + size);
97 XCTAssert(end != NULL, "Error decoding: %@", error);
102 XCTAssertEqual(end, [buffer mutableBytes] + size, @"readback didn't use all the buffer");
104 XCTAssertEqualObjects(string, recovered, @"Didn't get equal object");
109 [self roundTripString: [NSString stringWithCString:"Test" encoding:NSUTF8StringEncoding]];
110 [self roundTripString: [NSString stringWithCString:"ΓΌππΈβοΈβ§β" encoding:NSUTF8StringEncoding]];