summaryrefslogtreecommitdiffstats
path: root/jack-tests/tests/com/android/jack/shrob/ObfuscationWithoutMappingTests.java
blob: 26cba4954ac6ef001dd35f3d5bc4acb5d027640f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * 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.android.jack.shrob;

import com.android.jack.Options;
import com.android.jack.category.KnownBugs;
import com.android.jack.shrob.obfuscation.NameProviderFactory;
import com.android.jack.shrob.proguard.GrammarActions;
import com.android.jack.shrob.spec.Flags;
import com.android.jack.test.comparator.ComparatorMapping;
import com.android.jack.test.helper.SourceToDexComparisonTestHelper;
import com.android.jack.test.toolchain.AbstractTestTools;
import com.android.jack.test.toolchain.DummyToolchain;
import com.android.jack.test.toolchain.JackApiToolchain;

import org.junit.Test;
import org.junit.experimental.categories.Category;

import java.io.File;

import javax.annotation.Nonnull;

public class ObfuscationWithoutMappingTests extends AbstractTest {

  @Override
  protected void runTest(
      @Nonnull String testNumber,
      @Nonnull String flagNumber,
      @Nonnull String mappingNumber)
      throws Exception {

    String testName = "shrob/test" + testNumber;

    String testPackageName = "com.android.jack.shrob.test" + testNumber;
    File testFolder = AbstractTestTools.getTestRootDir(testPackageName);

    JackApiToolchain toolchain = AbstractTestTools.getCandidateToolchain(JackApiToolchain.class);
    Flags flags = new Flags();
    toolchain.setShrobFlags(flags);
    GrammarActions.parse("proguard.flags" + flagNumber, testFolder.getAbsolutePath(), flags);
    File refFolder = new File(testFolder, "refsObfuscationWithoutMapping");

    toolchain.addProperty(NameProviderFactory.NAMEPROVIDER.getName(), "rot13");
    toolchain.addProperty(Options.METHOD_FILTER.getName(), "supported-methods");

    File candidateOutputMapping = AbstractTestTools.createTempFile("mapping", ".txt");
    File refOutputMapping = new File(refFolder, "expected-" + flagNumber + ".txt");
    flags.setOutputMapping(candidateOutputMapping);
    flags.setPrintMapping(true);

    SourceToDexComparisonTestHelper env =
        new SourceToDexComparisonTestHelper(new File(testFolder, "jack"));

    env.setCandidateTestTools(toolchain);
    env.setReferenceTestTools(new DummyToolchain());

    env.runTest(new ComparatorMapping(candidateOutputMapping, refOutputMapping));

    //    // ==============================================================

    /*
     * Comment for reviewers: the following code is an attempt to use jack based toolchain.
     * It works, but requires to manipulate (copy, write...) shrob flags.
     * Here I added a shrob option manually, but ideally to be able to use
     * a Flags like object for CLI based toochains, we must be able to dump
     * the flags as a file (TBD). Plus, if flags have "-include" directives with relative
     * paths, files must be in the same location.
     */

//
//    String testName = "shrob/test" + testNumber;
//    File testFolder = TestTools.getJackTestFolder(testName);
//    File refFolder = new File(testFolder, "refsObfuscationWithoutMapping");
//
//    JackBasedToolchain jackToolchain = AbstractTestTools.getJackBasedToolchainAsCandidate();
//
//    jackToolchain.addProperty(NameProviderFactory.NAMEPROVIDER.getName(), "rot13");
//    jackToolchain.addProperty(Options.METHOD_FILTER.getName(), "supported-methods");
//
//    File candidateOutputMapping = TestTools.createTempFile("mapping", ".txt");
//    File refOutputMapping = new File(refFolder, "expected-" + flagNumber + ".txt");
//
//    jackToolchain.getCompilationResult().proguardMappingFile = candidateOutputMapping;
//
//    // TODO(jmhenaff): having to seems like a no go for JackBasedToolchain (i.e. cli)
//    File candidateFlags = new File(testFolder.getAbsolutePath(), "tmp-proguard.flags" + flagNumber);
//    candidateFlags.deleteOnExit();
//    appendStringToFileCopy(new File(testFolder.getAbsolutePath(), "proguard.flags" + flagNumber),
//        candidateFlags, "-printmapping " + candidateOutputMapping.getAbsolutePath());
//
//    SourceToDexComparisonTestEnv env = new SourceToDexComparisonTestEnv(bootclasspath, classpath,
//        TestTools.getJackTestsWithJackFolder(testName));
//
//    env.setProguardFlags(new ProguardFlags[] {new ProguardFlags(candidateFlags)});
//
//    CompilationResult compilationResult = new CompilationResult();
//    compilationResult.proguardMappingFile = refOutputMapping;
//
//    env.setCandidateTestTools(jackToolchain);
//    env.setReferenceTestTools(new DummyTestTools(compilationResult));
//
//    env.addComparator(env.createMappingComparator());
//    env.compare();

  }

  @Override
  @Test
  @Category(KnownBugs.class)
  public void test33_001() throws Exception {
    super.test33_001();
  }

  @Override
  @Test
  @Category(KnownBugs.class)
  public void test34_001() throws Exception {
    super.test34_001();
  }

  @Override
  @Test
  @Category(KnownBugs.class)
  public void test35_001() throws Exception {
    super.test35_001();
  }

//  private void appendStringToFileCopy(File source, File dest, String appended) throws IOException {
//    BufferedReader reader = null;
//    BufferedWriter writer = null;
//    try {
//      reader = new BufferedReader(new FileReader(source));
//      writer = new BufferedWriter(new FileWriter(dest));
//      String line;
//      while ((line = reader.readLine()) != null) {
//        writer.write(line);
//        writer.write("\n");
//      }
//      writer.write(appended);
//    } finally {
//      try {
//        reader.close();
//      } catch (IOException e) {
//      }
//      try {
//        writer.close();
//      } catch (IOException e) {
//      }
//    }
//  }

}