forked from facebook/buck
/
AndroidBinaryIntegrationTest.java
837 lines (724 loc) · 34.2 KB
/
AndroidBinaryIntegrationTest.java
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
/*
* Copyright 2013-present Facebook, 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.facebook.buck.android;
import static com.facebook.buck.testutil.RegexMatcher.containsRegex;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import com.facebook.buck.android.relinker.Symbols;
import com.facebook.buck.cxx.CxxPlatformUtils;
import com.facebook.buck.io.ProjectFilesystem;
import com.facebook.buck.model.BuildTarget;
import com.facebook.buck.model.BuildTargetFactory;
import com.facebook.buck.model.BuildTargets;
import com.facebook.buck.rules.BuildRuleResolver;
import com.facebook.buck.rules.DefaultOnDiskBuildInfo;
import com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer;
import com.facebook.buck.rules.SourcePathResolver;
import com.facebook.buck.rules.TargetGraph;
import com.facebook.buck.rules.Tool;
import com.facebook.buck.testutil.integration.BuckBuildLog;
import com.facebook.buck.testutil.integration.ProjectWorkspace;
import com.facebook.buck.testutil.integration.TemporaryPaths;
import com.facebook.buck.testutil.integration.TestDataHelper;
import com.facebook.buck.testutil.integration.ZipInspector;
import com.facebook.buck.util.ObjectMappers;
import com.facebook.buck.util.environment.Platform;
import com.facebook.buck.zip.ZipConstants;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.hash.Hashing;
import org.apache.commons.compress.archivers.zip.ZipUtil;
import org.hamcrest.Matchers;
import org.hamcrest.collection.IsIn;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
public class AndroidBinaryIntegrationTest {
@ClassRule
public static TemporaryPaths projectFolderWithPrebuiltTargets = new TemporaryPaths();
private static ProjectWorkspace workspaceWithPrebuiltTargets;
@Rule
public TemporaryPaths tmpFolder = new TemporaryPaths();
private ProjectWorkspace workspace;
private ProjectFilesystem filesystem;
private static final String SIMPLE_TARGET = "//apps/multidex:app";
private static final String RAW_DEX_TARGET = "//apps/multidex:app-art";
@BeforeClass
public static void setUpOnce() throws IOException {
AssumeAndroidPlatform.assumeSdkIsAvailable();
AssumeAndroidPlatform.assumeNdkIsAvailable();
workspaceWithPrebuiltTargets = TestDataHelper.createProjectWorkspaceForScenario(
new AndroidBinaryIntegrationTest(),
"android_project",
projectFolderWithPrebuiltTargets);
workspaceWithPrebuiltTargets.setUp();
workspaceWithPrebuiltTargets.runBuckBuild(SIMPLE_TARGET).assertSuccess();
}
@Before
public void setUp() throws IOException {
workspace = ProjectWorkspace.cloneExistingWorkspaceIntoNewFolder(
workspaceWithPrebuiltTargets,
tmpFolder);
workspace.setUp();
filesystem = new ProjectFilesystem(workspace.getDestPath());
}
@Test
public void testNonExopackageHasSecondary() throws IOException {
ZipInspector zipInspector = new ZipInspector(
workspace.getPath(
BuildTargets.getGenPath(
filesystem,
BuildTargetFactory.newInstance(SIMPLE_TARGET),
"%s.apk")));
zipInspector.assertFileExists("assets/secondary-program-dex-jars/metadata.txt");
zipInspector.assertFileExists("assets/secondary-program-dex-jars/secondary-1.dex.jar");
zipInspector.assertFileDoesNotExist("classes2.dex");
zipInspector.assertFileExists("classes.dex");
zipInspector.assertFileExists("lib/armeabi/libfakenative.so");
}
@Test
public void testRawSplitDexHasSecondary() throws IOException {
ProjectWorkspace.ProcessResult result = workspace.runBuckCommand("build", RAW_DEX_TARGET);
result.assertSuccess();
ZipInspector zipInspector = new ZipInspector(
workspace.getPath(
BuildTargets.getGenPath(
filesystem,
BuildTargetFactory.newInstance(RAW_DEX_TARGET),
"%s.apk")));
zipInspector.assertFileDoesNotExist("assets/secondary-program-dex-jars/metadata.txt");
zipInspector.assertFileDoesNotExist("assets/secondary-program-dex-jars/secondary-1.dex.jar");
zipInspector.assertFileExists("classes2.dex");
zipInspector.assertFileExists("classes.dex");
zipInspector.assertFileExists("lib/armeabi/libfakenative.so");
}
@Test
public void testDisguisedExecutableIsRenamed() throws IOException {
ZipInspector zipInspector = new ZipInspector(
workspace.getPath(
BuildTargets.getGenPath(
filesystem,
BuildTargetFactory.newInstance(SIMPLE_TARGET),
"%s.apk")));
zipInspector.assertFileExists("lib/armeabi/libmybinary.so");
}
@Test
public void testEditingPrimaryDexClassForcesRebuildForSimplePackage() throws IOException {
workspace.replaceFileContents(
"java/com/sample/app/MyApplication.java",
"package com",
"package\ncom");
workspace.resetBuildLogFile();
ProjectWorkspace.ProcessResult result = workspace.runBuckCommand("build", SIMPLE_TARGET);
result.assertSuccess();
BuckBuildLog buildLog = workspace.getBuildLog();
buildLog.assertTargetBuiltLocally(SIMPLE_TARGET);
}
@Test
public void testEditingSecondaryDexClassForcesRebuildForSimplePackage() throws IOException {
workspace.replaceFileContents(
"java/com/sample/lib/Sample.java",
"package com",
"package\ncom");
workspace.resetBuildLogFile();
ProjectWorkspace.ProcessResult result = workspace.runBuckCommand("build", SIMPLE_TARGET);
result.assertSuccess();
BuckBuildLog buildLog = workspace.getBuildLog();
buildLog.assertTargetBuiltLocally(SIMPLE_TARGET);
}
@Test
public void testNotAllJavaLibrariesFetched() throws IOException {
String target = "//apps/multidex:app_with_deeper_deps";
workspace.runBuckCommand("build", target).assertSuccess();
workspace.replaceFileContents(
"java/com/sample/app/MyApplication.java",
"package com",
"package\ncom");
workspace.resetBuildLogFile();
workspace.runBuckCommand("build", target).assertSuccess();
BuckBuildLog buildLog = workspace.getBuildLog();
buildLog.assertTargetBuiltLocally(target);
buildLog.assertTargetIsAbsent("//java/com/sample/lib:lib");
}
@Test
public void testPreprocessorForcesReDex() throws IOException {
String target = "//java/com/preprocess:disassemble";
Path outputFile = workspace.buildAndReturnOutput(target);
String output = new String(Files.readAllBytes(outputFile), UTF_8);
assertThat(output, containsString("content=2"));
workspace.replaceFileContents(
"java/com/preprocess/convert.py",
"content=2",
"content=3");
outputFile = workspace.buildAndReturnOutput(target);
output = new String(Files.readAllBytes(outputFile), UTF_8);
assertThat(output, containsString("content=3"));
}
@Test
public void testDxFindsReferencedResources() throws IOException {
DefaultOnDiskBuildInfo buildInfo = new DefaultOnDiskBuildInfo(
BuildTargetFactory.newInstance("//java/com/sample/lib:lib#dex"),
new ProjectFilesystem(tmpFolder.getRoot()),
ObjectMappers.newDefaultInstance());
Optional<ImmutableList<String>> resourcesFromMetadata =
buildInfo.getValues(DexProducedFromJavaLibrary.REFERENCED_RESOURCES);
assertTrue(resourcesFromMetadata.isPresent());
assertEquals(
ImmutableSet.of("com.sample.top_layout", "com.sample2.title"),
ImmutableSet.copyOf(resourcesFromMetadata.get()));
}
@Test
public void testCxxLibraryDep() throws IOException {
String target = "//apps/sample:app_cxx_lib_dep";
workspace.runBuckCommand("build", target).assertSuccess();
ZipInspector zipInspector = new ZipInspector(
workspace.getPath(
BuildTargets.getGenPath(filesystem, BuildTargetFactory.newInstance(target), "%s.apk")));
zipInspector.assertFileExists("lib/armeabi/libnative_cxx_lib.so");
zipInspector.assertFileExists("lib/armeabi/libgnustl_shared.so");
zipInspector.assertFileExists("lib/armeabi-v7a/libnative_cxx_lib.so");
zipInspector.assertFileExists("lib/armeabi-v7a/libgnustl_shared.so");
zipInspector.assertFileExists("lib/x86/libnative_cxx_lib.so");
zipInspector.assertFileExists("lib/x86/libgnustl_shared.so");
}
@Test
public void testCxxLibraryDepClang() throws IOException {
String target = "//apps/sample:app_cxx_lib_dep";
ProjectWorkspace.ProcessResult result =
workspace.runBuckCommand(
"build",
"-c", "ndk.compiler=clang",
"-c", "ndk.cxx_runtime=libcxx",
target);
result.assertSuccess();
ZipInspector zipInspector = new ZipInspector(
workspace.getPath(
BuildTargets.getGenPath(
filesystem,
BuildTargetFactory.newInstance(target),
"%s.apk")));
zipInspector.assertFileExists("lib/armeabi/libnative_cxx_lib.so");
zipInspector.assertFileExists("lib/armeabi/libc++_shared.so");
zipInspector.assertFileExists("lib/armeabi-v7a/libnative_cxx_lib.so");
zipInspector.assertFileExists("lib/armeabi-v7a/libc++_shared.so");
zipInspector.assertFileExists("lib/x86/libnative_cxx_lib.so");
zipInspector.assertFileExists("lib/x86/libc++_shared.so");
}
@Test
public void testCxxLibraryDepWithNoFilters() throws IOException {
String target = "//apps/sample:app_cxx_lib_dep_no_filters";
workspace.runBuckCommand("build", target).assertSuccess();
ZipInspector zipInspector = new ZipInspector(
workspace.getPath(
BuildTargets.getGenPath(filesystem, BuildTargetFactory.newInstance(target), "%s.apk")));
zipInspector.assertFileExists("lib/armeabi/libnative_cxx_lib.so");
zipInspector.assertFileExists("lib/armeabi-v7a/libnative_cxx_lib.so");
zipInspector.assertFileExists("lib/x86/libnative_cxx_lib.so");
}
@Test
public void testNoCxxDepsDoesNotIncludeNdkRuntime() throws IOException {
String target = "//apps/sample:app_no_cxx_deps";
workspace.runBuckCommand("build", target).assertSuccess();
ZipInspector zipInspector = new ZipInspector(
workspace.getPath(
BuildTargets.getGenPath(filesystem, BuildTargetFactory.newInstance(target), "%s.apk")));
zipInspector.assertFileDoesNotExist("lib/armeabi/libgnustl_shared.so");
zipInspector.assertFileDoesNotExist("lib/armeabi-v7a/libgnustl_shared.so");
zipInspector.assertFileDoesNotExist("lib/x86/libgnustl_shared.so");
}
@Test
public void testProguardDontObfuscateGeneratesMappingFile() throws IOException {
String target = "//apps/sample:app_proguard_dontobfuscate";
workspace.runBuckCommand("build", target).assertSuccess();
Path mapping = workspace.getPath(
BuildTargets.getGenPath(
filesystem,
BuildTargetFactory.newInstance(target)
.withFlavors(AndroidBinaryGraphEnhancer.AAPT_PACKAGE_FLAVOR),
"__%s__proguard__/.proguard/mapping.txt"));
assertTrue(Files.exists(mapping));
}
@Test
public void testStaticCxxLibraryDep() throws IOException {
String target = "//apps/sample:app_static_cxx_lib_dep";
workspace.runBuckCommand("build", target).assertSuccess();
ZipInspector zipInspector = new ZipInspector(
workspace.getPath(
BuildTargets.getGenPath(filesystem, BuildTargetFactory.newInstance(target), "%s.apk")));
zipInspector.assertFileExists("lib/x86/libnative_cxx_foo1.so");
zipInspector.assertFileExists("lib/x86/libnative_cxx_foo2.so");
zipInspector.assertFileDoesNotExist("lib/x86/libnative_cxx_bar.so");
}
private static Path unzip(Path tmpDir, Path zipPath, String name) throws IOException {
Path outPath = tmpDir.resolve(zipPath.getFileName());
try (ZipFile zipFile = new ZipFile(zipPath.toFile())) {
Files.copy(
zipFile.getInputStream(zipFile.getEntry(name)),
outPath,
StandardCopyOption.REPLACE_EXISTING);
return outPath;
}
}
@Test
public void testNativeLibraryMerging() throws IOException, InterruptedException {
NdkCxxPlatform platform = getNdkCxxPlatform();
SourcePathResolver pathResolver = new SourcePathResolver(
new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer())
);
Path tmpDir = tmpFolder.newFolder("merging_tmp");
SymbolGetter syms = new SymbolGetter(tmpDir, platform.getObjdump(), pathResolver);
SymbolsAndDtNeeded info;
workspace.replaceFileContents(
".buckconfig",
"#cpu_abis",
"cpu_abis = x86");
Map<String, Path> paths = workspace.buildMultipleAndReturnOutputs(
"//apps/sample:app_with_merged_libs",
"//apps/sample:app_with_alternate_merge_glue"
);
Path apkPath = paths.get("//apps/sample:app_with_merged_libs");
ZipInspector zipInspector = new ZipInspector(apkPath);
zipInspector.assertFileDoesNotExist("lib/x86/lib1a.so");
zipInspector.assertFileDoesNotExist("lib/x86/lib1b.so");
zipInspector.assertFileDoesNotExist("lib/x86/lib2e.so");
zipInspector.assertFileDoesNotExist("lib/x86/lib2f.so");
info = syms.getSymbolsAndDtNeeded(apkPath, "lib/x86/lib1.so");
assertThat(info.symbols.global, Matchers.hasItem("A"));
assertThat(info.symbols.global, Matchers.hasItem("B"));
assertThat(info.symbols.global, Matchers.hasItem("glue_1"));
assertThat(info.symbols.global, not(Matchers.hasItem("glue_2")));
assertThat(info.dtNeeded, Matchers.hasItem("libnative_merge_C.so"));
assertThat(info.dtNeeded, Matchers.hasItem("libnative_merge_D.so"));
assertThat(info.dtNeeded, not(Matchers.hasItem("libnative_merge_B.so")));
info = syms.getSymbolsAndDtNeeded(apkPath, "lib/x86/libnative_merge_C.so");
assertThat(info.symbols.global, Matchers.hasItem("C"));
assertThat(info.symbols.global, Matchers.hasItem("static_func_C"));
assertThat(info.symbols.global, not(Matchers.hasItem("glue_1")));
assertThat(info.symbols.global, not(Matchers.hasItem("glue_2")));
assertThat(info.dtNeeded, Matchers.hasItem("libnative_merge_D.so"));
assertThat(info.dtNeeded, Matchers.hasItem("libprebuilt_for_C.so"));
info = syms.getSymbolsAndDtNeeded(apkPath, "lib/x86/libnative_merge_D.so");
assertThat(info.symbols.global, Matchers.hasItem("D"));
assertThat(info.symbols.global, not(Matchers.hasItem("glue_1")));
assertThat(info.symbols.global, not(Matchers.hasItem("glue_2")));
assertThat(info.dtNeeded, Matchers.hasItem("lib2.so"));
assertThat(info.dtNeeded, not(Matchers.hasItem("libnative_merge_E.so")));
assertThat(info.dtNeeded, not(Matchers.hasItem("libnative_merge_F.so")));
info = syms.getSymbolsAndDtNeeded(apkPath, "lib/x86/lib2.so");
assertThat(info.symbols.global, Matchers.hasItem("E"));
assertThat(info.symbols.global, Matchers.hasItem("F"));
assertThat(info.symbols.global, Matchers.hasItem("static_func_F"));
assertThat(info.symbols.global, Matchers.hasItem("glue_1"));
assertThat(info.symbols.global, not(Matchers.hasItem("glue_2")));
assertThat(info.dtNeeded, Matchers.hasItem("libprebuilt_for_F.so"));
Path otherPath = paths.get("//apps/sample:app_with_alternate_merge_glue");
info = syms.getSymbolsAndDtNeeded(otherPath, "lib/x86/lib2.so");
assertThat(info.symbols.global, not(Matchers.hasItem("glue_1")));
assertThat(info.symbols.global, Matchers.hasItem("glue_2"));
assertThat(info.dtNeeded, Matchers.hasItem("libprebuilt_for_F.so"));
Path disassembly = workspace.buildAndReturnOutput(
"//apps/sample:disassemble_app_with_merged_libs_gencode");
List<String> disassembledLines = filesystem.readLines(disassembly);
Pattern fieldPattern = Pattern.compile(
"^\\.field public static final ([^:]+):Ljava/lang/String; = \"([^\"]+)\"$");
ImmutableMap.Builder<String, String> mapBuilder = ImmutableMap.builder();
for (String line : disassembledLines) {
Matcher m = fieldPattern.matcher(line);
if (!m.matches()) {
continue;
}
mapBuilder.put(m.group(1), m.group(2));
}
assertThat(mapBuilder.build(), Matchers.equalTo(ImmutableMap.of(
"lib1a_so", "lib1_so",
"lib1b_so", "lib1_so",
"lib2e_so", "lib2_so",
"lib2f_so", "lib2_so")));
}
@Test
public void testNativeLibraryMergeErrors() throws IOException, InterruptedException {
try {
workspace.runBuckBuild("//apps/sample:app_with_merge_lib_into_two_targets");
Assert.fail("No exception from trying to merge lib into two targets.");
} catch (RuntimeException e) {
assertThat(e.getMessage(), Matchers.containsString("into both"));
}
try {
workspace.runBuckBuild("//apps/sample:app_with_cross_asset_merged_libs");
Assert.fail("No exception from trying to merge between asset and non-asset.");
} catch (RuntimeException e) {
assertThat(e.getMessage(), Matchers.containsString("contains both asset and non-asset"));
}
// An older version of the code made this illegal.
// Keep the test around in case we want to restore this behavior.
// try {
// workspace.runBuckBuild("//apps/sample:app_with_merge_into_existing_lib");
// Assert.fail("No exception from trying to merge into existing name.");
// } catch (RuntimeException e) {
// assertThat(e.getMessage(), Matchers.containsString("already a library name"));
// }
try {
workspace.runBuckBuild("//apps/sample:app_with_circular_merged_libs");
Assert.fail("No exception from trying circular merged dep.");
} catch (RuntimeException e) {
assertThat(e.getMessage(), Matchers.containsString("Dependency cycle"));
assertThat(e.getCause().getMessage(), Matchers.containsString("Cycle found:"));
}
try {
workspace.runBuckBuild("//apps/sample:app_with_invalid_native_lib_merge_glue");
Assert.fail("No exception from trying invalid glue.");
} catch (RuntimeException e) {
assertThat(e.getMessage(), Matchers.matchesPattern(".*glue.*is not linkable.*"));
}
}
@Test
public void testNativeRelinker() throws IOException, InterruptedException {
NdkCxxPlatform platform = getNdkCxxPlatform();
SourcePathResolver pathResolver = new SourcePathResolver(
new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer())
);
Path tmpDir = tmpFolder.newFolder("xdso");
SymbolGetter syms = new SymbolGetter(tmpDir, platform.getObjdump(), pathResolver);
Symbols sym;
Path apkPath = workspace.buildAndReturnOutput("//apps/sample:app_xdso_dce");
sym = syms.getSymbols(apkPath, "lib/x86/libnative_xdsodce_top.so");
assertTrue(sym.global.contains("_Z10JNI_OnLoadii"));
assertTrue(sym.undefined.contains("_Z10midFromTopi"));
assertTrue(sym.undefined.contains("_Z10botFromTopi"));
assertFalse(sym.all.contains("_Z6unusedi"));
sym = syms.getSymbols(apkPath, "lib/x86/libnative_xdsodce_mid.so");
assertTrue(sym.global.contains("_Z10midFromTopi"));
assertTrue(sym.undefined.contains("_Z10botFromMidi"));
assertFalse(sym.all.contains("_Z6unusedi"));
sym = syms.getSymbols(apkPath, "lib/x86/libnative_xdsodce_bot.so");
assertTrue(sym.global.contains("_Z10botFromTopi"));
assertTrue(sym.global.contains("_Z10botFromMidi"));
assertFalse(sym.all.contains("_Z6unusedi"));
// Run some verification on the same apk with native_relinker disabled.
apkPath = workspace.buildAndReturnOutput("//apps/sample:app_no_xdso_dce");
sym = syms.getSymbols(apkPath, "lib/x86/libnative_xdsodce_top.so");
assertTrue(sym.all.contains("_Z6unusedi"));
sym = syms.getSymbols(apkPath, "lib/x86/libnative_xdsodce_mid.so");
assertTrue(sym.all.contains("_Z6unusedi"));
sym = syms.getSymbols(apkPath, "lib/x86/libnative_xdsodce_bot.so");
assertTrue(sym.all.contains("_Z6unusedi"));
}
private NdkCxxPlatform getNdkCxxPlatform() throws IOException, InterruptedException {
// TODO(cjhopman): is this really the simplest way to get the objdump tool?
AndroidDirectoryResolver androidResolver = new DefaultAndroidDirectoryResolver(
workspace.asCell().getRoot().getFileSystem(),
ImmutableMap.copyOf(System.getenv()),
Optional.<String>absent(),
Optional.<String>absent());
Optional<Path> ndkPath = androidResolver.getNdkOrAbsent();
assertTrue(ndkPath.isPresent());
Optional<String> ndkVersion =
DefaultAndroidDirectoryResolver.findNdkVersionFromDirectory(ndkPath.get());
String gccVersion = NdkCxxPlatforms.getDefaultGccVersionForNdk(ndkVersion);
ImmutableCollection<NdkCxxPlatform> platforms = NdkCxxPlatforms.getPlatforms(
CxxPlatformUtils.DEFAULT_CONFIG,
new ProjectFilesystem(ndkPath.get()),
NdkCxxPlatformCompiler.builder()
.setType(NdkCxxPlatforms.DEFAULT_COMPILER_TYPE)
.setVersion(gccVersion)
.setGccVersion(gccVersion)
.build(),
NdkCxxPlatforms.DEFAULT_CXX_RUNTIME,
NdkCxxPlatforms.DEFAULT_TARGET_APP_PLATFORM,
NdkCxxPlatforms.DEFAULT_CPU_ABIS,
Platform.detect()).values();
assertFalse(platforms.isEmpty());
return platforms.iterator().next();
}
private static class SymbolGetter {
private final Path tmpDir;
private final Tool objdump;
private final SourcePathResolver resolver;
private SymbolGetter(Path tmpDir, Tool objdump, SourcePathResolver resolver) {
this.tmpDir = tmpDir;
this.objdump = objdump;
this.resolver = resolver;
}
private Path unpack(Path apkPath, String libName) throws IOException {
new ZipInspector(apkPath).assertFileExists(libName);
return unzip(tmpDir, apkPath, libName);
}
Symbols getSymbols(Path apkPath, String libName) throws IOException, InterruptedException {
Path lib = unpack(apkPath, libName);
return Symbols.getSymbols(objdump, resolver, lib);
}
SymbolsAndDtNeeded getSymbolsAndDtNeeded(Path apkPath, String libName)
throws IOException, InterruptedException {
Path lib = unpack(apkPath, libName);
Symbols symbols = Symbols.getSymbols(objdump, resolver, lib);
ImmutableSet<String> dtNeeded = Symbols.getDtNeeded(objdump, resolver, lib);
return new SymbolsAndDtNeeded(symbols, dtNeeded);
}
}
private static class SymbolsAndDtNeeded {
final Symbols symbols;
final ImmutableSet<String> dtNeeded;
private SymbolsAndDtNeeded(Symbols symbols, ImmutableSet<String> dtNeeded) {
this.symbols = symbols;
this.dtNeeded = dtNeeded;
}
}
@Test
public void testHeaderOnlyCxxLibrary() throws IOException {
String target = "//apps/sample:app_header_only_cxx_lib_dep";
workspace.runBuckCommand("build", target).assertSuccess();
ZipInspector zipInspector = new ZipInspector(
workspace.getPath(
BuildTargets.getGenPath(filesystem, BuildTargetFactory.newInstance(target), "%s.apk")));
zipInspector.assertFileDoesNotExist("lib/x86/libnative_cxx_headeronly.so");
}
@Test
public void testX86OnlyCxxLibrary() throws IOException {
String target = "//apps/sample:app_with_x86_lib";
workspace.runBuckCommand("build", target).assertSuccess();
ZipInspector zipInspector = new ZipInspector(
workspace.getPath(
BuildTargets.getGenPath(filesystem, BuildTargetFactory.newInstance(target), "%s.apk")));
zipInspector.assertFileDoesNotExist("lib/armeabi-v7a/libnative_cxx_x86-only.so");
zipInspector.assertFileDoesNotExist("lib/armeabi-v7a/libgnustl_shared.so");
zipInspector.assertFileDoesNotExist("lib/armeabi/libnative_cxx_x86-only.so");
zipInspector.assertFileDoesNotExist("lib/armeabi/libgnustl_shared.so");
zipInspector.assertFileExists("lib/x86/libnative_cxx_x86-only.so");
zipInspector.assertFileExists("lib/x86/libgnustl_shared.so");
}
@Test
public void testApksHaveDeterministicTimestamps() throws IOException {
String target = "//apps/sample:app";
ProjectWorkspace.ProcessResult result = workspace.runBuckCommand("build", target);
result.assertSuccess();
// Iterate over each of the entries, expecting to see all zeros in the time fields.
Path apk = workspace.getPath(
BuildTargets.getGenPath(filesystem, BuildTargetFactory.newInstance(target), "%s.apk"));
Date dosEpoch = new Date(ZipUtil.dosToJavaTime(ZipConstants.DOS_FAKE_TIME));
try (ZipInputStream is = new ZipInputStream(Files.newInputStream(apk))) {
for (ZipEntry entry = is.getNextEntry(); entry != null; entry = is.getNextEntry()) {
assertThat(entry.getName(), new Date(entry.getTime()), Matchers.equalTo(dosEpoch));
}
}
}
@Test
public void testCxxLibraryAsAsset() throws IOException {
String target = "//apps/sample:app_cxx_lib_asset";
workspace.runBuckCommand("build", target).assertSuccess();
ZipInspector zipInspector = new ZipInspector(
workspace.getPath(
BuildTargets.getGenPath(filesystem, BuildTargetFactory.newInstance(target), "%s.apk")));
zipInspector.assertFileExists("assets/lib/x86/libnative_cxx_libasset.so");
zipInspector.assertFileDoesNotExist("lib/x86/libnative_cxx_libasset.so");
zipInspector.assertFileExists("lib/x86/libnative_cxx_foo1.so");
zipInspector.assertFileExists("lib/x86/libnative_cxx_foo2.so");
zipInspector.assertFileDoesNotExist("assets/lib/x86/libnative_cxx_foo1.so");
zipInspector.assertFileDoesNotExist("assets/lib/x86/libnative_cxx_foo2.so");
}
@Test
public void testCxxLibraryAsAssetWithoutPackaging() throws IOException {
String target = "//apps/sample:app_cxx_lib_asset_no_package";
workspace.runBuckCommand("build", target).assertSuccess();
ZipInspector zipInspector = new ZipInspector(
workspace.getPath(
BuildTargets.getGenPath(filesystem, BuildTargetFactory.newInstance(target), "%s.apk")));
zipInspector.assertFileDoesNotExist("assets/lib/x86/libnative_cxx_libasset.so");
zipInspector.assertFileExists("lib/x86/libnative_cxx_libasset.so");
}
@Test
public void testCompressAssetLibs() throws IOException {
String target = "//apps/sample:app_compress_lib_asset";
workspace.runBuckCommand("build", target).assertSuccess();
ZipInspector zipInspector = new ZipInspector(
workspace.getPath(
BuildTargets.getGenPath(filesystem, BuildTargetFactory.newInstance(target), "%s.apk")));
zipInspector.assertFileExists("assets/lib/libs.xzs");
zipInspector.assertFileExists("assets/lib/metadata.txt");
zipInspector.assertFileDoesNotExist("assets/lib/x86/libnative_cxx_libasset.so");
zipInspector.assertFileDoesNotExist("lib/x86/libnative_cxx_libasset.so");
zipInspector.assertFileExists("lib/x86/libnative_cxx_foo1.so");
zipInspector.assertFileExists("lib/x86/libnative_cxx_foo2.so");
zipInspector.assertFileDoesNotExist("assets/lib/x86/libnative_cxx_foo1.so");
zipInspector.assertFileDoesNotExist("assets/lib/x86/libnative_cxx_foo2.so");
}
@Test
public void testLibraryMetadataChecksum() throws IOException {
String target = "//apps/sample:app_cxx_lib_asset";
workspace.runBuckCommand("build", target).assertSuccess();
Path pathToZip = workspace.getPath(
BuildTargets.getGenPath(filesystem, BuildTargetFactory.newInstance(target), "%s.apk"));
ZipFile file = new ZipFile(pathToZip.toFile());
ZipEntry metadata = file.getEntry("assets/lib/metadata.txt");
assertNotNull(metadata);
BufferedReader contents = new BufferedReader(
new InputStreamReader(file.getInputStream(metadata)));
String line = contents.readLine();
byte[] buffer = new byte[512];
while (line != null) {
// Each line is of the form <filename> <filesize> <SHA256 checksum>
String[] tokens = line.split(" ");
assertSame(tokens.length, 3);
String filename = tokens[0];
int filesize = Integer.parseInt(tokens[1]);
String checksum = tokens[2];
ZipEntry lib = file.getEntry("assets/lib/" + filename);
assertNotNull(lib);
InputStream is = file.getInputStream(lib);
ByteArrayOutputStream out = new ByteArrayOutputStream();
while (filesize > 0) {
int read = is.read(buffer, 0, Math.min(buffer.length, filesize));
assertTrue(read >= 0);
out.write(buffer, 0, read);
filesize -= read;
}
String actualChecksum = Hashing.sha256().hashBytes(out.toByteArray()).toString();
assertEquals(checksum, actualChecksum);
is.close();
out.close();
line = contents.readLine();
}
file.close();
contents.close();
}
@Test
public void testStripRulesAreShared() throws IOException {
workspace.runBuckCommand("build", "//apps/sample:app_cxx_lib_asset").assertSuccess();
workspace.resetBuildLogFile();
workspace.runBuckCommand("build", "//apps/sample:app_cxx_different_rule_name").assertSuccess();
BuckBuildLog buildLog = workspace.getBuildLog();
for (BuildTarget target : buildLog.getAllTargets()) {
String rawTarget = target.toString();
if (rawTarget.contains("libgnustl_shared.so")) {
// Stripping the C++ runtime is currently not shared.
continue;
}
if (rawTarget.contains("strip")) {
buildLog.assertNotTargetBuiltLocally(rawTarget);
}
}
}
@Test
public void testApkWithNoResourcesBuildsCorrectly() throws IOException {
workspace.runBuckBuild("//apps/sample:app_with_no_res").assertSuccess();
workspace.runBuckBuild("//apps/sample:app_with_no_res_or_predex").assertSuccess();
}
@Test
public void testApkEmptyResDirectoriesBuildsCorrectly() throws IOException {
workspace.runBuckBuild("//apps/sample:app_with_aar_and_no_res").assertSuccess();
}
@Test
public void testInstrumentationApkWithEmptyResDepBuildsCorrectly() throws IOException {
workspace.runBuckBuild("//apps/sample:instrumentation_apk").assertSuccess();
}
@Test
public void testInvalidKeystoreKeyAlias() throws IOException {
workspace.replaceFileContents(
"keystores/debug.keystore.properties",
"key.alias=my_alias",
"key.alias=invalid_alias"
);
workspace.resetBuildLogFile();
ProjectWorkspace.ProcessResult result = workspace.runBuckCommand("build", SIMPLE_TARGET);
result.assertFailure("Invalid keystore key alias should fail.");
assertThat(
"error message for invalid keystore key alias is incorrect.",
result.getStderr(),
containsRegex("The keystore \\[.*\\] key\\.alias \\[.*\\].*does not exist"));
}
@Test
public void testResourcesTrimming() throws IOException {
// Enable trimming.
workspace.replaceFileContents(
"apps/multidex/BUCK",
"# ARGS_FOR_APP",
"trim_resource_ids = True, # ARGS_FOR_APP");
workspace.runBuckCommand("build", "//apps/multidex:disassemble_app_r_dot_java").assertSuccess();
// Make sure we only see what we expect.
verifyTrimmedRDotJava(ImmutableSet.of("top_layout", "title"));
// Make a change.
workspace.replaceFileContents(
"java/com/sample/lib/Sample.java",
"R.layout.top_layout",
"0 /* NO RESOURCE HERE */");
// Make sure everything gets rebuilt, and we only see what we expect.
workspace.resetBuildLogFile();
workspace.runBuckCommand("build", "//apps/multidex:disassemble_app_r_dot_java").assertSuccess();
BuckBuildLog buildLog = workspace.getBuildLog();
buildLog.assertTargetBuiltLocally("//apps/multidex:app#compile_uber_r_dot_java");
buildLog.assertTargetBuiltLocally("//apps/multidex:app#dex_uber_r_dot_java");
verifyTrimmedRDotJava(ImmutableSet.of("title"));
// Turn off trimming and turn on exopackage, and rebuilt.
workspace.replaceFileContents(
"apps/multidex/BUCK",
"trim_resource_ids = True, # ARGS_FOR_APP",
"exopackage_modes = ['secondary_dex'], # ARGS_FOR_APP");
workspace.runBuckCommand("build", SIMPLE_TARGET).assertSuccess();
// Make a change.
workspace.replaceFileContents(
"java/com/sample/lib/Sample.java",
"0 /* NO RESOURCE HERE */",
"R.layout.top_layout");
// rebuilt and verify that we get an ABI hit.
workspace.resetBuildLogFile();
workspace.runBuckCommand("build", SIMPLE_TARGET).assertSuccess();
buildLog = workspace.getBuildLog();
buildLog.assertTargetHadMatchingDepsAbi(SIMPLE_TARGET);
}
public static final Pattern SMALI_STATIC_FINAL_INT_PATTERN = Pattern.compile(
"\\.field public static final (\\w+):I = 0x[0-9A-fa-f]+");
private void verifyTrimmedRDotJava(ImmutableSet<String> expected) throws IOException {
List<String> lines = filesystem.readLines(
Paths.get("buck-out/gen/apps/multidex/disassemble_app_r_dot_java/all_r_fields.smali"));
ImmutableSet.Builder<String> found = ImmutableSet.builder();
for (String line : lines) {
Matcher m = SMALI_STATIC_FINAL_INT_PATTERN.matcher(line);
assertTrue("Could not match line: " + line, m.matches());
assertThat(m.group(1), IsIn.in(expected));
found.add(m.group(1));
}
assertEquals(expected, found.build());
}
}