| 1 |
// Protocol Buffers - Google's data interchange format |
| 2 |
// Copyright 2015 Google Inc. All rights reserved. |
| 3 |
// https://developers.google.com/protocol-buffers/ |
| 4 |
// |
| 5 |
// Redistribution and use in source and binary forms, with or without |
| 6 |
// modification, are permitted provided that the following conditions are |
| 7 |
// met: |
| 8 |
// |
| 9 |
// * Redistributions of source code must retain the above copyright |
| 10 |
// notice, this list of conditions and the following disclaimer. |
| 11 |
// * Redistributions in binary form must reproduce the above |
| 12 |
// copyright notice, this list of conditions and the following disclaimer |
| 13 |
// in the documentation and/or other materials provided with the |
| 14 |
// distribution. |
| 15 |
// * Neither the name of Google Inc. nor the names of its |
| 16 |
// contributors may be used to endorse or promote products derived from |
| 17 |
// this software without specific prior written permission. |
| 18 |
// |
| 19 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
|
| 31 |
#import <Foundation/Foundation.h> |
| 32 |
|
| 33 |
#import "Conformance.pbobjc.h" |
| 34 |
#import "google/protobuf/TestMessagesProto2.pbobjc.h" |
| 35 |
#import "google/protobuf/TestMessagesProto3.pbobjc.h" |
| 36 |
|
| 37 |
static void Die(NSString *format, ...) __dead2; |
| 38 |
|
| 39 |
static BOOL verbose = NO; |
| 40 |
static int32_t testCount = 0; |
| 41 |
|
| 42 |
static void Die(NSString *format, ...) { |
| 43 |
va_list args; |
| 44 |
va_start(args, format); |
| 45 |
NSString *msg = [[NSString alloc] initWithFormat:format arguments:args]; |
| 46 |
NSLog(@"%@", msg); |
| 47 |
va_end(args); |
| 48 |
[msg release]; |
| 49 |
exit(66); |
| 50 |
} |
| 51 |
|
| 52 |
static NSData *CheckedReadDataOfLength(NSFileHandle *handle, NSUInteger numBytes) { |
| 53 |
NSData *data = [handle readDataOfLength:numBytes]; |
| 54 |
NSUInteger dataLen = data.length; |
| 55 |
if (dataLen == 0) { |
| 56 |
return nil; // EOF. |
| 57 |
} |
| 58 |
if (dataLen != numBytes) { |
| 59 |
Die(@"Failed to read the request length (%d), only got: %@", numBytes, data); |
| 60 |
} |
| 61 |
return data; |
| 62 |
} |
| 63 |
|
| 64 |
static ConformanceResponse *DoTest(ConformanceRequest *request) { |
| 65 |
ConformanceResponse *response = [ConformanceResponse message]; |
| 66 |
GPBMessage *testMessage = nil; |
| 67 |
|
| 68 |
switch (request.payloadOneOfCase) { |
| 69 |
case ConformanceRequest_Payload_OneOfCase_GPBUnsetOneOfCase: |
| 70 |
response.runtimeError = |
| 71 |
[NSString stringWithFormat:@"Request didn't have a payload: %@", request]; |
| 72 |
break; |
| 73 |
|
| 74 |
case ConformanceRequest_Payload_OneOfCase_ProtobufPayload: { |
| 75 |
Class msgClass = nil; |
| 76 |
if ([request.messageType isEqual:@"protobuf_test_messages.proto3.TestAllTypesProto3"]) { |
| 77 |
msgClass = [Proto3TestAllTypesProto3 class]; |
| 78 |
} else if ([request.messageType |
| 79 |
isEqual:@"protobuf_test_messages.proto2.TestAllTypesProto2"]) { |
| 80 |
msgClass = [Proto2TestAllTypesProto2 class]; |
| 81 |
} else { |
| 82 |
response.runtimeError = |
| 83 |
[NSString stringWithFormat:@"Protobuf request had an unknown message_type: %@", |
| 84 |
request.messageType]; |
| 85 |
break; |
| 86 |
} |
| 87 |
NSError *error = nil; |
| 88 |
testMessage = [msgClass parseFromData:request.protobufPayload error:&error]; |
| 89 |
if (!testMessage) { |
| 90 |
response.parseError = [NSString stringWithFormat:@"Parse error: %@", error]; |
| 91 |
} |
| 92 |
break; |
| 93 |
} |
| 94 |
|
| 95 |
case ConformanceRequest_Payload_OneOfCase_JsonPayload: |
| 96 |
response.skipped = @"ObjC doesn't support parsing JSON"; |
| 97 |
break; |
| 98 |
|
| 99 |
case ConformanceRequest_Payload_OneOfCase_JspbPayload: |
| 100 |
response.skipped = @"ConformanceRequest had a jspb_payload ConformanceRequest.payload;" |
| 101 |
" those aren't supposed to happen with opensource."; |
| 102 |
break; |
| 103 |
|
| 104 |
case ConformanceRequest_Payload_OneOfCase_TextPayload: |
| 105 |
response.skipped = @"ObjC doesn't support parsing TextFormat"; |
| 106 |
break; |
| 107 |
} |
| 108 |
|
| 109 |
if (testMessage) { |
| 110 |
switch (request.requestedOutputFormat) { |
| 111 |
case ConformanceWireFormat_GPBUnrecognizedEnumeratorValue: |
| 112 |
case ConformanceWireFormat_Unspecified: |
| 113 |
response.runtimeError = |
| 114 |
[NSString stringWithFormat:@"Unrecognized/unspecified output format: %@", request]; |
| 115 |
break; |
| 116 |
|
| 117 |
case ConformanceWireFormat_Protobuf: |
| 118 |
response.protobufPayload = testMessage.data; |
| 119 |
if (!response.protobufPayload) { |
| 120 |
response.serializeError = |
| 121 |
[NSString stringWithFormat:@"Failed to make data from: %@", testMessage]; |
| 122 |
} |
| 123 |
break; |
| 124 |
|
| 125 |
case ConformanceWireFormat_Json: |
| 126 |
response.skipped = @"ObjC doesn't support generating JSON"; |
| 127 |
break; |
| 128 |
|
| 129 |
case ConformanceWireFormat_Jspb: |
| 130 |
response.skipped = |
| 131 |
@"ConformanceRequest had a requested_output_format of JSPB WireFormat; that" |
| 132 |
" isn't supposed to happen with opensource."; |
| 133 |
break; |
| 134 |
|
| 135 |
case ConformanceWireFormat_TextFormat: |
| 136 |
// ObjC only has partial objc generation, so don't attempt any tests that need |
| 137 |
// support. |
| 138 |
response.skipped = @"ObjC doesn't support generating TextFormat"; |
| 139 |
break; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
return response; |
| 144 |
} |
| 145 |
|
| 146 |
static uint32_t UInt32FromLittleEndianData(NSData *data) { |
| 147 |
if (data.length != sizeof(uint32_t)) { |
| 148 |
Die(@"Data not the right size for uint32_t: %@", data); |
| 149 |
} |
| 150 |
uint32_t value; |
| 151 |
memcpy(&value, data.bytes, sizeof(uint32_t)); |
| 152 |
return CFSwapInt32LittleToHost(value); |
| 153 |
} |
| 154 |
|
| 155 |
static NSData *UInt32ToLittleEndianData(uint32_t num) { |
| 156 |
uint32_t value = CFSwapInt32HostToLittle(num); |
| 157 |
return [NSData dataWithBytes:&value length:sizeof(uint32_t)]; |
| 158 |
} |
| 159 |
|
| 160 |
static BOOL DoTestIo(NSFileHandle *input, NSFileHandle *output) { |
| 161 |
// See conformance_test_runner.cc for the wire format. |
| 162 |
NSData *data = CheckedReadDataOfLength(input, sizeof(uint32_t)); |
| 163 |
if (!data) { |
| 164 |
// EOF. |
| 165 |
return NO; |
| 166 |
} |
| 167 |
uint32_t numBytes = UInt32FromLittleEndianData(data); |
| 168 |
data = CheckedReadDataOfLength(input, numBytes); |
| 169 |
if (!data) { |
| 170 |
Die(@"Failed to read request"); |
| 171 |
} |
| 172 |
|
| 173 |
NSError *error = nil; |
| 174 |
ConformanceRequest *request = [ConformanceRequest parseFromData:data error:&error]; |
| 175 |
if (!request) { |
| 176 |
Die(@"Failed to parse the message data: %@", error); |
| 177 |
} |
| 178 |
|
| 179 |
ConformanceResponse *response = DoTest(request); |
| 180 |
if (!response) { |
| 181 |
Die(@"Failed to make a reply from %@", request); |
| 182 |
} |
| 183 |
|
| 184 |
data = response.data; |
| 185 |
[output writeData:UInt32ToLittleEndianData((int32_t)data.length)]; |
| 186 |
[output writeData:data]; |
| 187 |
|
| 188 |
if (verbose) { |
| 189 |
NSLog(@"Request: %@", request); |
| 190 |
NSLog(@"Response: %@", response); |
| 191 |
} |
| 192 |
|
| 193 |
++testCount; |
| 194 |
return YES; |
| 195 |
} |
| 196 |
|
| 197 |
int main(int argc, const char *argv[]) { |
| 198 |
@autoreleasepool { |
| 199 |
NSFileHandle *input = [[NSFileHandle fileHandleWithStandardInput] retain]; |
| 200 |
NSFileHandle *output = [[NSFileHandle fileHandleWithStandardOutput] retain]; |
| 201 |
|
| 202 |
BOOL notDone = YES; |
| 203 |
while (notDone) { |
| 204 |
@autoreleasepool { |
| 205 |
notDone = DoTestIo(input, output); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
NSLog(@"Received EOF from test runner after %d tests, exiting.", testCount); |
| 210 |
} |
| 211 |
return 0; |
| 212 |
} |