Exemplo n.º 1
0
        /// <summary>
        /// Compares the files provided in the data and add to an internal result.
        /// </summary>
        /// <param name="data">The data for this entry.</param>
        public void CompareAndAddToResult(CompareData data)
        {
            // Check for equality by hash
            if (!data.Dummy && !Hasher.filesAreEqual(data.CorrectFile, data.ProducedFile))
            {
                // Upload result
                using (FileStream stream = File.Open(data.ProducedFile, FileMode.Open))
                {
                    UploadFile[] files = new[] {
                        new UploadFile {
                            Name        = "file", //Path.GetFileNameWithoutExtension(data.ProducedFile),
                            Filename    = Path.GetFileName(data.ProducedFile),
                            ContentType = "application/octet-stream",
                            Stream      = stream
                        }
                    };

                    NameValueCollection nv = new NameValueCollection {
                        { "type", "upload" },
                        { "test_id", data.TestID.ToString() },
                        { "test_file_id", data.TestFileID.ToString() }
                    };

                    byte[] result = UploadFiles(reportUrl, files, nv);
                }

                /*using (var wb = new WebClient())
                 * {
                 *  wb.Headers.Add("user-agent", userAgent);
                 *  // TODO: check if this works
                 *  var response = wb.UploadFile(reportUrl, data.ProducedFile);
                 * }*/
            }
            else
            {
                // Post equality status
                using (var wb = new WebClient())
                {
                    wb.Headers.Add("user-agent", userAgent);
                    var d = new NameValueCollection();
                    d["type"]         = "equality";
                    d["test_id"]      = data.TestID.ToString();
                    d["test_file_id"] = data.TestFileID.ToString();

                    var response = wb.UploadValues(reportUrl, "POST", d);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Compares the files provided in the data and add to an internal result.
        /// </summary>
        /// <param name="data">The data for this entry.</param>
        public void CompareAndAddToResult(CompareData data)
        {
            Builder.AppendLine("Time needed for this entry: " + data.RunTime.ToString());
            Builder.AppendLine("Used command: " + data.Command);
            Builder.AppendLine("Sample file: " + data.SampleFile);
            if (data.ExitCode != 0)
            {
                Builder.AppendLine("CCExtractor quit with exit code " + data.ExitCode);
            }
            else
            {
                if (!Hasher.filesAreEqual(data.CorrectFile, data.ProducedFile))
                {
                    ProcessStartInfo psi = new ProcessStartInfo("diff");
                    psi.UseShellExecute        = false;
                    psi.RedirectStandardError  = true;
                    psi.RedirectStandardOutput = true;
                    psi.CreateNoWindow         = true;

                    psi.Arguments = String.Format(@"-y ""{0}"" ""{1}""", data.CorrectFile, data.ProducedFile);
                    Process p = new Process();
                    p.StartInfo           = psi;
                    p.ErrorDataReceived  += processError;
                    p.OutputDataReceived += processOutput;
                    p.Start();
                    p.BeginOutputReadLine();
                    p.BeginErrorReadLine();
                    while (!p.HasExited)
                    {
                        Thread.Sleep(1000);
                    }
                }
                else
                {
                    Builder.AppendLine("Samples are equal (hash)");
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Compares the files provided in the data and add to an internal result.
        /// </summary>
        /// <param name="data">The data for this entry.</param>
        public void CompareAndAddToResult(CompareData data)
        {
            string onclick = "";
            string clss    = "green";
            int    changes = 0;

            if (data.ExitCode != 0)
            {
                changes = -1;
                clss    = "red";
                lock (this)
                {
                    BuilderDiff.WriteLine(String.Format(@"<div style=""display:none;"" id=""{0}"">CCExtractor quit with exit code {1}</div>", "entry_" + Count, data.ExitCode));
                    BuilderDiff.Flush();
                }
                onclick = String.Format(@"onclick=""toggle('{0}'); mark(this);""", "entry_" + Count);
            }
            else
            {
                SideBySideModel sbsm = null;
                if (!Hasher.filesAreEqual(data.CorrectFile, data.ProducedFile))
                {
                    string oldText = "ERROR - COULD NOT LOAD";
                    string newText = "ERROR - COULD NOT LOAD";
                    if (data.ProducedFile.EndsWith(".bin"))
                    {
                        if (File.Exists(data.CorrectFile))
                        {
                            using (FileStream fs = new FileStream(data.CorrectFile, FileMode.Open, FileAccess.Read))
                            {
                                StringBuilder sb = new StringBuilder();
                                int           hexIn, counter = 1;
                                while ((hexIn = fs.ReadByte()) != -1)
                                {
                                    sb.AppendFormat("{0:X2} ", hexIn);
                                    if (counter % 17 == 0)
                                    {
                                        sb.AppendLine();
                                        counter = 0;
                                    }
                                    counter++;
                                }
                                oldText = sb.ToString();
                            }
                        }
                        if (File.Exists(data.ProducedFile))
                        {
                            using (FileStream fs = new FileStream(data.ProducedFile, FileMode.Open, FileAccess.Read))
                            {
                                StringBuilder sb = new StringBuilder();
                                int           hexIn, counter = 1;
                                while ((hexIn = fs.ReadByte()) != -1)
                                {
                                    sb.AppendFormat("{0:X2} ", hexIn);
                                    if (counter % 17 == 0)
                                    {
                                        sb.AppendLine();
                                        counter = 0;
                                    }
                                    counter++;
                                }
                                newText = sb.ToString();
                            }
                        }
                    }
                    else
                    {
                        if (File.Exists(data.CorrectFile))
                        {
                            using (FileStream fs = new FileStream(data.CorrectFile, FileMode.Open, FileAccess.Read))
                            {
                                using (StreamReader streamReader = new StreamReader(fs, Encoding.UTF8))
                                {
                                    oldText = streamReader.ReadToEnd();
                                }
                            }
                        }
                        if (File.Exists(data.ProducedFile))
                        {
                            using (FileStream fs = new FileStream(data.ProducedFile, FileMode.Open, FileAccess.Read))
                            {
                                using (StreamReader streamReader = new StreamReader(fs, Encoding.UTF8))
                                {
                                    newText = streamReader.ReadToEnd();
                                }
                            }
                        }
                    }

                    sbsm    = Differ.BuildDiffModel(oldText, newText);
                    changes = sbsm.GetNumberOfChanges();
                    if ((oldText == "ERROR - COULD NOT LOAD" || newText == "ERROR - COULD NOT LOAD") && !data.Dummy)
                    {
                        changes = -1;
                    }
                }
                if (changes != 0)
                {
                    lock (this)
                    {
                        BuilderDiff.WriteLine(sbsm.GetDiffHTML(String.Format(@"style=""display:none;"" id=""{0}""", "entry_" + Count), Reduce));
                        BuilderDiff.Flush();
                    }
                    onclick = String.Format(@"onclick=""toggle('{0}'); mark(this);""", "entry_" + Count);
                    clss    = "red";
                }
                else
                {
                    Successes++;
                }
            }
            Builder.AppendFormat(
                @"<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td class=""{4}"" {5}>{6}</td></tr>",
                data.SampleFile,
                data.Command,
                data.RunTime.ToString(),
                data.ProducedFile,
                clss,
                onclick,
                changes);
            Count++;
        }