conformance_objc.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. }
  86. if (testMessage) {
  87. switch (request.requestedOutputFormat) {
  88. case WireFormat_GPBUnrecognizedEnumeratorValue:
  89. case WireFormat_Unspecified:
  90. Die(@"Unrecognized/unspecified output format: %@", request);
  91. break;
  92. case WireFormat_Protobuf:
  93. response.protobufPayload = testMessage.data;
  94. if (!response.protobufPayload) {
  95. response.serializeError =
  96. [NSString stringWithFormat:@"Failed to make data from: %@", testMessage];
  97. }
  98. break;
  99. case WireFormat_Json:
  100. response.skipped = @"ObjC doesn't support generating JSON";
  101. break;
  102. }
  103. }
  104. return response;
  105. }
  106. static uint32_t UInt32FromLittleEndianData(NSData *data) {
  107. if (data.length != sizeof(uint32_t)) {
  108. Die(@"Data not the right size for uint32_t: %@", data);
  109. }
  110. uint32_t value;
  111. memcpy(&value, data.bytes, sizeof(uint32_t));
  112. return CFSwapInt32LittleToHost(value);
  113. }
  114. static NSData *UInt32ToLittleEndianData(uint32_t num) {
  115. uint32_t value = CFSwapInt32HostToLittle(num);
  116. return [NSData dataWithBytes:&value length:sizeof(uint32_t)];
  117. }
  118. static BOOL DoTestIo(NSFileHandle *input, NSFileHandle *output) {
  119. // See conformance_test_runner.cc for the wire format.
  120. NSData *data = CheckedReadDataOfLength(input, sizeof(uint32_t));
  121. if (!data) {
  122. // EOF.
  123. return NO;
  124. }
  125. uint32_t numBytes = UInt32FromLittleEndianData(data);
  126. data = CheckedReadDataOfLength(input, numBytes);
  127. if (!data) {
  128. Die(@"Failed to read request");
  129. }
  130. NSError *error = nil;
  131. ConformanceRequest *request = [ConformanceRequest parseFromData:data
  132. error:&error];
  133. if (!request) {
  134. Die(@"Failed to parse the message data: %@", error);
  135. }
  136. ConformanceResponse *response = DoTest(request);
  137. if (!response) {
  138. Die(@"Failed to make a reply from %@", request);
  139. }
  140. data = response.data;
  141. [output writeData:UInt32ToLittleEndianData((int32_t)data.length)];
  142. [output writeData:data];
  143. if (verbose) {
  144. NSLog(@"Request: %@", request);
  145. NSLog(@"Response: %@", response);
  146. }
  147. ++testCount;
  148. return YES;
  149. }
  150. int main(int argc, const char *argv[]) {
  151. @autoreleasepool {
  152. NSFileHandle *input = [[NSFileHandle fileHandleWithStandardInput] retain];
  153. NSFileHandle *output = [[NSFileHandle fileHandleWithStandardOutput] retain];
  154. BOOL notDone = YES;
  155. while (notDone) {
  156. @autoreleasepool {
  157. notDone = DoTestIo(input, output);
  158. }
  159. }
  160. NSLog(@"Received EOF from test runner after %d tests, exiting.", testCount);
  161. }
  162. return 0;
  163. }