ruby_memprofiler_pprof 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (200) hide show
  1. checksums.yaml +7 -0
  2. data/ext/ruby_memprofiler_pprof/backtrace.c +429 -0
  3. data/ext/ruby_memprofiler_pprof/collector.c +1055 -0
  4. data/ext/ruby_memprofiler_pprof/compat.c +182 -0
  5. data/ext/ruby_memprofiler_pprof/extconf.rb +72 -0
  6. data/ext/ruby_memprofiler_pprof/pprof.upb.c +170 -0
  7. data/ext/ruby_memprofiler_pprof/pprof.upb.h +848 -0
  8. data/ext/ruby_memprofiler_pprof/pprof_out.c +285 -0
  9. data/ext/ruby_memprofiler_pprof/ruby_memprofiler_pprof.c +11 -0
  10. data/ext/ruby_memprofiler_pprof/ruby_memprofiler_pprof.h +301 -0
  11. data/ext/ruby_memprofiler_pprof/strtab.c +391 -0
  12. data/ext/ruby_memprofiler_pprof/vendor/upb/BUILD +719 -0
  13. data/ext/ruby_memprofiler_pprof/vendor/upb/CONTRIBUTING.md +37 -0
  14. data/ext/ruby_memprofiler_pprof/vendor/upb/DESIGN.md +201 -0
  15. data/ext/ruby_memprofiler_pprof/vendor/upb/LICENSE +26 -0
  16. data/ext/ruby_memprofiler_pprof/vendor/upb/README.md +78 -0
  17. data/ext/ruby_memprofiler_pprof/vendor/upb/WORKSPACE +58 -0
  18. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/BUILD +53 -0
  19. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/amalgamate.py +129 -0
  20. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/build_defs.bzl +160 -0
  21. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/lua.BUILD +127 -0
  22. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/protobuf.patch +54 -0
  23. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/py_proto_library.bzl +137 -0
  24. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/python_downloads.bzl +84 -0
  25. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/system_python.bzl +101 -0
  26. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/upb_proto_library.bzl +388 -0
  27. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/workspace_deps.bzl +89 -0
  28. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/BUILD +252 -0
  29. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/BUILD.googleapis +54 -0
  30. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/benchmark.cc +333 -0
  31. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/build_defs.bzl +88 -0
  32. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/compare.py +118 -0
  33. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/descriptor.proto +888 -0
  34. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/descriptor_sv.proto +890 -0
  35. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/empty.proto +6 -0
  36. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/gen_protobuf_binary_cc.py +64 -0
  37. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/gen_synthetic_protos.py +118 -0
  38. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/gen_upb_binary_c.py +65 -0
  39. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/BUILD.bazel +102 -0
  40. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/README.md +23 -0
  41. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/build_defs.bzl +73 -0
  42. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/make_cmakelists.py +340 -0
  43. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/staleness_test.py +57 -0
  44. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/staleness_test_lib.py +186 -0
  45. data/ext/ruby_memprofiler_pprof/vendor/upb/docs/render.py +43 -0
  46. data/ext/ruby_memprofiler_pprof/vendor/upb/docs/style-guide.md +65 -0
  47. data/ext/ruby_memprofiler_pprof/vendor/upb/docs/vs-cpp-protos.md +255 -0
  48. data/ext/ruby_memprofiler_pprof/vendor/upb/docs/wrapping-upb.md +444 -0
  49. data/ext/ruby_memprofiler_pprof/vendor/upb/python/BUILD +216 -0
  50. data/ext/ruby_memprofiler_pprof/vendor/upb/python/convert.c +394 -0
  51. data/ext/ruby_memprofiler_pprof/vendor/upb/python/convert.h +63 -0
  52. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor.c +1694 -0
  53. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor.h +80 -0
  54. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_containers.c +704 -0
  55. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_containers.h +114 -0
  56. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_pool.c +650 -0
  57. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_pool.h +48 -0
  58. data/ext/ruby_memprofiler_pprof/vendor/upb/python/dist/BUILD.bazel +193 -0
  59. data/ext/ruby_memprofiler_pprof/vendor/upb/python/dist/dist.bzl +190 -0
  60. data/ext/ruby_memprofiler_pprof/vendor/upb/python/extension_dict.c +247 -0
  61. data/ext/ruby_memprofiler_pprof/vendor/upb/python/extension_dict.h +39 -0
  62. data/ext/ruby_memprofiler_pprof/vendor/upb/python/map.c +522 -0
  63. data/ext/ruby_memprofiler_pprof/vendor/upb/python/map.h +66 -0
  64. data/ext/ruby_memprofiler_pprof/vendor/upb/python/message.c +1909 -0
  65. data/ext/ruby_memprofiler_pprof/vendor/upb/python/message.h +101 -0
  66. data/ext/ruby_memprofiler_pprof/vendor/upb/python/minimal_test.py +183 -0
  67. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/BUILD +70 -0
  68. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/README.md +11 -0
  69. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/descriptor_database_test_wrapper.py +30 -0
  70. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/descriptor_pool_test_wrapper.py +45 -0
  71. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/descriptor_test_wrapper.py +46 -0
  72. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/generator_test_wrapper.py +30 -0
  73. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/json_format_test_wrapper.py +30 -0
  74. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/keywords_test_wrapper.py +30 -0
  75. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/message_factory_test_wrapper.py +37 -0
  76. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/message_test_wrapper.py +52 -0
  77. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/proto_builder_test_wrapper.py +32 -0
  78. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/pyproto_test_wrapper.bzl +36 -0
  79. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/reflection_test_wrapper.py +45 -0
  80. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/service_reflection_test_wrapper.py +30 -0
  81. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/symbol_database_test_wrapper.py +30 -0
  82. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/text_encoding_test_wrapper.py +30 -0
  83. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/text_format_test_wrapper.py +30 -0
  84. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/unknown_fields_test_wrapper.py +30 -0
  85. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/well_known_types_test_wrapper.py +36 -0
  86. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/wire_format_test_wrapper.py +30 -0
  87. data/ext/ruby_memprofiler_pprof/vendor/upb/python/protobuf.c +350 -0
  88. data/ext/ruby_memprofiler_pprof/vendor/upb/python/protobuf.h +230 -0
  89. data/ext/ruby_memprofiler_pprof/vendor/upb/python/py_extension.bzl +55 -0
  90. data/ext/ruby_memprofiler_pprof/vendor/upb/python/python_api.h +61 -0
  91. data/ext/ruby_memprofiler_pprof/vendor/upb/python/repeated.c +828 -0
  92. data/ext/ruby_memprofiler_pprof/vendor/upb/python/repeated.h +69 -0
  93. data/ext/ruby_memprofiler_pprof/vendor/upb/python/unknown_fields.c +404 -0
  94. data/ext/ruby_memprofiler_pprof/vendor/upb/python/unknown_fields.h +39 -0
  95. data/ext/ruby_memprofiler_pprof/vendor/upb/python/version_script.lds +6 -0
  96. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/LICENSE +32 -0
  97. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/README.google +9 -0
  98. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/console.lua +156 -0
  99. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/lunit.lua +725 -0
  100. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/BUILD +19 -0
  101. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/LICENSE +21 -0
  102. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/naive.c +92 -0
  103. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/range2-neon.c +157 -0
  104. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/range2-sse.c +170 -0
  105. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/utf8_range.h +9 -0
  106. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/BUILD.bazel +129 -0
  107. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/README.md +8 -0
  108. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/def.c +939 -0
  109. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/lua_proto_library.bzl +138 -0
  110. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/main.c +83 -0
  111. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/msg.c +1118 -0
  112. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/test.proto +69 -0
  113. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/test_upb.lua +846 -0
  114. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upb.c +258 -0
  115. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upb.h +132 -0
  116. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upb.lua +58 -0
  117. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upbc.cc +134 -0
  118. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/collections.c +192 -0
  119. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/collections.h +174 -0
  120. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/conformance_upb.c +346 -0
  121. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/conformance_upb_failures.txt +1 -0
  122. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode.c +1221 -0
  123. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode.h +94 -0
  124. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode_fast.c +1055 -0
  125. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode_fast.h +153 -0
  126. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode_internal.h +211 -0
  127. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/def.c +3262 -0
  128. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/def.h +414 -0
  129. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/def.hpp +438 -0
  130. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/empty.proto +1 -0
  131. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/encode.c +604 -0
  132. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/encode.h +71 -0
  133. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/fuzz/BUILD +13 -0
  134. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/fuzz/file_descriptor_parsenew_fuzzer.cc +43 -0
  135. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_decode.c +1509 -0
  136. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_decode.h +47 -0
  137. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_encode.c +776 -0
  138. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_encode.h +62 -0
  139. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table.c +1147 -0
  140. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table.h +189 -0
  141. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table.hpp +112 -0
  142. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors.c +363 -0
  143. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors.h +263 -0
  144. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors_internal.h +59 -0
  145. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors_test.cc +425 -0
  146. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_test.cc +230 -0
  147. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg.c +428 -0
  148. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg.h +114 -0
  149. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg_internal.h +836 -0
  150. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg_test.cc +491 -0
  151. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg_test.proto +195 -0
  152. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/port_def.inc +261 -0
  153. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/port_undef.inc +62 -0
  154. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/reflection.c +323 -0
  155. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/reflection.h +109 -0
  156. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/reflection.hpp +37 -0
  157. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/table.c +926 -0
  158. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/table_internal.h +385 -0
  159. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test.proto +74 -0
  160. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_cpp.cc +186 -0
  161. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_cpp.proto +12 -0
  162. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_generated_code.cc +977 -0
  163. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_table.cc +580 -0
  164. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/text_encode.c +472 -0
  165. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/text_encode.h +64 -0
  166. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb.c +362 -0
  167. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb.h +378 -0
  168. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb.hpp +115 -0
  169. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb_internal.h +68 -0
  170. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/BUILD +121 -0
  171. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/README.md +7 -0
  172. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/compare.c +300 -0
  173. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/compare.h +66 -0
  174. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/compare_test.cc +236 -0
  175. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto.c +572 -0
  176. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto.h +62 -0
  177. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_public_import_test.proto +32 -0
  178. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_regular_import_test.proto +36 -0
  179. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_test.cc +143 -0
  180. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_test.proto +119 -0
  181. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_weak_import_test.proto +28 -0
  182. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_wweak_import_test.proto +28 -0
  183. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields.c +311 -0
  184. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields.h +94 -0
  185. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields_test.cc +202 -0
  186. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields_test.proto +48 -0
  187. data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/BUILD +78 -0
  188. data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/common.cc +77 -0
  189. data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/common.h +112 -0
  190. data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/protoc-gen-upb.cc +1997 -0
  191. data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/protoc-gen-upbdefs.cc +193 -0
  192. data/lib/ruby_memprofiler_pprof/atfork.rb +77 -0
  193. data/lib/ruby_memprofiler_pprof/block_flusher.rb +61 -0
  194. data/lib/ruby_memprofiler_pprof/file_flusher.rb +45 -0
  195. data/lib/ruby_memprofiler_pprof/profile_app.rb +30 -0
  196. data/lib/ruby_memprofiler_pprof/profile_data.rb +18 -0
  197. data/lib/ruby_memprofiler_pprof/version.rb +5 -0
  198. data/lib/ruby_memprofiler_pprof.rb +8 -0
  199. data/libexec/ruby_memprofiler_pprof_profile +16 -0
  200. metadata +257 -0
