conformance_objc.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. Die(@"Request didn't have a payload: %@", request);
  64. break;
  65. case ConformanceRequest_Payload_OneOfCase_ProtobufPayload: {
  66. Class msgClass = nil;
  67. if ([request.messageType isEqual:@"protobuf_test_messages.proto3.TestAllTypesProto3"]) {
  68. msgClass = [Proto3TestAllTypesProto3 class];
  69. } else if ([request.messageType isEqual:@"protobuf_test_messages.proto2.TestAllTypesProto2"]) {
  70. msgClass = [TestAllTypesProto2 class];
  71. } else {
  72. Die(@"Protobuf request had an unknown message_type: %@", request.messageType);
  73. }
  74. NSError *error = nil;
  75. testMessage = [msgClass parseFromData:request.protobufPayload error:&error];
  76. if (!testMessage) {
  77. response.parseError =
  78. [NSString stringWithFormat:@"Parse error: %@", error];
  79. }
  80. break;
  81. }
  82. case ConformanceRequest_Payload_OneOfCase_JsonPayload:
  83. response.skipped = @"ObjC doesn't support parsing JSON";
  84. break;
  85. case ConformanceRequest_Payload_OneOfCase_JspbPayload:
  86. response.skipped =
  87. @"ConformanceRequest had a jspb_payload ConformanceRequest.payload;"
  88. " those aren't supposed to happen with opensource.";
  89. break;
  90. case ConformanceRequest_Payload_OneOfCase_TextPayload:
  91. response.skipped = @"ObjC doesn't support parsing TextFormat";
  92. break;
  93. }
  94. if (testMessage) {
  95. switch (request.requestedOutputFormat) {
  96. case WireFormat_GPBUnrecognizedEnumeratorValue:
  97. case WireFormat_Unspecified:
  98. Die(@"Unrecognized/unspecified output format: %@", request);
  99. break;
  100. case WireFormat_Protobuf:
  101. response.protobufPayload = testMessage.data;
  102. if (!response.protobufPayload) {
  103. response.serializeError =
  104. [NSString stringWithFormat:@"Failed to make data from: %@", testMessage];
  105. }
  106. break;
  107. case WireFormat_Json:
  108. response.skipped = @"ObjC doesn't support generating JSON";
  109. break;
  110. case WireFormat_Jspb:
  111. response.skipped =
  112. @"ConformanceRequest had a requested_output_format of JSPB WireFormat; that"
  113. " isn't supposed to happen with opensource.";
  114. break;
  115. case WireFormat_TextFormat:
  116. // ObjC only has partial objc generation, so don't attempt any tests that need
  117. // support.
  118. response.skipped = @"ObjC doesn't support generating TextFormat";
  119. break;
  120. }
  121. }
  122. return response;
  123. }
  124. static uint32_t UInt32FromLittleEndianData(NSData *data) {
  125. if (data.length != sizeof(uint32_t)) {
  126. Die(@"Data not the right size for uint32_t: %@", data);
  127. }
  128. uint32_t value;
  129. memcpy(&value, data.bytes, sizeof(uint32_t));
  130. return CFSwapInt32LittleToHost(value);
  131. }
  132. static NSData *UInt32ToLittleEndianData(uint32_t num) {
  133. uint32_t value = CFSwapInt32HostToLittle(num);
  134. return [NSData dataWithBytes:&value length:sizeof(uint32_t)];
  135. }
  136. static BOOL DoTestIo(NSFileHandle *input, NSFileHandle *output) {
  137. // See conformance_test_runner.cc for the wire format.
  138. NSData *data = CheckedReadDataOfLength(input, sizeof(uint32_t));
  139. if (!data) {
  140. // EOF.
  141. return NO;
  142. }
  143. uint32_t numBytes = UInt32FromLittleEndianData(data);
  144. data = CheckedReadDataOfLength(input, numBytes);
  145. if (!data) {
  146. Die(@"Failed to read request");
  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. ConformanceResponse *response = DoTest(request);
  155. if (!response) {
  156. Die(@"Failed to make a reply from %@", request);
  157. }
  158. data = response.data;
  159. [output writeData:UInt32ToLittleEndianData((int32_t)data.length)];
  160. [output writeData:data];
  161. if (verbose) {
  162. NSLog(@"Request: %@", request);
  163. NSLog(@"Response: %@", response);
  164. }
  165. ++testCount;
  166. return YES;
  167. }
  168. int main(int argc, const char *argv[]) {
  169. @autoreleasepool {
  170. NSFileHandle *input = [[NSFileHandle fileHandleWithStandardInput] retain];
  171. NSFileHandle *output = [[NSFileHandle fileHandleWithStandardOutput] retain];
  172. BOOL notDone = YES;
  173. while (notDone) {
  174. @autoreleasepool {
  175. notDone = DoTestIo(input, output);
  176. }
  177. }
  178. NSLog(@"Received EOF from test runner after %d tests, exiting.", testCount);
  179. }
  180. return 0;
  181. }