conformance_cpp.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 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. #include <errno.h>
  31. #include <stdarg.h>
  32. #include <unistd.h>
  33. #include "conformance.pb.h"
  34. using std::string;
  35. using conformance::ConformanceRequest;
  36. using conformance::ConformanceResponse;
  37. using conformance::TestAllTypes;
  38. int test_count = 0;
  39. bool verbose = false;
  40. bool CheckedRead(int fd, void *buf, size_t len) {
  41. size_t ofs = 0;
  42. while (len > 0) {
  43. ssize_t bytes_read = read(fd, (char*)buf + ofs, len);
  44. if (bytes_read == 0) return false;
  45. if (bytes_read < 0) {
  46. GOOGLE_LOG(FATAL) << "Error reading from test runner: " << strerror(errno);
  47. }
  48. len -= bytes_read;
  49. ofs += bytes_read;
  50. }
  51. return true;
  52. }
  53. void CheckedWrite(int fd, const void *buf, size_t len) {
  54. if (write(fd, buf, len) != len) {
  55. GOOGLE_LOG(FATAL) << "Error writing to test runner: " << strerror(errno);
  56. }
  57. }
  58. void DoTest(const ConformanceRequest& request, ConformanceResponse* response) {
  59. TestAllTypes test_message;
  60. switch (request.payload_case()) {
  61. case ConformanceRequest::kProtobufPayload:
  62. if (!test_message.ParseFromString(request.protobuf_payload())) {
  63. // Getting parse details would involve something like:
  64. // http://stackoverflow.com/questions/22121922/how-can-i-get-more-details-about-errors-generated-during-protobuf-parsing-c
  65. response->set_parse_error("Parse error (no more details available).");
  66. return;
  67. }
  68. break;
  69. case ConformanceRequest::kJsonPayload:
  70. response->set_runtime_error("JSON input is not yet supported.");
  71. break;
  72. case ConformanceRequest::PAYLOAD_NOT_SET:
  73. GOOGLE_LOG(FATAL) << "Request didn't have payload.";
  74. break;
  75. }
  76. switch (request.requested_output()) {
  77. case ConformanceRequest::UNSPECIFIED:
  78. GOOGLE_LOG(FATAL) << "Unspecified output format";
  79. break;
  80. case ConformanceRequest::PROTOBUF:
  81. test_message.SerializeToString(response->mutable_protobuf_payload());
  82. break;
  83. case ConformanceRequest::JSON:
  84. response->set_runtime_error("JSON output is not yet supported.");
  85. break;
  86. }
  87. }
  88. bool DoTestIo() {
  89. string serialized_input;
  90. string serialized_output;
  91. ConformanceRequest request;
  92. ConformanceResponse response;
  93. uint32_t bytes;
  94. if (!CheckedRead(STDIN_FILENO, &bytes, sizeof(uint32_t))) {
  95. // EOF.
  96. return false;
  97. }
  98. serialized_input.resize(bytes);
  99. if (!CheckedRead(STDIN_FILENO, (char*)serialized_input.c_str(), bytes)) {
  100. GOOGLE_LOG(ERROR) << "Unexpected EOF on stdin. " << strerror(errno);
  101. }
  102. if (!request.ParseFromString(serialized_input)) {
  103. GOOGLE_LOG(FATAL) << "Parse of ConformanceRequest proto failed.";
  104. return false;
  105. }
  106. DoTest(request, &response);
  107. response.SerializeToString(&serialized_output);
  108. bytes = serialized_output.size();
  109. CheckedWrite(STDOUT_FILENO, &bytes, sizeof(uint32_t));
  110. CheckedWrite(STDOUT_FILENO, serialized_output.c_str(), bytes);
  111. if (verbose) {
  112. fprintf(stderr, "conformance-cpp: request=%s, response=%s\n",
  113. request.ShortDebugString().c_str(),
  114. response.ShortDebugString().c_str());
  115. }
  116. test_count++;
  117. return true;
  118. }
  119. int main() {
  120. while (1) {
  121. if (!DoTestIo()) {
  122. fprintf(stderr, "conformance-cpp: received EOF from test runner "
  123. "after %d tests, exiting\n", test_count);
  124. return 0;
  125. }
  126. }
  127. }