Skip to content

Commit

Permalink
Add testcase for obfuscated enum class
Browse files Browse the repository at this point in the history
  • Loading branch information
ganadist committed Apr 7, 2019
1 parent 61f20b6 commit 4c8b3f5
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 0 deletions.
97 changes: 97 additions & 0 deletions gson/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.0.14</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -73,6 +79,97 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.coderplus.maven.plugins</groupId>
<artifactId>copy-rename-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>pre-obfuscate-class</id>
<phase>process-test-classes</phase>
<goals>
<goal>rename</goal>
</goals>
<configuration>
<fileSets>
<fileSet>
<sourceFile>${project.build.directory}/test-classes/com/google/gson/functional/EnumWithObfuscatedTest.class</sourceFile>
<destinationFile>${project.build.directory}/test-classes-proguard-injar/com/google/gson/functional/EnumWithObfuscatedTest.class</destinationFile>
</fileSet>
<fileSet>
<sourceFile>${project.build.directory}/test-classes/com/google/gson/functional/EnumWithObfuscatedTest$Gender.class</sourceFile>
<destinationFile>${project.build.directory}/test-classes-proguard-injar/com/google/gson/functional/EnumWithObfuscatedTest$Gender.class</destinationFile>
</fileSet>
</fileSets>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<executions>
<execution>
<phase>process-test-classes</phase>
<goals><goal>proguard</goal></goals>
</execution>
</executions>
<configuration>
<proguardVersion>6.0.2</proguardVersion>
<injar>test-classes-proguard-injar</injar>
<outjar>test-classes-proguard-outjar</outjar>
<inFilter>**/*.class</inFilter>
<options>
<!-- options from Android Gradle Plugins -->
<option>-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*</option>
<option>-optimizationpasses 1</option>
<option>-allowaccessmodification</option>
<option>-keepattributes *Annotation*,Signature,InnerClasses,EnclosingMethod</option>
<option>-keepclassmembers enum * { public static **[] values(); public static ** valueOf(java.lang.String); }</option>
<option>-keep enum com.google.gson.functional.EnumWithObfuscatedTest$Gender</option>
<option>-keep class com.google.gson.functional.EnumWithObfuscatedTest { <![CDATA[<init>]]>(...); public void test*(); protected void setUp(); }</option>
<option>-dontwarn com.google.gson.functional.EnumWithObfuscatedTest</option>
<option>-dontwarn junit.framework.TestCase</option>
</options>
<libs>
<lib>${project.build.directory}/classes</lib>
<lib>${java.home}/jmods/java.base.jmod</lib>
</libs>
</configuration>
<dependencies>
<dependency>
<groupId>net.sf.proguard</groupId>
<artifactId>proguard-base</artifactId>
<version>6.0.2</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>post-obfuscate-class</id>
<phase>process-test-classes</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/test-classes/com/google/gson/functional</outputDirectory>
<resources>
<resource>
<directory>${project.build.directory}/test-classes-proguard-outjar/classes/classes/com/google/gson/functional</directory>
<includes>
<include>EnumWithObfuscatedTest.class</include>
<include>EnumWithObfuscatedTest$Gender.class</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (C) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.gson.functional;

import java.lang.reflect.Field;

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;

import junit.framework.TestCase;

/**
* Functional tests for enums with Proguard.
*
* @author Young Cha
*/
public class EnumWithObfuscatedTest extends TestCase {
private Gson gson;

@Override
protected void setUp() throws Exception {
super.setUp();
gson = new Gson();
}

public enum Gender {
@SerializedName("MAIL")
MALE,

@SerializedName("FEMAIL")
FEMALE
}

public void testEnumClassWithObfuscated() {
boolean foundNonObfuscatedEnumConstant = false;
for (Gender enumConstant: Gender.class.getEnumConstants()) {
try {
Gender.class.getField(enumConstant.name());
foundNonObfuscatedEnumConstant = true;
} catch (NoSuchFieldException ignore) {
}
}
assertFalse("Enum is not obfuscated", foundNonObfuscatedEnumConstant);

assertEquals(Gender.MALE, gson.fromJson("\"MAIL\"", Gender.class));
assertEquals("\"MAIL\"", gson.toJson(Gender.MALE, Gender.class));
}
}

0 comments on commit 4c8b3f5

Please sign in to comment.