Пример #1
0
        protected void TestAgainstTestFile(IHash a_hash, TestData a_test_data = null)
        {
            if (a_test_data == null)
            {
                a_test_data = TestData.Load(a_hash);
            }

            for (int i = 0; i < a_test_data.Count; i++)
            {
                string output_array = Converters.ConvertBytesToHexString(a_test_data.GetHash(i));

                if (a_test_data.GetRepeat(i) != 1)
                {
                    continue;
                }

                if (a_hash is IWithKey)
                {
                    (a_hash as IWithKey).Key = a_test_data.GetKey(i);
                }

                Assert.AreEqual(output_array,
                                a_hash.ComputeBytes(a_test_data.GetData(i)).ToString(), String.Format("{0}, {1}", a_hash.Name, i));
            }
        }
Пример #2
0
        protected void TestExtremelyLong()
        {
            const int   SLEEP_TIME_MS   = 10;
            int         THREADS         = System.Environment.ProcessorCount;
            const float TARGET_CPU_LOAD = 0.4f;

            Assert.IsTrue(THREADS <= System.Environment.ProcessorCount);

            ProgressIndicator pi = new ProgressIndicator("Crypto +4GB Test");

            pi.AddLine(String.Format("Configuration: {0} threads, CPU Load: {1}%", THREADS, TARGET_CPU_LOAD * 100));

            CancellationTokenSource src = new CancellationTokenSource();

            Task regulator = Task.Factory.StartNew(token =>
            {
                PerformanceCounter pc = new PerformanceCounter("Processor", "% Processor Time", "_Total");

                List <float> cpu_load = new List <float>();

                for (;;)
                {
                    const int PROBE_DELTA_MS = 200;
                    const int PROBE_COUNT    = 30;
                    const int REGULATE_COUNT = 10;

                    for (int i = 0; i < REGULATE_COUNT; i++)
                    {
                        System.Threading.Thread.Sleep(PROBE_DELTA_MS);

                        cpu_load.Add(pc.NextValue() / 100);

                        if (src.IsCancellationRequested)
                        {
                            break;
                        }
                    }

                    while (cpu_load.Count > PROBE_COUNT)
                    {
                        cpu_load.RemoveFirst();
                    }

                    int old_transform_rounds = transform_rounds;

                    float avg_cpu_load = cpu_load.Average();

                    if (avg_cpu_load >= TARGET_CPU_LOAD)
                    {
                        transform_rounds = (int)Math.Round(transform_rounds * 0.9);

                        if (old_transform_rounds == transform_rounds)
                        {
                            transform_rounds--;
                        }
                    }
                    else
                    {
                        transform_rounds = (int)Math.Round(transform_rounds * 1.1);

                        if (old_transform_rounds == transform_rounds)
                        {
                            transform_rounds++;
                        }
                    }

                    if (transform_rounds == 0)
                    {
                        transform_rounds = 1;
                    }

                    if (src.IsCancellationRequested)
                    {
                        break;
                    }
                }
            }, src.Token);

            var partitioner = Partitioner.Create(Hashes.CryptoAll, EnumerablePartitionerOptions.None);

            Parallel.ForEach(partitioner, new ParallelOptions()
            {
                MaxDegreeOfParallelism = THREADS
            }, ht =>
            {
                IHash hash = (IHash)Activator.CreateInstance(ht);

                pi.AddLine(String.Format("{0} / {1} - {2} - {3}%", Hashes.CryptoAll.IndexOf(ht) + 1,
                                         Hashes.CryptoAll.Count, hash.Name, 0));

                TestData test_data = TestData.Load(hash);

                int test_data_index = 0;

                for (int i = 0; i < test_data.Count; i++)
                {
                    if (test_data.GetRepeat(i) == 1)
                    {
                        continue;
                    }

                    test_data_index++;

                    ulong repeats          = (ulong)test_data.GetRepeat(i);
                    byte[] data            = test_data.GetData(i);
                    string expected_result = Converters.ConvertBytesToHexString(test_data.GetHash(i));

                    hash.Initialize();

                    int transform_counter = transform_rounds;
                    DateTime progress     = DateTime.Now;

                    for (ulong j = 0; j < repeats; j++)
                    {
                        hash.TransformBytes(data);

                        transform_counter--;
                        if (transform_counter == 0)
                        {
                            System.Threading.Thread.Sleep(SLEEP_TIME_MS);
                            transform_counter = transform_rounds;
                        }

                        if (DateTime.Now - progress > TimeSpan.FromSeconds(5))
                        {
                            pi.AddLine(String.Format("{0} / {1} / {2} - {3} - {4}%", Hashes.CryptoAll.IndexOf(ht) + 1,
                                                     test_data_index, Hashes.CryptoAll.Count, hash.Name, j * 100 / repeats));
                            progress = DateTime.Now;
                        }
                    }

                    HashResult result = hash.TransformFinal();

                    Assert.AreEqual(expected_result, Converters.ConvertBytesToHexString(result.GetBytes()), hash.ToString());

                    pi.AddLine(String.Format("{0} / {1} / {2} - {3} - {4}", Hashes.CryptoAll.IndexOf(ht) + 1,
                                             test_data_index, Hashes.CryptoAll.Count, hash.Name, "OK"));
                }
            });

            src.Cancel();
            regulator.Wait();
        }
Пример #3
0
        protected void TestAgainstTestFile(IHash a_hash, TestData a_testData)
        {
            a_testData.Load();

            for(int i = 0; i < a_testData.Count; i++)
            {
                byte[] output_array = a_testData.GetHash(i);

                CollectionAssert.AreEqual(output_array,
                                          a_hash.ComputeBytes(a_testData.GetData(i)).GetBytes(), String.Format("{0}, {1}", a_hash.Name, i));

                CollectionAssert.AreEqual(output_array, a_testData.GetHash(i), a_hash.Name);
            }
        }
Пример #4
0
        protected void TestAgainstTestFile(IHash a_hash, TestData a_test_data = null)
        {
            if (a_test_data == null)
                a_test_data = TestData.Load(a_hash);

            for (int i = 0; i < a_test_data.Count; i++)
            {
                string output_array = Converters.ConvertBytesToHexString(a_test_data.GetHash(i));

                if (a_test_data.GetRepeat(i) != 1)
                    continue;

                if (a_hash is IWithKey)
                    (a_hash as IWithKey).Key = a_test_data.GetKey(i);

                Assert.AreEqual(output_array,
                    a_hash.ComputeBytes(a_test_data.GetData(i)).ToString(), String.Format("{0}, {1}", a_hash.Name, i));
            }
        }