conformance_objc.m 5.7 KB

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