PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/baksmali/src/test/java/org/jf/baksmali/ImplicitReferenceTest.java

https://gitlab.com/cde/debian_android-tools_libsmali-java
Java | 260 lines | 194 code | 36 blank | 30 comment | 0 complexity | 23ec3937e7edc9f0ccbfbf50e390783c MD5 | raw file
  1. /*
  2. * Copyright 2014, Google Inc.
  3. * All rights reserved.
  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. */
  31. package org.jf.baksmali;
  32. import org.antlr.runtime.RecognitionException;
  33. import org.junit.Test;
  34. import java.io.IOException;
  35. public class ImplicitReferenceTest {
  36. @Test
  37. public void testImplicitMethodReferences() throws IOException, RecognitionException {
  38. String source = "" +
  39. ".class public LHelloWorld;\n" +
  40. ".super Ljava/lang/Object;\n" +
  41. ".method public static main([Ljava/lang/String;)V\n" +
  42. " .registers 1\n" +
  43. " invoke-static {p0}, LHelloWorld;->toString()V\n" +
  44. " invoke-static {p0}, LHelloWorld;->V()V\n" +
  45. " invoke-static {p0}, LHelloWorld;->I()V\n" +
  46. " return-void\n" +
  47. ".end method";
  48. String expected = "" +
  49. ".class public LHelloWorld;\n" +
  50. ".super Ljava/lang/Object;\n" +
  51. "# direct methods\n" +
  52. ".method public static main([Ljava/lang/String;)V\n" +
  53. ".registers 1\n" +
  54. "invoke-static {p0}, toString()V\n" +
  55. "invoke-static {p0}, V()V\n" +
  56. "invoke-static {p0}, I()V\n" +
  57. "return-void\n" +
  58. ".end method\n";
  59. BaksmaliOptions options = new BaksmaliOptions();
  60. options.implicitReferences = true;
  61. BaksmaliTestUtils.assertSmaliCompiledEquals(source, expected, options);
  62. }
  63. @Test
  64. public void testExplicitMethodReferences() throws IOException, RecognitionException {
  65. String source = "" +
  66. ".class public LHelloWorld;\n" +
  67. ".super Ljava/lang/Object;\n" +
  68. ".method public static main([Ljava/lang/String;)V\n" +
  69. " .registers 1\n" +
  70. " invoke-static {p0}, LHelloWorld;->toString()V\n" +
  71. " invoke-static {p0}, LHelloWorld;->V()V\n" +
  72. " invoke-static {p0}, LHelloWorld;->I()V\n" +
  73. " return-void\n" +
  74. ".end method";
  75. String expected = "" +
  76. ".class public LHelloWorld;\n" +
  77. ".super Ljava/lang/Object;\n" +
  78. "# direct methods\n" +
  79. ".method public static main([Ljava/lang/String;)V\n" +
  80. " .registers 1\n" +
  81. " invoke-static {p0}, LHelloWorld;->toString()V\n" +
  82. " invoke-static {p0}, LHelloWorld;->V()V\n" +
  83. " invoke-static {p0}, LHelloWorld;->I()V\n" +
  84. " return-void\n" +
  85. ".end method\n";
  86. BaksmaliOptions options = new BaksmaliOptions();
  87. options.implicitReferences = false;
  88. BaksmaliTestUtils.assertSmaliCompiledEquals(source, expected, options);
  89. }
  90. @Test
  91. public void testImplicitMethodLiterals() throws IOException, RecognitionException {
  92. String source = "" +
  93. ".class public LHelloWorld;\n" +
  94. ".super Ljava/lang/Object;\n" +
  95. ".field public static field1:Ljava/lang/reflect/Method; = LHelloWorld;->toString()V\n" +
  96. ".field public static field2:Ljava/lang/reflect/Method; = LHelloWorld;->V()V\n" +
  97. ".field public static field3:Ljava/lang/reflect/Method; = LHelloWorld;->I()V\n" +
  98. ".field public static field4:Ljava/lang/Class; = I";
  99. String expected = "" +
  100. ".class public LHelloWorld;\n" +
  101. ".super Ljava/lang/Object;\n" +
  102. "# static fields\n" +
  103. ".field public static field1:Ljava/lang/reflect/Method; = toString()V\n" +
  104. ".field public static field2:Ljava/lang/reflect/Method; = V()V\n" +
  105. ".field public static field3:Ljava/lang/reflect/Method; = I()V\n" +
  106. ".field public static field4:Ljava/lang/Class; = I\n";
  107. BaksmaliOptions options = new BaksmaliOptions();
  108. options.implicitReferences = true;
  109. BaksmaliTestUtils.assertSmaliCompiledEquals(source, expected, options);
  110. }
  111. @Test
  112. public void testExplicitMethodLiterals() throws IOException, RecognitionException {
  113. String source = "" +
  114. ".class public LHelloWorld;\n" +
  115. ".super Ljava/lang/Object;\n" +
  116. ".field public static field1:Ljava/lang/reflect/Method; = LHelloWorld;->toString()V\n" +
  117. ".field public static field2:Ljava/lang/reflect/Method; = LHelloWorld;->V()V\n" +
  118. ".field public static field3:Ljava/lang/reflect/Method; = LHelloWorld;->I()V\n" +
  119. ".field public static field4:Ljava/lang/Class; = I";
  120. String expected = "" +
  121. ".class public LHelloWorld;\n" +
  122. ".super Ljava/lang/Object;\n" +
  123. "# static fields\n" +
  124. ".field public static field1:Ljava/lang/reflect/Method; = LHelloWorld;->toString()V\n" +
  125. ".field public static field2:Ljava/lang/reflect/Method; = LHelloWorld;->V()V\n" +
  126. ".field public static field3:Ljava/lang/reflect/Method; = LHelloWorld;->I()V\n" +
  127. ".field public static field4:Ljava/lang/Class; = I\n";
  128. BaksmaliOptions options = new BaksmaliOptions();
  129. options.implicitReferences = false;
  130. BaksmaliTestUtils.assertSmaliCompiledEquals(source, expected, options);
  131. }
  132. @Test
  133. public void testImplicitFieldReferences() throws IOException, RecognitionException {
  134. String source = "" +
  135. ".class public LHelloWorld;\n" +
  136. ".super Ljava/lang/Object;\n" +
  137. ".method public static main([Ljava/lang/String;)V\n" +
  138. " .registers 1\n" +
  139. " sget v0, LHelloWorld;->someField:I\n" +
  140. " sget v0, LHelloWorld;->I:I\n" +
  141. " sget v0, LHelloWorld;->V:I\n" +
  142. " return-void\n" +
  143. ".end method";
  144. String expected = "" +
  145. ".class public LHelloWorld;\n" +
  146. ".super Ljava/lang/Object;\n" +
  147. "# direct methods\n" +
  148. ".method public static main([Ljava/lang/String;)V\n" +
  149. " .registers 1\n" +
  150. " sget p0, someField:I\n" +
  151. " sget p0, I:I\n" +
  152. " sget p0, V:I\n" +
  153. " return-void\n" +
  154. ".end method\n";
  155. BaksmaliOptions options = new BaksmaliOptions();
  156. options.implicitReferences = true;
  157. BaksmaliTestUtils.assertSmaliCompiledEquals(source, expected, options);
  158. }
  159. @Test
  160. public void testExplicitFieldReferences() throws IOException, RecognitionException {
  161. String source = "" +
  162. ".class public LHelloWorld;\n" +
  163. ".super Ljava/lang/Object;\n" +
  164. ".method public static main([Ljava/lang/String;)V\n" +
  165. " .registers 1\n" +
  166. " sget v0, LHelloWorld;->someField:I\n" +
  167. " sget v0, LHelloWorld;->I:I\n" +
  168. " sget v0, LHelloWorld;->V:I\n" +
  169. " return-void\n" +
  170. ".end method";
  171. String expected = "" +
  172. ".class public LHelloWorld;\n" +
  173. ".super Ljava/lang/Object;\n" +
  174. "# direct methods\n" +
  175. ".method public static main([Ljava/lang/String;)V\n" +
  176. " .registers 1\n" +
  177. " sget p0, LHelloWorld;->someField:I\n" +
  178. " sget p0, LHelloWorld;->I:I\n" +
  179. " sget p0, LHelloWorld;->V:I\n" +
  180. " return-void\n" +
  181. ".end method\n";
  182. BaksmaliOptions options = new BaksmaliOptions();
  183. options.implicitReferences = false;
  184. BaksmaliTestUtils.assertSmaliCompiledEquals(source, expected, options);
  185. }
  186. @Test
  187. public void testImplicitFieldLiterals() throws IOException, RecognitionException {
  188. String source = "" +
  189. ".class public LHelloWorld;\n" +
  190. ".super Ljava/lang/Object;\n" +
  191. ".field public static field1:Ljava/lang/reflect/Field; = LHelloWorld;->someField:I\n" +
  192. ".field public static field2:Ljava/lang/reflect/Field; = LHelloWorld;->V:I\n" +
  193. ".field public static field3:Ljava/lang/reflect/Field; = LHelloWorld;->I:I";
  194. String expected = "" +
  195. ".class public LHelloWorld;\n" +
  196. ".super Ljava/lang/Object;\n" +
  197. "# static fields\n" +
  198. ".field public static field1:Ljava/lang/reflect/Field; = someField:I\n" +
  199. ".field public static field2:Ljava/lang/reflect/Field; = V:I\n" +
  200. ".field public static field3:Ljava/lang/reflect/Field; = I:I\n";
  201. BaksmaliOptions options = new BaksmaliOptions();
  202. options.implicitReferences = true;
  203. BaksmaliTestUtils.assertSmaliCompiledEquals(source, expected, options);
  204. }
  205. @Test
  206. public void testExplicitFieldLiterals() throws IOException, RecognitionException {
  207. String source = "" +
  208. ".class public LHelloWorld;\n" +
  209. ".super Ljava/lang/Object;\n" +
  210. ".field public static field1:Ljava/lang/reflect/Field; = LHelloWorld;->someField:I\n" +
  211. ".field public static field2:Ljava/lang/reflect/Field; = LHelloWorld;->V:I\n" +
  212. ".field public static field3:Ljava/lang/reflect/Field; = LHelloWorld;->I:I";
  213. String expected = "" +
  214. ".class public LHelloWorld;\n" +
  215. ".super Ljava/lang/Object;\n" +
  216. "# static fields\n" +
  217. ".field public static field1:Ljava/lang/reflect/Field; = LHelloWorld;->someField:I\n" +
  218. ".field public static field2:Ljava/lang/reflect/Field; = LHelloWorld;->V:I\n" +
  219. ".field public static field3:Ljava/lang/reflect/Field; = LHelloWorld;->I:I\n";
  220. BaksmaliOptions options = new BaksmaliOptions();
  221. options.implicitReferences = false;
  222. BaksmaliTestUtils.assertSmaliCompiledEquals(source, expected, options);
  223. }
  224. }