]> git.saurik.com Git - apple/security.git/blob - KeychainCircle/Tests/KCDerTest.m
Security-59754.80.3.tar.gz
[apple/security.git] / KeychainCircle / Tests / KCDerTest.m
1 //
2 // KCDerTest.m
3 // Security
4 //
5 //
6
7 #import <XCTest/XCTest.h>
8
9 #import <Foundation/Foundation.h>
10 #import <KeychainCircle/KCDer.h>
11
12 @interface KCDerTest : XCTestCase
13
14 @end
15
16 @implementation KCDerTest
17
18 - (void)setUp {
19 [super setUp];
20 // Put setup code here. This method is called before the invocation of each test method in the class.
21 }
22
23 - (void)tearDown {
24 // Put teardown code here. This method is called after the invocation of each test method in the class.
25 [super tearDown];
26 }
27
28 - (void) roundTripData: (NSData*) data {
29 NSError* error = nil;
30 size_t size = kcder_sizeof_data(data, &error);
31
32 XCTAssert(size != 0, @"Bad size: %@", data);
33
34 if (size == 0)
35 return;
36
37 NSMutableData *buffer = [NSMutableData dataWithLength:size];
38 error = nil;
39 uint8_t* beginning = kcder_encode_data(data, &error, [buffer mutableBytes], [buffer mutableBytes] + size);
40
41 XCTAssert(beginning != NULL, "Error encoding: %@", error);
42
43 if (beginning == NULL)
44 return;
45
46 XCTAssertEqual(beginning, [buffer mutableBytes], @"Size != buffer use");
47
48 NSData* recovered = nil;
49
50 error = nil;
51 const uint8_t* end = kcder_decode_data(&recovered, &error, [buffer mutableBytes], [buffer mutableBytes] + size);
52
53 XCTAssert(end != NULL, "Error decoding: %@", error);
54
55 if (end == NULL)
56 return;
57
58 XCTAssertEqual(end, [buffer mutableBytes] + size, @"readback didn't use all the buffer");
59
60 XCTAssertEqualObjects(data, recovered, @"Didn't get equal object");
61
62 }
63
64 - (void)testData {
65 [self roundTripData: [NSData data]];
66
67 uint8_t bytes[] = { 1, 2, 3, 0xFF, 4, 0x0, 0xA };
68 [self roundTripData: [NSData dataWithBytes:bytes length:sizeof(bytes)]];
69 }
70
71 - (void) roundTripString: (NSString*) string {
72 NSError* error = nil;
73
74 size_t size = kcder_sizeof_string(string, &error);
75
76 XCTAssert(size != 0, @"Bad size: %@", string);
77
78 if (size == 0)
79 return;
80
81 NSMutableData *buffer = [NSMutableData dataWithLength:size];
82 error = nil;
83 uint8_t* beginning = kcder_encode_string(string, &error, [buffer mutableBytes], [buffer mutableBytes] + size);
84
85 XCTAssert(beginning != NULL, "Error encoding: %@", error);
86
87 if (beginning == NULL)
88 return;
89
90 XCTAssertEqual(beginning, [buffer mutableBytes], @"Size != buffer use");
91
92 NSString* recovered = nil;
93
94 error = nil;
95 const uint8_t* end = kcder_decode_string(&recovered, &error, [buffer mutableBytes], [buffer mutableBytes] + size);
96
97 XCTAssert(end != NULL, "Error decoding: %@", error);
98
99 if (end == NULL)
100 return;
101
102 XCTAssertEqual(end, [buffer mutableBytes] + size, @"readback didn't use all the buffer");
103
104 XCTAssertEqualObjects(string, recovered, @"Didn't get equal object");
105
106 }
107
108 - (void)testString {
109 [self roundTripString: [NSString stringWithCString:"Test" encoding:NSUTF8StringEncoding]];
110 [self roundTripString: [NSString stringWithCString:"ΓΌπŸ˜πŸΈβœοΈβ‚§βžœ" encoding:NSUTF8StringEncoding]];
111 }
112
113
114 @end