Thomas Van Lenten | 1745f7e | 2015-11-17 10:18:49 -0500 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2015 Google Inc. All rights reserved. |
Thomas Van Lenten | 1745f7e | 2015-11-17 10:18:49 -0500 | [diff] [blame] | 3 | // |
Joshua Haberman | 8a4bfd6 | 2023-09-08 19:26:10 -0700 | [diff] [blame] | 4 | // Use of this source code is governed by a BSD-style |
| 5 | // license that can be found in the LICENSE file or at |
| 6 | // https://developers.google.com/open-source/licenses/bsd |
Thomas Van Lenten | 1745f7e | 2015-11-17 10:18:49 -0500 | [diff] [blame] | 7 | |
| 8 | #import <Foundation/Foundation.h> |
| 9 | |
| 10 | #import "Conformance.pbobjc.h" |
Thomas Van Lenten | c2831a3 | 2017-07-26 15:58:07 -0400 | [diff] [blame] | 11 | #import "google/protobuf/TestMessagesProto2.pbobjc.h" |
Joshua Haberman | f1ce60e | 2016-12-03 11:51:25 -0500 | [diff] [blame] | 12 | #import "google/protobuf/TestMessagesProto3.pbobjc.h" |
Thomas Van Lenten | 1745f7e | 2015-11-17 10:18:49 -0500 | [diff] [blame] | 13 | |
| 14 | static void Die(NSString *format, ...) __dead2; |
| 15 | |
| 16 | static BOOL verbose = NO; |
| 17 | static int32_t testCount = 0; |
| 18 | |
| 19 | static void Die(NSString *format, ...) { |
| 20 | va_list args; |
| 21 | va_start(args, format); |
| 22 | NSString *msg = [[NSString alloc] initWithFormat:format arguments:args]; |
| 23 | NSLog(@"%@", msg); |
| 24 | va_end(args); |
| 25 | [msg release]; |
| 26 | exit(66); |
| 27 | } |
| 28 | |
| 29 | static NSData *CheckedReadDataOfLength(NSFileHandle *handle, NSUInteger numBytes) { |
| 30 | NSData *data = [handle readDataOfLength:numBytes]; |
| 31 | NSUInteger dataLen = data.length; |
| 32 | if (dataLen == 0) { |
| 33 | return nil; // EOF. |
| 34 | } |
| 35 | if (dataLen != numBytes) { |
Thomas Van Lenten | d459155 | 2022-10-07 10:46:49 -0400 | [diff] [blame] | 36 | Die(@"Failed to read the request length (%d), only got: %@", numBytes, data); |
Thomas Van Lenten | 1745f7e | 2015-11-17 10:18:49 -0500 | [diff] [blame] | 37 | } |
| 38 | return data; |
| 39 | } |
| 40 | |
| 41 | static ConformanceResponse *DoTest(ConformanceRequest *request) { |
| 42 | ConformanceResponse *response = [ConformanceResponse message]; |
Thomas Van Lenten | c2831a3 | 2017-07-26 15:58:07 -0400 | [diff] [blame] | 43 | GPBMessage *testMessage = nil; |
Thomas Van Lenten | 1745f7e | 2015-11-17 10:18:49 -0500 | [diff] [blame] | 44 | |
| 45 | switch (request.payloadOneOfCase) { |
| 46 | case ConformanceRequest_Payload_OneOfCase_GPBUnsetOneOfCase: |
Thomas Van Lenten | 400d388 | 2019-07-18 17:17:01 -0400 | [diff] [blame] | 47 | response.runtimeError = |
| 48 | [NSString stringWithFormat:@"Request didn't have a payload: %@", request]; |
Thomas Van Lenten | 1745f7e | 2015-11-17 10:18:49 -0500 | [diff] [blame] | 49 | break; |
| 50 | |
| 51 | case ConformanceRequest_Payload_OneOfCase_ProtobufPayload: { |
Thomas Van Lenten | c2831a3 | 2017-07-26 15:58:07 -0400 | [diff] [blame] | 52 | Class msgClass = nil; |
| 53 | if ([request.messageType isEqual:@"protobuf_test_messages.proto3.TestAllTypesProto3"]) { |
| 54 | msgClass = [Proto3TestAllTypesProto3 class]; |
Thomas Van Lenten | d459155 | 2022-10-07 10:46:49 -0400 | [diff] [blame] | 55 | } else if ([request.messageType |
| 56 | isEqual:@"protobuf_test_messages.proto2.TestAllTypesProto2"]) { |
Thomas Van Lenten | f1a939f | 2022-09-23 13:46:22 -0400 | [diff] [blame] | 57 | msgClass = [Proto2TestAllTypesProto2 class]; |
Thomas Van Lenten | c2831a3 | 2017-07-26 15:58:07 -0400 | [diff] [blame] | 58 | } else { |
Thomas Van Lenten | 400d388 | 2019-07-18 17:17:01 -0400 | [diff] [blame] | 59 | response.runtimeError = |
Rafi Kamal | 4f02f05 | 2019-08-22 16:14:22 -0700 | [diff] [blame] | 60 | [NSString stringWithFormat:@"Protobuf request had an unknown message_type: %@", |
| 61 | request.messageType]; |
Thomas Van Lenten | 400d388 | 2019-07-18 17:17:01 -0400 | [diff] [blame] | 62 | break; |
Thomas Van Lenten | c2831a3 | 2017-07-26 15:58:07 -0400 | [diff] [blame] | 63 | } |
Thomas Van Lenten | 3caf9fd | 2017-07-26 16:24:05 -0400 | [diff] [blame] | 64 | NSError *error = nil; |
| 65 | testMessage = [msgClass parseFromData:request.protobufPayload error:&error]; |
| 66 | if (!testMessage) { |
Thomas Van Lenten | d459155 | 2022-10-07 10:46:49 -0400 | [diff] [blame] | 67 | response.parseError = [NSString stringWithFormat:@"Parse error: %@", error]; |
Thomas Van Lenten | 1745f7e | 2015-11-17 10:18:49 -0500 | [diff] [blame] | 68 | } |
| 69 | break; |
| 70 | } |
| 71 | |
| 72 | case ConformanceRequest_Payload_OneOfCase_JsonPayload: |
| 73 | response.skipped = @"ObjC doesn't support parsing JSON"; |
| 74 | break; |
Thomas Van Lenten | 24d344c | 2019-01-07 17:03:37 -0500 | [diff] [blame] | 75 | |
| 76 | case ConformanceRequest_Payload_OneOfCase_JspbPayload: |
Thomas Van Lenten | d459155 | 2022-10-07 10:46:49 -0400 | [diff] [blame] | 77 | response.skipped = @"ConformanceRequest had a jspb_payload ConformanceRequest.payload;" |
| 78 | " those aren't supposed to happen with opensource."; |
Thomas Van Lenten | 24d344c | 2019-01-07 17:03:37 -0500 | [diff] [blame] | 79 | break; |
Thomas Van Lenten | 0c95791 | 2019-01-14 10:28:21 -0500 | [diff] [blame] | 80 | |
| 81 | case ConformanceRequest_Payload_OneOfCase_TextPayload: |
| 82 | response.skipped = @"ObjC doesn't support parsing TextFormat"; |
| 83 | break; |
Thomas Van Lenten | 1745f7e | 2015-11-17 10:18:49 -0500 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | if (testMessage) { |
| 87 | switch (request.requestedOutputFormat) { |
Thomas Van Lenten | 76c3e69 | 2022-10-03 12:39:16 -0400 | [diff] [blame] | 88 | case ConformanceWireFormat_GPBUnrecognizedEnumeratorValue: |
| 89 | case ConformanceWireFormat_Unspecified: |
Thomas Van Lenten | 400d388 | 2019-07-18 17:17:01 -0400 | [diff] [blame] | 90 | response.runtimeError = |
| 91 | [NSString stringWithFormat:@"Unrecognized/unspecified output format: %@", request]; |
Thomas Van Lenten | 1745f7e | 2015-11-17 10:18:49 -0500 | [diff] [blame] | 92 | break; |
| 93 | |
Thomas Van Lenten | 76c3e69 | 2022-10-03 12:39:16 -0400 | [diff] [blame] | 94 | case ConformanceWireFormat_Protobuf: |
Thomas Van Lenten | 1745f7e | 2015-11-17 10:18:49 -0500 | [diff] [blame] | 95 | response.protobufPayload = testMessage.data; |
| 96 | if (!response.protobufPayload) { |
Thomas Van Lenten | df4e4d8 | 2016-01-05 16:31:40 -0500 | [diff] [blame] | 97 | response.serializeError = |
Thomas Van Lenten | d459155 | 2022-10-07 10:46:49 -0400 | [diff] [blame] | 98 | [NSString stringWithFormat:@"Failed to make data from: %@", testMessage]; |
Thomas Van Lenten | 1745f7e | 2015-11-17 10:18:49 -0500 | [diff] [blame] | 99 | } |
| 100 | break; |
| 101 | |
Thomas Van Lenten | 76c3e69 | 2022-10-03 12:39:16 -0400 | [diff] [blame] | 102 | case ConformanceWireFormat_Json: |
Thomas Van Lenten | 1745f7e | 2015-11-17 10:18:49 -0500 | [diff] [blame] | 103 | response.skipped = @"ObjC doesn't support generating JSON"; |
| 104 | break; |
Thomas Van Lenten | 24d344c | 2019-01-07 17:03:37 -0500 | [diff] [blame] | 105 | |
Thomas Van Lenten | 76c3e69 | 2022-10-03 12:39:16 -0400 | [diff] [blame] | 106 | case ConformanceWireFormat_Jspb: |
Thomas Van Lenten | 24d344c | 2019-01-07 17:03:37 -0500 | [diff] [blame] | 107 | response.skipped = |
| 108 | @"ConformanceRequest had a requested_output_format of JSPB WireFormat; that" |
Thomas Van Lenten | d459155 | 2022-10-07 10:46:49 -0400 | [diff] [blame] | 109 | " isn't supposed to happen with opensource."; |
Thomas Van Lenten | 24d344c | 2019-01-07 17:03:37 -0500 | [diff] [blame] | 110 | break; |
Thomas Van Lenten | 0c95791 | 2019-01-14 10:28:21 -0500 | [diff] [blame] | 111 | |
Thomas Van Lenten | 76c3e69 | 2022-10-03 12:39:16 -0400 | [diff] [blame] | 112 | case ConformanceWireFormat_TextFormat: |
Thomas Van Lenten | 0c95791 | 2019-01-14 10:28:21 -0500 | [diff] [blame] | 113 | // ObjC only has partial objc generation, so don't attempt any tests that need |
| 114 | // support. |
| 115 | response.skipped = @"ObjC doesn't support generating TextFormat"; |
| 116 | break; |
Thomas Van Lenten | 1745f7e | 2015-11-17 10:18:49 -0500 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | return response; |
| 121 | } |
| 122 | |
| 123 | static uint32_t UInt32FromLittleEndianData(NSData *data) { |
| 124 | if (data.length != sizeof(uint32_t)) { |
| 125 | Die(@"Data not the right size for uint32_t: %@", data); |
| 126 | } |
| 127 | uint32_t value; |
| 128 | memcpy(&value, data.bytes, sizeof(uint32_t)); |
| 129 | return CFSwapInt32LittleToHost(value); |
| 130 | } |
| 131 | |
| 132 | static NSData *UInt32ToLittleEndianData(uint32_t num) { |
| 133 | uint32_t value = CFSwapInt32HostToLittle(num); |
| 134 | return [NSData dataWithBytes:&value length:sizeof(uint32_t)]; |
| 135 | } |
| 136 | |
| 137 | static BOOL DoTestIo(NSFileHandle *input, NSFileHandle *output) { |
| 138 | // See conformance_test_runner.cc for the wire format. |
| 139 | NSData *data = CheckedReadDataOfLength(input, sizeof(uint32_t)); |
| 140 | if (!data) { |
| 141 | // EOF. |
| 142 | return NO; |
| 143 | } |
| 144 | uint32_t numBytes = UInt32FromLittleEndianData(data); |
| 145 | data = CheckedReadDataOfLength(input, numBytes); |
| 146 | if (!data) { |
| 147 | Die(@"Failed to read request"); |
| 148 | } |
| 149 | |
| 150 | NSError *error = nil; |
Thomas Van Lenten | d459155 | 2022-10-07 10:46:49 -0400 | [diff] [blame] | 151 | ConformanceRequest *request = [ConformanceRequest parseFromData:data error:&error]; |
Thomas Van Lenten | 1745f7e | 2015-11-17 10:18:49 -0500 | [diff] [blame] | 152 | if (!request) { |
| 153 | Die(@"Failed to parse the message data: %@", error); |
| 154 | } |
| 155 | |
| 156 | ConformanceResponse *response = DoTest(request); |
| 157 | if (!response) { |
| 158 | Die(@"Failed to make a reply from %@", request); |
| 159 | } |
| 160 | |
| 161 | data = response.data; |
| 162 | [output writeData:UInt32ToLittleEndianData((int32_t)data.length)]; |
| 163 | [output writeData:data]; |
| 164 | |
| 165 | if (verbose) { |
| 166 | NSLog(@"Request: %@", request); |
| 167 | NSLog(@"Response: %@", response); |
| 168 | } |
| 169 | |
| 170 | ++testCount; |
| 171 | return YES; |
| 172 | } |
| 173 | |
| 174 | int main(int argc, const char *argv[]) { |
| 175 | @autoreleasepool { |
| 176 | NSFileHandle *input = [[NSFileHandle fileHandleWithStandardInput] retain]; |
| 177 | NSFileHandle *output = [[NSFileHandle fileHandleWithStandardOutput] retain]; |
| 178 | |
| 179 | BOOL notDone = YES; |
| 180 | while (notDone) { |
| 181 | @autoreleasepool { |
| 182 | notDone = DoTestIo(input, output); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | NSLog(@"Received EOF from test runner after %d tests, exiting.", testCount); |
| 187 | } |
| 188 | return 0; |
| 189 | } |