Пример #1
0
        public void ProtobuildSchemeResolvesToCorrectProtocol()
        {
            // TODO: Make these tests not depend on a network connection.

            var packageLookup = GetPackageLookup();

            var metadata = packageLookup.Lookup(new PackageRequestRef("http://protobuild.org/hach-que/TestEmptyPackage", "master", "Windows", false));

            _assert.IsType <ProtobuildPackageMetadata>(metadata);

            var protobuildMetadata = (ProtobuildPackageMetadata)metadata;

            _assert.Equal("http://protobuild.org/hach-que/TestEmptyPackage", protobuildMetadata.ReferenceURI);
            _assert.Equal("50a2a4e9b12739b20932d152e211239db88cbb49", protobuildMetadata.GitCommit);
            _assert.Equal(PackageManager.ARCHIVE_FORMAT_TAR_LZMA, protobuildMetadata.BinaryFormat);
            _assert.Equal("https://storage.googleapis.com/protobuild-packages/6011817390243840.pkg", protobuildMetadata.BinaryURI);
            _assert.Null(protobuildMetadata.SourceURI);
            _assert.Equal(PackageManager.PACKAGE_TYPE_LIBRARY, protobuildMetadata.PackageType);
            _assert.Equal("Windows", protobuildMetadata.Platform);

            metadata = packageLookup.Lookup(new PackageRequestRef("https://protobuild.org/hach-que/TestEmptyPackage", "master", "Windows", false));

            _assert.IsType <ProtobuildPackageMetadata>(metadata);

            protobuildMetadata = (ProtobuildPackageMetadata)metadata;

            _assert.Equal("https://protobuild.org/hach-que/TestEmptyPackage", protobuildMetadata.ReferenceURI);
            _assert.Equal("50a2a4e9b12739b20932d152e211239db88cbb49", protobuildMetadata.GitCommit);
            _assert.Equal(PackageManager.ARCHIVE_FORMAT_TAR_LZMA, protobuildMetadata.BinaryFormat);
            _assert.Equal("https://storage.googleapis.com/protobuild-packages/6011817390243840.pkg", protobuildMetadata.BinaryURI);
            _assert.Null(protobuildMetadata.SourceURI);
            _assert.Equal(PackageManager.PACKAGE_TYPE_LIBRARY, protobuildMetadata.PackageType);
            _assert.Equal("Windows", protobuildMetadata.Platform);
        }
Пример #2
0
        /// <summary>
        /// Test that large amounts of data can be sent over the reliable connection
        /// without data becoming corrupt or lost.
        /// </summary>
        public void TestLargeReliableData()
        {
            const string Text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
Sed id enim et est aliquet mollis. Suspendisse tempus libero in mauris 
iaculis interdum. Ut quis purus in libero euismod suscipit a non est. 
Vestibulum nec scelerisque tellus. Nullam porttitor, metus vitae placerat 
dignissim, nibh ante vehicula felis, vestibulum commodo dui urna sed enim. 
Fusce id neque non neque pellentesque tincidunt. In vehicula lacus vitae nibh 
iaculis scelerisque. Ut libero felis, aliquet nec fringilla sit amet, dignissim
sed quam. Ut a pulvinar quam. Proin mollis dictum ante vel elementum. Sed 
elementum neque libero, ac hendrerit ante semper at. Donec pretium hendrerit 
nisl, non dapibus urna. Phasellus et suscipit nibh, sed tempus magna. Aliquam 
porttitor malesuada ligula, a posuere enim pellentesque quis.

Proin id neque varius, porta eros eget, pellentesque massa. Suspendisse viverra
ligula at lorem dignissim fringilla. Proin viverra nunc neque, nec dignissim 
velit viverra vitae. Vestibulum fringilla eget nunc id cursus cras amet.";

            var udpClient1 = new UdpClient(0);
            var udpClient2 = new UdpClient(0);

            var dispatcher1 = new MxDispatcher(udpClient1);
            var dispatcher2 = new MxDispatcher(udpClient2);

            var port1 = ((IPEndPoint)udpClient1.Client.LocalEndPoint).Port;
            var port2 = ((IPEndPoint)udpClient2.Client.LocalEndPoint).Port;

            try
            {
                string receivedText = null;

                dispatcher1.MessageReceived +=
                    (sender, args) => { receivedText = Encoding.ASCII.GetString(args.Payload); };

                var client      = dispatcher2.Connect(new IPEndPoint(IPAddress.Loopback, port1));
                var clientGroup = dispatcher2.PlaceInGroup(client, "Group2");

                this.SimulateNetworkCycles(4, dispatcher1, dispatcher2);

                dispatcher2.Send(
                    clientGroup,
                    Encoding.ASCII.GetBytes(Text),
                    true);

                _assert.Null(receivedText);

                this.SimulateNetworkCycles(14, dispatcher1, dispatcher2);

                _assert.Equal(Text, receivedText);
            }
            finally
            {
                dispatcher1.Close();
                dispatcher2.Close();
            }
        }