@@ -0,0 +1,846 @@
1
+ --[[--------------------------------------------------------------------------
2
+
3
+ Copyright (c) 2009-2021, Google LLC
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+ * Redistributions of source code must retain the above copyright
9
+ notice, this list of conditions and the following disclaimer.
10
+ * Redistributions in binary form must reproduce the above copyright
11
+ notice, this list of conditions and the following disclaimer in the
12
+ documentation and/or other materials provided with the distribution.
13
+ * Neither the name of Google LLC nor the
14
+ names of its contributors may be used to endorse or promote products
15
+ derived from this software without specific prior written permission.
16
+
17
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
+ DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY
21
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+
28
+ --]]--------------------------------------------------------------------------
29
+
30
+ local upb = require "lupb"
31
+ local lunit = require "lunit"
32
+ local upb_test = require "upb.bindings.lua.test_pb"
33
+ local test_messages_proto3 = require "google.protobuf.test_messages_proto3_pb"
34
+ local test_messages_proto2 = require "google.protobuf.test_messages_proto2_pb"
35
+ local descriptor = require "google.protobuf.descriptor_pb"
36
+ local empty = require "google.protobuf.empty_pb"
37
+
38
+ if _VERSION >= 'Lua 5.2' then
39
+ _ENV = lunit.module("testupb", "seeall")
40
+ else
41
+ module("testupb", lunit.testcase, package.seeall)
42
+ end
43
+
44
+ function iter_to_array(iter)
45
+ local arr = {}
46
+ for v in iter do
47
+ arr[#arr + 1] = v
48
+ end
49
+ return arr
50
+ end
51
+
52
+ function test_def_readers()
53
+ local m = test_messages_proto3.TestAllTypesProto3
54
+ assert_equal("TestAllTypesProto3", m:name())
55
+ assert_equal("protobuf_test_messages.proto3.TestAllTypesProto3", m:full_name())
56
+
57
+ -- field
58
+ local f = m:field("optional_int32")
59
+ local f2 = m:field(1)
60
+ assert_equal(f, f2)
61
+ assert_equal(1, f:number())
62
+ assert_equal("optional_int32", f:name())
63
+ assert_equal(upb.LABEL_OPTIONAL, f:label())
64
+ assert_equal(upb.DESCRIPTOR_TYPE_INT32, f:descriptor_type())
65
+ assert_equal(upb.TYPE_INT32, f:type())
66
+ assert_nil(f:containing_oneof())
67
+ assert_equal(m, f:containing_type())
68
+ assert_equal(0, f:default())
69
+ local message_field_count = 0
70
+ for field in m:fields() do
71
+ message_field_count = message_field_count + 1
72
+ end
73
+ assert_equal(message_field_count, #m)
74
+
75
+ local message_oneof_count = 0
76
+ for oneof in m:oneofs() do
77
+ message_oneof_count = message_oneof_count + 1
78
+ end
79
+ assert_equal(message_oneof_count, m:oneof_count())
80
+
81
+ -- oneof
82
+ local o = m:lookup_name("oneof_field")
83
+ assert_equal("oneof_field", o:name())
84
+ assert_equal(m, o:containing_type())
85
+ local oneof_field_count = 0
86
+ for field in o:fields() do
87
+ oneof_field_count = oneof_field_count + 1
88
+ end
89
+ assert_equal(oneof_field_count, #o)
90
+
91
+ -- enum
92
+ local e = test_messages_proto3['TestAllTypesProto3.NestedEnum']
93
+ assert_true(#e > 3 and #e < 10)
94
+ assert_equal(2, e:value("BAZ"):number())
95
+ end
96
+
97
+ function test_msg_map()
98
+ msg = test_messages_proto3.TestAllTypesProto3()
99
+ msg.map_int32_int32[5] = 10
100
+ msg.map_int32_int32[6] = 12
101
+ assert_equal(10, msg.map_int32_int32[5])
102
+ assert_equal(12, msg.map_int32_int32[6])
103
+
104
+ -- Test overwrite.
105
+ msg.map_int32_int32[5] = 20
106
+ assert_equal(20, msg.map_int32_int32[5])
107
+ assert_equal(12, msg.map_int32_int32[6])
108
+ msg.map_int32_int32[5] = 10
109
+
110
+ -- Test delete.
111
+ msg.map_int32_int32[5] = nil
112
+ assert_nil(msg.map_int32_int32[5])
113
+ assert_equal(12, msg.map_int32_int32[6])
114
+ msg.map_int32_int32[5] = 10
115
+
116
+ local serialized = upb.encode(msg)
117
+ assert_true(#serialized > 0)
118
+ local msg2 = upb.decode(test_messages_proto3.TestAllTypesProto3, serialized)
119
+ assert_equal(10, msg2.map_int32_int32[5])
120
+ assert_equal(12, msg2.map_int32_int32[6])
121
+ end
122
+
123
+ function test_map_sorting()
124
+ function msg_with_int32_entries(start, expand)
125
+ local msg = test_messages_proto3.TestAllTypesProto3()
126
+ for i=start,start + 8 do
127
+ msg.map_int32_int32[i] = i * 2
128
+ end
129
+
130
+ if expand then
131
+ for i=start+20,200 do
132
+ msg.map_int32_int32[i] = i
133
+ end
134
+ for i=start+20,200 do
135
+ msg.map_int32_int32[i] = nil
136
+ end
137
+ end
138
+ return msg
139
+ end
140
+
141
+ function msg_with_msg_entries(expand)
142
+ local msg = test_messages_proto3.TestAllTypesProto3()
143
+ -- 8! = 40320 possible orderings makes it overwhelmingly likely that two
144
+ -- random orderings will be different.
145
+ for i=1,8 do
146
+ local submsg = test_messages_proto3.TestAllTypesProto3.NestedMessage()
147
+ submsg.corecursive = msg_with_int32_entries(i, expand)
148
+ msg.map_string_nested_message[tostring(i)] = submsg
149
+ end
150
+
151
+ expand = false
152
+ if expand then
153
+ for i=21,2000 do
154
+ local submsg = test_messages_proto3.TestAllTypesProto3.NestedMessage()
155
+ submsg.corecursive = msg_with_int32_entries(i, expand)
156
+ msg.map_string_nested_message[tostring(i)] = submsg
157
+ end
158
+ for i=21,2000 do
159
+ msg.map_string_nested_message[tostring(i)] = nil
160
+ end
161
+ end
162
+ return msg
163
+ end
164
+
165
+ -- Create two messages with the same contents but (hopefully) different
166
+ -- map table orderings.
167
+ local msg = msg_with_msg_entries(false)
168
+ local msg2 = msg_with_msg_entries(true)
169
+
170
+ local text1 = upb.text_encode(msg)
171
+ local text2 = upb.text_encode(msg2)
172
+ assert_equal(text1, text2)
173
+
174
+ local binary1 = upb.encode(msg, {upb.ENCODE_DETERMINISTIC})
175
+ local binary2 = upb.encode(msg2, {upb.ENCODE_DETERMINISTIC})
176
+ assert_equal(binary1, binary2)
177
+
178
+ -- Non-sorted map should compare different.
179
+ local text3 = upb.text_encode(msg, {upb.TXTENC_NOSORT})
180
+ assert_not_equal(text1, text3)
181
+
182
+ local binary3 = upb.encode(msg)
183
+ assert_not_equal(binary1, binary3)
184
+ end
185
+
186
+ function test_utf8()
187
+ local proto2_msg = test_messages_proto2.TestAllTypesProto2()
188
+ proto2_msg.optional_string = "\xff"
189
+ local serialized = upb.encode(proto2_msg)
190
+
191
+ -- Decoding invalid UTF-8 succeeds in proto2.
192
+ upb.decode(test_messages_proto2.TestAllTypesProto2, serialized)
193
+
194
+ -- Decoding invalid UTF-8 fails in proto2.
195
+ assert_error_match("Error decoding protobuf", function()
196
+ upb.decode(test_messages_proto3.TestAllTypesProto3, serialized)
197
+ end)
198
+
199
+ -- TODO(haberman): should proto3 accessors also check UTF-8 at set time?
200
+ end
201
+
202
+ function test_string_double_map()
203
+ msg = upb_test.MapTest()
204
+ msg.map_string_double["one"] = 1.0
205
+ msg.map_string_double["two point five"] = 2.5
206
+ assert_equal(1, msg.map_string_double["one"])
207
+ assert_equal(2.5, msg.map_string_double["two point five"])
208
+
209
+ -- Test overwrite.
210
+ msg.map_string_double["one"] = 2
211
+ assert_equal(2, msg.map_string_double["one"])
212
+ assert_equal(2.5, msg.map_string_double["two point five"])
213
+ msg.map_string_double["one"] = 1.0
214
+
215
+ -- Test delete.
216
+ msg.map_string_double["one"] = nil
217
+ assert_nil(msg.map_string_double["one"])
218
+ assert_equal(2.5, msg.map_string_double["two point five"])
219
+ msg.map_string_double["one"] = 1
220
+
221
+ local serialized = upb.encode(msg)
222
+ assert_true(#serialized > 0)
223
+ local msg2 = upb.decode(upb_test.MapTest, serialized)
224
+ assert_equal(1, msg2.map_string_double["one"])
225
+ assert_equal(2.5, msg2.map_string_double["two point five"])
226
+ end
227
+
228
+ function test_string_double_map()
229
+ local function fill_msg(msg)
230
+ msg.i32_packed[1] = 100
231
+ msg.i32_packed[2] = 200
232
+ msg.i32_packed[3] = 50000
233
+
234
+ msg.i64_packed[1] = 101
235
+ msg.i64_packed[2] = 201
236
+ msg.i64_packed[3] = 50001
237
+
238
+ msg.f32_packed[1] = 102
239
+ msg.f32_packed[2] = 202
240
+ msg.f32_packed[3] = 50002
241
+
242
+ msg.f64_packed[1] = 103
243
+ msg.f64_packed[2] = 203
244
+ msg.f64_packed[3] = 50003
245
+ end
246
+
247
+ local function check_msg(msg)
248
+ assert_equal(100, msg.i32_packed[1])
249
+ assert_equal(200, msg.i32_packed[2])
250
+ assert_equal(50000, msg.i32_packed[3])
251
+ assert_equal(3, #msg.i32_packed)
252
+
253
+ assert_equal(101, msg.i64_packed[1])
254
+ assert_equal(201, msg.i64_packed[2])
255
+ assert_equal(50001, msg.i64_packed[3])
256
+ assert_equal(3, #msg.i64_packed)
257
+
258
+ assert_equal(102, msg.f32_packed[1])
259
+ assert_equal(202, msg.f32_packed[2])
260
+ assert_equal(50002, msg.f32_packed[3])
261
+ assert_equal(3, #msg.f32_packed)
262
+
263
+ assert_equal(103, msg.f64_packed[1])
264
+ assert_equal(203, msg.f64_packed[2])
265
+ assert_equal(50003, msg.f64_packed[3])
266
+ assert_equal(3, #msg.f64_packed)
267
+ end
268
+
269
+ local msg = upb_test.PackedTest()
270
+ fill_msg(msg)
271
+ check_msg(msg)
272
+
273
+ local serialized_packed = upb.encode(msg)
274
+ local msg2 = upb.decode(upb_test.PackedTest, serialized_packed)
275
+ local msg3 = upb.decode(upb_test.UnpackedTest, serialized_packed)
276
+ check_msg(msg2)
277
+ check_msg(msg3)
278
+
279
+ serialized_unpacked = upb.encode(msg3)
280
+ local msg4 = upb.decode(upb_test.PackedTest, serialized_unpacked)
281
+ local msg5 = upb.decode(upb_test.PackedTest, serialized_unpacked)
282
+ check_msg(msg4)
283
+ check_msg(msg5)
284
+
285
+ end
286
+
287
+ function test_msg_string_map()
288
+ msg = test_messages_proto3.TestAllTypesProto3()
289
+ msg.map_string_string["foo"] = "bar"
290
+ msg.map_string_string["baz"] = "quux"
291
+ assert_nil(msg.map_string_string["abc"])
292
+ assert_equal("bar", msg.map_string_string["foo"])
293
+ assert_equal("quux", msg.map_string_string["baz"])
294
+
295
+ -- Test overwrite.
296
+ msg.map_string_string["foo"] = "123"
297
+ assert_equal("123", msg.map_string_string["foo"])
298
+ assert_equal("quux", msg.map_string_string["baz"])
299
+ msg.map_string_string["foo"] = "bar"
300
+
301
+ -- Test delete
302
+ msg.map_string_string["foo"] = nil
303
+ assert_nil(msg.map_string_string["foo"])
304
+ assert_equal("quux", msg.map_string_string["baz"])
305
+ msg.map_string_string["foo"] = "bar"
306
+
307
+ local serialized = upb.encode(msg)
308
+ assert_true(#serialized > 0)
309
+ local msg2 = upb.decode(test_messages_proto3.TestAllTypesProto3, serialized)
310
+ assert_equal("bar", msg2.map_string_string["foo"])
311
+ assert_equal("quux", msg2.map_string_string["baz"])
312
+ end
313
+
314
+ function test_msg_array()
315
+ msg = test_messages_proto3.TestAllTypesProto3()
316
+
317
+ assert_not_nil(msg.repeated_int32)
318
+ assert_equal(msg.repeated_int32, msg.repeated_int32)
319
+ assert_equal(0, #msg.repeated_int32)
320
+
321
+ msg.repeated_int32[1] = 2
322
+ assert_equal(1, #msg.repeated_int32);
323
+ assert_equal(2, msg.repeated_int32[1]);
324
+
325
+ -- Can't assign a scalar; array is expected.
326
+ assert_error_match("lupb.array expected", function() msg.repeated_int32 = 5 end)
327
+
328
+ -- Can't assign array of the wrong type.
329
+ local function assign_int64()
330
+ msg.repeated_int32 = upb.Array(upb.TYPE_INT64)
331
+ end
332
+ assert_error_match("array type mismatch", assign_int64)
333
+
334
+ local arr = upb.Array(upb.TYPE_INT32)
335
+ arr[1] = 6
336
+ assert_equal(1, #arr)
337
+ msg.repeated_int32 = arr
338
+ assert_equal(msg.repeated_int32, msg.repeated_int32)
339
+ assert_equal(arr, msg.repeated_int32)
340
+ assert_equal(1, #msg.repeated_int32)
341
+ assert_equal(6, msg.repeated_int32[1])
342
+
343
+ -- Can't assign other Lua types.
344
+ assert_error_match("array expected", function() msg.repeated_int32 = "abc" end)
345
+ assert_error_match("array expected", function() msg.repeated_int32 = true end)
346
+ assert_error_match("array expected", function() msg.repeated_int32 = false end)
347
+ assert_error_match("array expected", function() msg.repeated_int32 = nil end)
348
+ assert_error_match("array expected", function() msg.repeated_int32 = {} end)
349
+ assert_error_match("array expected", function() msg.repeated_int32 = print end)
350
+ end
351
+
352
+ function test_array_append()
353
+ local arr = upb.Array(upb.TYPE_INT32)
354
+ for i=1,200000 do
355
+ arr[i] = i
356
+ end
357
+ for i=1,200000 do
358
+ assert_equal(i, arr[i])
359
+ end
360
+ end
361
+
362
+ function test_msg_submsg()
363
+ --msg = test_messages_proto3.TestAllTypesProto3()
364
+ msg = test_messages_proto3['TestAllTypesProto3']()
365
+
366
+ assert_nil(msg.optional_nested_message)
367
+
368
+ -- Can't assign message of the wrong type.
369
+ local function assign_int64()
370
+ msg.optional_nested_message = test_messages_proto3.TestAllTypesProto3()
371
+ end
372
+ assert_error_match("message type mismatch", assign_int64)
373
+
374
+ local nested = test_messages_proto3['TestAllTypesProto3.NestedMessage']()
375
+ msg.optional_nested_message = nested
376
+ assert_equal(nested, msg.optional_nested_message)
377
+
378
+ -- Can't assign other Lua types.
379
+ assert_error_match("msg expected", function() msg.optional_nested_message = "abc" end)
380
+ assert_error_match("msg expected", function() msg.optional_nested_message = true end)
381
+ assert_error_match("msg expected", function() msg.optional_nested_message = false end)
382
+ assert_error_match("msg expected", function() msg.optional_nested_message = nil end)
383
+ assert_error_match("msg expected", function() msg.optional_nested_message = {} end)
384
+ assert_error_match("msg expected", function() msg.optional_nested_message = print end)
385
+ end
386
+
387
+ -- Lua 5.1 and 5.2 have slightly different semantics for how a finalizer
388
+ -- can be defined in Lua.
389
+ if _VERSION >= 'Lua 5.2' then
390
+ function defer(fn)
391
+ setmetatable({}, { __gc = fn })
392
+ end
393
+ else
394
+ function defer(fn)
395
+ getmetatable(newproxy(true)).__gc = fn
396
+ end
397
+ end
398
+
399
+ function test_finalizer()
400
+ -- Tests that we correctly handle a call into an already-finalized object.
401
+ -- Collectible objects are finalized in the opposite order of creation.
402
+ do
403
+ local t = {}
404
+ defer(function()
405
+ assert_error_match("called into dead object", function()
406
+ -- Generic def call.
407
+ t[1]:lookup_msg("abc")
408
+ end)
409
+ end)
410
+ t = {
411
+ upb.SymbolTable(),
412
+ }
413
+ end
414
+ collectgarbage()
415
+ end
416
+
417
+ -- in-range of 64-bit types but not exactly representable as double
418
+ local bad64 = 2^68 - 1
419
+
420
+ local numeric_types = {
421
+ [upb.TYPE_UINT32] = {
422
+ valid_val = 2^32 - 1,
423
+ too_big = 2^32,
424
+ too_small = -1,
425
+ other_bad = 5.1
426
+ },
427
+ [upb.TYPE_UINT64] = {
428
+ valid_val = 2^63,
429
+ too_big = 2^64,
430
+ too_small = -1,
431
+ other_bad = bad64
432
+ },
433
+ [upb.TYPE_INT32] = {
434
+ valid_val = 2^31 - 1,
435
+ too_big = 2^31,
436
+ too_small = -2^31 - 1,
437
+ other_bad = 5.1
438
+ },
439
+ -- Enums don't exist at a language level in Lua, so we just represent enum
440
+ -- values as int32s.
441
+ [upb.TYPE_ENUM] = {
442
+ valid_val = 2^31 - 1,
443
+ too_big = 2^31,
444
+ too_small = -2^31 - 1,
445
+ other_bad = 5.1
446
+ },
447
+ [upb.TYPE_INT64] = {
448
+ valid_val = 2^62,
449
+ too_big = 2^63,
450
+ too_small = -2^64,
451
+ other_bad = bad64
452
+ },
453
+ [upb.TYPE_FLOAT] = {
454
+ valid_val = 340282306073709652508363335590014353408
455
+ },
456
+ [upb.TYPE_DOUBLE] = {
457
+ valid_val = 10^101
458
+ },
459
+ }
460
+
461
+ function test_utf8()
462
+ local invalid_utf8 = "\xff"
463
+ local proto2_msg = test_messages_proto2.TestAllTypesProto2{
464
+ optional_string = invalid_utf8,
465
+ }
466
+
467
+ -- As proto2, invalid UTF-8 parses and serializes fine.
468
+ local serialized = upb.encode(proto2_msg)
469
+ local proto2_msg2 = upb.decode(test_messages_proto2.TestAllTypesProto2, serialized)
470
+
471
+ -- Decoding as proto3 fails.
472
+ assert_error(function()
473
+ upb.decode(test_messages_proto3.TestAllTypesProto3, serialized)
474
+ end)
475
+ end
476
+
477
+ function test_msg_primitives()
478
+ local msg = test_messages_proto3.TestAllTypesProto3{
479
+ optional_int32 = 10,
480
+ optional_uint32 = 20,
481
+ optional_int64 = 30,
482
+ optional_uint64 = 40,
483
+ optional_double = 50,
484
+ optional_float = 60,
485
+ optional_sint32 = 70,
486
+ optional_sint64 = 80,
487
+ optional_fixed32 = 90,
488
+ optional_fixed64 = 100,
489
+ optional_sfixed32 = 110,
490
+ optional_sfixed64 = 120,
491
+ optional_bool = true,
492
+ optional_string = "abc",
493
+ optional_nested_message = test_messages_proto3['TestAllTypesProto3.NestedMessage']{a = 123},
494
+ }
495
+
496
+ -- Attempts to access non-existent fields fail.
497
+ assert_error_match("no such field", function() msg.no_such = 1 end)
498
+
499
+ assert_equal(10, msg.optional_int32)
500
+ assert_equal(20, msg.optional_uint32)
501
+ assert_equal(30, msg.optional_int64)
502
+ assert_equal(40, msg.optional_uint64)
503
+ assert_equal(50, msg.optional_double)
504
+ assert_equal(60, msg.optional_float)
505
+ assert_equal(70, msg.optional_sint32)
506
+ assert_equal(80, msg.optional_sint64)
507
+ assert_equal(90, msg.optional_fixed32)
508
+ assert_equal(100, msg.optional_fixed64)
509
+ assert_equal(110, msg.optional_sfixed32)
510
+ assert_equal(120, msg.optional_sfixed64)
511
+ assert_equal(true, msg.optional_bool)
512
+ assert_equal("abc", msg.optional_string)
513
+ assert_equal(123, msg.optional_nested_message.a)
514
+ end
515
+
516
+
517
+ function test_string_array()
518
+ local function test_for_string_type(upb_type)
519
+ local array = upb.Array(upb_type)
520
+ assert_equal(0, #array)
521
+
522
+ -- 0 is never a valid index in Lua.
523
+ assert_error_match("array index", function() return array[0] end)
524
+ -- Past the end of the array.
525
+ assert_error_match("array index", function() return array[1] end)
526
+
527
+ array[1] = "foo"
528
+ assert_equal("foo", array[1])
529
+ assert_equal(1, #array)
530
+ -- Past the end of the array.
531
+ assert_error_match("array index", function() return array[2] end)
532
+
533
+ local array2 = upb.Array(upb_type)
534
+ assert_equal(0, #array2)
535
+
536
+ array[2] = "bar"
537
+ assert_equal("foo", array[1])
538
+ assert_equal("bar", array[2])
539
+ assert_equal(2, #array)
540
+ -- Past the end of the array.
541
+ assert_error_match("array index", function() return array[3] end)
542
+
543
+ -- Can't assign other Lua types.
544
+ assert_error_match("Expected string", function() array[3] = 123 end)
545
+ assert_error_match("Expected string", function() array[3] = true end)
546
+ assert_error_match("Expected string", function() array[3] = false end)
547
+ assert_error_match("Expected string", function() array[3] = nil end)
548
+ assert_error_match("Expected string", function() array[3] = {} end)
549
+ assert_error_match("Expected string", function() array[3] = print end)
550
+ assert_error_match("Expected string", function() array[3] = array end)
551
+ end
552
+
553
+ test_for_string_type(upb.TYPE_STRING)
554
+ test_for_string_type(upb.TYPE_BYTES)
555
+ end
556
+
557
+ function test_numeric_array()
558
+ local function test_for_numeric_type(upb_type)
559
+ local array = upb.Array(upb_type)
560
+ local vals = numeric_types[upb_type]
561
+ assert_equal(0, #array)
562
+
563
+ -- 0 is never a valid index in Lua.
564
+ assert_error_match("array index", function() return array[0] end)
565
+ -- Past the end of the array.
566
+ assert_error_match("array index", function() return array[1] end)
567
+
568
+ array[1] = vals.valid_val
569
+ assert_equal(vals.valid_val, array[1])
570
+ assert_equal(1, #array)
571
+ assert_equal(vals.valid_val, array[1])
572
+ -- Past the end of the array.
573
+ assert_error_match("array index", function() return array[2] end)
574
+
575
+ array[2] = 10
576
+ assert_equal(vals.valid_val, array[1])
577
+ assert_equal(10, array[2])
578
+ assert_equal(2, #array)
579
+ -- Past the end of the array.
580
+ assert_error_match("array index", function() return array[3] end)
581
+
582
+ -- Values that are out of range.
583
+ local errmsg = "not an integer or out of range"
584
+ if vals.too_small then
585
+ assert_error_match(errmsg, function() array[3] = vals.too_small end)
586
+ end
587
+ if vals.too_big then
588
+ assert_error_match(errmsg, function() array[3] = vals.too_big end)
589
+ end
590
+ if vals.other_bad then
591
+ assert_error_match(errmsg, function() array[3] = vals.other_bad end)
592
+ end
593
+
594
+ -- Can't assign other Lua types.
595
+ errmsg = "bad argument #3"
596
+ assert_error_match(errmsg, function() array[3] = "abc" end)
597
+ assert_error_match(errmsg, function() array[3] = true end)
598
+ assert_error_match(errmsg, function() array[3] = false end)
599
+ assert_error_match(errmsg, function() array[3] = nil end)
600
+ assert_error_match(errmsg, function() array[3] = {} end)
601
+ assert_error_match(errmsg, function() array[3] = print end)
602
+ assert_error_match(errmsg, function() array[3] = array end)
603
+ end
604
+
605
+ for k in pairs(numeric_types) do
606
+ test_for_numeric_type(k)
607
+ end
608
+ end
609
+
610
+ function test_numeric_map()
611
+ local function test_for_numeric_types(key_type, val_type)
612
+ local map = upb.Map(key_type, val_type)
613
+ local key_vals = numeric_types[key_type]
614
+ local val_vals = numeric_types[val_type]
615
+
616
+ assert_equal(0, #map)
617
+
618
+ -- Unset keys return nil
619
+ assert_nil(map[key_vals.valid_val])
620
+
621
+ map[key_vals.valid_val] = val_vals.valid_val
622
+ assert_equal(1, #map)
623
+ assert_equal(val_vals.valid_val, map[key_vals.valid_val])
624
+
625
+ i = 0
626
+ for k, v in pairs(map) do
627
+ assert_equal(key_vals.valid_val, k)
628
+ assert_equal(val_vals.valid_val, v)
629
+ end
630
+
631
+ -- Out of range key/val
632
+ local errmsg = "not an integer or out of range"
633
+ if key_vals.too_small then
634
+ assert_error_match(errmsg, function() map[key_vals.too_small] = 1 end)
635
+ end
636
+ if key_vals.too_big then
637
+ assert_error_match(errmsg, function() map[key_vals.too_big] = 1 end)
638
+ end
639
+ if key_vals.other_bad then
640
+ assert_error_match(errmsg, function() map[key_vals.other_bad] = 1 end)
641
+ end
642
+
643
+ if val_vals.too_small then
644
+ assert_error_match(errmsg, function() map[1] = val_vals.too_small end)
645
+ end
646
+ if val_vals.too_big then
647
+ assert_error_match(errmsg, function() map[1] = val_vals.too_big end)
648
+ end
649
+ if val_vals.other_bad then
650
+ assert_error_match(errmsg, function() map[1] = val_vals.other_bad end)
651
+ end
652
+ end
653
+
654
+ for k in pairs(numeric_types) do
655
+ for v in pairs(numeric_types) do
656
+ test_for_numeric_types(k, v)
657
+ end
658
+ end
659
+ end
660
+
661
+ function test_unknown()
662
+ local bytes = string.rep("\x38\x00", 1000)
663
+ for i=1,1000 do
664
+ local msg = upb.decode(test_messages_proto3.TestAllTypesProto3, bytes)
665
+ end
666
+ end
667
+
668
+ function test_foo()
669
+ local symtab = upb.SymbolTable()
670
+ local filename = "external/com_google_protobuf/descriptor_proto-descriptor-set.proto.bin"
671
+ local file = io.open(filename, "rb") or io.open("bazel-bin/" .. filename, "rb")
672
+ assert_not_nil(file)
673
+ local descriptor = file:read("*a")
674
+ assert_true(#descriptor > 0)
675
+ symtab:add_set(descriptor)
676
+ local FileDescriptorSet = symtab:lookup_msg("google.protobuf.FileDescriptorSet")
677
+ assert_not_nil(FileDescriptorSet)
678
+ set = FileDescriptorSet()
679
+ assert_equal(#set.file, 0)
680
+ assert_error_match("lupb.array expected", function () set.file = 1 end)
681
+
682
+ set = upb.decode(FileDescriptorSet, descriptor)
683
+
684
+ -- Test that we can at least call this without crashing.
685
+ set_textformat = tostring(set)
686
+
687
+ -- print(set_textformat)
688
+ assert_equal(#set.file, 1)
689
+ assert_equal(set.file[1].name, "google/protobuf/descriptor.proto")
690
+ end
691
+
692
+ function test_descriptor()
693
+ local symtab = upb.SymbolTable()
694
+ local file_proto = descriptor.FileDescriptorProto {
695
+ name = "test.proto",
696
+ message_type = upb.Array(descriptor.DescriptorProto, {
697
+ descriptor.DescriptorProto{
698
+ name = "ABC",
699
+ },
700
+ })
701
+ }
702
+ local file = symtab:add_file(upb.encode(file_proto))
703
+ assert_equal(file:symtab(), symtab)
704
+ end
705
+
706
+ function test_descriptor_error()
707
+ local symtab = upb.SymbolTable()
708
+ local file = descriptor.FileDescriptorProto()
709
+ file.name = "test.proto"
710
+ file.message_type[1] = descriptor.DescriptorProto{
711
+ name = "ABC"
712
+ }
713
+ file.message_type[2] = descriptor.DescriptorProto{
714
+ name = "BC."
715
+ }
716
+ assert_error(function () symtab:add_file(upb.encode(file)) end)
717
+ assert_nil(symtab:lookup_msg("ABC"))
718
+ end
719
+
720
+ function test_duplicate_enumval()
721
+ local symtab = upb.SymbolTable()
722
+ local file_proto = descriptor.FileDescriptorProto {
723
+ name = "test.proto",
724
+ message_type = upb.Array(descriptor.DescriptorProto, {
725
+ descriptor.DescriptorProto{
726
+ name = "ABC",
727
+ },
728
+ }),
729
+ enum_type = upb.Array(descriptor.EnumDescriptorProto, {
730
+ descriptor.EnumDescriptorProto{
731
+ name = "MyEnum",
732
+ value = upb.Array(descriptor.EnumValueDescriptorProto, {
733
+ descriptor.EnumValueDescriptorProto{
734
+ name = "ABC",
735
+ number = 1,
736
+ }
737
+ }),
738
+ },
739
+ })
740
+ }
741
+ assert_error(function () symtab:add_file(upb.encode(file_proto)) end)
742
+ end
743
+
744
+ function test_duplicate_filename_error()
745
+ local symtab = upb.SymbolTable()
746
+ local file = descriptor.FileDescriptorProto()
747
+ file.name = "test.proto"
748
+ symtab:add_file(upb.encode(file))
749
+ -- Second add with the same filename fails.
750
+ assert_error(function () symtab:add_file(upb.encode(file)) end)
751
+ end
752
+
753
+ function test_encode_skipunknown()
754
+ -- Test that upb.ENCODE_SKIPUNKNOWN does not encode unknown fields.
755
+ local msg = test_messages_proto3.TestAllTypesProto3{
756
+ optional_int32 = 10,
757
+ optional_uint32 = 20,
758
+ optional_int64 = 30,
759
+ }
760
+ -- SKIPUNKNOWN here tests that it does *not* affect regular fields.
761
+ local serialized = upb.encode(msg, {upb.ENCODE_SKIPUNKNOWN})
762
+ assert_true(#serialized > 0)
763
+ local empty_with_unknown = upb.decode(empty.Empty, serialized)
764
+ assert_true(#upb.encode(empty_with_unknown) > 0)
765
+ -- Verify that unknown fields are not serialized.
766
+ assert_true(#upb.encode(empty_with_unknown, {upb.ENCODE_SKIPUNKNOWN}) == 0)
767
+ end
768
+
769
+ function test_json_emit_defaults()
770
+ local msg = test_messages_proto3.TestAllTypesProto3()
771
+ local json = upb.json_encode(msg, {upb.JSONENC_EMITDEFAULTS})
772
+ end
773
+
774
+ function test_json_locale()
775
+ local msg = test_messages_proto3.TestAllTypesProto3()
776
+ msg.optional_double = 1.1
777
+ local original_locale = os.setlocale(nil)
778
+ os.setlocale("C")
779
+ local json = upb.json_encode(msg)
780
+ os.setlocale("de_DE.utf8")
781
+ assert_equal(json, upb.json_encode(msg))
782
+ os.setlocale(original_locale) -- Restore.
783
+ end
784
+
785
+ function test_encode_depth_limit()
786
+ local msg = test_messages_proto3.TestAllTypesProto3()
787
+ msg.recursive_message = msg
788
+ assert_error(function() upb.encode(msg) end)
789
+ end
790
+
791
+ function test_large_field_number()
792
+ local msg = upb_test.TestLargeFieldNumber()
793
+ msg.i32 = 5
794
+ local serialized = upb.encode(msg)
795
+ local msg2 = upb.decode(upb_test.TestLargeFieldNumber, serialized)
796
+ assert_equal(msg.i32, msg2.i32)
797
+ end
798
+
799
+ function test_timestamp_minutes()
800
+ local msg = upb.json_decode(upb_test.TestTimestamp, '{"ts": "2000-01-01T00:00:00-06:59"}')
801
+ assert_equal(msg.ts.seconds, 946684800 + ((6 * 60) + 59) * 60)
802
+ end
803
+
804
+ function test_gc()
805
+ local top = test_messages_proto3.TestAllTypesProto3()
806
+ local n = 100
807
+ local m
808
+
809
+ for i=1,n do
810
+ local inner = test_messages_proto3.TestAllTypesProto3()
811
+ m = inner
812
+ for j=1,n do
813
+ local tmp = m
814
+ m = test_messages_proto3.TestAllTypesProto3()
815
+ -- This will cause the arenas to fuse. But we stop referring to the child,
816
+ -- so the Lua object is eligible for collection (and therefore its original
817
+ -- arena can be collected too). Only the fusing will keep the C mem alivd.
818
+ m.recursive_message = tmp
819
+
820
+ end
821
+ top.recursive_message = m
822
+ end
823
+
824
+ collectgarbage()
825
+
826
+ for i=1,n do
827
+ -- Verify we can touch all the messages again and without accessing freed
828
+ -- memory.
829
+ m = m.recursive_message
830
+ assert_not_nil(m)
831
+ end
832
+ end
833
+
834
+ function test_b9440()
835
+ local m = upb_test.HelloRequest()
836
+ m.id = 8
837
+ assert_equal(8, m.id)
838
+ m.version = "1"
839
+ assert_equal(8, m.id)
840
+ end
841
+
842
+ local stats = lunit.main()
843
+
844
+ if stats.failed > 0 or stats.errors > 0 then
845
+ error("One or more errors in test suite")
846
+ end