// Compare the debug checksums.
        // Get a hash for the MsApp file.
        // First pass adds file/hash to comp.
        // Second pass checks hash equality and removes files from comp.
        // After second pass, comp should be 0. Any files in comp were missing from 2nd pass.
        public static void CompareChecksums(string pathToZip, TextWriter log, Dictionary <string, byte[]> comp, bool first, ErrorContainer errorContainer)
        {
            // Path to the directory where we are creating the normalized form
            string normFormDir = ".\\diffFiles";

            // Create directory if doesn't exist
            if (!Directory.Exists(normFormDir))
            {
                Directory.CreateDirectory(normFormDir);
            }

            using (var zip = ZipFile.OpenRead(pathToZip))
            {
                foreach (ZipArchiveEntry entry in zip.Entries.OrderBy(x => x.FullName))
                {
                    var newContents = ChecksumMaker.ChecksumFile <DebugTextHashMaker>(entry.FullName, entry.ToBytes());
                    if (newContents == null)
                    {
                        continue;
                    }

                    // Do easy diffs
                    {
                        if (first)
                        {
                            comp.Add(entry.FullName, newContents);
                        }
                        else
                        {
                            byte[] originalContents;
                            if (comp.TryGetValue(entry.FullName, out originalContents))
                            {
                                bool same = newContents.SequenceEqual(originalContents);

                                if (!same)
                                {
                                    var jsonDictionary1 = FlattenJson(originalContents);
                                    var jsonDictionary2 = FlattenJson(newContents);

                                    // Add JSONMismatch error if JSON property was changed or removed
                                    CheckPropertyChangedRemoved(jsonDictionary1, jsonDictionary2, errorContainer, "");

                                    // Add JSONMismatch error if JSON property was added
                                    CheckPropertyAdded(jsonDictionary1, jsonDictionary2, errorContainer, "");
#if DEBUG
                                    DebugMismatch(entry, originalContents, newContents, normFormDir);
#endif
                                }

                                comp.Remove(entry.FullName);
                            }
                            else
                            {
                                // Missing file!
                                Console.WriteLine("FAIL: 2nd has added file: " + entry.FullName);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        // Compare the debug checksums.
        // Get a hash for the MsApp file.
        // First pass adds file/hash to comp.
        // Second pass checks hash equality and removes files from comp.
        // AFter second pass, comp should be 0. any files in comp were missing from 2nd pass.
        public static void DebugChecksum(string pathToZip, TextWriter log, Dictionary <string, byte[]> comp, bool first)
        {
            using (var z = ZipFile.OpenRead(pathToZip))
            {
                foreach (ZipArchiveEntry e in z.Entries.OrderBy(x => x.FullName))
                {
                    var key = ChecksumMaker.ChecksumFile <DebugTextHashMaker>(e.FullName, e.ToBytes());
                    if (key == null)
                    {
                        continue;
                    }

                    // Do easy diffs
                    {
                        if (first)
                        {
                            comp.Add(e.FullName, key);
                        }
                        else
                        {
                            byte[] otherContents;
                            if (comp.TryGetValue(e.FullName, out otherContents))
                            {
                                bool same = key.SequenceEqual(otherContents);

                                if (!same)
                                {
                                    // Fail! Mismatch
                                    Console.WriteLine("FAIL: hash mismatch: " + e.FullName);

                                    // Write out normalized form. Easier to spot the diff.
                                    File.WriteAllBytes(@"c:\temp\a1.json", otherContents);
                                    File.WriteAllBytes(@"c:\temp\b1.json", key);

                                    // For debugging. Help find exactly where the difference is.
                                    for (int i = 0; i < otherContents.Length; i++)
                                    {
                                        if (i >= key.Length)
                                        {
                                            break;
                                        }
                                        if (otherContents[i] != key[i])
                                        {
                                        }
                                    }
                                }
                                else
                                {
                                    // success
                                }
                                comp.Remove(e.FullName);
                            }
                            else
                            {
                                // Missing file!
                                Console.WriteLine("FAIL: 2nd has added file: " + e.FullName);
                            }
                        }
                    }
                }
            }
        }