• 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
35static void Die(NSString *format, ...) __dead2;
36
37static BOOL verbose = NO;
38static int32_t testCount = 0;
39
40static void Die(NSString *format, ...) {
41  va_list args;
42  va_start(args, format);
43  NSString *msg = [[NSString alloc] initWithFormat:format arguments:args];
44  NSLog(@"%@", msg);
45  va_end(args);
46  [msg release];
47  exit(66);
48}
49
50static NSData *CheckedReadDataOfLength(NSFileHandle *handle, NSUInteger numBytes) {
51  NSData *data = [handle readDataOfLength:numBytes];
52  NSUInteger dataLen = data.length;
53  if (dataLen == 0) {
54    return nil;  // EOF.
55  }
56  if (dataLen != numBytes) {
57    Die(@"Failed to read the request length (%d), only got: %@",
58        numBytes, data);
59  }
60  return data;
61}
62
63static ConformanceResponse *DoTest(ConformanceRequest *request) {
64  ConformanceResponse *response = [ConformanceResponse message];
65  TestAllTypes *testMessage = nil;
66
67  switch (request.payloadOneOfCase) {
68    case ConformanceRequest_Payload_OneOfCase_GPBUnsetOneOfCase:
69      Die(@"Request didn't have a payload: %@", request);
70      break;
71
72    case ConformanceRequest_Payload_OneOfCase_ProtobufPayload: {
73      NSError *error = nil;
74      testMessage = [TestAllTypes parseFromData:request.protobufPayload
75                                          error:&error];
76      if (!testMessage) {
77        response.parseError =
78            [NSString stringWithFormat:@"Parse error: %@", error];
79      }
80      break;
81    }
82
83    case ConformanceRequest_Payload_OneOfCase_JsonPayload:
84      response.skipped = @"ObjC doesn't support parsing JSON";
85      break;
86  }
87
88  if (testMessage) {
89    switch (request.requestedOutputFormat) {
90      case WireFormat_GPBUnrecognizedEnumeratorValue:
91      case WireFormat_Unspecified:
92        Die(@"Unrecognized/unspecified output format: %@", request);
93        break;
94
95      case WireFormat_Protobuf:
96        response.protobufPayload = testMessage.data;
97        if (!response.protobufPayload) {
98          response.serializeError =
99            [NSString stringWithFormat:@"Failed to make data from: %@", testMessage];
100        }
101        break;
102
103      case WireFormat_Json:
104        response.skipped = @"ObjC doesn't support generating JSON";
105        break;
106    }
107  }
108
109  return response;
110}
111
112static uint32_t UInt32FromLittleEndianData(NSData *data) {
113  if (data.length != sizeof(uint32_t)) {
114    Die(@"Data not the right size for uint32_t: %@", data);
115  }
116  uint32_t value;
117  memcpy(&value, data.bytes, sizeof(uint32_t));
118  return CFSwapInt32LittleToHost(value);
119}
120
121static NSData *UInt32ToLittleEndianData(uint32_t num) {
122  uint32_t value = CFSwapInt32HostToLittle(num);
123  return [NSData dataWithBytes:&value length:sizeof(uint32_t)];
124}
125
126static BOOL DoTestIo(NSFileHandle *input, NSFileHandle *output) {
127  // See conformance_test_runner.cc for the wire format.
128  NSData *data = CheckedReadDataOfLength(input, sizeof(uint32_t));
129  if (!data) {
130    // EOF.
131    return NO;
132  }
133  uint32_t numBytes = UInt32FromLittleEndianData(data);
134  data = CheckedReadDataOfLength(input, numBytes);
135  if (!data) {
136    Die(@"Failed to read request");
137  }
138
139  NSError *error = nil;
140  ConformanceRequest *request = [ConformanceRequest parseFromData:data
141                                                            error:&error];
142  if (!request) {
143    Die(@"Failed to parse the message data: %@", error);
144  }
145
146  ConformanceResponse *response = DoTest(request);
147  if (!response) {
148    Die(@"Failed to make a reply from %@", request);
149  }
150
151  data = response.data;
152  [output writeData:UInt32ToLittleEndianData((int32_t)data.length)];
153  [output writeData:data];
154
155  if (verbose) {
156    NSLog(@"Request: %@", request);
157    NSLog(@"Response: %@", response);
158  }
159
160  ++testCount;
161  return YES;
162}
163
164int main(int argc, const char *argv[]) {
165  @autoreleasepool {
166    NSFileHandle *input = [[NSFileHandle fileHandleWithStandardInput] retain];
167    NSFileHandle *output = [[NSFileHandle fileHandleWithStandardOutput] retain];
168
169    BOOL notDone = YES;
170    while (notDone) {
171      @autoreleasepool {
172        notDone = DoTestIo(input, output);
173      }
174    }
175
176    NSLog(@"Received EOF from test runner after %d tests, exiting.", testCount);
177  }
178  return 0;
179}
180