Search in sources :

Example 81 with HashCode

use of com.google.common.hash.HashCode in project buck by facebook.

the class ForwardingRuleKeyHasherTest method testHashAndOnHash.

@Test
public void testHashAndOnHash() {
    final String string = "hash";
    final HashCode hash = createMock(HashCode.class);
    @SuppressWarnings("unchecked") RuleKeyHasher<String> stringHasher = createStrictMock(RuleKeyHasher.class);
    @SuppressWarnings("unchecked") RuleKeyHasher<HashCode> guavaHasher = createStrictMock(RuleKeyHasher.class);
    expect(guavaHasher.hash()).andReturn(hash);
    expect(stringHasher.hash()).andReturn(string);
    replay(stringHasher, guavaHasher);
    ForwardingRuleKeyHasher<HashCode, String> hasher = new ForwardingRuleKeyHasher<HashCode, String>(guavaHasher, stringHasher) {

        @Override
        protected void onHash(HashCode firstHash, String secondHash) {
            assertSame(string, secondHash);
            assertSame(hash, firstHash);
        }
    };
    assertSame(hash, hasher.hash());
}
Also used : HashCode(com.google.common.hash.HashCode) Sha1HashCode(com.facebook.buck.util.sha1.Sha1HashCode) Test(org.junit.Test)

Example 82 with HashCode

use of com.google.common.hash.HashCode in project buck by facebook.

the class CountingRuleKeyHasherTest method testCounting.

