conformance_objc.m 7.2 KB

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