private static void TestFingerprintEquality <TStruct>( Func <Fingerprint, TStruct> construct, Func <TStruct, TStruct, bool> eq, Func <TStruct, TStruct, bool> neq) where TStruct : struct, IEquatable <TStruct> { // SHA1 hash of "This is a sacrifice to the code coverage gods" Fingerprint baseValueHash; XAssert.IsTrue(Fingerprint.TryParse("64be6ae7b487e04701056542c4943e3cfe52280f", out baseValueHash)); TStruct baseValue = construct(baseValueHash); // The first four bytes of the hash should be part of the hash code. StructTester.TestEquality( baseValue: baseValue, equalValue: construct(baseValueHash), notEqualValues: Enumerable.Range(0, 4).Select(i => construct(TwiddleHashByte(baseValueHash, i))).ToArray(), eq: eq, neq: neq, skipHashCodeForNotEqualValues: false); // The remaining bytes do not impact the hash code, so we opt out of the hash check. StructTester.TestEquality( baseValue: baseValue, equalValue: construct(baseValueHash), notEqualValues: Enumerable.Range(4, FingerprintUtilities.FingerprintLength - 4).Select(i => construct(TwiddleHashByte(baseValueHash, i))).ToArray(), eq: eq, neq: neq, skipHashCodeForNotEqualValues: true); }
public void TryParseFail(string value) { Fingerprint v; Assert.False(Fingerprint.TryParse(value, out v)); Assert.Equal(0, v.Length); }
public void TryParseSuccess(string value) { Fingerprint v; Assert.True(Fingerprint.TryParse(value, out v)); Assert.Equal(value.Length / 2, v.Length); }
/// <summary> /// Parses a hex string that represents a <see cref="Hash" /> (such as one returned by <see cref="ToString" />). /// The hex string must not have an 0x prefix. It must consist of repetitions of hex bytes, e.g. A4. /// </summary> public static bool TryParse(string value, out Hash parsed) { Contract.Requires(value != null); if (!Fingerprint.TryParse(value, out var raw)) { parsed = default(Hash); return(false); } parsed = new Hash(raw); return(true); }
private IEnumerable <StrongFingerprint> EnumerateUniqueStrongFingerprints(StreamReader reader) { ConcurrentDictionary <StrongFingerprint, int> uniqueStrongFingerprints = new ConcurrentDictionary <StrongFingerprint, int>(); // Look for pattern: GetContentHashList(WeakFingerprint=[8033C0365DE491734D48A85A5709099B9B6A02D2],Selector=[ContentHash=[VSO0:000000000000000000000000000000000000000000000000000000000000000000], Output=[D697E34F2B7242DE55AFA03220E72DE2ED1D7DE0]]) start // Hits on this will also hit AddOrGetContentHashList: // AddOrGetContentHashList(WeakFingerprint=[AF00A265EB9B856129B5CBB41D5B7FE15D0CBC26],Selector=[ContentHash=[VSO0:000000000000000000000000000000000000000000000000000000000000000000], Output=[56552E044A46FA8AB1AC8660C62221A4BE8497C4]]) start const string strongFingerprintPattern = @"WeakFingerprint=\[(?<weakFingerprint>\w*)\],Selector=\[ContentHash=\[(?<selectorHash>[^\]]*)\], Output=\[(?<selectorOutput>\w*)\]\]"; Regex sfpRegex = new Regex(strongFingerprintPattern, RegexOptions.IgnoreCase); string currentLine = reader.ReadLine(); while (currentLine != null) { Match match = sfpRegex.Match(currentLine); if (match.Success && match.Groups["weakFingerprint"].Success && match.Groups["selectorHash"].Success && match.Groups["selectorOutput"].Success) { string weakFingerprintString = match.Groups["weakFingerprint"].Value; string selectorHashString = match.Groups["selectorHash"].Value; string selectorOutputString = match.Groups["selectorOutput"].Value; Fingerprint weakFingerprint; ContentHash selectorHash; byte[] selectorOutput = null; if (Fingerprint.TryParse(weakFingerprintString, out weakFingerprint) && ContentHash.TryParse(selectorHashString, out selectorHash)) { if (!string.IsNullOrEmpty(selectorOutputString)) { selectorOutput = HexUtilities.HexToBytes(selectorOutputString); } StrongFingerprint strongFingerprint = new StrongFingerprint( weakFingerprint, new Selector(selectorHash, selectorOutput)); if (uniqueStrongFingerprints.TryAdd(strongFingerprint, 0)) { yield return(strongFingerprint); } } } currentLine = reader.ReadLine(); } }