@Test
public void testCounting() {
    CountingRuleKeyHasher<HashCode> hasher = newCountHasher();
    int count = 0;
    assertEquals(count, hasher.getCount());
    hasher.putKey("");
    assertEquals(++count, hasher.getCount());
    hasher.putKey("42").putKey("43").putKey("44");
    assertEquals(count += 3, hasher.getCount());
    hasher.putNull();
    assertEquals(++count, hasher.getCount());
    hasher.putBoolean(true);
    assertEquals(++count, hasher.getCount());
    hasher.putBoolean(false).putBoolean(true);
    assertEquals(count += 2, hasher.getCount());
    hasher.putNumber(0);
    assertEquals(++count, hasher.getCount());
    hasher.putNumber(42).putNumber(43);
    assertEquals(count += 2, hasher.getCount());
    hasher.putNumber((long) 0);
    assertEquals(++count, hasher.getCount());
    hasher.putNumber((long) 42).putNumber((long) 43);
    assertEquals(count += 2, hasher.getCount());
    hasher.putNumber((short) 0);
    assertEquals(++count, hasher.getCount());
    hasher.putNumber((short) 42).putNumber((short) 43);
    assertEquals(count += 2, hasher.getCount());
    hasher.putNumber((byte) 0);
    assertEquals(++count, hasher.getCount());
    hasher.putNumber((byte) 42).putNumber((byte) 43);
    assertEquals(count += 2, hasher.getCount());
    hasher.putNumber((float) 0);
    assertEquals(++count, hasher.getCount());
    hasher.putNumber((float) 42).putNumber((float) 43);
    assertEquals(count += 2, hasher.getCount());
    hasher.putNumber((double) 0);
    assertEquals(++count, hasher.getCount());
    hasher.putNumber((double) 42).putNumber((double) 43);
    assertEquals(count += 2, hasher.getCount());
    hasher.putString("");
    assertEquals(++count, hasher.getCount());
    hasher.putString("42").putString("43");
    assertEquals(count += 2, hasher.getCount());
    hasher.putBytes(new byte[0]);
    assertEquals(++count, hasher.getCount());
    hasher.putBytes(new byte[] { 42 });
    assertEquals(++count, hasher.getCount());
    hasher.putBytes(new byte[] { 42, 42 }).putBytes(new byte[] { 43 });
    assertEquals(count += 2, hasher.getCount());
    hasher.putPattern(Pattern.compile(""));
    assertEquals(++count, hasher.getCount());
    hasher.putPattern(Pattern.compile("42")).putPattern(Pattern.compile("43"));
    assertEquals(count += 2, hasher.getCount());
    hasher.putSha1(Sha1HashCode.of("a002b39af204cdfaa5fdb67816b13867c32ac52c"));
    assertEquals(++count, hasher.getCount());
    hasher.putSha1(Sha1HashCode.of("b67816b13867c32ac52ca002b39af204cdfaa5fd")).putSha1(Sha1HashCode.of("a002b39af204cdfaa5fdb67816b13867c32ac52c"));
    assertEquals(count += 2, hasher.getCount());
    hasher.putPath(Paths.get(""), HashCode.fromInt(0));
    assertEquals(++count, hasher.getCount());
    hasher.putPath(Paths.get(""), HashCode.fromInt(42));
    assertEquals(++count, hasher.getCount());
    hasher.putPath(Paths.get("42"), HashCode.fromInt(0));
    assertEquals(++count, hasher.getCount());
    hasher.putPath(Paths.get("42"), HashCode.fromInt(42));
    assertEquals(++count, hasher.getCount());
    hasher.putPath(Paths.get("42/42"), HashCode.fromInt(42)).putPath(Paths.get("43"), HashCode.fromInt(43));
    assertEquals(count += 2, hasher.getCount());
    hasher.putArchiveMemberPath(newArchiveMember("", ""), HashCode.fromInt(0));
    assertEquals(++count, hasher.getCount());
    hasher.putArchiveMemberPath(newArchiveMember("", ""), HashCode.fromInt(42));
    assertEquals(++count, hasher.getCount());
    hasher.putArchiveMemberPath(newArchiveMember("42", "42"), HashCode.fromInt(0));
    assertEquals(++count, hasher.getCount());
    hasher.putArchiveMemberPath(newArchiveMember("42", "42"), HashCode.fromInt(42));
    assertEquals(++count, hasher.getCount());
    hasher.putArchiveMemberPath(newArchiveMember("42/42", "42/42"), HashCode.fromInt(42)).putArchiveMemberPath(newArchiveMember("43/43", "43/43"), HashCode.fromInt(43));
    assertEquals(count += 2, hasher.getCount());
    hasher.putNonHashingPath("");
    assertEquals(++count, hasher.getCount());
    hasher.putNonHashingPath("42").putNonHashingPath("43");
    assertEquals(count += 2, hasher.getCount());
    hasher.putSourceRoot(new SourceRoot(""));
    assertEquals(++count, hasher.getCount());
    hasher.putSourceRoot(new SourceRoot("42")).putSourceRoot(new SourceRoot("43"));
    assertEquals(count += 2, hasher.getCount());
    hasher.putRuleKey(RULE_KEY_1);
    assertEquals(++count, hasher.getCount());
    hasher.putRuleKey(RULE_KEY_2).putRuleKey(RULE_KEY_1);
    assertEquals(count += 2, hasher.getCount());
    hasher.putBuildRuleType(BuildRuleType.of(""));
    assertEquals(++count, hasher.getCount());
    hasher.putBuildRuleType(BuildRuleType.of("42")).putBuildRuleType(BuildRuleType.of("43"));
    assertEquals(count += 2, hasher.getCount());
    hasher.putBuildTarget(TARGET_1);
    assertEquals(++count, hasher.getCount());
    hasher.putBuildTarget(TARGET_2).putBuildTarget(TARGET_1);
    assertEquals(count += 2, hasher.getCount());
    hasher.putBuildTargetSourcePath(new DefaultBuildTargetSourcePath(TARGET_1));
    assertEquals(++count, hasher.getCount());
    hasher.putBuildTargetSourcePath(new DefaultBuildTargetSourcePath(TARGET_2)).putBuildTargetSourcePath(new DefaultBuildTargetSourcePath(TARGET_1));
    assertEquals(count += 2, hasher.getCount());
    hasher.putContainer(RuleKeyHasher.Container.LIST, 0);
    assertEquals(++count, hasher.getCount());
    hasher.putContainer(RuleKeyHasher.Container.LIST, 42).putContainer(RuleKeyHasher.Container.LIST, 43);
    assertEquals(count += 2, hasher.getCount());
    hasher.putContainer(RuleKeyHasher.Container.MAP, 0);
    assertEquals(++count, hasher.getCount());
    hasher.putContainer(RuleKeyHasher.Container.MAP, 42).putContainer(RuleKeyHasher.Container.MAP, 43);
    assertEquals(count += 2, hasher.getCount());
    hasher.putWrapper(RuleKeyHasher.Wrapper.SUPPLIER);
    assertEquals(++count, hasher.getCount());
    hasher.putWrapper(RuleKeyHasher.Wrapper.OPTIONAL);
    assertEquals(++count, hasher.getCount());
    hasher.putWrapper(RuleKeyHasher.Wrapper.EITHER_LEFT);
    assertEquals(++count, hasher.getCount());
    hasher.putWrapper(RuleKeyHasher.Wrapper.EITHER_RIGHT);
    assertEquals(++count, hasher.getCount());
    hasher.putWrapper(RuleKeyHasher.Wrapper.EITHER_RIGHT);
    assertEquals(++count, hasher.getCount());
    hasher.putWrapper(RuleKeyHasher.Wrapper.BUILD_RULE);
    assertEquals(++count, hasher.getCount());
    hasher.putWrapper(RuleKeyHasher.Wrapper.APPENDABLE);
    assertEquals(++count, hasher.getCount());
    hasher.putWrapper(RuleKeyHasher.Wrapper.SUPPLIER).putWrapper(RuleKeyHasher.Wrapper.OPTIONAL).putWrapper(RuleKeyHasher.Wrapper.EITHER_LEFT).putWrapper(RuleKeyHasher.Wrapper.EITHER_RIGHT).putWrapper(RuleKeyHasher.Wrapper.BUILD_RULE).putWrapper(RuleKeyHasher.Wrapper.APPENDABLE).putWrapper(RuleKeyHasher.Wrapper.OPTIONAL);
    assertEquals(count += 7, hasher.getCount());
    hasher.putKey("key").putContainer(RuleKeyHasher.Container.LIST, 3).putString("a").putNumber(1).putNull();
    assertEquals(count += 5, hasher.getCount());
}
Also used : HashCode(com.google.common.hash.HashCode) Sha1HashCode(com.facebook.buck.util.sha1.Sha1HashCode) SourceRoot(com.facebook.buck.rules.SourceRoot) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) Test(org.junit.Test)

