blob: 84a43811fa1e27102d615971c37a7b3674fc529a [file] [log] [blame]
Thomas Van Lenten1745f7e2015-11-17 10:18:49 -05001// 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"
Thomas Van Lentenc2831a32017-07-26 15:58:07 -040034#import "google/protobuf/TestMessagesProto2.pbobjc.h"
Joshua Habermanf1ce60e2016-12-03 11:51:25 -050035#import "google/protobuf/TestMessagesProto3.pbobjc.h"
Thomas Van Lenten1745f7e2015-11-17 10:18:49 -050036
37static void Die(NSString *format, ...) __dead2;
38
39static BOOL verbose = NO;
40static int32_t testCount = 0;
41
42static 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
52static 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: %@",
60 numBytes, data);
61 }
62 return data;
63}
64
65static ConformanceResponse *DoTest(ConformanceRequest *request) {
66 ConformanceResponse *response = [ConformanceResponse message];
Thomas Van Lentenc2831a32017-07-26 15:58:07 -040067 GPBMessage *testMessage = nil;
Thomas Van Lenten1745f7e2015-11-17 10:18:49 -050068
69 switch (request.payloadOneOfCase) {
70 case ConformanceRequest_Payload_OneOfCase_GPBUnsetOneOfCase:
71 Die(@"Request didn't have a payload: %@", request);
72 break;
73
74 case ConformanceRequest_Payload_OneOfCase_ProtobufPayload: {
Thomas Van Lentenc2831a32017-07-26 15:58:07 -040075 Class msgClass = nil;
76 if ([request.messageType isEqual:@"protobuf_test_messages.proto3.TestAllTypesProto3"]) {
77 msgClass = [Proto3TestAllTypesProto3 class];
78 } else if ([request.messageType isEqual:@"protobuf_test_messages.proto2.TestAllTypesProto2"]) {
79 msgClass = [TestAllTypesProto2 class];
80 } else {
Thomas Van Lenten3caf9fd2017-07-26 16:24:05 -040081 Die(@"Protobuf request had an unknown message_type: %@", request.messageType);
Thomas Van Lentenc2831a32017-07-26 15:58:07 -040082 }
Thomas Van Lenten3caf9fd2017-07-26 16:24:05 -040083 NSError *error = nil;
84 testMessage = [msgClass parseFromData:request.protobufPayload error:&error];
85 if (!testMessage) {
86 response.parseError =
87 [NSString stringWithFormat:@"Parse error: %@", error];
Thomas Van Lenten1745f7e2015-11-17 10:18:49 -050088 }
89 break;
90 }
91
92 case ConformanceRequest_Payload_OneOfCase_JsonPayload:
93 response.skipped = @"ObjC doesn't support parsing JSON";
94 break;
95 }
96
97 if (testMessage) {
98 switch (request.requestedOutputFormat) {
99 case WireFormat_GPBUnrecognizedEnumeratorValue:
100 case WireFormat_Unspecified:
101 Die(@"Unrecognized/unspecified output format: %@", request);
102 break;
103
104 case WireFormat_Protobuf:
105 response.protobufPayload = testMessage.data;
106 if (!response.protobufPayload) {
Thomas Van Lentendf4e4d82016-01-05 16:31:40 -0500107 response.serializeError =
Thomas Van Lenten1745f7e2015-11-17 10:18:49 -0500108 [NSString stringWithFormat:@"Failed to make data from: %@", testMessage];
109 }
110 break;
111
112 case WireFormat_Json:
113 response.skipped = @"ObjC doesn't support generating JSON";
114 break;
115 }
116 }
117
118 return response;
119}
120
121static uint32_t UInt32FromLittleEndianData(NSData *data) {
122 if (data.length != sizeof(uint32_t)) {
123 Die(@"Data not the right size for uint32_t: %@", data);
124 }
125 uint32_t value;
126 memcpy(&value, data.bytes, sizeof(uint32_t));
127 return CFSwapInt32LittleToHost(value);
128}
129
130static NSData *UInt32ToLittleEndianData(uint32_t num) {
131 uint32_t value = CFSwapInt32HostToLittle(num);
132 return [NSData dataWithBytes:&value length:sizeof(uint32_t)];
133}
134
135static BOOL DoTestIo(NSFileHandle *input, NSFileHandle *output) {
136 // See conformance_test_runner.cc for the wire format.
137 NSData *data = CheckedReadDataOfLength(input, sizeof(uint32_t));
138 if (!data) {
139 // EOF.
140 return NO;
141 }
142 uint32_t numBytes = UInt32FromLittleEndianData(data);
143 data = CheckedReadDataOfLength(input, numBytes);
144 if (!data) {
145 Die(@"Failed to read request");
146 }
147
148 NSError *error = nil;
149 ConformanceRequest *request = [ConformanceRequest parseFromData:data
150 error:&error];
151 if (!request) {
152 Die(@"Failed to parse the message data: %@", error);
153 }
154
155 ConformanceResponse *response = DoTest(request);
156 if (!response) {
157 Die(@"Failed to make a reply from %@", request);
158 }
159
160 data = response.data;
161 [output writeData:UInt32ToLittleEndianData((int32_t)data.length)];
162 [output writeData:data];
163
164 if (verbose) {
165 NSLog(@"Request: %@", request);
166 NSLog(@"Response: %@", response);
167 }
168
169 ++testCount;
170 return YES;
171}
172
173int main(int argc, const char *argv[]) {
174 @autoreleasepool {
175 NSFileHandle *input = [[NSFileHandle fileHandleWithStandardInput] retain];
176 NSFileHandle *output = [[NSFileHandle fileHandleWithStandardOutput] retain];
177
178 BOOL notDone = YES;
179 while (notDone) {
180 @autoreleasepool {
181 notDone = DoTestIo(input, output);
182 }
183 }
184
185 NSLog(@"Received EOF from test runner after %d tests, exiting.", testCount);
186 }
187 return 0;
188}