Пример #3
0
    public void Ctor()
    {
        using var fake = new FakeDbConnection();
        using var conn = new DataConnection(fake, new DataSqlDialect());

        assert.Equal(ConnectionState.Closed, conn.State);
        assert.Null(conn.ConnectionString);
        assert.NotNull(conn.SqlDialect);
        assert.Equal("Sqlite", conn.Provider);
    }
Пример #4
0
 /// <summary>
 /// <see cref="IAssert{TAssert}.Current"/>为null
 /// </summary>
 /// <param name="assert"></param>
 /// <returns></returns>
 public static IAssert <TAssert> Null <TAssert>(this IAssert <TAssert> assert) where TAssert : class
 {
     return(assert.Null($"{assert.Name},不为null"));
 }
Пример #5
0
        public void TestFeatureSetIsNullWhenMissing()
        {
            var module = LoadModuleXml("FeatureSetIsNull.xml");

            _assert.Null(module.FeatureSet);
        }
    public void ReaderHeader_WithPrivateKey()
    {
        using (var options = new SymmetricEncryptionProviderOptions())
            using (var engine = new SymmetricEncryptionProvider())
            {
                byte[] privateKey = null;
                using (var rng = new RandomNumberGenerator())
                {
                    privateKey = rng.NextBytes(20);
                }

                byte[] data    = null;
                var    header1 = engine.GenerateHeader(options, privateKey: privateKey);
                data = new byte[header1.Bytes.Memory.Length];
                header1.Bytes.Memory.CopyTo(data);


                var ms = new MemoryStream(data);
                using (var header = engine.ReadHeader(ms, options, privateKey))
                {
                    ms.Position = 0;

                    var data2 = new byte[header.Bytes.Memory.Length];
                    header.Bytes.Memory.CopyTo(data2);

                    assert.Equal(data.Length, data2.Length);
                    assert.Equal(data, data2);

                    assert.NotNull(header);
                    assert.Equal(1, header.Version);
                    assert.Equal(SymmetricAlgorithmType.AES, header.SymmetricAlgorithmType);
                    assert.Equal(KeyedHashAlgorithmType.HMACSHA256, header.KeyedHashAlgorithmType);
                    assert.Equal(0, header.MetaDataSize);
                    assert.NotEqual(0, header.SigningSaltSize);
                    assert.NotEqual(0, header.SymmetricSaltSize);
                    assert.NotEqual(0, header.IvSize);
                    assert.NotEqual(0, header.HashSize);
                    assert.NotEqual(0, header.Iterations);
                    assert.NotNull(header.SymmetricKey);
                    assert.NotNull(header.IV);
                    assert.NotNull(header.SigningKey);
                    assert.NotNull(header.Bytes);

                    assert.Ok(!header.SymmetricKey.Memory.IsEmpty);
                    assert.Ok(!header.IV.Memory.IsEmpty);
                    assert.Ok(!header.SigningKey.Memory.IsEmpty);

                    var temp = new byte[header.SymmetricKey.Memory.Length];
                    header.SymmetricKey.Memory.CopyTo(temp);
                    assert.NotEqual(privateKey, temp);

                    assert.Ok(!header.Bytes.Memory.IsEmpty);

                    ms.Position = 0;
                    using (var br = new BinaryReader(ms))
                    {
                        assert.Equal(1, br.ReadInt16());
                        assert.Equal((short)SymmetricAlgorithmType.AES, br.ReadInt16());
                        assert.Equal((short)KeyedHashAlgorithmType.HMACSHA256, br.ReadInt16());
                        assert.Equal(header.MetaDataSize, br.ReadInt32());
                        assert.Equal(header.Iterations, br.ReadInt32());
                        assert.Equal(header.SymmetricSaltSize, br.ReadInt16());
                        assert.Equal(header.SigningSaltSize, br.ReadInt16());
                        assert.Equal(header.IvSize, br.ReadInt16());
                        assert.Equal(header.SymmetricKeySize, br.ReadInt16());
                        assert.Equal(header.HashSize, br.ReadInt16());

                        assert.Equal(header.Version, header1.Version);
                        assert.Equal(header.KeyedHashAlgorithmType, header1.KeyedHashAlgorithmType);
                        assert.Equal(header.SymmetricAlgorithmType, header1.SymmetricAlgorithmType);
                        assert.Equal(header.MetaDataSize, header1.MetaDataSize);
                        assert.Equal(header.Iterations, header1.Iterations);
                        assert.Equal(header.SymmetricSaltSize, header1.SymmetricSaltSize);
                        assert.Equal(header.SigningSaltSize, header1.SigningSaltSize);
                        assert.Equal(header.IvSize, header1.IvSize);
                        assert.Equal(header.SymmetricKeySize, header1.SymmetricKeySize);
                        assert.Equal(header.HashSize, header1.HashSize);

                        byte[] metadata      = null;
                        byte[] symmetricSalt = null;
                        byte[] signingSalt   = null;
                        byte[] iv            = null;
                        byte[] symmetricKey  = null;
                        byte[] hash          = null;

                        // header values
                        // 1. version
                        // 2. metadataSize
                        // 3. iterations
                        // 4. symmetricSaltSize
                        // 5. signingSaltSize
                        // 6. ivSize
                        // 7. symmetricKeySize
                        // 8. hashSize

                        // header values
                        // 1. metadata (optional)
                        // 2. symmetricSalt (optional)
                        // 3. signingSalt (optional)
                        // 4. iv
                        // 5. symmetricKey (optional)
                        // 6. hash

                        if (header.MetaDataSize > 0)
                        {
                            metadata = br.ReadBytes(header.MetaDataSize);
                        }

                        if (header.SymmetricSaltSize > 0)
                        {
                            assert.Equal(options.SaltSize / 8, header.SymmetricSaltSize);
                            var name = System.Security.Cryptography.HashAlgorithmName.SHA256;
                            symmetricSalt = br.ReadBytes(header.SymmetricSaltSize);
                            using (var generator = new Rfc2898DeriveBytes(privateKey, symmetricSalt, options.Iterations, name))
                            {
                                symmetricKey = generator.GetBytes(options.KeySize / 8);
                                var p1 = new byte[header.SymmetricKey.Memory.Length];
                                var p2 = new byte[header1.SymmetricKey.Memory.Length];
                                header.SymmetricKey.Memory.CopyTo(p1);
                                header1.SymmetricKey.Memory.CopyTo(p2);
                                assert.Equal(p1, p2);

                                assert.Equal(symmetricKey, p1);
                            }

                            symmetricKey = null;
                        }

                        if (header.SigningSaltSize > 0)
                        {
                            signingSalt = br.ReadBytes(header.SigningSaltSize);
                            var name = System.Security.Cryptography.HashAlgorithmName.SHA256;
                            using (var generator = new Rfc2898DeriveBytes(privateKey, signingSalt, options.Iterations, name))
                            {
                                var signingKey = generator.GetBytes(options.KeySize / 8);
                                var p1         = new byte[header.SymmetricKey.Memory.Length];
                                header.SigningKey.Memory.CopyTo(p1);

                                assert.Equal(signingKey, p1);
                            }
                        }

                        if (header.IvSize > 0)
                        {
                            iv = br.ReadBytes(header.IvSize);

                            var iv2 = new byte[header.IvSize];
                            header.IV.Memory.CopyTo(iv2);
                            assert.Equal(iv, iv2);
                        }

                        if (header.SymmetricKeySize > 0)
                        {
                            symmetricKey = br.ReadBytes(header.SymmetricKeySize);
                        }

                        if (header.HashSize > 0)
                        {
                            hash = br.ReadBytes(header.HashSize);

                            var hash2 = new byte[header.HashSize];
                            header.Hash.Memory.CopyTo(hash2);
                            assert.Equal(hash, hash2);
                        }

                        assert.Null(metadata);
                        assert.NotNull(hash);
                        assert.NotNull(signingSalt);
                        assert.NotNull(symmetricSalt);

                        // header property has a copy but does not
                        // write it to the file header when a private key
                        // is provided.
                        assert.Null(symmetricKey);
                        assert.NotNull(iv);
                        assert.NotEmpty(hash);
                        assert.NotEmpty(signingSalt);
                        assert.NotEmpty(symmetricSalt);

                        assert.NotEmpty(iv);
                    }
                }
            }
    }