conformance_test.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 <stdarg.h>
  31. #include <string>
  32. #include "conformance.pb.h"
  33. #include "conformance_test.h"
  34. #include <google/protobuf/stubs/common.h>
  35. #include <google/protobuf/stubs/stringprintf.h>
  36. #include <google/protobuf/wire_format_lite.h>
  37. using conformance::ConformanceRequest;
  38. using conformance::ConformanceResponse;
  39. using conformance::TestAllTypes;
  40. using google::protobuf::Descriptor;
  41. using google::protobuf::FieldDescriptor;
  42. using google::protobuf::internal::WireFormatLite;
  43. using std::string;
  44. namespace {
  45. /* Routines for building arbitrary protos *************************************/
  46. // We would use CodedOutputStream except that we want more freedom to build
  47. // arbitrary protos (even invalid ones).
  48. const string empty;
  49. string cat(const string& a, const string& b,
  50. const string& c = empty,
  51. const string& d = empty,
  52. const string& e = empty,
  53. const string& f = empty,
  54. const string& g = empty,
  55. const string& h = empty,
  56. const string& i = empty,
  57. const string& j = empty,
  58. const string& k = empty,
  59. const string& l = empty) {
  60. string ret;
  61. ret.reserve(a.size() + b.size() + c.size() + d.size() + e.size() + f.size() +
  62. g.size() + h.size() + i.size() + j.size() + k.size() + l.size());
  63. ret.append(a);
  64. ret.append(b);
  65. ret.append(c);
  66. ret.append(d);
  67. ret.append(e);
  68. ret.append(f);
  69. ret.append(g);
  70. ret.append(h);
  71. ret.append(i);
  72. ret.append(j);
  73. ret.append(k);
  74. ret.append(l);
  75. return ret;
  76. }
  77. // The maximum number of bytes that it takes to encode a 64-bit varint.
  78. #define VARINT_MAX_LEN 10
  79. size_t vencode64(uint64_t val, char *buf) {
  80. if (val == 0) { buf[0] = 0; return 1; }
  81. size_t i = 0;
  82. while (val) {
  83. uint8_t byte = val & 0x7fU;
  84. val >>= 7;
  85. if (val) byte |= 0x80U;
  86. buf[i++] = byte;
  87. }
  88. return i;
  89. }
  90. string varint(uint64_t x) {
  91. char buf[VARINT_MAX_LEN];
  92. size_t len = vencode64(x, buf);
  93. return string(buf, len);
  94. }
  95. // TODO: proper byte-swapping for big-endian machines.
  96. string fixed32(void *data) { return string(static_cast<char*>(data), 4); }
  97. string fixed64(void *data) { return string(static_cast<char*>(data), 8); }
  98. string delim(const string& buf) { return cat(varint(buf.size()), buf); }
  99. string uint32(uint32_t u32) { return fixed32(&u32); }
  100. string uint64(uint64_t u64) { return fixed64(&u64); }
  101. string flt(float f) { return fixed32(&f); }
  102. string dbl(double d) { return fixed64(&d); }
  103. string zz32(int32_t x) { return varint(WireFormatLite::ZigZagEncode32(x)); }
  104. string zz64(int64_t x) { return varint(WireFormatLite::ZigZagEncode64(x)); }
  105. string tag(uint32_t fieldnum, char wire_type) {
  106. return varint((fieldnum << 3) | wire_type);
  107. }
  108. string submsg(uint32_t fn, const string& buf) {
  109. return cat( tag(fn, WireFormatLite::WIRETYPE_LENGTH_DELIMITED), delim(buf) );
  110. }
  111. #define UNKNOWN_FIELD 666
  112. uint32_t GetFieldNumberForType(WireFormatLite::FieldType type, bool repeated) {
  113. const Descriptor* d = TestAllTypes().GetDescriptor();
  114. for (int i = 0; i < d->field_count(); i++) {
  115. const FieldDescriptor* f = d->field(i);
  116. if (static_cast<WireFormatLite::FieldType>(f->type()) == type &&
  117. f->is_repeated() == repeated) {
  118. return f->number();
  119. }
  120. }
  121. GOOGLE_LOG(FATAL) << "Couldn't find field with type " << (int)type;
  122. return 0;
  123. }
  124. } // anonymous namespace
  125. namespace google {
  126. namespace protobuf {
  127. void ConformanceTestSuite::ReportSuccess() {
  128. successes_++;
  129. }
  130. void ConformanceTestSuite::ReportFailure(const char *fmt, ...) {
  131. va_list args;
  132. va_start(args, fmt);
  133. StringAppendV(&output_, fmt, args);
  134. va_end(args);
  135. failures_++;
  136. }
  137. void ConformanceTestSuite::RunTest(const ConformanceRequest& request,
  138. ConformanceResponse* response) {
  139. string serialized_request;
  140. string serialized_response;
  141. request.SerializeToString(&serialized_request);
  142. runner_->RunTest(serialized_request, &serialized_response);
  143. if (!response->ParseFromString(serialized_response)) {
  144. response->Clear();
  145. response->set_runtime_error("response proto could not be parsed.");
  146. }
  147. if (verbose_) {
  148. StringAppendF(&output_, "conformance test: request=%s, response=%s\n",
  149. request.ShortDebugString().c_str(),
  150. response->ShortDebugString().c_str());
  151. }
  152. }
  153. void ConformanceTestSuite::DoExpectParseFailureForProto(const string& proto,
  154. int line) {
  155. ConformanceRequest request;
  156. ConformanceResponse response;
  157. request.set_protobuf_payload(proto);
  158. // We don't expect output, but if the program erroneously accepts the protobuf
  159. // we let it send its response as this. We must not leave it unspecified.
  160. request.set_requested_output(ConformanceRequest::PROTOBUF);
  161. RunTest(request, &response);
  162. if (response.result_case() == ConformanceResponse::kParseError) {
  163. ReportSuccess();
  164. } else {
  165. ReportFailure("Should have failed, but didn't. Line: %d, Request: %s, "
  166. "response: %s\n",
  167. line,
  168. request.ShortDebugString().c_str(),
  169. response.ShortDebugString().c_str());
  170. }
  171. }
  172. // Expect that this precise protobuf will cause a parse error.
  173. #define ExpectParseFailureForProto(proto) DoExpectParseFailureForProto(proto, __LINE__)
  174. // Expect that this protobuf will cause a parse error, even if it is followed
  175. // by valid protobuf data. We can try running this twice: once with this
  176. // data verbatim and once with this data followed by some valid data.
  177. //
  178. // TODO(haberman): implement the second of these.
  179. #define ExpectHardParseFailureForProto(proto) DoExpectParseFailureForProto(proto, __LINE__)
  180. void ConformanceTestSuite::TestPrematureEOFForType(
  181. WireFormatLite::FieldType type) {
  182. // Incomplete values for each wire type.
  183. static const string incompletes[6] = {
  184. string("\x80"), // VARINT
  185. string("abcdefg"), // 64BIT
  186. string("\x80"), // DELIMITED (partial length)
  187. string(), // START_GROUP (no value required)
  188. string(), // END_GROUP (no value required)
  189. string("abc") // 32BIT
  190. };
  191. uint32_t fieldnum = GetFieldNumberForType(type, false);
  192. uint32_t rep_fieldnum = GetFieldNumberForType(type, true);
  193. WireFormatLite::WireType wire_type =
  194. WireFormatLite::WireTypeForFieldType(type);
  195. const string& incomplete = incompletes[wire_type];
  196. // EOF before a known non-repeated value.
  197. ExpectParseFailureForProto(tag(fieldnum, wire_type));
  198. // EOF before a known repeated value.
  199. ExpectParseFailureForProto(tag(rep_fieldnum, wire_type));
  200. // EOF before an unknown value.
  201. ExpectParseFailureForProto(tag(UNKNOWN_FIELD, wire_type));
  202. // EOF inside a known non-repeated value.
  203. ExpectParseFailureForProto(
  204. cat( tag(fieldnum, wire_type), incomplete ));
  205. // EOF inside a known repeated value.
  206. ExpectParseFailureForProto(
  207. cat( tag(rep_fieldnum, wire_type), incomplete ));
  208. // EOF inside an unknown value.
  209. ExpectParseFailureForProto(
  210. cat( tag(UNKNOWN_FIELD, wire_type), incomplete ));
  211. if (wire_type == WireFormatLite::WIRETYPE_LENGTH_DELIMITED) {
  212. // EOF in the middle of delimited data for known non-repeated value.
  213. ExpectParseFailureForProto(
  214. cat( tag(fieldnum, wire_type), varint(1) ));
  215. // EOF in the middle of delimited data for known repeated value.
  216. ExpectParseFailureForProto(
  217. cat( tag(rep_fieldnum, wire_type), varint(1) ));
  218. // EOF in the middle of delimited data for unknown value.
  219. ExpectParseFailureForProto(
  220. cat( tag(UNKNOWN_FIELD, wire_type), varint(1) ));
  221. if (type == WireFormatLite::TYPE_MESSAGE) {
  222. // Submessage ends in the middle of a value.
  223. string incomplete_submsg =
  224. cat( tag(WireFormatLite::TYPE_INT32, WireFormatLite::WIRETYPE_VARINT),
  225. incompletes[WireFormatLite::WIRETYPE_VARINT] );
  226. ExpectHardParseFailureForProto(
  227. cat( tag(fieldnum, WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
  228. varint(incomplete_submsg.size()),
  229. incomplete_submsg ));
  230. }
  231. } else if (type != WireFormatLite::TYPE_GROUP) {
  232. // Non-delimited, non-group: eligible for packing.
  233. // Packed region ends in the middle of a value.
  234. ExpectHardParseFailureForProto(
  235. cat( tag(rep_fieldnum, WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
  236. varint(incomplete.size()),
  237. incomplete ));
  238. // EOF in the middle of packed region.
  239. ExpectParseFailureForProto(
  240. cat( tag(rep_fieldnum, WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
  241. varint(1) ));
  242. }
  243. }
  244. void ConformanceTestSuite::RunSuite(ConformanceTestRunner* runner,
  245. std::string* output) {
  246. runner_ = runner;
  247. output_.clear();
  248. successes_ = 0;
  249. failures_ = 0;
  250. for (int i = 1; i <= FieldDescriptor::MAX_TYPE; i++) {
  251. if (i == FieldDescriptor::TYPE_GROUP) continue;
  252. TestPrematureEOFForType(static_cast<WireFormatLite::FieldType>(i));
  253. }
  254. StringAppendF(&output_,
  255. "CONFORMANCE SUITE FINISHED: completed %d tests, %d successes, "
  256. "%d failures.\n",
  257. successes_ + failures_, successes_, failures_);
  258. output->assign(output_);
  259. }
  260. } // namespace protobuf
  261. } // namespace google