conformance_php.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. require_once("Conformance/WireFormat.php");
  3. require_once("Conformance/ConformanceResponse.php");
  4. require_once("Conformance/ConformanceRequest.php");
  5. require_once("Conformance/FailureSet.php");
  6. require_once("Conformance/JspbEncodingConfig.php");
  7. require_once("Conformance/TestCategory.php");
  8. require_once("Protobuf_test_messages/Proto3/ForeignMessage.php");
  9. require_once("Protobuf_test_messages/Proto3/ForeignEnum.php");
  10. require_once("Protobuf_test_messages/Proto3/TestAllTypesProto3.php");
  11. require_once("Protobuf_test_messages/Proto3/TestAllTypesProto3/AliasedEnum.php");
  12. require_once("Protobuf_test_messages/Proto3/TestAllTypesProto3/NestedMessage.php");
  13. require_once("Protobuf_test_messages/Proto3/TestAllTypesProto3/NestedEnum.php");
  14. require_once("GPBMetadata/Conformance.php");
  15. require_once("GPBMetadata/Google/Protobuf/TestMessagesProto3.php");
  16. use \Conformance\TestCategory;
  17. use \Conformance\WireFormat;
  18. if (!ini_get("date.timezone")) {
  19. ini_set("date.timezone", "UTC");
  20. }
  21. $test_count = 0;
  22. function doTest($request)
  23. {
  24. $test_message = new \Protobuf_test_messages\Proto3\TestAllTypesProto3();
  25. $response = new \Conformance\ConformanceResponse();
  26. if ($request->getPayload() == "protobuf_payload") {
  27. if ($request->getMessageType() == "conformance.FailureSet") {
  28. $response->setProtobufPayload("");
  29. return $response;
  30. } elseif ($request->getMessageType() == "protobuf_test_messages.proto3.TestAllTypesProto3") {
  31. try {
  32. $test_message->mergeFromString($request->getProtobufPayload());
  33. } catch (Exception $e) {
  34. $response->setParseError($e->getMessage());
  35. return $response;
  36. }
  37. } elseif ($request->getMessageType() == "protobuf_test_messages.proto2.TestAllTypesProto2") {
  38. $response->setSkipped("PHP doesn't support proto2");
  39. return $response;
  40. } else {
  41. trigger_error("Protobuf request doesn't have specific payload type", E_USER_ERROR);
  42. }
  43. } elseif ($request->getPayload() == "json_payload") {
  44. $ignore_json_unknown =
  45. ($request->getTestCategory() ==
  46. TestCategory::JSON_IGNORE_UNKNOWN_PARSING_TEST);
  47. try {
  48. $test_message->mergeFromJsonString($request->getJsonPayload(),
  49. $ignore_json_unknown);
  50. } catch (Exception $e) {
  51. $response->setParseError($e->getMessage());
  52. return $response;
  53. }
  54. } elseif ($request->getPayload() == "text_payload") {
  55. $response->setSkipped("PHP doesn't support text format yet");
  56. return $response;
  57. } else {
  58. trigger_error("Request didn't have payload.", E_USER_ERROR);
  59. }
  60. if ($request->getRequestedOutputFormat() == WireFormat::UNSPECIFIED) {
  61. trigger_error("Unspecified output format.", E_USER_ERROR);
  62. } elseif ($request->getRequestedOutputFormat() == WireFormat::PROTOBUF) {
  63. $response->setProtobufPayload($test_message->serializeToString());
  64. } elseif ($request->getRequestedOutputFormat() == WireFormat::JSON) {
  65. try {
  66. $response->setJsonPayload($test_message->serializeToJsonString());
  67. } catch (Exception $e) {
  68. $response->setSerializeError($e->getMessage());
  69. return $response;
  70. }
  71. }
  72. return $response;
  73. }
  74. function doTestIO()
  75. {
  76. $length_bytes = fread(STDIN, 4);
  77. if (strlen($length_bytes) == 0) {
  78. return false; # EOF
  79. } elseif (strlen($length_bytes) != 4) {
  80. fwrite(STDERR, "I/O error\n");
  81. return false;
  82. }
  83. $length = unpack("V", $length_bytes)[1];
  84. $serialized_request = fread(STDIN, $length);
  85. if (strlen($serialized_request) != $length) {
  86. trigger_error("I/O error", E_USER_ERROR);
  87. }
  88. $request = new \Conformance\ConformanceRequest();
  89. $request->mergeFromString($serialized_request);
  90. $response = doTest($request);
  91. $serialized_response = $response->serializeToString();
  92. fwrite(STDOUT, pack("V", strlen($serialized_response)));
  93. fwrite(STDOUT, $serialized_response);
  94. $GLOBALS['test_count'] += 1;
  95. return true;
  96. }
  97. while(true){
  98. if (!doTestIO()) {
  99. fprintf(STDERR,
  100. "conformance_php: received EOF from test runner " +
  101. "after %d tests, exiting\n", $test_count);
  102. exit;
  103. }
  104. }