test_plugin.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. import zipfile
  2. from io import BytesIO
  3. from django.core.files.uploadedfile import SimpleUploadedFile
  4. from django.urls import reverse
  5. from sentry.testutils import RelayStoreHelper, TransactionTestCase
  6. from sentry.testutils.helpers.datetime import before_now, iso_format
  7. PROGUARD_UUID = "6dc7fdb0-d2fb-4c8e-9d6b-bb1aa98929b1"
  8. PROGUARD_SOURCE = b"""\
  9. org.slf4j.helpers.Util$ClassContextSecurityManager -> org.a.b.g$a:
  10. 65:65:void <init>() -> <init>
  11. 67:67:java.lang.Class[] getClassContext() -> a
  12. 69:69:java.lang.Class[] getExtraClassContext() -> a
  13. 65:65:void <init>(org.slf4j.helpers.Util$1) -> <init>
  14. """
  15. PROGUARD_INLINE_UUID = "d748e578-b3d1-5be5-b0e5-a42e8c9bf8e0"
  16. PROGUARD_INLINE_SOURCE = b"""\
  17. # compiler: R8
  18. # compiler_version: 2.0.74
  19. # min_api: 16
  20. # pg_map_id: 5b46fdc
  21. # common_typos_disable
  22. $r8$backportedMethods$utility$Objects$2$equals -> a:
  23. boolean equals(java.lang.Object,java.lang.Object) -> a
  24. $r8$twr$utility -> b:
  25. void $closeResource(java.lang.Throwable,java.lang.Object) -> a
  26. android.support.v4.app.RemoteActionCompatParcelizer -> android.support.v4.app.RemoteActionCompatParcelizer:
  27. 1:1:void <init>():11:11 -> <init>
  28. io.sentry.sample.-$$Lambda$r3Avcbztes2hicEObh02jjhQqd4 -> e.a.c.a:
  29. io.sentry.sample.MainActivity f$0 -> b
  30. io.sentry.sample.MainActivity -> io.sentry.sample.MainActivity:
  31. 1:1:void <init>():15:15 -> <init>
  32. 1:1:boolean onCreateOptionsMenu(android.view.Menu):60:60 -> onCreateOptionsMenu
  33. 1:1:boolean onOptionsItemSelected(android.view.MenuItem):69:69 -> onOptionsItemSelected
  34. 2:2:boolean onOptionsItemSelected(android.view.MenuItem):76:76 -> onOptionsItemSelected
  35. 1:1:void bar():54:54 -> t
  36. 1:1:void foo():44 -> t
  37. 1:1:void onClickHandler(android.view.View):40 -> t
  38. """
  39. PROGUARD_BUG_UUID = "071207ac-b491-4a74-957c-2c94fd9594f2"
  40. PROGUARD_BUG_SOURCE = b"x"
  41. class BasicResolvingIntegrationTest(RelayStoreHelper, TransactionTestCase):
  42. def test_basic_resolving(self):
  43. url = reverse(
  44. "sentry-api-0-dsym-files",
  45. kwargs={
  46. "organization_slug": self.project.organization.slug,
  47. "project_slug": self.project.slug,
  48. },
  49. )
  50. self.login_as(user=self.user)
  51. out = BytesIO()
  52. f = zipfile.ZipFile(out, "w")
  53. f.writestr("proguard/%s.txt" % PROGUARD_UUID, PROGUARD_SOURCE)
  54. f.writestr("ignored-file.txt", b"This is just some stuff")
  55. f.close()
  56. response = self.client.post(
  57. url,
  58. {
  59. "file": SimpleUploadedFile(
  60. "symbols.zip", out.getvalue(), content_type="application/zip"
  61. )
  62. },
  63. format="multipart",
  64. )
  65. assert response.status_code == 201, response.content
  66. assert len(response.data) == 1
  67. event_data = {
  68. "user": {"ip_address": "31.172.207.97"},
  69. "extra": {},
  70. "project": self.project.id,
  71. "platform": "java",
  72. "debug_meta": {"images": [{"type": "proguard", "uuid": PROGUARD_UUID}]},
  73. "exception": {
  74. "values": [
  75. {
  76. "stacktrace": {
  77. "frames": [
  78. {
  79. "function": "a",
  80. "abs_path": None,
  81. "module": "org.a.b.g$a",
  82. "filename": None,
  83. "lineno": 67,
  84. },
  85. {
  86. "function": "a",
  87. "abs_path": None,
  88. "module": "org.a.b.g$a",
  89. "filename": None,
  90. "lineno": 69,
  91. },
  92. ]
  93. },
  94. "module": "org.a.b",
  95. "type": "g$a",
  96. "value": "Oh no",
  97. }
  98. ]
  99. },
  100. "timestamp": iso_format(before_now(seconds=1)),
  101. }
  102. event = self.post_and_retrieve_event(event_data)
  103. if not self.use_relay():
  104. # We measure the number of queries after an initial post,
  105. # because there are many queries polluting the array
  106. # before the actual "processing" happens (like, auth_user)
  107. with self.assertWriteQueries(
  108. {
  109. "nodestore_node": 2,
  110. "sentry_eventuser": 1,
  111. "sentry_groupedmessage": 1,
  112. "sentry_userreport": 1,
  113. }
  114. ):
  115. self.post_and_retrieve_event(event_data)
  116. exc = event.interfaces["exception"].values[0]
  117. bt = exc.stacktrace
  118. frames = bt.frames
  119. assert exc.type == "Util$ClassContextSecurityManager"
  120. assert exc.module == "org.slf4j.helpers"
  121. assert frames[0].function == "getClassContext"
  122. assert frames[0].module == "org.slf4j.helpers.Util$ClassContextSecurityManager"
  123. assert frames[1].function == "getExtraClassContext"
  124. assert frames[1].module == "org.slf4j.helpers.Util$ClassContextSecurityManager"
  125. assert event.culprit == (
  126. "org.slf4j.helpers.Util$ClassContextSecurityManager " "in getExtraClassContext"
  127. )
  128. def test_resolving_inline(self):
  129. url = reverse(
  130. "sentry-api-0-dsym-files",
  131. kwargs={
  132. "organization_slug": self.project.organization.slug,
  133. "project_slug": self.project.slug,
  134. },
  135. )
  136. self.login_as(user=self.user)
  137. out = BytesIO()
  138. f = zipfile.ZipFile(out, "w")
  139. f.writestr("proguard/%s.txt" % PROGUARD_INLINE_UUID, PROGUARD_INLINE_SOURCE)
  140. f.writestr("ignored-file.txt", b"This is just some stuff")
  141. f.close()
  142. response = self.client.post(
  143. url,
  144. {
  145. "file": SimpleUploadedFile(
  146. "symbols.zip", out.getvalue(), content_type="application/zip"
  147. )
  148. },
  149. format="multipart",
  150. )
  151. assert response.status_code == 201, response.content
  152. assert len(response.data) == 1
  153. event_data = {
  154. "user": {"ip_address": "31.172.207.97"},
  155. "extra": {},
  156. "project": self.project.id,
  157. "platform": "java",
  158. "debug_meta": {"images": [{"type": "proguard", "uuid": PROGUARD_INLINE_UUID}]},
  159. "exception": {
  160. "values": [
  161. {
  162. "stacktrace": {
  163. "frames": [
  164. {
  165. "function": "onClick",
  166. "abs_path": None,
  167. "module": "e.a.c.a",
  168. "filename": None,
  169. "lineno": 2,
  170. },
  171. {
  172. "function": "t",
  173. "abs_path": None,
  174. "module": "io.sentry.sample.MainActivity",
  175. "filename": "MainActivity.java",
  176. "lineno": 1,
  177. },
  178. ]
  179. },
  180. "module": "org.a.b",
  181. "type": "g$a",
  182. "value": "Oh no",
  183. }
  184. ]
  185. },
  186. "timestamp": iso_format(before_now(seconds=1)),
  187. }
  188. event = self.post_and_retrieve_event(event_data)
  189. if not self.use_relay():
  190. # We measure the number of queries after an initial post,
  191. # because there are many queries polluting the array
  192. # before the actual "processing" happens (like, auth_user)
  193. with self.assertWriteQueries(
  194. {
  195. "nodestore_node": 2,
  196. "sentry_eventuser": 1,
  197. "sentry_groupedmessage": 1,
  198. "sentry_userreport": 1,
  199. }
  200. ):
  201. self.post_and_retrieve_event(event_data)
  202. exc = event.interfaces["exception"].values[0]
  203. bt = exc.stacktrace
  204. frames = bt.frames
  205. assert len(frames) == 4
  206. assert frames[0].function == "onClick"
  207. assert frames[0].module == "io.sentry.sample.-$$Lambda$r3Avcbztes2hicEObh02jjhQqd4"
  208. assert frames[1].filename == "MainActivity.java"
  209. assert frames[1].module == "io.sentry.sample.MainActivity"
  210. assert frames[1].function == "onClickHandler"
  211. assert frames[1].lineno == 40
  212. assert frames[2].function == "foo"
  213. assert frames[2].lineno == 44
  214. assert frames[3].function == "bar"
  215. assert frames[3].lineno == 54
  216. assert frames[3].filename == "MainActivity.java"
  217. assert frames[3].module == "io.sentry.sample.MainActivity"
  218. def test_error_on_resolving(self):
  219. url = reverse(
  220. "sentry-api-0-dsym-files",
  221. kwargs={
  222. "organization_slug": self.project.organization.slug,
  223. "project_slug": self.project.slug,
  224. },
  225. )
  226. self.login_as(user=self.user)
  227. out = BytesIO()
  228. f = zipfile.ZipFile(out, "w")
  229. f.writestr("proguard/%s.txt" % PROGUARD_BUG_UUID, PROGUARD_BUG_SOURCE)
  230. f.close()
  231. response = self.client.post(
  232. url,
  233. {
  234. "file": SimpleUploadedFile(
  235. "symbols.zip", out.getvalue(), content_type="application/zip"
  236. )
  237. },
  238. format="multipart",
  239. )
  240. assert response.status_code == 201, response.content
  241. assert len(response.data) == 1
  242. event_data = {
  243. "user": {"ip_address": "31.172.207.97"},
  244. "extra": {},
  245. "project": self.project.id,
  246. "platform": "java",
  247. "debug_meta": {"images": [{"type": "proguard", "uuid": PROGUARD_BUG_UUID}]},
  248. "exception": {
  249. "values": [
  250. {
  251. "stacktrace": {
  252. "frames": [
  253. {
  254. "function": "a",
  255. "abs_path": None,
  256. "module": "org.a.b.g$a",
  257. "filename": None,
  258. "lineno": 67,
  259. },
  260. {
  261. "function": "a",
  262. "abs_path": None,
  263. "module": "org.a.b.g$a",
  264. "filename": None,
  265. "lineno": 69,
  266. },
  267. ]
  268. },
  269. "type": "RuntimeException",
  270. "value": "Oh no",
  271. }
  272. ]
  273. },
  274. "timestamp": iso_format(before_now(seconds=1)),
  275. }
  276. event = self.post_and_retrieve_event(event_data)
  277. assert len(event.data["errors"]) == 1
  278. assert event.data["errors"][0] == {
  279. "mapping_uuid": "071207ac-b491-4a74-957c-2c94fd9594f2",
  280. "type": "proguard_missing_lineno",
  281. }