test_plugin.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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": "Attempt to invoke virtual method 'org.a.b.g$a.a' on a null object reference",
  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 (
  121. exc.value
  122. == "Attempt to invoke virtual method 'org.slf4j.helpers.Util$ClassContextSecurityManager.getExtraClassContext' on a null object reference"
  123. )
  124. assert exc.module == "org.slf4j.helpers"
  125. assert frames[0].function == "getClassContext"
  126. assert frames[0].module == "org.slf4j.helpers.Util$ClassContextSecurityManager"
  127. assert frames[1].function == "getExtraClassContext"
  128. assert frames[1].module == "org.slf4j.helpers.Util$ClassContextSecurityManager"
  129. assert event.culprit == (
  130. "org.slf4j.helpers.Util$ClassContextSecurityManager " "in getExtraClassContext"
  131. )
  132. def test_resolving_inline(self):
  133. url = reverse(
  134. "sentry-api-0-dsym-files",
  135. kwargs={
  136. "organization_slug": self.project.organization.slug,
  137. "project_slug": self.project.slug,
  138. },
  139. )
  140. self.login_as(user=self.user)
  141. out = BytesIO()
  142. f = zipfile.ZipFile(out, "w")
  143. f.writestr("proguard/%s.txt" % PROGUARD_INLINE_UUID, PROGUARD_INLINE_SOURCE)
  144. f.writestr("ignored-file.txt", b"This is just some stuff")
  145. f.close()
  146. response = self.client.post(
  147. url,
  148. {
  149. "file": SimpleUploadedFile(
  150. "symbols.zip", out.getvalue(), content_type="application/zip"
  151. )
  152. },
  153. format="multipart",
  154. )
  155. assert response.status_code == 201, response.content
  156. assert len(response.data) == 1
  157. event_data = {
  158. "user": {"ip_address": "31.172.207.97"},
  159. "extra": {},
  160. "project": self.project.id,
  161. "platform": "java",
  162. "debug_meta": {"images": [{"type": "proguard", "uuid": PROGUARD_INLINE_UUID}]},
  163. "exception": {
  164. "values": [
  165. {
  166. "stacktrace": {
  167. "frames": [
  168. {
  169. "function": "onClick",
  170. "abs_path": None,
  171. "module": "e.a.c.a",
  172. "filename": None,
  173. "lineno": 2,
  174. },
  175. {
  176. "function": "t",
  177. "abs_path": None,
  178. "module": "io.sentry.sample.MainActivity",
  179. "filename": "MainActivity.java",
  180. "lineno": 1,
  181. },
  182. ]
  183. },
  184. "module": "org.a.b",
  185. "type": "g$a",
  186. "value": "Oh no",
  187. }
  188. ]
  189. },
  190. "timestamp": iso_format(before_now(seconds=1)),
  191. }
  192. event = self.post_and_retrieve_event(event_data)
  193. if not self.use_relay():
  194. # We measure the number of queries after an initial post,
  195. # because there are many queries polluting the array
  196. # before the actual "processing" happens (like, auth_user)
  197. with self.assertWriteQueries(
  198. {
  199. "nodestore_node": 2,
  200. "sentry_eventuser": 1,
  201. "sentry_groupedmessage": 1,
  202. "sentry_userreport": 1,
  203. }
  204. ):
  205. self.post_and_retrieve_event(event_data)
  206. exc = event.interfaces["exception"].values[0]
  207. bt = exc.stacktrace
  208. frames = bt.frames
  209. assert len(frames) == 4
  210. assert frames[0].function == "onClick"
  211. assert frames[0].module == "io.sentry.sample.-$$Lambda$r3Avcbztes2hicEObh02jjhQqd4"
  212. assert frames[1].filename == "MainActivity.java"
  213. assert frames[1].module == "io.sentry.sample.MainActivity"
  214. assert frames[1].function == "onClickHandler"
  215. assert frames[1].lineno == 40
  216. assert frames[2].function == "foo"
  217. assert frames[2].lineno == 44
  218. assert frames[3].function == "bar"
  219. assert frames[3].lineno == 54
  220. assert frames[3].filename == "MainActivity.java"
  221. assert frames[3].module == "io.sentry.sample.MainActivity"
  222. def test_error_on_resolving(self):
  223. url = reverse(
  224. "sentry-api-0-dsym-files",
  225. kwargs={
  226. "organization_slug": self.project.organization.slug,
  227. "project_slug": self.project.slug,
  228. },
  229. )
  230. self.login_as(user=self.user)
  231. out = BytesIO()
  232. f = zipfile.ZipFile(out, "w")
  233. f.writestr("proguard/%s.txt" % PROGUARD_BUG_UUID, PROGUARD_BUG_SOURCE)
  234. f.close()
  235. response = self.client.post(
  236. url,
  237. {
  238. "file": SimpleUploadedFile(
  239. "symbols.zip", out.getvalue(), content_type="application/zip"
  240. )
  241. },
  242. format="multipart",
  243. )
  244. assert response.status_code == 201, response.content
  245. assert len(response.data) == 1
  246. event_data = {
  247. "user": {"ip_address": "31.172.207.97"},
  248. "extra": {},
  249. "project": self.project.id,
  250. "platform": "java",
  251. "debug_meta": {"images": [{"type": "proguard", "uuid": PROGUARD_BUG_UUID}]},
  252. "exception": {
  253. "values": [
  254. {
  255. "stacktrace": {
  256. "frames": [
  257. {
  258. "function": "a",
  259. "abs_path": None,
  260. "module": "org.a.b.g$a",
  261. "filename": None,
  262. "lineno": 67,
  263. },
  264. {
  265. "function": "a",
  266. "abs_path": None,
  267. "module": "org.a.b.g$a",
  268. "filename": None,
  269. "lineno": 69,
  270. },
  271. ]
  272. },
  273. "type": "RuntimeException",
  274. "value": "Oh no",
  275. }
  276. ]
  277. },
  278. "timestamp": iso_format(before_now(seconds=1)),
  279. }
  280. event = self.post_and_retrieve_event(event_data)
  281. assert len(event.data["errors"]) == 1
  282. assert event.data["errors"][0] == {
  283. "mapping_uuid": "071207ac-b491-4a74-957c-2c94fd9594f2",
  284. "type": "proguard_missing_lineno",
  285. }