Example 83 with HashCode

use of com.google.common.hash.HashCode in project buck by facebook.

the class AbstractProvisioningProfileMetadata method fromProvisioningProfilePath.

public static ProvisioningProfileMetadata fromProvisioningProfilePath(ProcessExecutor executor, ImmutableList<String> readCommand, Path profilePath) throws IOException, InterruptedException {
    Set<ProcessExecutor.Option> options = EnumSet.of(ProcessExecutor.Option.EXPECTING_STD_OUT);
    // Extract the XML from its signed message wrapper.
    ProcessExecutorParams processExecutorParams = ProcessExecutorParams.builder().addAllCommand(readCommand).addCommand(profilePath.toString()).build();
    ProcessExecutor.Result result;
    result = executor.launchAndExecute(processExecutorParams, options, /* stdin */
    Optional.empty(), /* timeOutMs */
    Optional.empty(), /* timeOutHandler */
    Optional.empty());
    if (result.getExitCode() != 0) {
        throw new IOException(result.getMessageForResult("Invalid provisioning profile: " + profilePath));
    }
    try {
        NSDictionary plist = (NSDictionary) PropertyListParser.parse(result.getStdout().get().getBytes());
        Date expirationDate = ((NSDate) plist.get("ExpirationDate")).getDate();
        String uuid = ((NSString) plist.get("UUID")).getContent();
        ImmutableSet.Builder<HashCode> certificateFingerprints = ImmutableSet.builder();
        NSArray certificates = (NSArray) plist.get("DeveloperCertificates");
        HashFunction hasher = Hashing.sha1();
        if (certificates != null) {
            for (NSObject item : certificates.getArray()) {
                certificateFingerprints.add(hasher.hashBytes(((NSData) item).bytes()));
            }
        }
        ImmutableMap.Builder<String, NSObject> builder = ImmutableMap.builder();
        NSDictionary entitlements = ((NSDictionary) plist.get("Entitlements"));
        for (String key : entitlements.keySet()) {
            builder = builder.put(key, entitlements.objectForKey(key));
        }
        String appID = entitlements.get("application-identifier").toString();
        ProvisioningProfileMetadata.Builder provisioningProfileMetadata = ProvisioningProfileMetadata.builder();
        if (plist.get("Platform") != null) {
            for (Object platform : (Object[]) plist.get("Platform").toJavaObject()) {
                provisioningProfileMetadata.addPlatforms((String) platform);
            }
        }
        return provisioningProfileMetadata.setAppID(ProvisioningProfileMetadata.splitAppID(appID)).setExpirationDate(expirationDate).setUUID(uuid).setProfilePath(profilePath).setEntitlements(builder.build()).setDeveloperCertificateFingerprints(certificateFingerprints.build()).build();
    } catch (Exception e) {
        throw new IllegalArgumentException("Malformed embedded plist: " + e);
    }
}
Also used : NSObject(com.dd.plist.NSObject) NSData(com.dd.plist.NSData) NSArray(com.dd.plist.NSArray) NSDictionary(com.dd.plist.NSDictionary) NSString(com.dd.plist.NSString) NSString(com.dd.plist.NSString) HashCode(com.google.common.hash.HashCode) ImmutableSet(com.google.common.collect.ImmutableSet) NSDate(com.dd.plist.NSDate) ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) IOException(java.io.IOException) ProcessExecutor(com.facebook.buck.util.ProcessExecutor) Date(java.util.Date) NSDate(com.dd.plist.NSDate) ImmutableMap(com.google.common.collect.ImmutableMap) IOException(java.io.IOException) HashFunction(com.google.common.hash.HashFunction) NSObject(com.dd.plist.NSObject)

