public static void CreateFromValue(string testDataName, bool viaTry) { TimestampTokenTestData testData = TimestampTokenTestData.GetTestData(testDataName); ValidateTokenInfo( testData.TokenInfoBytes, testData, viaTry ? testData.TokenInfoBytes.Length : (int?)null); }
public static void ParseDocument_ExcessData(string testDataName) { TimestampTokenTestData testData = TimestampTokenTestData.GetTestData(testDataName); int baseLen = testData.FullTokenBytes.Length; byte[] tooMuchData = new byte[baseLen + 30]; testData.FullTokenBytes.CopyTo(tooMuchData); // Look like an octet string of the remainder of the payload. Should be ignored. tooMuchData[baseLen] = 0x04; tooMuchData[baseLen + 1] = 28; TestParseDocument(tooMuchData, testData, baseLen); }
public static void CreateFromParameters(string testDataName) { TimestampTokenTestData testData = TimestampTokenTestData.GetTestData(testDataName); Oid policyId = new Oid(testData.PolicyId, testData.PolicyId); Oid hashAlgorithmOid = new Oid(testData.HashAlgorithmId); byte[] messageHash = testData.HashBytes.ToArray(); byte[] serial = testData.SerialNumberBytes.ToArray(); DateTimeOffset nonUtcTimestamp = testData.Timestamp.ToOffset(TimeSpan.FromHours(-8)); long? accuracyMicrosec = testData.AccuracyInMicroseconds; byte[] nonce = testData.NonceBytes?.ToArray(); byte[] tsaNameBytes = testData.TsaNameBytes?.ToArray(); ReadOnlyMemory <byte>?nonceMemory = null; ReadOnlyMemory <byte>?tsaMemory = null; if (nonce != null) { nonceMemory = nonce; } if (tsaNameBytes != null) { tsaMemory = tsaNameBytes; } var tokenInfo = new Rfc3161TimestampTokenInfo( policyId, hashAlgorithmOid, messageHash, serial, nonUtcTimestamp, accuracyMicrosec, testData.IsOrdering, nonceMemory, tsaMemory); // Since AssertEqual will check all the fields the remaining checks in this method are about // input/output value/reference associations. AssertEqual(testData, tokenInfo); Assert.NotSame(policyId, tokenInfo.PolicyId); Assert.NotSame(hashAlgorithmOid, tokenInfo.HashAlgorithmId); Assert.Equal(nonUtcTimestamp, tokenInfo.Timestamp); Assert.Equal(TimeSpan.Zero, tokenInfo.Timestamp.Offset); Assert.Equal(messageHash.ByteArrayToHex(), tokenInfo.GetMessageHash().ByteArrayToHex()); // Detached from the original data messageHash[0] ^= 0xFF; Assert.NotEqual(messageHash.ByteArrayToHex(), tokenInfo.GetMessageHash().ByteArrayToHex()); Assert.Equal(serial.ByteArrayToHex(), tokenInfo.GetSerialNumber().ByteArrayToHex()); // Detached from the original data serial[1] ^= 0xFF; Assert.NotEqual(serial.ByteArrayToHex(), tokenInfo.GetSerialNumber().ByteArrayToHex()); if (nonce != null) { ReadOnlyMemory <byte>?tokenNonce = tokenInfo.GetNonce(); Assert.True(tokenNonce.HasValue, "tokenInfo.GetNonce().HasValue"); Assert.Equal(nonce.ByteArrayToHex(), tokenNonce.Value.ByteArrayToHex()); // Detached from the original data nonce[0] ^= 0xFF; Assert.NotEqual(nonce.ByteArrayToHex(), tokenNonce.Value.ByteArrayToHex()); } ReadOnlyMemory <byte>?nameFromToken = tokenInfo.GetTimestampAuthorityName(); if (tsaNameBytes != null) { Assert.True(nameFromToken.HasValue, "nameFromToken.HasValue"); Assert.Equal(tsaNameBytes.ByteArrayToHex(), nameFromToken.Value.ByteArrayToHex()); // Detached from the original data tsaNameBytes[5] ^= 0xFF; Assert.NotEqual(tsaNameBytes.ByteArrayToHex(), nameFromToken.Value.ByteArrayToHex()); } if (testData.ExtensionsBytes == null) { Assert.False(tokenInfo.HasExtensions, "tokenInfo.HasExtensions"); Assert.NotNull(tokenInfo.GetExtensions()); Assert.Equal(0, tokenInfo.GetExtensions().Count); // GetExtensions always returns a new collection. Assert.NotSame(tokenInfo.GetExtensions(), tokenInfo.GetExtensions()); } else { Assert.True(tokenInfo.HasExtensions, "tokenInfo.HasExtensions"); Assert.NotNull(tokenInfo.GetExtensions()); Assert.True(false, "A test handler has been written for extensions..."); // GetExtensions always returns a new collection. Assert.NotSame(tokenInfo.GetExtensions(), tokenInfo.GetExtensions()); } // Because the token is DER encoded, we should produce byte-for-byte the same value. Assert.Equal(testData.TokenInfoBytes.ByteArrayToHex(), tokenInfo.Encode().ByteArrayToHex()); }
public static void ParseDocument(string testDataName) { TimestampTokenTestData testData = TimestampTokenTestData.GetTestData(testDataName); TestParseDocument(testData.FullTokenBytes, testData, testData.FullTokenBytes.Length); }