• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
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];
67  GPBMessage *testMessage = nil;
68
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: {
75      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 {
81        Die(@"Protobuf request had an unknown message_type: %@", request.messageType);
82      }
83      NSError *error = nil;
84      testMessage = [msgClass parseFromData:request.protobufPayload error:&error];
85      if (!testMessage) {
86        response.parseError =
87            [NSString stringWithFormat:@"Parse error: %@", error];
88      }
89      break;
90    }
91
92    case ConformanceRequest_Payload_OneOfCase_JsonPayload:
93      response.skipped = @"ObjC doesn't support parsing JSON";
94      break;
95
96    case ConformanceRequest_Payload_OneOfCase_JspbPayload:
97      response.skipped =
98          @"ConformanceRequest had a jspb_payload ConformanceRequest.payload;"
99          " those aren't supposed to happen with opensource.";
100      break;
101
102    case ConformanceRequest_Payload_OneOfCase_TextPayload:
103      response.skipped = @"ObjC doesn't support parsing TextFormat";
104      break;
105  }
106
107  if (testMessage) {
108    switch (request.requestedOutputFormat) {
109      case WireFormat_GPBUnrecognizedEnumeratorValue:
110      case WireFormat_Unspecified:
111        Die(@"Unrecognized/unspecified output format: %@", request);
112        break;
113
114      case WireFormat_Protobuf:
115        response.protobufPayload = testMessage.data;
116        if (!response.protobufPayload) {
117          response.serializeError =
118            [NSString stringWithFormat:@"Failed to make data from: %@", testMessage];
119        }
120        break;
121
122      case WireFormat_Json:
123        response.skipped = @"ObjC doesn't support generating JSON";
124        break;
125
126      case WireFormat_Jspb:
127        response.skipped =
128            @"ConformanceRequest had a requested_output_format of JSPB WireFormat; that"
129            " isn't supposed to happen with opensource.";
130        break;
131
132      case WireFormat_TextFormat:
133        // ObjC only has partial objc generation, so don't attempt any tests that need
134        // support.
135        response.skipped = @"ObjC doesn't support generating TextFormat";
136        break;
137    }
138  }
139
140  return response;
141}
142
143static uint32_t UInt32FromLittleEndianData(NSData *data) {
144  if (data.length != sizeof(uint32_t)) {
145    Die(@"Data not the right size for uint32_t: %@", data);
146  }
147  uint32_t value;
148  memcpy(&value, data.bytes, sizeof(uint32_t));
149  return CFSwapInt32LittleToHost(value);
150}
151
152static NSData *UInt32ToLittleEndianData(uint32_t num) {
153  uint32_t value = CFSwapInt32HostToLittle(num);
154  return [NSData dataWithBytes:&value length:sizeof(uint32_t)];
155}
156
157static BOOL DoTestIo(NSFileHandle *input, NSFileHandle *output) {
158  // See conformance_test_runner.cc for the wire format.
159  NSData *data = CheckedReadDataOfLength(input, sizeof(uint32_t));
160  if (!data) {
161    // EOF.
162    return NO;
163  }
164  uint32_t numBytes = UInt32FromLittleEndianData(data);
165  data = CheckedReadDataOfLength(input, numBytes);
166  if (!data) {
167    Die(@"Failed to read request");
168  }
169
170  NSError *error = nil;
171  ConformanceRequest *request = [ConformanceRequest parseFromData:data
172                                                            error:&error];
173  if (!request) {
174    Die(@"Failed to parse the message data: %@", error);
175  }
176
177  ConformanceResponse *response = DoTest(request);
178  if (!response) {
179    Die(@"Failed to make a reply from %@", request);
180  }
181
182  data = response.data;
183  [output writeData:UInt32ToLittleEndianData((int32_t)data.length)];
184  [output writeData:data];
185
186  if (verbose) {
187    NSLog(@"Request: %@", request);
188    NSLog(@"Response: %@", response);
189  }
190
191  ++testCount;
192  return YES;
193}
194
195int main(int argc, const char *argv[]) {
196  @autoreleasepool {
197    NSFileHandle *input = [[NSFileHandle fileHandleWithStandardInput] retain];
198    NSFileHandle *output = [[NSFileHandle fileHandleWithStandardOutput] retain];
199
200    BOOL notDone = YES;
201    while (notDone) {
202      @autoreleasepool {
203        notDone = DoTestIo(input, output);
204      }
205    }
206
207    NSLog(@"Received EOF from test runner after %d tests, exiting.", testCount);
208  }
209  return 0;
210}
211