blob: 5060ce9ecff3f20a9380b6ab00df4da88eebcaf1 [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.
Thomas Van Lenten1745f7e2015-11-17 10:18:49 -05003//
Joshua Haberman8a4bfd62023-09-08 19:26:10 -07004// 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 Lenten1745f7e2015-11-17 10:18:49 -05007
8#import <Foundation/Foundation.h>
9
10#import "Conformance.pbobjc.h"
Thomas Van Lentenc2831a32017-07-26 15:58:07 -040011#import "google/protobuf/TestMessagesProto2.pbobjc.h"
Joshua Habermanf1ce60e2016-12-03 11:51:25 -050012#import "google/protobuf/TestMessagesProto3.pbobjc.h"
Thomas Van Lenten1745f7e2015-11-17 10:18:49 -050013
14static void Die(NSString *format, ...) __dead2;
15
16static BOOL verbose = NO;
17static int32_t testCount = 0;
18
19static 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
29static 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 Lentend4591552022-10-07 10:46:49 -040036 Die(@"Failed to read the request length (%d), only got: %@", numBytes, data);
Thomas Van Lenten1745f7e2015-11-17 10:18:49 -050037 }
38 return data;
39}
40
41static ConformanceResponse *DoTest(ConformanceRequest *request) {
42 ConformanceResponse *response = [ConformanceResponse message];
Thomas Van Lentenc2831a32017-07-26 15:58:07 -040043 GPBMessage *testMessage = nil;
Thomas Van Lenten1745f7e2015-11-17 10:18:49 -050044
45 switch (request.payloadOneOfCase) {
46 case ConformanceRequest_Payload_OneOfCase_GPBUnsetOneOfCase:
Thomas Van Lenten400d3882019-07-18 17:17:01 -040047 response.runtimeError =
48 [NSString stringWithFormat:@"Request didn't have a payload: %@", request];
Thomas Van Lenten1745f7e2015-11-17 10:18:49 -050049 break;
50
51 case ConformanceRequest_Payload_OneOfCase_ProtobufPayload: {
Thomas Van Lentenc2831a32017-07-26 15:58:07 -040052 Class msgClass = nil;
53 if ([request.messageType isEqual:@"protobuf_test_messages.proto3.TestAllTypesProto3"]) {
54 msgClass = [Proto3TestAllTypesProto3 class];
Thomas Van Lentend4591552022-10-07 10:46:49 -040055 } else if ([request.messageType
56 isEqual:@"protobuf_test_messages.proto2.TestAllTypesProto2"]) {
Thomas Van Lentenf1a939f2022-09-23 13:46:22 -040057 msgClass = [Proto2TestAllTypesProto2 class];
Thomas Van Lentenc2831a32017-07-26 15:58:07 -040058 } else {
Thomas Van Lenten400d3882019-07-18 17:17:01 -040059 response.runtimeError =
Rafi Kamal4f02f052019-08-22 16:14:22 -070060 [NSString stringWithFormat:@"Protobuf request had an unknown message_type: %@",
61 request.messageType];
Thomas Van Lenten400d3882019-07-18 17:17:01 -040062 break;
Thomas Van Lentenc2831a32017-07-26 15:58:07 -040063 }
Thomas Van Lenten3caf9fd2017-07-26 16:24:05 -040064 NSError *error = nil;
65 testMessage = [msgClass parseFromData:request.protobufPayload error:&error];
66 if (!testMessage) {
Thomas Van Lentend4591552022-10-07 10:46:49 -040067 response.parseError = [NSString stringWithFormat:@"Parse error: %@", error];
Thomas Van Lenten1745f7e2015-11-17 10:18:49 -050068 }
69 break;
70 }
71
72 case ConformanceRequest_Payload_OneOfCase_JsonPayload:
73 response.skipped = @"ObjC doesn't support parsing JSON";
74 break;
Thomas Van Lenten24d344c2019-01-07 17:03:37 -050075
76 case ConformanceRequest_Payload_OneOfCase_JspbPayload:
Thomas Van Lentend4591552022-10-07 10:46:49 -040077 response.skipped = @"ConformanceRequest had a jspb_payload ConformanceRequest.payload;"
78 " those aren't supposed to happen with opensource.";
Thomas Van Lenten24d344c2019-01-07 17:03:37 -050079 break;
Thomas Van Lenten0c957912019-01-14 10:28:21 -050080
81 case ConformanceRequest_Payload_OneOfCase_TextPayload:
82 response.skipped = @"ObjC doesn't support parsing TextFormat";
83 break;
Thomas Van Lenten1745f7e2015-11-17 10:18:49 -050084 }
85
86 if (testMessage) {
87 switch (request.requestedOutputFormat) {
Thomas Van Lenten76c3e692022-10-03 12:39:16 -040088 case ConformanceWireFormat_GPBUnrecognizedEnumeratorValue:
89 case ConformanceWireFormat_Unspecified:
Thomas Van Lenten400d3882019-07-18 17:17:01 -040090 response.runtimeError =
91 [NSString stringWithFormat:@"Unrecognized/unspecified output format: %@", request];
Thomas Van Lenten1745f7e2015-11-17 10:18:49 -050092 break;
93
Thomas Van Lenten76c3e692022-10-03 12:39:16 -040094 case ConformanceWireFormat_Protobuf:
Thomas Van Lenten1745f7e2015-11-17 10:18:49 -050095 response.protobufPayload = testMessage.data;
96 if (!response.protobufPayload) {
Thomas Van Lentendf4e4d82016-01-05 16:31:40 -050097 response.serializeError =
Thomas Van Lentend4591552022-10-07 10:46:49 -040098 [NSString stringWithFormat:@"Failed to make data from: %@", testMessage];
Thomas Van Lenten1745f7e2015-11-17 10:18:49 -050099 }
100 break;
101
Thomas Van Lenten76c3e692022-10-03 12:39:16 -0400102 case ConformanceWireFormat_Json:
Thomas Van Lenten1745f7e2015-11-17 10:18:49 -0500103 response.skipped = @"ObjC doesn't support generating JSON";
104 break;
Thomas Van Lenten24d344c2019-01-07 17:03:37 -0500105
Thomas Van Lenten76c3e692022-10-03 12:39:16 -0400106 case ConformanceWireFormat_Jspb:
Thomas Van Lenten24d344c2019-01-07 17:03:37 -0500107 response.skipped =
108 @"ConformanceRequest had a requested_output_format of JSPB WireFormat; that"
Thomas Van Lentend4591552022-10-07 10:46:49 -0400109 " isn't supposed to happen with opensource.";
Thomas Van Lenten24d344c2019-01-07 17:03:37 -0500110 break;
Thomas Van Lenten0c957912019-01-14 10:28:21 -0500111
Thomas Van Lenten76c3e692022-10-03 12:39:16 -0400112 case ConformanceWireFormat_TextFormat:
Thomas Van Lenten0c957912019-01-14 10:28:21 -0500113 // 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 Lenten1745f7e2015-11-17 10:18:49 -0500117 }
118 }
119
120 return response;
121}
122
123static 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
132static NSData *UInt32ToLittleEndianData(uint32_t num) {
133 uint32_t value = CFSwapInt32HostToLittle(num);
134 return [NSData dataWithBytes:&value length:sizeof(uint32_t)];
135}
136
137static 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 Lentend4591552022-10-07 10:46:49 -0400151 ConformanceRequest *request = [ConformanceRequest parseFromData:data error:&error];
Thomas Van Lenten1745f7e2015-11-17 10:18:49 -0500152 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
174int 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}