public void TestClear() { // Length > 32 Fingerprint fp = new Fingerprint(65); for (int i = 0; i < 65; ++i) { fp.Touch(i); Assert.True(fp.Get(i)); } fp.Clear(); for (int i = 0; i < 65; ++i) { Assert.False(fp.Get(i)); } // Length <= 32 var fp2 = new Fingerprint(6); for (int i = 0; i < 6; ++i) { fp2.Touch(i); Assert.True(fp2.Get(i)); } fp.Clear(); for (int i = 0; i < 6; ++i) { Assert.False(fp.Get(i)); } }
public void TestCreation() { var fp = new Fingerprint(7); var window = new Capo<bool>(fp, 0); Assert.AreEqual(7, window.Length); window = new Capo<bool>(fp, 3); Assert.AreEqual(4, window.Length); // Invalid offset initialization attempt never throws Assert.DoesNotThrow(() => { window = new Capo<bool>(fp, 7); }); }
public override int GetHashCode(Fingerprint fingerprint) { var hash = new Hash(base.GetHashCode(fingerprint)); if (fingerprint.Length <= tag.Offset) { return hash.Code; } var touched = new Capo<bool>(fingerprint, tag.Offset); if (touched[0]) { hash.Update(message_); } return hash.Code; }
public void TestComparison() { Fingerprint fp1 = new Fingerprint(65); Fingerprint fp2 = new Fingerprint(65); Fingerprint fp3 = new Fingerprint(64); Fingerprint fp4 = new Fingerprint(66); // Length first Assert.Less(fp3.CompareTo(fp1), 0); Assert.Greater(fp1.CompareTo(fp3), 0); fp3.Touch(2); Assert.Less(fp3.CompareTo(fp1), 0); Assert.Greater(fp1.CompareTo(fp3), 0); Assert.Greater(fp4.CompareTo(fp2), 0); Assert.Less(fp2.CompareTo(fp4), 0); fp2.Touch(64); Assert.Greater(fp4.CompareTo(fp2), 0); Assert.Less(fp2.CompareTo(fp4), 0); fp2.Wipe(64); // Bits second Assert.AreEqual(0, fp1.CompareTo(fp2)); fp1.Touch(31); Assert.Greater(fp1.CompareTo(fp2), 0); Assert.Less(fp2.CompareTo(fp1), 0); fp2.Touch(32); Assert.Less(fp1.CompareTo(fp2), 0); Assert.Greater(fp2.CompareTo(fp1), 0); fp1.Touch(32); Assert.Greater(fp1.CompareTo(fp2), 0); Assert.Less(fp2.CompareTo(fp1), 0); fp2.Touch(31); Assert.AreEqual(0, fp1.CompareTo(fp2)); Assert.AreEqual(0, fp2.CompareTo(fp1)); fp2.Touch(64); Assert.Less(fp1.CompareTo(fp2), 0); Assert.Greater(fp2.CompareTo(fp1), 0); }
public void TestCapoing() { var fp = new Fingerprint(8); fp.Touch(2); fp.Touch(4); var window = new Capo<bool>(fp, 3); Assert.False(window[0]); Assert.True(window[1]); Assert.False(window[2]); fp.Wipe(4); Assert.False(window[1]); // Out-of-range indexing never throws Assert.False(window[-8]); Assert.False(window[8]); }
public void TestCapoing() { var fp = new Fingerprint(8); fp.Touch(2); fp.Touch(4); // Capo just displaces Fingerprint index with a provided offset. var window = new Capo<bool>(fp, 3); Assert.False(window[0]); Assert.True(window[1]); Assert.False(window[2]); fp.Wipe(4); Assert.False(window[1]); // Out-of-range indexing never throws Assert.False(window[-8]); Assert.False(window[8]); }
public void TestAccessors() { var fp = new Fingerprint(33); Assert.Throws(typeof(IndexOutOfRangeException), () => { fp.Get(-1); }); Assert.Throws(typeof(IndexOutOfRangeException), () => { fp.Get(33); }); Assert.False(fp.Get(31)); fp.Touch(31); Assert.True(fp.Get(31)); fp.Wipe(31); Assert.False(fp.Get(31)); Assert.False(fp.Get(32)); fp.Touch(32); Assert.True(fp.Get(32)); fp.Wipe(32); Assert.False(fp.Get(32)); }
/// <summary> /// Overridden by subclasses to build a hash code generator chain. /// </summary> public virtual int GetHashCode(Fingerprint fingerprint) { return Hash.Seed; }
protected override bool IsEquivalent(Cell other, Fingerprint fingerprint) { if (!base.IsEquivalent(other, fingerprint)) { return false; } TimeoutEvent o = (TimeoutEvent)other; var touched = new Capo<bool>(fingerprint, tag.Offset); if (touched[0]) { if (key_ != o.key_) { return false; } } if (touched[1]) { if (intParam_ != o.intParam_) { return false; } } return true; }
/// <nodoc/> public static WeakFingerprintHash Random() { return(new WeakFingerprintHash(new Hash(Fingerprint.Random(FingerprintUtilities.FingerprintLength)))); }
public void TestNegativeLength() { Assert.Throws<ArgumentOutOfRangeException>(() => { var fp = new Fingerprint(-1); }); }
public void TestEquality() { Fingerprint fp1 = new Fingerprint(65); Fingerprint fp2 = new Fingerprint(65); Fingerprint fp3 = new Fingerprint(64); Fingerprint fp4 = new Fingerprint(66); // Reference first Assert.True(fp1.Equals(fp1)); Assert.True(fp2.Equals(fp2)); Assert.True(fp3.Equals(fp3)); Assert.True(fp4.Equals(fp4)); // Type second Assert.False(fp1.Equals(new Object())); // Length third Assert.False(fp3.Equals(fp1)); Assert.False(fp1.Equals(fp3)); Assert.False(fp4.Equals(fp2)); Assert.False(fp2.Equals(fp4)); // Bits forth Assert.True(fp1.Equals(fp2)); Assert.True(fp2.Equals(fp1)); fp1.Touch(32); Assert.False(fp1.Equals(fp2)); Assert.False(fp2.Equals(fp1)); fp2.Touch(32); Assert.True(fp1.Equals(fp2)); Assert.True(fp2.Equals(fp1)); // Length <= 32 var fp5 = new Fingerprint(7); var fp6 = new Fingerprint(7); fp5.Touch(0); Assert.False(fp5.Equals(fp6)); fp6.Touch(0); Assert.True(fp5.Equals(fp6)); }
/// <inheritdoc /> public override Result <IReadOnlyList <Selector> > GetSelectors(OperationContext context, Fingerprint weakFingerprint) { throw new NotImplementedException(); }
/// <summary> /// <see cref="ILevelSelectorsProvider.GetLevelSelectorsAsync(Context, Fingerprint, CancellationToken, int)"/> /// </summary> public Task <Result <LevelSelectors> > GetLevelSelectorsAsync(OperationContext context, Fingerprint weakFingerprint, int level) { return(context.PerformOperationAsync(Tracer, () => GetLevelSelectorsCoreAsync(context, weakFingerprint, level), extraEndMessage: _ => $"WeakFingerprint=[{weakFingerprint}], Level=[{level}]", traceOperationStarted: false, traceErrorsOnly: true)); }
public override int GetHashCode() { int hashCode = 424336074; if (Id != null) { hashCode += Id.GetHashCode(); } if (AccountNumberSuffix != null) { hashCode += AccountNumberSuffix.GetHashCode(); } if (Country != null) { hashCode += Country.GetHashCode(); } if (Currency != null) { hashCode += Currency.GetHashCode(); } if (AccountType != null) { hashCode += AccountType.GetHashCode(); } if (HolderName != null) { hashCode += HolderName.GetHashCode(); } if (PrimaryBankIdentificationNumber != null) { hashCode += PrimaryBankIdentificationNumber.GetHashCode(); } if (SecondaryBankIdentificationNumber != null) { hashCode += SecondaryBankIdentificationNumber.GetHashCode(); } if (DebitMandateReferenceId != null) { hashCode += DebitMandateReferenceId.GetHashCode(); } if (ReferenceId != null) { hashCode += ReferenceId.GetHashCode(); } if (LocationId != null) { hashCode += LocationId.GetHashCode(); } if (Status != null) { hashCode += Status.GetHashCode(); } hashCode += Creditable.GetHashCode(); hashCode += Debitable.GetHashCode(); if (Fingerprint != null) { hashCode += Fingerprint.GetHashCode(); } if (Version != null) { hashCode += Version.GetHashCode(); } if (BankName != null) { hashCode += BankName.GetHashCode(); } return(hashCode); }
/// <inheritdoc /> protected override Task <Result <LevelSelectors> > GetLevelSelectorsCoreAsync(OperationContext context, Fingerprint weakFingerprint, int level) { return(_store.GetLevelSelectorsAsync(context, weakFingerprint, level)); }
/// <inheritdoc /> public Task <BlobSelectorsResponse> GetSelectors(string cacheNamespace, Fingerprint weakFingerprint) { var queryParameters = new Dictionary <string, string>(); return(GetSelectorsInternal(cacheNamespace, weakFingerprint, queryParameters)); }
/// <inheritdoc /> public async Task <Result <LevelSelectors> > GetLevelSelectorsAsync(Context context, Fingerprint weakFingerprint, CancellationToken cts, int level) { if (_innerCacheSession is IReadOnlyMemoizationSessionWithLevelSelectors withLevelSelectors) { var result = await MetadataCache.GetOrAddSelectorsAsync( context, weakFingerprint, fingerprint => withLevelSelectors.GetAllSelectorsAsync(context, weakFingerprint, cts)); return(LevelSelectors.Single(result)); } throw new NotSupportedException($"Inner store {_innerCacheSession.GetType().Name} does not support GetLevelSelectors functionality."); }
/// <inheritdoc /> public Task <Result <LevelSelectors> > GetLevelSelectorsAsync(Context context, Fingerprint weakFingerprint, CancellationToken cts, int level) { return(Task.FromResult(LevelSelectors.Single(MemoizationStore.GetSelectorsCore(context, weakFingerprint, cts)))); }
/// <nodoc/> internal CalculatedFingerprintTuple(Fingerprint fingerprint, string fingerprintText) { Fingerprint = fingerprint; FingerprintText = fingerprintText; }
public override Fingerprint GetFingerprint(IBuildContext ctxt, Fingerprint cached) { return(GenericFingerprints.Null); }
/// <summary> /// Overridden by subclasses to build an equivalence test chain. /// </summary> protected virtual bool IsEquivalent(Cell other, Fingerprint fingerprint) { return true; }
public override int GetHashCode(Fingerprint fingerprint) { var hash = new Hash(base.GetHashCode(fingerprint)); if (fingerprint.Length <= tag.Offset) { return hash.Code; } var touched = new Capo<bool>(fingerprint, tag.Offset); if (touched[0]) { hash.Update(linkName); } if (touched[1]) { hash.Update(result); } if (touched[2]) { hash.Update(context); } return hash.Code; }
public FingerprintService() { _fp = new Fingerprint(); }
public void TestNegativeLength() { Fingerprint fp = new Fingerprint(-1); }
/// <inheritdoc /> public Async::System.Collections.Generic.IAsyncEnumerable <GetSelectorResult> GetSelectors(Context context, Fingerprint weakFingerprint, CancellationToken cts, UrgencyHint urgencyHint = UrgencyHint.Nominal) { throw new System.NotImplementedException(); }
public void TestCreation() { var fp1 = new Fingerprint(1); Assert.AreEqual(1, fp1.Length); Assert.False(fp1.Get(0)); Fingerprint fp2 = new Fingerprint(33); Assert.AreEqual(33, fp2.Length); for (int i = 0; i < 33; ++i) { Assert.False(fp2.Get(i)); } }
public void TestNegativeLength() { Assert.Throws <ArgumentOutOfRangeException>(() => { var fp = new Fingerprint(-1); }); }
public void TestGetLength() { var fp1 = new Fingerprint(99); Assert.AreEqual(99, fp1.Length); var blen = fp1.GetLength(); var lengthInBytes = (((fp1.Length - 1) >> 3) + 1); var expectedBytesLen = lengthInBytes + Serializer.GetLengthVariableNonnegative(99); Assert.AreEqual(expectedBytesLen, blen); }
/// <inheritdoc /> protected override Task <Result <LevelSelectors> > GetLevelSelectorsCoreAsync(OperationContext context, Fingerprint weakFingerprint, int level) { return(Task.FromResult(LevelSelectors.Single(Database.GetSelectors(context, weakFingerprint)))); }
public void TestProcessExecutionResultSerialization() { var reportedAccess = CreateRandomReportedFileAccess(); Fingerprint fingerprint = FingerprintUtilities.CreateRandom(); var processExecutionResult = ExecutionResult.CreateSealed( result: PipResultStatus.Succeeded, numberOfWarnings: 12, outputContent: ReadOnlyArray <(FileArtifact, FileMaterializationInfo, PipOutputOrigin)> .FromWithoutCopy(CreateRandomOutputContent(), CreateRandomOutputContent()), directoryOutputs: ReadOnlyArray <(DirectoryArtifact, ReadOnlyArray <FileArtifactWithAttributes>)> .FromWithoutCopy(CreateRandomOutputDirectory(), CreateRandomOutputDirectory()), performanceInformation: new ProcessPipExecutionPerformance( PipExecutionLevel.Executed, DateTime.UtcNow, DateTime.UtcNow + TimeSpan.FromMinutes(2), FingerprintUtilities.ZeroFingerprint, TimeSpan.FromMinutes(2), new FileMonitoringViolationCounters(2, 3, 4), default(IOCounters), TimeSpan.FromMinutes(3), TimeSpan.FromMinutes(3), ProcessMemoryCounters.CreateFromBytes(12324, 12325, 12326, 12326), 33, 7, 0), fingerprint: new WeakContentFingerprint(fingerprint), fileAccessViolationsNotAllowlisted: new[] { reportedAccess, CreateRandomReportedFileAccess(), // Create reported file access that uses the same process to test deduplication during deserialization CreateRandomReportedFileAccess(reportedAccess.Process), }, allowlistedFileAccessViolations: new ReportedFileAccess[0], mustBeConsideredPerpetuallyDirty: true, dynamicObservations: ReadOnlyArray <(AbsolutePath, DynamicObservationKind)> .FromWithoutCopy( (CreateSourceFile().Path, DynamicObservationKind.ObservedFile), (CreateSourceFile().Path, DynamicObservationKind.ObservedFile), (CreateSourceFile().Path, DynamicObservationKind.ProbedFile), (CreateSourceFile().Path, DynamicObservationKind.ProbedFile), (CreateSourceFile().Path, DynamicObservationKind.ProbedFile), (CreateSourceFile().Path, DynamicObservationKind.Enumeration), (CreateSourceFile().Path, DynamicObservationKind.AbsentPathProbeUnderOutputDirectory), (CreateSourceFile().Path, DynamicObservationKind.AbsentPathProbeUnderOutputDirectory) ), allowedUndeclaredSourceReads: new ReadOnlyHashSet <AbsolutePath> { CreateSourceFile().Path, CreateSourceFile().Path }, twoPhaseCachingInfo: new TwoPhaseCachingInfo( new WeakContentFingerprint(Fingerprint.Random(FingerprintUtilities.FingerprintLength)), ContentHashingUtilities.CreateRandom(), new StrongContentFingerprint(Fingerprint.Random(FingerprintUtilities.FingerprintLength)), new CacheEntry(ContentHashingUtilities.CreateRandom(), null, CreateRandomContentHashArray())), pipCacheDescriptorV2Metadata: null, converged: true, pathSet: null, cacheLookupStepDurations: null, pipProperties: new Dictionary <string, int> { { "Foo", 1 }, { "Bar", 9 } }, createdDirectories: new ReadOnlyHashSet <AbsolutePath> { CreateSourceFile().Path }, hasUserRetries: true, exitCode: 0, cacheMissType: PipCacheMissType.Hit); ExecutionResultSerializer serializer = new ExecutionResultSerializer(0, Context); ExecutionResult deserializedProcessExecutionResult; using (var stream = new MemoryStream()) using (var writer = new BuildXLWriter(false, stream, true, false)) using (var reader = new BuildXLReader(false, stream, true)) { serializer.Serialize(writer, processExecutionResult, preservePathCasing: false); var position = stream.Position; stream.Position = 0; deserializedProcessExecutionResult = serializer.Deserialize(reader, processExecutionResult.PerformanceInformation.WorkerId); // make sure we read the same amount of content from the stream we wrote there XAssert.AreEqual(position, stream.Position); } // Ensure successful pip result is changed to not materialized. XAssert.AreEqual(PipResultStatus.NotMaterialized, deserializedProcessExecutionResult.Result); AssertEqual(processExecutionResult, deserializedProcessExecutionResult, r => r.NumberOfWarnings, r => r.Converged, r => r.OutputContent.Length, r => r.DirectoryOutputs.Length, r => r.PerformanceInformation.ExecutionLevel, r => r.PerformanceInformation.ExecutionStop, r => r.PerformanceInformation.ExecutionStart, r => r.PerformanceInformation.ProcessExecutionTime, r => r.PerformanceInformation.FileMonitoringViolations.NumFileAccessViolationsNotAllowlisted, r => r.PerformanceInformation.FileMonitoringViolations.NumFileAccessesAllowlistedAndCacheable, r => r.PerformanceInformation.FileMonitoringViolations.NumFileAccessesAllowlistedButNotCacheable, r => r.PerformanceInformation.UserTime, r => r.PerformanceInformation.KernelTime, r => r.PerformanceInformation.MemoryCounters.PeakWorkingSetMb, r => r.PerformanceInformation.MemoryCounters.AverageWorkingSetMb, r => r.PerformanceInformation.MemoryCounters.PeakCommitSizeMb, r => r.PerformanceInformation.MemoryCounters.AverageCommitSizeMb, r => r.PerformanceInformation.NumberOfProcesses, r => r.FileAccessViolationsNotAllowlisted.Count, r => r.MustBeConsideredPerpetuallyDirty, r => r.DynamicObservations.Length, r => r.AllowedUndeclaredReads.Count, r => r.TwoPhaseCachingInfo.WeakFingerprint, r => r.TwoPhaseCachingInfo.StrongFingerprint, r => r.TwoPhaseCachingInfo.PathSetHash, r => r.TwoPhaseCachingInfo.CacheEntry.MetadataHash, r => r.TwoPhaseCachingInfo.CacheEntry.OriginatingCache, r => r.TwoPhaseCachingInfo.CacheEntry.ReferencedContent.Length, r => r.PipProperties.Count, r => r.HasUserRetries, r => r.CreatedDirectories, r => r.RetryInfo, r => r.ExitCode, r => r.CacheMissType ); for (int i = 0; i < processExecutionResult.OutputContent.Length; i++) { int j = i; AssertEqual(processExecutionResult, deserializedProcessExecutionResult, r => r.OutputContent[j].Item1, r => r.OutputContent[j].Item2 ); // Ensure that output origin from deserialzed output content is changed to not materialized. XAssert.AreEqual(PipOutputOrigin.NotMaterialized, deserializedProcessExecutionResult.OutputContent[i].Item3); } for (int i = 0; i < processExecutionResult.DirectoryOutputs.Length; i++) { var expected = processExecutionResult.DirectoryOutputs[i]; var result = deserializedProcessExecutionResult.DirectoryOutputs[i]; XAssert.AreEqual(expected.Item1, result.Item1); XAssert.AreEqual(expected.Item2.Length, result.Item2.Length); for (int j = 0; j < expected.Item2.Length; j++) { XAssert.AreEqual(expected.Item2[j], result.Item2[j]); } } for (int i = 0; i < processExecutionResult.FileAccessViolationsNotAllowlisted.Count; i++) { // Compare individual fields for ReportedFileAccess since it uses reference // equality for reported process which would not work for serialization/deserialization AssertEqual(processExecutionResult.FileAccessViolationsNotAllowlisted[i], deserializedProcessExecutionResult.FileAccessViolationsNotAllowlisted[i]); } // Ensure that reported process instances are deduplicated. XAssert.AreSame(deserializedProcessExecutionResult.FileAccessViolationsNotAllowlisted[0].Process, deserializedProcessExecutionResult.FileAccessViolationsNotAllowlisted[2].Process); for (int i = 0; i < processExecutionResult.DynamicObservations.Length; i++) { AssertEqual(processExecutionResult.DynamicObservations[i], deserializedProcessExecutionResult.DynamicObservations[i]); } XAssert.AreSetsEqual(processExecutionResult.AllowedUndeclaredReads, deserializedProcessExecutionResult.AllowedUndeclaredReads, expectedResult: true); var referencedContentLength = processExecutionResult.TwoPhaseCachingInfo.CacheEntry.ReferencedContent.Length; for (int i = 0; i < referencedContentLength; i++) { XAssert.AreEqual( processExecutionResult.TwoPhaseCachingInfo.CacheEntry.ReferencedContent[i], deserializedProcessExecutionResult.TwoPhaseCachingInfo.CacheEntry.ReferencedContent[i]); } XAssert.AreEqual(9, deserializedProcessExecutionResult.PipProperties["Bar"]); XAssert.AreSetsEqual(processExecutionResult.CreatedDirectories, deserializedProcessExecutionResult.CreatedDirectories, expectedResult: true); }
/// <inheritdoc /> public IAsyncEnumerable <GetSelectorResult> GetSelectors(Context context, Fingerprint weakFingerprint, CancellationToken cts, UrgencyHint urgencyHint = UrgencyHint.Nominal) { return(this.GetSelectorsAsAsyncEnumerable(context, weakFingerprint, cts, urgencyHint)); }
public override int GetHashCode(Fingerprint fingerprint) { var hash = new Hash(base.GetHashCode(fingerprint)); if (fingerprint.Length <= tag.Offset) { return hash.Code; } var touched = new Capo<bool>(fingerprint, tag.Offset); if (touched[0]) { hash.Update(tag.Offset + 0); hash.Update(key_); } if (touched[1]) { hash.Update(tag.Offset + 1); hash.Update(intParam_); } return hash.Code; }
/// <inheritdoc /> public async Task <Result <LevelSelectors> > GetLevelSelectorsAsync(Context context, Fingerprint weakFingerprint, CancellationToken cts, int level) { return(LevelSelectors.Single(await MemoizationStore.GetSelectorsCoreAsync(context, weakFingerprint))); }
/// <summary> /// Determines whether the specified Cell object is equivalent to this /// one based on the given fingerprint. /// </summary> public bool Equivalent(Cell other, Fingerprint fingerprint) { if (!other.IsKindOf(this)) { return false; } if (!fingerprint.Equivalent(other.fingerprint)) { return false; } return IsEquivalent(other, fingerprint); }
public void Validate() { long i = 0; m_Status = "Validating"; LoadLog(); foreach (Fingerprint ManifestFingerprint in m_ManifestFileList) { if (Kill) { return; } i++; m_current = ManifestFingerprint.FullName; ProgressEventArgs e = new ProgressEventArgs(i, m_ManifestFileList.Count); Fingerprint LocalFingerprint; if (!ManifestFingerprint.PathIsSafe) { // This manifest entry sets off our path traversal detection! // 1. Don't attempt to download it, delete it, etc. // 2. Write a log entry about it. MyToolkit.ActivityLog("Manifest contains path traversal! File named '" + ManifestFingerprint.FileName + "' would be written to '" + ManifestFingerprint.AbsolutePath + "'. Skipping."); } else if (ManifestFingerprint.Size == 0) { // File is to be deleted if (System.IO.File.Exists(ManifestFingerprint.FullName)) { System.IO.File.Delete(ManifestFingerprint.FullName); } } else if (System.IO.File.Exists(ManifestFingerprint.FullName)) { // File exists locally, lets start verifying it. First check if // the checksum matches the one in our last run log, if so there // is no need to download the file nor do a checksum if (AlreadyVerified(ManifestFingerprint.FileName, ManifestFingerprint.Size, ManifestFingerprint.Checksum)) { FlagVerified(ManifestFingerprint.FileName, ManifestFingerprint.Size, ManifestFingerprint.Checksum); } else { // Get an md5 checksum for the local copy of the file LocalFingerprint = new Fingerprint(ManifestFingerprint.RootPath, ManifestFingerprint.FileName); if (LocalFingerprint.Equals(ManifestFingerprint)) { FlagVerified(ManifestFingerprint.FileName, ManifestFingerprint.Size, ManifestFingerprint.Checksum); } else { // There was no match, lets add the file to our download queue AddToDownloadQueue(ManifestFingerprint); } } } else { // File does not exist locally, we must download it. Add to our download queue. AddToDownloadQueue(ManifestFingerprint); } m_progress = (int)(Math.Round((i / 100.0f) * 100.0f)); } DownloadFiles(); }
/// <summary> /// Sets the fingerprint of this cell as the specified one. /// </summary> internal void SetFingerprint(Fingerprint fingerprint) { this.fingerprint = fingerprint; }
public void DownloadFiles() { foreach (Fingerprint file in m_DownloadQueue) { if (Kill) { return; } HTTP client = new HTTP(); bool keepTrying = true; string DownloadURL = file.DownloadURL;; while (keepTrying) { try { MyToolkit.ActivityLog("Downloading file \"" + file.FullName + "\" from \"" + DownloadURL + "\""); if (client.StartDownload(new AsyncCompletedEventHandler(DownloadFileComplete), new DownloadProgressChangedEventHandler(dlProgress), DownloadURL, file.FullName + ".download")) { m_Status = "Downloading"; m_DownloadActive = true; } } catch (Exception ex) { string er = ex.Message; } m_current = file.FullName; while (client.Active) { if (Kill) { client.CancelDownload(); return; } System.Threading.Thread.Sleep(10); } Fingerprint Downloaded = new Fingerprint(file.RootPath, file.FileName + ".download"); if (!Downloaded.Equals(file)) { // OK this file is no good, delete it. File.Delete(file.FullName + ".download"); // lets try a different url... DownloadURL = file.DownloadURL; // Did we get a blank URL? if (DownloadURL == "") { MyToolkit.ActivityLog("Download failed, no more URL's to try from"); // OK stop trying and report error... keepTrying = false; string Msg = "Download error: " + file.FileName; if (Downloaded.Size == 0) { Msg += "\r\nWas unable to download file"; } else { if (Downloaded.Size != file.Size) { Msg += "\r\nSize mismatch (" + Downloaded.Size + " vs " + file.Size + ")"; } if (Downloaded.Checksum != file.Checksum) { Msg += "\r\nChecksum Mismatch (" + Downloaded.Checksum + " vs " + file.Checksum + ")"; } } if (file.Warn) { m_ErrorLog.Add(Msg); } else { m_WarningLog.Add(Msg); } } else { MyToolkit.ActivityLog("Download failed, trying from a different URL"); } } else { if (File.Exists(file.FullName)) { File.SetAttributes(file.FullName, File.GetAttributes(file.FullName) & ~FileAttributes.ReadOnly); File.Delete(file.FullName); } // We are done, we dont need to keep trying (infinite loop if we dont set this) keepTrying = false; File.Move(file.FullName + ".download", file.FullName); FlagVerified(file.FullName, file.Size, file.Checksum); } } m_Downloaded += file.Size; } m_Status = "Done"; m_current = ""; }
/// <summary> /// Initializes a new Cell instance with the given fingerprint length. /// </summary> protected Cell(int length) { fingerprint = new Fingerprint(length); }
void ManifestDownloadComplete(object sender, AsyncCompletedEventArgs e) { // Check if we had any HTTP download errors if (e != null) { if (e.Error != null) { MyToolkit.ActivityLog("Manifest download error for " + ManifestURL + "\r\n" + e.Error.Message); m_ErrorLog.Add("Manifest download error for " + ManifestURL + "\r\n" + e.Error.Message); m_Status = "Done"; return; } } // Check if the downloaded file is where it should be if (!File.Exists(LocalManifest)) { MyToolkit.ActivityLog("Error downloading manifest, download complete but no file found locally."); m_ErrorLog.Add("Error downloading manifest"); m_Status = "Done"; return; } // Make certain the downloaded manifest and the one we // requested match in size FileInfo dlInfo = new FileInfo(LocalManifest); if (dlInfo.Length != client.Length) { MyToolkit.ActivityLog("Error downloading manifest, downloaded file not the right size. Expected: " + dlInfo.Length + " received: " + client.Length); m_ErrorLog.Add("Error downloading manifest"); m_Status = "Done"; return; } // We got a manifest, lets start reading through it m_current = ""; MyToolkit.ActivityLog("Manifest downloaded successfully, starting to process it."); m_ManifestFileList = new ArrayList(); try { m_manifest = XElement.Load(LocalManifest); // try to get the forum URL IEnumerable <XElement> forumLinks = m_manifest.Descendants("webpage"); foreach (XElement forumLink in forumLinks) { ForumURL = forumLink.Value; break; } SelfPatch(); m_Status = "Reading manifest"; IEnumerable <XElement> files = m_manifest.Descendants("file"); foreach (XElement file in files) { if (Kill) { return; } // Lets get this file's manifest information long size; bool parseSucceed = long.TryParse(file.Attribute("size").Value.ToString(), out size); bool Warn = true; if (file.Attribute("warn") != null) { if (file.Attribute("warn").Value == "no") { Warn = false; } } string md5 = file.Attribute("md5").Value; string fileName = file.Attribute("name").Value; if (fileName.Trim() != "") { Fingerprint ManifestFingerprint = new Fingerprint(PathRoot, fileName, md5, size); ManifestFingerprint.Warn = Warn; IEnumerable <XElement> URLs = file.Descendants("url"); foreach (XElement URL in URLs) { ManifestFingerprint.AddDownloadURL(URL.Value.ToString().Trim()); } m_ManifestFileList.Add(ManifestFingerprint); } } m_progress = 0; m_Status = "Verifying"; myWorkThread = new Thread(new ThreadStart(Validate)); myWorkThread.Start(); } catch (Exception ex) { MessageBox.Show(ex.Message, "WorkThread.ManifestDownloadComplete()"); string a = ex.Message; } }
protected override bool IsEquivalent(Cell other, Fingerprint fingerprint) { if (!base.IsEquivalent(other, fingerprint)) { return false; } HelloResp o = (HelloResp)other; var touched = new Capo<bool>(fingerprint, tag.Offset); if (touched[0]) { if (result_ != o.result_) { return false; } } return true; }
void SelfPatch() { try { Fingerprint myFingerprint = new Fingerprint(Settings.GamePath, "Tequila.exe"); if (DontSelfUpdate) { return; } MyToolkit.ActivityLog("Starting self-patch process."); // Before we go far... lets see if there are any old temp files hanging around and get rid of them string[] oldFiles = Directory.GetFiles(Settings.GamePath, "*.old"); foreach (string oldFile in oldFiles) { try { File.Delete(oldFile); } catch (Exception ex) { } } // OK now thats out of the way, lets determine if we need to self patch or not!! IEnumerable <XElement> launchers = m_manifest.Descendants("launcher"); m_Status = "Self patching"; foreach (XElement launcher in launchers) { if (launcher.Attribute("id").Value == "tequila") { long size = 0; long.TryParse(launcher.Attribute("size").Value.ToString(), out size); string md5 = launcher.Attribute("md5").Value; Fingerprint remoteLauncher = new Fingerprint(Settings.GamePath, "Tequila.exe", md5, size); if (!myFingerprint.Equals(remoteLauncher)) { MyToolkit.ActivityLog("Patcher out of date..."); // We need to update!!! yay... IEnumerable <XElement> urls = launcher.Descendants("url"); // Get every possible download URL into the remoteLauncher fingerprint foreach (XElement url in urls) { remoteLauncher.AddDownloadURL(url.Value); } // Start the download process HTTP selfPatcherClient = new HTTP(); m_DownloadSize = remoteLauncher.Size; string downloadURL = remoteLauncher.DownloadURL; MyToolkit.ActivityLog("Downloading new version from \"" + downloadURL + "\""); if (selfPatcherClient.StartDownload(new AsyncCompletedEventHandler(DownloadFileComplete), new DownloadProgressChangedEventHandler(dlProgress), downloadURL, remoteLauncher.FullName + ".download")) { m_Status = "Downloading"; m_DownloadActive = true; } m_current = remoteLauncher.FullName; // Wait until download is complete MyToolkit.ActivityLog("Waiting for patcher download to complete..."); while (selfPatcherClient.Active) { if (Kill) { selfPatcherClient.CancelDownload(); return; } System.Threading.Thread.Sleep(10); } m_DownloadActive = false; MyToolkit.ActivityLog("New patcher version downloaded..."); // Make sure the downloaded file is not corrupted Fingerprint downloadedFile = new Fingerprint(remoteLauncher.RootPath, remoteLauncher.FileName + ".download"); if (!downloadedFile.Equals(remoteLauncher)) { string error = "Was unable to self patch. Downloaded file did not match expected checksum."; error += "\r\n" + remoteLauncher.FileName + "\r\n md5: " + remoteLauncher.Checksum + " vs " + downloadedFile.Checksum + "\r\n size: " + remoteLauncher.Size + " vs " + downloadedFile.Size; MyToolkit.ActivityLog(error); File.Delete(downloadedFile.FullName + ".download"); m_ErrorLog.Add(error); m_Status = "Done"; return; } else { // Find an available _#.old file name long i = 0; string TrashName = myFingerprint.FullName + "_"; while (File.Exists(TrashName + i.ToString() + ".old")) { i++; } TrashName = TrashName + i.ToString() + ".old"; File.Move(myFingerprint.FullName, TrashName); File.Move(myFingerprint.FullName + ".download", myFingerprint.FullName); var startInfo = new ProcessStartInfo(); startInfo.FileName = myFingerprint.FullName; startInfo.Arguments = MyToolkit.AllArgs(); MyToolkit.ActivityLog("Tequila has been patched successfuly. Restarting."); Process.Start(startInfo); Application.Exit(); return; } } } } MyToolkit.ActivityLog("Self patching process complete."); } catch (Exception ex) { MessageBox.Show(ex.Message, "WorkThread.SelfPatch()"); } }
internal async Task UploadAsync( AgentTaskPluginExecutionContext context, Fingerprint fingerprint, string path, CancellationToken cancellationToken, string preferredContentFormat = ContentFormatConstants.Files) { VssConnection connection = context.VssConnection; BlobStoreClientTelemetry clientTelemetry; DedupManifestArtifactClient dedupManifestClient = DedupManifestArtifactClientFactory.Instance.CreateDedupManifestClient(context, connection, cancellationToken, out clientTelemetry); PipelineCacheClient pipelineCacheClient = this.CreateClient(clientTelemetry, context, connection); using (clientTelemetry) { // Check if the key exists. PipelineCacheActionRecord cacheRecordGet = clientTelemetry.CreateRecord <PipelineCacheActionRecord>((level, uri, type) => new PipelineCacheActionRecord(level, uri, type, PipelineArtifactConstants.RestoreCache, context)); PipelineCacheArtifact getResult = await pipelineCacheClient.GetPipelineCacheArtifactAsync(new [] { fingerprint }, cancellationToken, cacheRecordGet); // Send results to CustomerIntelligence context.PublishTelemetry(area: PipelineArtifactConstants.AzurePipelinesAgent, feature: PipelineArtifactConstants.PipelineCache, record: cacheRecordGet); //If cache exists, return. if (getResult != null) { context.Output($"Cache with fingerprint `{getResult.Fingerprint}` already exists."); return; } string uploadPath = await this.GetUploadPathAsync(preferredContentFormat, context, path, cancellationToken); //Upload the pipeline artifact. PipelineCacheActionRecord uploadRecord = clientTelemetry.CreateRecord <PipelineCacheActionRecord>((level, uri, type) => new PipelineCacheActionRecord(level, uri, type, nameof(dedupManifestClient.PublishAsync), context)); PublishResult result = await clientTelemetry.MeasureActionAsync( record : uploadRecord, actionAsync : async() => { return(await dedupManifestClient.PublishAsync(uploadPath, cancellationToken)); }); CreatePipelineCacheArtifactContract options = new CreatePipelineCacheArtifactContract { Fingerprint = fingerprint, RootId = result.RootId, ManifestId = result.ManifestId, ProofNodes = result.ProofNodes.ToArray(), ContentFormat = preferredContentFormat }; // delete archive file if it's tar. if (string.Equals(preferredContentFormat, ContentFormatConstants.SingleTar, StringComparison.OrdinalIgnoreCase)) { try { if (File.Exists(uploadPath)) { File.Delete(uploadPath); } } catch { } } // Cache the artifact PipelineCacheActionRecord cacheRecord = clientTelemetry.CreateRecord <PipelineCacheActionRecord>((level, uri, type) => new PipelineCacheActionRecord(level, uri, type, PipelineArtifactConstants.SaveCache, context)); CreateStatus status = await pipelineCacheClient.CreatePipelineCacheArtifactAsync(options, cancellationToken, cacheRecord); // Send results to CustomerIntelligence context.PublishTelemetry(area: PipelineArtifactConstants.AzurePipelinesAgent, feature: PipelineArtifactConstants.PipelineCache, record: uploadRecord); context.PublishTelemetry(area: PipelineArtifactConstants.AzurePipelinesAgent, feature: PipelineArtifactConstants.PipelineCache, record: cacheRecord); context.Output("Saved item."); } }
/// <summary> /// Creates a content fingerprint from the given hash. /// The hash should have been constructed according to the definition of a strong content fingerprint. /// </summary> public StrongContentFingerprint(Fingerprint hash) { Contract.Requires(hash.Length == FingerprintUtilities.FingerprintLength); Hash = hash; }
public void TestSerialization() { Fingerprint fp1 = new Fingerprint(65); Fingerprint fp2 = new Fingerprint(65); fp1.Touch(0); fp1.Touch(2); fp1.Touch(31); fp1.Touch(32); Assert.False(fp2.Equals(fp1)); /* Buffer buffer = new Buffer(12); fp1.Dump(buffer); buffer.Rewind(); fp2.Load(buffer); Assert.True(fp2.Equals(fp1)); */ }
private string GetKey(Fingerprint weakFingerprint) => $"WF_{weakFingerprint.Serialize()}";
public void TestCopyCreation() { Fingerprint fp1 = new Fingerprint(65); fp1.Touch(32); Fingerprint fp2 = new Fingerprint(fp1); Assert.True(fp2.Get(32)); // Ensure that the original block array is not shared fp1.Touch(64); Assert.False(fp2.Get(64)); }
public async Task <IActionResult> CreatePSBT( [ModelBinder(BinderType = typeof(NetworkModelBinder))] NBXplorerNetwork network, [ModelBinder(BinderType = typeof(DerivationStrategyModelBinder))] DerivationStrategyBase strategy, [FromBody] JObject body) { if (body == null) { throw new ArgumentNullException(nameof(body)); } CreatePSBTRequest request = ParseJObject <CreatePSBTRequest>(body, network); if (strategy == null) { throw new ArgumentNullException(nameof(strategy)); } var repo = RepositoryProvider.GetRepository(network); var txBuilder = request.Seed is int s?network.NBitcoinNetwork.CreateTransactionBuilder(s) : network.NBitcoinNetwork.CreateTransactionBuilder(); CreatePSBTSuggestions suggestions = null; if (!(request.DisableFingerprintRandomization is true)) { suggestions ??= new CreatePSBTSuggestions(); var distribution = fingerprintService.GetDistribution(network); var known = new List <(Fingerprint feature, bool value)>(); if (request.RBF is bool rbf) { known.Add((Fingerprint.RBF, rbf)); } if (request.DiscourageFeeSniping is bool feeSnipping) { known.Add((Fingerprint.FeeSniping, feeSnipping)); } if (request.LockTime is LockTime l) { if (l == LockTime.Zero) { known.Add((Fingerprint.TimelockZero, true)); } } if (request.Version is uint version) { if (version == 1) { known.Add((Fingerprint.V1, true)); } if (version == 2) { known.Add((Fingerprint.V2, true)); } } known.Add((Fingerprint.SpendFromMixed, false)); known.Add((Fingerprint.SequenceMixed, false)); if (strategy is DirectDerivationStrategy direct) { if (direct.Segwit) { known.Add((Fingerprint.SpendFromP2WPKH, true)); } else { known.Add((Fingerprint.SpendFromP2PKH, true)); } } else { // TODO: What if multisig? For now we consider it p2wpkh known.Add((Fingerprint.SpendFromP2SHP2WPKH, true)); } Fingerprint fingerprint = distribution.PickFingerprint(txBuilder.ShuffleRandom); try { fingerprint = distribution.KnowingThat(known.ToArray()) .PickFingerprint(txBuilder.ShuffleRandom); } catch (InvalidOperationException) { } request.RBF ??= fingerprint.HasFlag(Fingerprint.RBF); request.DiscourageFeeSniping ??= fingerprint.HasFlag(Fingerprint.FeeSniping); if (request.LockTime is null && fingerprint.HasFlag(Fingerprint.TimelockZero)) { request.LockTime = new LockTime(0); } if (request.Version is null && fingerprint.HasFlag(Fingerprint.V1)) { request.Version = 1; } if (request.Version is null && fingerprint.HasFlag(Fingerprint.V2)) { request.Version = 2; } suggestions.ShouldEnforceLowR = fingerprint.HasFlag(Fingerprint.LowR); } var waiter = Waiters.GetWaiter(network); if (waiter.NetworkInfo?.GetRelayFee() is FeeRate feeRate) { txBuilder.StandardTransactionPolicy.MinRelayTxFee = feeRate; } txBuilder.OptInRBF = !(request.RBF is false); if (request.LockTime is LockTime lockTime) { txBuilder.SetLockTime(lockTime); } // Discourage fee sniping. // // For a large miner the value of the transactions in the best block and // the mempool can exceed the cost of deliberately attempting to mine two // blocks to orphan the current best block. By setting nLockTime such that // only the next block can include the transaction, we discourage this // practice as the height restricted and limited blocksize gives miners // considering fee sniping fewer options for pulling off this attack. // // A simple way to think about this is from the wallet's point of view we // always want the blockchain to move forward. By setting nLockTime this // way we're basically making the statement that we only want this // transaction to appear in the next block; we don't want to potentially // encourage reorgs by allowing transactions to appear at lower heights // than the next block in forks of the best chain. // // Of course, the subsidy is high enough, and transaction volume low // enough, that fee sniping isn't a problem yet, but by implementing a fix // now we ensure code won't be written that makes assumptions about // nLockTime that preclude a fix later. else if (!(request.DiscourageFeeSniping is false)) { if (waiter.State is BitcoinDWaiterState.Ready) { int blockHeight = ChainProvider.GetChain(network).Height; // Secondly occasionally randomly pick a nLockTime even further back, so // that transactions that are delayed after signing for whatever reason, // e.g. high-latency mix networks and some CoinJoin implementations, have // better privacy. if (txBuilder.ShuffleRandom.Next(0, 10) == 0) { blockHeight = Math.Max(0, blockHeight - txBuilder.ShuffleRandom.Next(0, 100)); } txBuilder.SetLockTime(new LockTime(blockHeight)); } else { txBuilder.SetLockTime(new LockTime(0)); } } var utxos = (await GetUTXOs(network.CryptoCode, strategy, null)).As <UTXOChanges>().GetUnspentCoins(request.MinConfirmations); var availableCoinsByOutpoint = utxos.ToDictionary(o => o.Outpoint); if (request.IncludeOnlyOutpoints != null) { var includeOnlyOutpoints = request.IncludeOnlyOutpoints.ToHashSet(); availableCoinsByOutpoint = availableCoinsByOutpoint.Where(c => includeOnlyOutpoints.Contains(c.Key)).ToDictionary(o => o.Key, o => o.Value); } if (request.ExcludeOutpoints?.Any() is true) { var excludedOutpoints = request.ExcludeOutpoints.ToHashSet(); availableCoinsByOutpoint = availableCoinsByOutpoint.Where(c => !excludedOutpoints.Contains(c.Key)).ToDictionary(o => o.Key, o => o.Value); } if (request.MinValue != null) { availableCoinsByOutpoint = availableCoinsByOutpoint.Where(c => request.MinValue >= c.Value.Amount).ToDictionary(o => o.Key, o => o.Value); } txBuilder.AddCoins(availableCoinsByOutpoint.Values); foreach (var dest in request.Destinations) { if (dest.SweepAll) { try { txBuilder.SendAll(dest.Destination); } catch { throw new NBXplorerException(new NBXplorerError(400, "not-enough-funds", "You can't sweep funds, because you don't have any.")); } } else { txBuilder.Send(dest.Destination, dest.Amount); if (dest.SubstractFees) { try { txBuilder.SubtractFees(); } catch { throw new NBXplorerException(new NBXplorerError(400, "not-enough-funds", "You can't substract fee on this destination, because not enough money was sent to it")); } } } } (Script ScriptPubKey, KeyPath KeyPath)change = (null, null); bool hasChange = false; if (request.ExplicitChangeAddress == null) { var keyInfo = await repo.GetUnused(strategy, DerivationFeature.Change, 0, false); change = (keyInfo.ScriptPubKey, keyInfo.KeyPath); } else { // The provided explicit change might have a known keyPath, let's change for it KeyPath keyPath = null; var keyInfos = await repo.GetKeyInformations(new[] { request.ExplicitChangeAddress.ScriptPubKey }); if (keyInfos.TryGetValue(request.ExplicitChangeAddress.ScriptPubKey, out var kis)) { keyPath = kis.FirstOrDefault(k => k.DerivationStrategy == strategy)?.KeyPath; } change = (request.ExplicitChangeAddress.ScriptPubKey, keyPath); } txBuilder.SetChange(change.ScriptPubKey); PSBT psbt = null; try { if (request.FeePreference?.ExplicitFeeRate is FeeRate explicitFeeRate) { txBuilder.SendEstimatedFees(explicitFeeRate); } else if (request.FeePreference?.BlockTarget is int blockTarget) { try { var rate = await GetFeeRate(blockTarget, network.CryptoCode); txBuilder.SendEstimatedFees(rate.FeeRate); } catch (NBXplorerException e) when(e.Error.Code == "fee-estimation-unavailable" && request.FeePreference?.FallbackFeeRate is FeeRate fallbackFeeRate) { txBuilder.SendEstimatedFees(fallbackFeeRate); } } else if (request.FeePreference?.ExplicitFee is Money explicitFee) { txBuilder.SendFees(explicitFee); } else { try { var rate = await GetFeeRate(1, network.CryptoCode); txBuilder.SendEstimatedFees(rate.FeeRate); } catch (NBXplorerException e) when(e.Error.Code == "fee-estimation-unavailable" && request.FeePreference?.FallbackFeeRate is FeeRate fallbackFeeRate) { txBuilder.SendEstimatedFees(fallbackFeeRate); } } psbt = txBuilder.BuildPSBT(false); hasChange = psbt.Outputs.Any(o => o.ScriptPubKey == change.ScriptPubKey); } catch (NotEnoughFundsException) { throw new NBXplorerException(new NBXplorerError(400, "not-enough-funds", "Not enough funds for doing this transaction")); } // We made sure we can build the PSBT, so now we can reserve the change address if we need to if (hasChange && request.ExplicitChangeAddress == null && request.ReserveChangeAddress) { var derivation = await repo.GetUnused(strategy, DerivationFeature.Change, 0, true); // In most of the time, this is the same as previously, so no need to rebuild PSBT if (derivation.ScriptPubKey != change.ScriptPubKey) { change = (derivation.ScriptPubKey, derivation.KeyPath); txBuilder.SetChange(change.ScriptPubKey); psbt = txBuilder.BuildPSBT(false); } } var tx = psbt.GetOriginalTransaction(); if (request.Version is uint v) { tx.Version = v; } psbt = txBuilder.CreatePSBTFrom(tx, false, SigHash.All); var update = new UpdatePSBTRequest() { DerivationScheme = strategy, PSBT = psbt, RebaseKeyPaths = request.RebaseKeyPaths }; await UpdatePSBTCore(update, network); var resp = new CreatePSBTResponse() { PSBT = update.PSBT, ChangeAddress = hasChange ? change.ScriptPubKey.GetDestinationAddress(network.NBitcoinNetwork) : null, Suggestions = suggestions }; return(Json(resp, network.JsonSerializerSettings)); }
public static Session ToSession(this Tharga.Quilt4Net.DataTransfer.Session item, Fingerprint applicationVersionId, Guid applicationId, DateTime serverStartTime, DateTime?serverEndTime, DateTime?serverLastKnown, string callerIp) { var machineId = (Fingerprint)item.Machine.Fingerprint; var userId = (Fingerprint)item.User.Fingerprint; return(new Session(item.SessionGuid, applicationVersionId, item.Environment, applicationId, machineId, userId, item.ClientStartTime, serverStartTime, serverEndTime, serverLastKnown, callerIp)); }
// FIXME: return fingerprint of resource stream somehow public override Fingerprint GetFingerprint (IBuildContext ctxt, Fingerprint cached) { return GenericFingerprints.Null; }
public void TestEquivalence() { Fingerprint fp1 = new Fingerprint(65); Fingerprint fp2 = new Fingerprint(65); Fingerprint fp3 = new Fingerprint(64); Fingerprint fp4 = new Fingerprint(66); // Reference first Assert.True(fp1.Equivalent(fp1)); Assert.True(fp2.Equivalent(fp2)); Assert.True(fp3.Equivalent(fp3)); Assert.True(fp4.Equivalent(fp4)); // Length second Assert.True(fp3.Equivalent(fp1)); Assert.False(fp1.Equivalent(fp3)); Assert.False(fp4.Equivalent(fp2)); Assert.True(fp2.Equivalent(fp4)); // Bits third Assert.True(fp1.Equivalent(fp2)); Assert.True(fp2.Equivalent(fp1)); fp1.Touch(32); Assert.False(fp1.Equivalent(fp2)); Assert.True(fp2.Equivalent(fp1)); fp2.Touch(32); Assert.True(fp1.Equivalent(fp2)); Assert.True(fp2.Equivalent(fp1)); fp2.Touch(31); Assert.True(fp1.Equivalent(fp2)); Assert.False(fp2.Equivalent(fp1)); fp4.Touch(31); fp4.Touch(32); fp4.Touch(33); Assert.True(fp2.Equivalent(fp4)); Assert.False(fp4.Equivalent(fp2)); }
private void ChangeBMP(Bitmap bmp) { picDedo.Image = bmp; picHist.Image = null; if (bmp == null) { return; } bmpLast = new Bitmap(bmp); int width = bmp.Width; int height = bmp.Height; try { var hist = new SortedList <byte, int>(); int max = 0; var img = new List <byte>(); for (int iy = 0; iy < bmp.Height; iy++) { for (int ix = 0; ix < bmp.Width; ix++) { var c = bmp.GetPixel(ix, iy); if (c.R < tr1.Value) { //byte j = (byte)((double)c.R * img / 100); //c = Color.FromArgb(255, j, j, j); c = Color.FromArgb(255, 0, 0, 0); } else if (c.R > tr2.Value) { //byte j = (byte)((double)c.R * (100-img) / 100); //c = Color.FromArgb(255, j, j, j); c = Color.FromArgb(255, 255, 255, 255); } if (!hist.ContainsKey(c.R)) { hist.Add(c.R, 1); } else { hist[c.R]++; } if (hist[c.R] > max) { max = hist[c.R]; } bmp.SetPixel(ix, iy, c); img.Add(c.R); } } //height = image.Length / width; // 83200 //using (var fp = new Fingerprint(image, width, height)) if (chkBitmap.Checked) { using (var fp = new Fingerprint(bmp)) txtOut.Text += string.Format("\r\nBITMAP {0}x{1} Quality: {2}", width, height, fp.Quality); } else { using (var fp = new Fingerprint(img.ToArray(), width, height)) txtOut.Text += string.Format("\r\nRAW IMAGE {0}x{1} Quality: {2}", width, height, fp.Quality); } int pico = max / 10; Bitmap bHist = new Bitmap(hist.Count, pico); Graphics g = Graphics.FromImage(bHist); g.Clear(Color.White); foreach (var k in hist.Keys) { int m = hist[k]; if (m > pico) { m = pico; } g.DrawLine(Pens.Black, k, pico, k, pico - m); txtOut.Text += "\r\n" + k + ": " + hist[k]; } picHist.Image = bHist; picDedo.Image = bmp; } catch (Exception ex) { ShowError(ex); } }
public void TestHashing() { Fingerprint fp1 = new Fingerprint(65); Fingerprint fp2 = new Fingerprint(65); Fingerprint fp3 = new Fingerprint(64); Fingerprint fp4 = new Fingerprint(66); Assert.AreEqual(fp1.GetHashCode(), fp2.GetHashCode()); Assert.AreNotEqual(fp1.GetHashCode(), fp3.GetHashCode()); Assert.AreNotEqual(fp2.GetHashCode(), fp4.GetHashCode()); fp1.Touch(32); Assert.AreNotEqual(fp1.GetHashCode(), fp2.GetHashCode()); fp2.Touch(32); Assert.AreEqual(fp1.GetHashCode(), fp2.GetHashCode()); }
public override int GetHashCode(Fingerprint fingerprint) { var hash = new Hash(base.GetHashCode(fingerprint)); return hash.Code; }
public void TestSerialization() { Fingerprint fp1 = new Fingerprint(65); Fingerprint fp2 = new Fingerprint(65); fp1.Touch(0); fp1.Touch(2); fp1.Touch(31); fp1.Touch(32); Assert.False(fp2.Equals(fp1)); var buffer = new x2.Buffer(); fp1.Serialize(new Serializer(buffer)); buffer.Rewind(); fp2.Deserialize(new Deserializer(buffer)); Assert.True(fp2.Equals(fp1)); }
protected override bool IsEquivalent(Cell other, Fingerprint fingerprint) { if (!base.IsEquivalent(other, fingerprint)) { return false; } return true; }
public byte[] Encode(IStunContext ctx) { bool useMessageIntegrity = !string.IsNullOrEmpty(ctx?.Username) && !string.IsNullOrEmpty(ctx?.Password) && !string.IsNullOrEmpty(ctx?.Realm); var c = (ushort)Class; var m = (ushort)Method; int type = (m & 0x0f80) << 2 | (m & 0x0070) << 1 | (m & 0x000f) << 0 | (c & 0x2) << 7 | (c & 0x1) << 4; using (var ms = new MemoryStream()) { List <Attribute> attrs = Attributes.ToList(); if (!string.IsNullOrEmpty(ctx?.Username)) { attrs.Add(new Username(ctx.Username)); } if (ctx?.Nonce != null) { attrs.Add(new Attributes.Nonce(ctx.Nonce)); } if (!string.IsNullOrEmpty(ctx?.Realm)) { attrs.Add(new Realm(ctx.Realm)); } byte[] encodedAttrs; using (var ams = new MemoryStream()) { foreach (Attribute attr in attrs) { byte[] asBytes = attr.ToByteArray(TransactionId); ams.Write(asBytes, 0, asBytes.Length); } encodedAttrs = ams.ToArray(); } // 8 bytes for Fingerprint var messageLength = (ushort)(encodedAttrs.Length + FingerprintBytes); if (useMessageIntegrity) { messageLength += MessageIntegrityBytes; } ms.Write(((ushort)type).ToBytes(), 0, 2); ms.Write(messageLength.ToBytes(), 0, 2); ms.Write(MagicCookie, 0, MagicCookie.Length); ms.Write(TransactionId, 0, TransactionId.Length); ms.Write(encodedAttrs, 0, encodedAttrs.Length); if (useMessageIntegrity) { var lengthWithoutFingerprint = (ushort)(messageLength - FingerprintBytes); byte[] toCalc = ms.ToArray(); lengthWithoutFingerprint.ToBytes().CopyTo(toCalc, 2); MessageIntegrity mi = MessageIntegrity.Calculate( ctx?.Username, ctx?.Password, ctx?.Realm, toCalc); ms.Write(mi.ToByteArray(), 0, MessageIntegrityBytes); } Fingerprint fingerprint = Fingerprint.FromMessage( ms.ToArray() ); ms.Write(fingerprint.ToByteArray(), 0, FingerprintBytes); return(ms.ToArray()); } }
/// <nodoc /> protected abstract Task <Result <LevelSelectors> > GetLevelSelectorsCoreAsync(OperationContext context, Fingerprint weakFingerprint, int level);
public override int GetHashCode(Fingerprint fingerprint) { return Hash.Update(base.GetHashCode(fingerprint), tag.TypeId); }
internal static IEnumerable <Attribute> ParseAttributes( IEnumerable <byte> bytes, byte[] transactionId = null ) { while (bytes.Any()) { var type = (Attribute.AttributeType)bytes.Take(2).ToUShort(); ushort length = bytes.Skip(2).Take(2).ToUShort(); byte[] payload = bytes.Skip(4).Take(length).ToArray(); switch (type) { case Attribute.AttributeType.ErrorCode: yield return(ErrorCode.Parse(payload)); break; case Attribute.AttributeType.Realm: yield return(Realm.Parse(payload)); break; case Attribute.AttributeType.Nonce: yield return(Stun.Attributes.Nonce.Parse(payload)); break; case Attribute.AttributeType.Software: yield return(Software.Parse(payload)); break; case Attribute.AttributeType.Fingerprint: yield return(Fingerprint.Parse(payload)); break; case Attribute.AttributeType.XorMappedAddress: yield return(XorMappedAddress.Parse( payload, transactionId)); break; case Attribute.AttributeType.XorRelayedAddress: yield return(XorRelayedAddress.Parse( payload, transactionId)); break; case Attribute.AttributeType.ConnectionId: yield return(new ConnectionId(payload)); break; case Attribute.AttributeType.Lifetime: yield return(new Lifetime((int)payload.ToUInt())); break; } // Detect padding var padBytes = (ushort)((4 + length) % 4); if (padBytes > 0) { length += padBytes; } bytes = bytes.Skip(4 + length); } }