conformance_objc.m 5.8 KB

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