From 29a244097df9f68633c362f8d22daca54063071a Mon Sep 17 00:00:00 2001 From: YOUNG CHA Date: Mon, 25 Mar 2019 11:37:09 +0900 Subject: [PATCH] Add testcase for obfuscated enum class --- gson/pom.xml | 87 +++++++++++++++++++ .../functional/EnumWithObfuscatedTest.java | 60 +++++++++++++ gson/testcases-proguard.conf | 20 +++++ 3 files changed, 167 insertions(+) create mode 100644 gson/src/test/java/com/google/gson/functional/EnumWithObfuscatedTest.java create mode 100644 gson/testcases-proguard.conf diff --git a/gson/pom.xml b/gson/pom.xml index cf32962ad0..299caeda15 100644 --- a/gson/pom.xml +++ b/gson/pom.xml @@ -16,6 +16,12 @@ junit test + + com.github.wvengen + proguard-maven-plugin + 2.0.14 + test + @@ -69,6 +75,87 @@ + + com.coderplus.maven.plugins + copy-rename-maven-plugin + 1.0 + + + pre-obfuscate-class + process-test-classes + + rename + + + + + ${project.build.directory}/test-classes/com/google/gson/functional/EnumWithObfuscatedTest.class + ${project.build.directory}/test-classes-obfuscated-injar/com/google/gson/functional/EnumWithObfuscatedTest.class + + + ${project.build.directory}/test-classes/com/google/gson/functional/EnumWithObfuscatedTest$Gender.class + ${project.build.directory}/test-classes-obfuscated-injar/com/google/gson/functional/EnumWithObfuscatedTest$Gender.class + + + + + + + + com.github.wvengen + proguard-maven-plugin + + + process-test-classes + proguard + + + + 6.0.2 + true + test-classes-obfuscated-injar + test-classes-obfuscated-outjar + **/*.class + ${basedir}/testcases-proguard.conf + + ${project.build.directory}/classes + ${java.home}/jmods/java.base.jmod + + + + + net.sf.proguard + proguard-base + 6.0.2 + runtime + + + + + maven-resources-plugin + 2.7 + + + post-obfuscate-class + process-test-classes + + copy-resources + + + ${project.build.directory}/test-classes/com/google/gson/functional + + + ${project.build.directory}/test-classes-obfuscated-outjar/classes/classes/com/google/gson/functional + + EnumWithObfuscatedTest.class + EnumWithObfuscatedTest$Gender.class + + + + + + + diff --git a/gson/src/test/java/com/google/gson/functional/EnumWithObfuscatedTest.java b/gson/src/test/java/com/google/gson/functional/EnumWithObfuscatedTest.java new file mode 100644 index 0000000000..a829b07349 --- /dev/null +++ b/gson/src/test/java/com/google/gson/functional/EnumWithObfuscatedTest.java @@ -0,0 +1,60 @@ +/* + * 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() { + for (Gender enumConstant: Gender.class.getEnumConstants()) { + try { + Gender.class.getField(enumConstant.name()); + fail("Enum is not obfuscated"); + } catch (NoSuchFieldException ignore) { + } + } + + assertEquals(Gender.MALE, gson.fromJson("\"MAIL\"", Gender.class)); + assertEquals("\"MAIL\"", gson.toJson(Gender.MALE, Gender.class)); + } +} diff --git a/gson/testcases-proguard.conf b/gson/testcases-proguard.conf new file mode 100644 index 0000000000..d42da0abef --- /dev/null +++ b/gson/testcases-proguard.conf @@ -0,0 +1,20 @@ +# Options from Android Gradle Plugins +# https://android.googlesource.com/platform/tools/base/+/refs/heads/studio-master-dev/build-system/gradle-core/src/main/resources/com/android/build/gradle +-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/* +-optimizationpasses 5 +-allowaccessmodification +-keepattributes *Annotation*,Signature,InnerClasses,EnclosingMethod +-keepclassmembers enum * { + public static **[] values(); + public static ** valueOf(java.lang.String); +} + +-keep enum com.google.gson.functional.EnumWithObfuscatedTest$Gender +-keep class com.google.gson.functional.EnumWithObfuscatedTest { + public void test*(); + protected void setUp(); +} + +-dontwarn com.google.gson.functional.EnumWithObfuscatedTest +-dontwarn junit.framework.TestCase +