1#  Copyright (C) 2022 The Android Open Source Project
2#
3#  Licensed under the Apache License, Version 2.0 (the "License");
4#  you may not use this file except in compliance with the License.
5#  You may obtain a copy of the License at
6#
7#       http://www.apache.org/licenses/LICENSE-2.0
8#
9#  Unless required by applicable law or agreed to in writing, software
10#  distributed under the License is distributed on an "AS IS" BASIS,
11#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12#  See the License for the specific language governing permissions and
13#  limitations under the License.
14#
15#  Licensed under the Apache License, Version 2.0 (the "License");
16#  you may not use this file except in compliance with the License.
17#  You may obtain a copy of the License at
18#
19#       http://www.apache.org/licenses/LICENSE-2.0
20#
21#  Unless required by applicable law or agreed to in writing, software
22#  distributed under the License is distributed on an "AS IS" BASIS,
23#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24#  See the License for the specific language governing permissions and
25#  limitations under the License.
26
27import gen_jarjar
28import unittest
29
30
31class TestGenJarjar(unittest.TestCase):
32    def test_gen_rules(self):
33        args = gen_jarjar.parse_arguments([
34            "jarjar-rules-generator-testjavalib.jar",
35            "--prefix", "jarjar.prefix",
36            "--output", "test-output-rules.txt",
37            "--apistubs", "framework-connectivity.stubs.module_lib.jar",
38            "--unsupportedapi", ":testdata/test-unsupportedappusage.txt",
39            "--excludes", "testdata/test-jarjar-excludes.txt",
40        ])
41        gen_jarjar.make_jarjar_rules(args)
42
43        with open(args.output) as out:
44            lines = out.readlines()
45
46        self.maxDiff = None
47        self.assertListEqual([
48            'rule android.net.IpSecTransform jarjar.prefix.@0\n',
49            'rule android.net.IpSecTransformTest jarjar.prefix.@0\n',
50            'rule android.net.IpSecTransformTest$* jarjar.prefix.@0\n',
51            'rule test.unsupportedappusage.OtherUnsupportedUsageClass jarjar.prefix.@0\n',
52            'rule test.unsupportedappusage.OtherUnsupportedUsageClassTest jarjar.prefix.@0\n',
53            'rule test.unsupportedappusage.OtherUnsupportedUsageClassTest$* jarjar.prefix.@0\n',
54            'rule test.utils.TestUtilClass jarjar.prefix.@0\n',
55            'rule test.utils.TestUtilClassTest jarjar.prefix.@0\n',
56            'rule test.utils.TestUtilClassTest$* jarjar.prefix.@0\n',
57            'rule test.utils.TestUtilClass$TestInnerClass jarjar.prefix.@0\n',
58            'rule test.utils.TestUtilClass$TestInnerClassTest jarjar.prefix.@0\n',
59            'rule test.utils.TestUtilClass$TestInnerClassTest$* jarjar.prefix.@0\n'
60        ], lines)
61
62    def test_gen_rules_repeated_args(self):
63        args = gen_jarjar.parse_arguments([
64            "jarjar-rules-generator-testjavalib.jar",
65            "--prefix", "jarjar.prefix",
66            "--output", "test-output-rules.txt",
67            "--apistubs", "framework-connectivity.stubs.module_lib.jar",
68            "--apistubs", "framework-connectivity-t.stubs.module_lib.jar",
69            "--unsupportedapi",
70            "testdata/test-unsupportedappusage.txt:testdata/test-other-unsupportedappusage.txt",
71            "--excludes", "testdata/test-jarjar-excludes.txt",
72        ])
73        gen_jarjar.make_jarjar_rules(args)
74
75        with open(args.output) as out:
76            lines = out.readlines()
77
78        self.maxDiff = None
79        self.assertListEqual([
80            'rule test.utils.TestUtilClass jarjar.prefix.@0\n',
81            'rule test.utils.TestUtilClassTest jarjar.prefix.@0\n',
82            'rule test.utils.TestUtilClassTest$* jarjar.prefix.@0\n',
83            'rule test.utils.TestUtilClass$TestInnerClass jarjar.prefix.@0\n',
84            'rule test.utils.TestUtilClass$TestInnerClassTest jarjar.prefix.@0\n',
85            'rule test.utils.TestUtilClass$TestInnerClassTest$* jarjar.prefix.@0\n'], lines)
86
87    def test_gen_rules_repeated_testclass_excluded(self):
88        args = gen_jarjar.parse_arguments([
89            "jarjar-rules-generator-testjavalib.jar",
90            "--prefix", "jarjar.prefix",
91            "--output", "test-output-rules.txt",
92            "--apistubs", "framework-connectivity.stubs.module_lib.jar",
93            "--unsupportedapi", ":testdata/test-unsupportedappusage.txt",
94            "--excludes", "testdata/test-jarjar-excludes-testclass.txt",
95        ])
96        gen_jarjar.make_jarjar_rules(args)
97
98        with open(args.output) as out:
99            lines = out.readlines()
100
101        self.maxDiff = None
102        self.assertListEqual([
103            'rule android.net.IpSecTransform jarjar.prefix.@0\n',
104            'rule test.unsupportedappusage.OtherUnsupportedUsageClass jarjar.prefix.@0\n',
105            'rule test.unsupportedappusage.OtherUnsupportedUsageClassTest jarjar.prefix.@0\n',
106            'rule test.unsupportedappusage.OtherUnsupportedUsageClassTest$* jarjar.prefix.@0\n',
107            'rule test.utils.TestUtilClass jarjar.prefix.@0\n',
108            'rule test.utils.TestUtilClass$TestInnerClass jarjar.prefix.@0\n',
109            'rule test.utils.TestUtilClass$TestInnerClassTest jarjar.prefix.@0\n',
110            'rule test.utils.TestUtilClass$TestInnerClassTest$* jarjar.prefix.@0\n'], lines)
111
112
113if __name__ == '__main__':
114    # Need verbosity=2 for the test results parser to find results
115    unittest.main(verbosity=2)
116