Example 84 with HashCode

use of com.google.common.hash.HashCode in project buck by facebook.

the class DexProducedFromJavaLibrary method computeAbiKey.

@VisibleForTesting
static Sha1HashCode computeAbiKey(ImmutableSortedMap<String, HashCode> classNames) {
    Hasher hasher = Hashing.sha1().newHasher();
    for (Map.Entry<String, HashCode> entry : classNames.entrySet()) {
        hasher.putUnencodedChars(entry.getKey());
        hasher.putByte((byte) 0);
        hasher.putUnencodedChars(entry.getValue().toString());
        hasher.putByte((byte) 0);
    }
    return Sha1HashCode.fromHashCode(hasher.hash());
}
Also used : Hasher(com.google.common.hash.Hasher) HashCode(com.google.common.hash.HashCode) Sha1HashCode(com.facebook.buck.util.sha1.Sha1HashCode) Map(java.util.Map) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 85 with HashCode

use of com.google.common.hash.HashCode in project buck by facebook.

the class DexProducedFromJavaLibrary method getBuildSteps.

@Override
public ImmutableList<Step> getBuildSteps(BuildContext context, final BuildableContext buildableContext) {
    ImmutableList.Builder<Step> steps = ImmutableList.builder();
    steps.add(new RmStep(getProjectFilesystem(), getPathToDex()));
    // Make sure that the buck-out/gen/ directory exists for this.buildTarget.
    steps.add(new MkdirStep(getProjectFilesystem(), getPathToDex().getParent()));
    // If there are classes, run dx.
    final ImmutableSortedMap<String, HashCode> classNamesToHashes = javaLibrary.getClassNamesToHashes();
    final boolean hasClassesToDx = !classNamesToHashes.isEmpty();
    final Supplier<Integer> weightEstimate;
    @Nullable final DxStep dx;
    if (hasClassesToDx) {
        Path pathToOutputFile = context.getSourcePathResolver().getAbsolutePath(javaLibrarySourcePath);
        EstimateDexWeightStep estimate = new EstimateDexWeightStep(getProjectFilesystem(), pathToOutputFile);
        steps.add(estimate);
        weightEstimate = estimate;
        // To be conservative, use --force-jumbo for these intermediate .dex files so that they can be
        // merged into a final classes.dex that uses jumbo instructions.
        dx = new DxStep(getProjectFilesystem(), getPathToDex(), Collections.singleton(pathToOutputFile), EnumSet.of(DxStep.Option.USE_CUSTOM_DX_IF_AVAILABLE, DxStep.Option.RUN_IN_PROCESS, DxStep.Option.NO_OPTIMIZE, DxStep.Option.FORCE_JUMBO));
        steps.add(dx);
        // The `DxStep` delegates to android tools to build a ZIP with timestamps in it, making
        // the output non-deterministic.  So use an additional scrubbing step to zero these out.
        steps.add(new ZipScrubberStep(getProjectFilesystem(), getPathToDex()));
    } else {
        dx = null;
        weightEstimate = Suppliers.ofInstance(0);
    }
    // Run a step to record artifacts and metadata. The values recorded depend upon whether dx was
    // run.
    String stepName = hasClassesToDx ? "record_dx_success" : "record_empty_dx";
    AbstractExecutionStep recordArtifactAndMetadataStep = new AbstractExecutionStep(stepName) {

        @Override
        public StepExecutionResult execute(ExecutionContext context) throws IOException {
            if (hasClassesToDx) {
                buildableContext.recordArtifact(getPathToDex());
                @Nullable Collection<String> referencedResources = dx.getResourcesReferencedInCode();
                if (referencedResources != null) {
                    buildableContext.addMetadata(REFERENCED_RESOURCES, Ordering.natural().immutableSortedCopy(referencedResources));
                }
            }
            buildableContext.addMetadata(WEIGHT_ESTIMATE, String.valueOf(weightEstimate.get()));
            // Record the classnames to hashes map.
            buildableContext.addMetadata(CLASSNAMES_TO_HASHES, context.getObjectMapper().writeValueAsString(Maps.transformValues(classNamesToHashes, Object::toString)));
            return StepExecutionResult.SUCCESS;
        }
    };
    steps.add(recordArtifactAndMetadataStep);
    return steps.build();
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) ImmutableList(com.google.common.collect.ImmutableList) MkdirStep(com.facebook.buck.step.fs.MkdirStep) RmStep(com.facebook.buck.step.fs.RmStep) Step(com.facebook.buck.step.Step) MkdirStep(com.facebook.buck.step.fs.MkdirStep) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) ZipScrubberStep(com.facebook.buck.zip.ZipScrubberStep) EstimateDexWeightStep(com.facebook.buck.dalvik.EstimateDexWeightStep) RmStep(com.facebook.buck.step.fs.RmStep) HashCode(com.google.common.hash.HashCode) Sha1HashCode(com.facebook.buck.util.sha1.Sha1HashCode) ExecutionContext(com.facebook.buck.step.ExecutionContext) EstimateDexWeightStep(com.facebook.buck.dalvik.EstimateDexWeightStep) ZipScrubberStep(com.facebook.buck.zip.ZipScrubberStep) Nullable(javax.annotation.Nullable)

Aggregations

HashCode (com.google.common.hash.HashCode)115 Path (java.nio.file.Path)41 Test (org.junit.Test)39 BuildTarget (com.facebook.buck.model.BuildTarget)19 FakeFileHashCache (com.facebook.buck.testutil.FakeFileHashCache)13 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)12 ImmutableMap (com.google.common.collect.ImmutableMap)12 IOException (java.io.IOException)12 PathSourcePath (com.facebook.buck.rules.PathSourcePath)11 FileHashCache (com.facebook.buck.util.cache.FileHashCache)11 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)10 Sha1HashCode (com.facebook.buck.util.sha1.Sha1HashCode)10 Hasher (com.google.common.hash.Hasher)10 Map (java.util.Map)8 UnflavoredBuildTarget (com.facebook.buck.model.UnflavoredBuildTarget)7 SourcePath (com.facebook.buck.rules.SourcePath)7 BuckEventBus (com.facebook.buck.event.BuckEventBus)6 DefaultBuildTargetSourcePath (com.facebook.buck.rules.DefaultBuildTargetSourcePath)6 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)5 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)5