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