|
| 1 | +// Copyright (c) 2019 SIL International |
| 2 | +// This software is licensed under the LGPL, version 2.1 or later |
| 3 | +// (http://www.gnu.org/licenses/lgpl-2.1.html) |
| 4 | + |
| 5 | +using System.Diagnostics; |
| 6 | +using System.IO; |
| 7 | +using System.Security.Cryptography; |
| 8 | +using System.Text; |
| 9 | + |
| 10 | +namespace SIL.InstallValidator |
| 11 | +{ |
| 12 | + public class InstallValidator |
| 13 | + { |
| 14 | + // ENHANCE (Hasso) 2019.07: look for extra files? |
| 15 | + // ENHANCE (Hasso) 2019.07: option to catalog files that have already been installed (for testing against other upgrade scenarios) |
| 16 | + public static void Main(string[] args) |
| 17 | + { |
| 18 | + if (args.Length == 0 || !File.Exists(args[0])) |
| 19 | + { |
| 20 | + // The user double-clicked (poor user won't see this), ran w/o args, or ran w/ invalid args. |
| 21 | + Debug.WriteLine("SIL Installation Validator"); |
| 22 | + Debug.WriteLine("Copyright (c) 2019 SIL International"); |
| 23 | + Debug.WriteLine(string.Empty); |
| 24 | + Debug.WriteLine("This program may be installed in the same directory as another SIL program,"); |
| 25 | + Debug.WriteLine("but users should not use it. Its purpose is to help verify that the program"); |
| 26 | + Debug.WriteLine(" was installed correctly."); |
| 27 | + Debug.WriteLine("Usage:"); |
| 28 | + Debug.WriteLine(" - Drop installerTestMetadata.csv on this exe to generate a report"); |
| 29 | + Debug.WriteLine(" - InstallValidator.exe installerTestMetadata.csv [alternate report location]"); |
| 30 | + Debug.WriteLine(" (for unit tests)"); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + var logFile = SafeGetAt(args, 1) ?? Path.Combine(Path.GetTempPath(), "FlexInstallationReport.csv"); |
| 35 | + |
| 36 | + using (var expected = new StreamReader(args[0])) |
| 37 | + using (var actual = new StreamWriter(logFile)) |
| 38 | + { |
| 39 | + actual.WriteLine("File, Result, Expected Version, Actual Version, Expected Date, Actual Date Modified (UTC)"); |
| 40 | + expected.ReadLine(); // skip headers |
| 41 | + string file; |
| 42 | + while ((file = expected.ReadLine()) != null) |
| 43 | + { |
| 44 | + var info = file.Split(','); |
| 45 | + if (info.Length < 2) |
| 46 | + { |
| 47 | + actual.WriteLine($"Bad input (or EOF), {file}"); |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + var filePath = info[0].Trim(); |
| 52 | + actual.Write(filePath); |
| 53 | + var fullPath = Path.Combine(Directory.GetCurrentDirectory(), filePath); |
| 54 | + if (!File.Exists(fullPath)) |
| 55 | + { |
| 56 | + actual.WriteLine(", is missing"); |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + var expectedMd5 = info[1].Trim(); |
| 61 | + var actualMd5 = ComputeMd5Sum(fullPath); |
| 62 | + if (string.Equals(expectedMd5, actualMd5)) |
| 63 | + { |
| 64 | + actual.WriteLine(", was installed correctly"); |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + var expectedVersion = SafeGetAt(info, 2); |
| 69 | + var actualVersion = FileVersionInfo.GetVersionInfo(fullPath).FileVersion; |
| 70 | + var expectedDate = SafeGetAt(info, 3); |
| 71 | + var actualDate = File.GetLastWriteTimeUtc(fullPath); |
| 72 | + actual.WriteLine( |
| 73 | + $", incorrect file is present, {expectedVersion}, {actualVersion}, {expectedDate}, {actualDate:yyyy-MM-dd HH:mm:ss}"); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // If we ran the program by dropping installerTestMetadata.csv, open the report using the default program |
| 78 | + if (args.Length == 1) |
| 79 | + { |
| 80 | + Process.Start(logFile); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private static string SafeGetAt(string[] arr, int index) |
| 85 | + { |
| 86 | + return arr.Length > index ? arr[index].Trim() : null; |
| 87 | + } |
| 88 | + |
| 89 | + private static readonly HashAlgorithm Hasher = HashAlgorithm.Create("MD5"); |
| 90 | + |
| 91 | + public static string ComputeMd5Sum(string filename) |
| 92 | + { |
| 93 | + byte[] checksumBytes; |
| 94 | + using (var file = File.OpenRead(filename)) |
| 95 | + { |
| 96 | + checksumBytes = Hasher.ComputeHash(file); |
| 97 | + } |
| 98 | + var bldr = new StringBuilder(); |
| 99 | + foreach (var b in checksumBytes) |
| 100 | + { |
| 101 | + bldr.AppendFormat("{0:x2}", b); |
| 102 | + } |
| 103 | + |
| 104 | + return bldr.ToString(); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments