Exemple #1
0
        public static void TestDoubleDisposeAndObjectDisposedException()
        {
            SetupAssembly.FakeRuntimeEnvironment.WebCallerCreator = () =>
            {
                return(new FakeWebCaller(@"{""U"":""http://localhost/AxCrypt/Downloads.html"",""V"":""2.0.307.0"",""R"":307,""S"":0,""M"":""OK""}"));
            };

            DateTime utcNow = DateTime.UtcNow;

            SetupAssembly.FakeRuntimeEnvironment.TimeFunction = () => { return(utcNow); };

            Version          thisVersion      = new Version(2, 0, 300, 0);
            Version          newVersion       = new Version(2, 0, 307, 0);
            Uri              restApiUrl       = new Uri("http://localhost/RestApi.asxh/axcrypt2version");
            Uri              updateWebPageUrl = new Uri("http://www.axantum.com/");
            VersionEventArgs eventArgs        = null;
            UpdateCheck      updateCheck      = new UpdateCheck(thisVersion);

            updateCheck.VersionUpdate += (object sender, VersionEventArgs e) =>
            {
                eventArgs = e;
            };
            updateCheck.CheckInBackground(DateTime.MinValue, UpdateCheck.VersionUnknown.ToString(), restApiUrl, updateWebPageUrl);
            updateCheck.WaitForBackgroundCheckComplete();
            updateCheck.Dispose();

            Assert.DoesNotThrow(updateCheck.Dispose);
            Assert.Throws <ObjectDisposedException>(() => { updateCheck.CheckInBackground(DateTime.MinValue, UpdateCheck.VersionUnknown.ToString(), restApiUrl, updateWebPageUrl); });
            Assert.Throws <ObjectDisposedException>(updateCheck.WaitForBackgroundCheckComplete);

            Assert.That(eventArgs, Is.Not.Null, "The VersionUpdate event should be called with non-null VersionEventArgs.");
            Assert.That(eventArgs.VersionUpdateStatus, Is.EqualTo(VersionUpdateStatus.NewerVersionIsAvailable), "The new version was newer.");
            Assert.That(eventArgs.UpdateWebpageUrl, Is.EqualTo(new Uri("http://localhost/AxCrypt/Downloads.html")), "The right URL should be passed in the event args.");
            Assert.That(eventArgs.Version, Is.EqualTo(newVersion), "The new version should be passed back.");
        }
Exemple #2
0
        public AppDelegate()
        {
            OS.Current = new AxCrypt.Core.MacOsx.RuntimeEnvironment();

            UpdateCheck updatecheck = new UpdateCheck(UpdateCheck.VersionUnknown);
            Uri restApiUri = new Uri("https://www.axantum.com/Xecrets/RestApi.ashx/axcrypt2version/mac");
            Uri versionUri = new Uri("http://www.axantum.com/");
            string currentVersion = UpdateCheck.VersionUnknown.ToString();

            updatecheck.VersionUpdate += (sender, versionArguments) => {
                if (versionArguments.VersionUpdateStatus == VersionUpdateStatus.NewerVersionIsAvailable) {
                    int response = NSAlert.WithMessage("New version available!", "Update now", "Update later", null,
                                        "A new version of Axantum AxCrypt for Mac is available! " +
                                        "Would you like to download and install it now?")
                        .RunModal();

                    if (response == 1) {
                        Process.Start(versionArguments.UpdateWebpageUrl.AbsoluteUri);
                        NSApplication.SharedApplication.Terminate(this);
                    }
                }
            };

            updatecheck.CheckInBackground(DateTime.UtcNow, currentVersion, restApiUri, versionUri);
        }
Exemple #3
0
        public static void TestVersionSameAndCheckedRecently()
        {
            SetupAssembly.FakeRuntimeEnvironment.WebCallerCreator = () =>
            {
                return(new FakeWebCaller(@"{""U"":""http://localhost/AxCrypt/Downloads.html"",""V"":""2.0.300.0"",""R"":300,""S"":0,""M"":""OK""}"));
            };

            DateTime utcNow = DateTime.UtcNow;

            SetupAssembly.FakeRuntimeEnvironment.TimeFunction = () => { return(utcNow); };

            Version          thisVersion      = new Version(2, 0, 300, 0);
            Version          newVersion       = new Version(2, 0, 300, 0);
            Uri              restApiUrl       = new Uri("http://localhost/RestApi.asxh/axcrypt2version");
            Uri              updateWebPageUrl = new Uri("http://www.axantum.com/");
            VersionEventArgs eventArgs        = null;

            using (UpdateCheck updateCheck = new UpdateCheck(thisVersion))
            {
                updateCheck.VersionUpdate += (object sender, VersionEventArgs e) =>
                {
                    eventArgs = e;
                };
                updateCheck.CheckInBackground(utcNow.AddDays(-2), UpdateCheck.VersionUnknown.ToString(), restApiUrl, updateWebPageUrl);
                updateCheck.WaitForBackgroundCheckComplete();
            }

            Assert.That(eventArgs, Is.Not.Null, "The VersionUpdate event should be called with non-null VersionEventArgs.");
            Assert.That(eventArgs.VersionUpdateStatus, Is.EqualTo(VersionUpdateStatus.IsUpToDateOrRecentlyChecked), "The new version was the same and we checked recently.");
            Assert.That(eventArgs.UpdateWebpageUrl, Is.EqualTo(new Uri("http://localhost/AxCrypt/Downloads.html")), "The right URL should be passed in the event args.");
            Assert.That(eventArgs.Version, Is.EqualTo(newVersion), "The new version should be passed back.");
        }
Exemple #4
0
        public static void TestExceptionDuringVersionCallButRecentlyChecked()
        {
            FakeWebCaller webCaller = new FakeWebCaller(@"{""U"":""http://localhost/AxCrypt/Downloads.html"",""V"":""2.0.400.0"",""R"":300,""S"":0,""M"":""OK""}");

            webCaller.Calling += (object sender, EventArgs e) => { throw new InvalidOperationException("Oops - a forced exception during the call."); };
            SetupAssembly.FakeRuntimeEnvironment.WebCallerCreator = () =>
            {
                return(webCaller);
            };

            DateTime utcNow = DateTime.UtcNow;

            SetupAssembly.FakeRuntimeEnvironment.TimeFunction = () => { return(utcNow); };

            Version          thisVersion      = new Version(2, 0, 300, 0);
            Uri              restApiUrl       = new Uri("http://localhost/RestApi.asxh/axcrypt2version");
            Uri              updateWebPageUrl = new Uri("http://www.axantum.com/");
            VersionEventArgs eventArgs        = null;

            using (UpdateCheck updateCheck = new UpdateCheck(thisVersion))
            {
                updateCheck.VersionUpdate += (object sender, VersionEventArgs e) =>
                {
                    eventArgs = e;
                };
                updateCheck.CheckInBackground(utcNow.AddDays(-2), UpdateCheck.VersionUnknown.ToString(), restApiUrl, updateWebPageUrl);
                updateCheck.WaitForBackgroundCheckComplete();
            }

            Assert.That(eventArgs, Is.Not.Null, "The VersionUpdate event should be called with non-null VersionEventArgs.");
            Assert.That(eventArgs.VersionUpdateStatus, Is.EqualTo(VersionUpdateStatus.ShortTimeSinceLastSuccessfulCheck), "Although the check failed, a check was recently made a short time ago.");
            Assert.That(eventArgs.UpdateWebpageUrl, Is.EqualTo(updateWebPageUrl), "The original URL should be passed since the call failed.");
            Assert.That(eventArgs.Version, Is.EqualTo(UpdateCheck.VersionUnknown), "An unknown version should be returned, since the call failed.");
        }
Exemple #5
0
        public static void TestInvalidVersionReturned()
        {
            SetupAssembly.FakeRuntimeEnvironment.WebCallerCreator = () =>
            {
                return(new FakeWebCaller(@"{""U"":""http://localhost/AxCrypt/Downloads.html"",""V"":""x.y.z.z"",""R"":207,""S"":0,""M"":""OK""}"));
            };

            DateTime utcNow = DateTime.UtcNow;

            SetupAssembly.FakeRuntimeEnvironment.TimeFunction = () => { return(utcNow); };

            Version          thisVersion      = new Version(2, 0, 300, 0);
            Uri              restApiUrl       = new Uri("http://localhost/RestApi.asxh/axcrypt2version");
            Uri              updateWebPageUrl = new Uri("http://www.axantum.com/");
            VersionEventArgs eventArgs        = null;

            using (UpdateCheck updateCheck = new UpdateCheck(thisVersion))
            {
                updateCheck.VersionUpdate += (object sender, VersionEventArgs e) =>
                {
                    eventArgs = e;
                };
                updateCheck.CheckInBackground(DateTime.MinValue, UpdateCheck.VersionUnknown.ToString(), restApiUrl, updateWebPageUrl);
                updateCheck.WaitForBackgroundCheckComplete();
            }

            Assert.That(eventArgs, Is.Not.Null, "The VersionUpdate event should be called even when an invalid version is returned.");
            Assert.That(eventArgs.VersionUpdateStatus, Is.EqualTo(VersionUpdateStatus.LongTimeSinceLastSuccessfulCheck), "No check has been performed previously and no new version is known.");
            Assert.That(eventArgs.UpdateWebpageUrl, Is.EqualTo(new Uri("http://localhost/AxCrypt/Downloads.html")), "The right URL should be passed in the event args.");
            Assert.That(eventArgs.Version, Is.EqualTo(UpdateCheck.VersionUnknown), "The version is not known since it could not be parsed.");
        }
Exemple #6
0
        public AppDelegate()
        {
            OS.Current = new AxCrypt.Core.MacOsx.RuntimeEnvironment();

            UpdateCheck updatecheck    = new UpdateCheck(UpdateCheck.VersionUnknown);
            Uri         restApiUri     = new Uri("https://www.axantum.com/Xecrets/RestApi.ashx/axcrypt2version/mac");
            Uri         versionUri     = new Uri("http://www.axantum.com/");
            string      currentVersion = UpdateCheck.VersionUnknown.ToString();

            updatecheck.VersionUpdate += (sender, versionArguments) => {
                if (versionArguments.VersionUpdateStatus == VersionUpdateStatus.NewerVersionIsAvailable)
                {
                    int response = NSAlert.WithMessage("New version available!", "Update now", "Update later", null,
                                                       "A new version of Axantum AxCrypt for Mac is available! " +
                                                       "Would you like to download and install it now?")
                                   .RunModal();

                    if (response == 1)
                    {
                        Process.Start(versionArguments.UpdateWebpageUrl.AbsoluteUri);
                        NSApplication.SharedApplication.Terminate(this);
                    }
                }
            };

            updatecheck.CheckInBackground(DateTime.UtcNow, currentVersion, restApiUri, versionUri);
        }
        public static void TestArgumentNullException()
        {
            string nullVersion = null;
            Uri nullUrl = null;
            Version thisVersion = new Version(2, 0, 300, 0);
            Version newVersion = new Version(2, 0, 307, 0);
            Uri restApiUrl = new Uri("http://localhost/RestApi.asxh/axcrypt2version");
            Uri updateWebPageUrl = new Uri("http://www.axantum.com/");
            using (UpdateCheck updateCheck = new UpdateCheck(thisVersion))
            {
                Assert.Throws<ArgumentNullException>(() => { updateCheck.CheckInBackground(DateTime.MinValue, nullVersion, restApiUrl, updateWebPageUrl); }, "Null argument should throw.");
                Assert.Throws<ArgumentNullException>(() => { updateCheck.CheckInBackground(DateTime.MinValue, newVersion.ToString(), nullUrl, updateWebPageUrl); }, "Null argument should throw.");
                Assert.Throws<ArgumentNullException>(() => { updateCheck.CheckInBackground(DateTime.MinValue, newVersion.ToString(), restApiUrl, nullUrl); }, "Null argument should throw.");

                updateCheck.WaitForBackgroundCheckComplete();
            }
        }
Exemple #8
0
        public static void TestArgumentNullException()
        {
            string  nullVersion      = null;
            Uri     nullUrl          = null;
            Version thisVersion      = new Version(2, 0, 300, 0);
            Version newVersion       = new Version(2, 0, 307, 0);
            Uri     restApiUrl       = new Uri("http://localhost/RestApi.asxh/axcrypt2version");
            Uri     updateWebPageUrl = new Uri("http://www.axantum.com/");

            using (UpdateCheck updateCheck = new UpdateCheck(thisVersion))
            {
                Assert.Throws <ArgumentNullException>(() => { updateCheck.CheckInBackground(DateTime.MinValue, nullVersion, restApiUrl, updateWebPageUrl); }, "Null argument should throw.");
                Assert.Throws <ArgumentNullException>(() => { updateCheck.CheckInBackground(DateTime.MinValue, newVersion.ToString(), nullUrl, updateWebPageUrl); }, "Null argument should throw.");
                Assert.Throws <ArgumentNullException>(() => { updateCheck.CheckInBackground(DateTime.MinValue, newVersion.ToString(), restApiUrl, nullUrl); }, "Null argument should throw.");

                updateCheck.WaitForBackgroundCheckComplete();
            }
        }
Exemple #9
0
        public static void TestVersionUpdatedWithInvalidVersionFormatFromServer()
        {
            SetupAssembly.FakeRuntimeEnvironment.WebCallerCreator = () =>
            {
                // The version returned has 5 components - bad!
                return(new FakeWebCaller(@"{""U"":""http://localhost/AxCrypt/Downloads.html"",""V"":""2.0.307.0.0"",""R"":307,""S"":0,""M"":""OK""}"));
            };

            DateTime utcNow = DateTime.UtcNow;

            SetupAssembly.FakeRuntimeEnvironment.TimeFunction = () => { return(utcNow); };

            Version          thisVersion      = new Version(2, 0, 300, 0);
            Uri              restApiUrl       = new Uri("http://localhost/RestApi.asxh/axcrypt2version");
            Uri              updateWebPageUrl = new Uri("http://www.axantum.com/");
            VersionEventArgs eventArgs        = null;

            using (UpdateCheck updateCheck = new UpdateCheck(thisVersion))
            {
                updateCheck.VersionUpdate += (object sender, VersionEventArgs e) =>
                {
                    eventArgs = e;
                };
                updateCheck.CheckInBackground(DateTime.MinValue, UpdateCheck.VersionUnknown.ToString(), restApiUrl, updateWebPageUrl);
                updateCheck.WaitForBackgroundCheckComplete();
            }

            Assert.That(eventArgs, Is.Not.Null, "The VersionUpdate event should be called with non-null VersionEventArgs.");
            Assert.That(eventArgs.VersionUpdateStatus, Is.EqualTo(VersionUpdateStatus.LongTimeSinceLastSuccessfulCheck), "This is not a successful check, and it was DateTime.MinValue since the last.");
            Assert.That(eventArgs.UpdateWebpageUrl, Is.EqualTo(new Uri("http://localhost/AxCrypt/Downloads.html")), "The right URL should be passed in the event args.");
            Assert.That(eventArgs.Version, Is.EqualTo(UpdateCheck.VersionUnknown), "The new version has 5 components, and should be parsed as unknown.");

            SetupAssembly.FakeRuntimeEnvironment.WebCallerCreator = () =>
            {
                // The version returned is an empty string - bad!
                return(new FakeWebCaller(@"{""U"":""http://localhost/AxCrypt/Downloads.html"",""V"":"""",""R"":307,""S"":0,""M"":""OK""}"));
            };

            using (UpdateCheck updateCheck = new UpdateCheck(thisVersion))
            {
                updateCheck.VersionUpdate += (object sender, VersionEventArgs e) =>
                {
                    eventArgs = e;
                };
                updateCheck.CheckInBackground(DateTime.MinValue, UpdateCheck.VersionUnknown.ToString(), restApiUrl, updateWebPageUrl);
                updateCheck.WaitForBackgroundCheckComplete();
            }

            Assert.That(eventArgs, Is.Not.Null, "The VersionUpdate event should be called with non-null VersionEventArgs.");
            Assert.That(eventArgs.VersionUpdateStatus, Is.EqualTo(VersionUpdateStatus.LongTimeSinceLastSuccessfulCheck), "This is not a successful check, and it was DateTime.MinValue since the last.");
            Assert.That(eventArgs.UpdateWebpageUrl, Is.EqualTo(new Uri("http://localhost/AxCrypt/Downloads.html")), "The right URL should be passed in the event args.");
            Assert.That(eventArgs.Version, Is.EqualTo(UpdateCheck.VersionUnknown), "The new version is an empty string and should be parse as unknown.");
        }
Exemple #10
0
        public static void TestOnlyOneCallMadeWhenCheckIsMadeWithCheckPending()
        {
            int calls                  = 0;
            ManualResetEvent wait      = new ManualResetEvent(false);
            FakeWebCaller    webCaller = new FakeWebCaller(@"{""U"":""http://localhost/AxCrypt/Downloads.html"",""V"":""2.0.400.0"",""R"":300,""S"":0,""M"":""OK""}");

            webCaller.Calling += (object sender, EventArgs e) => { wait.WaitOne(); ++calls; };
            SetupAssembly.FakeRuntimeEnvironment.WebCallerCreator = () =>
            {
                return(webCaller);
            };

            DateTime utcNow = DateTime.UtcNow;

            SetupAssembly.FakeRuntimeEnvironment.TimeFunction = () => { return(utcNow); };

            Version          thisVersion      = new Version(2, 0, 300, 0);
            Version          newVersion       = new Version(2, 0, 400, 0);
            Uri              restApiUrl       = new Uri("http://localhost/RestApi.asxh/axcrypt2version");
            Uri              updateWebPageUrl = new Uri("http://www.axantum.com/");
            VersionEventArgs eventArgs        = null;

            using (UpdateCheck updateCheck = new UpdateCheck(thisVersion))
            {
                updateCheck.VersionUpdate += (object sender, VersionEventArgs e) =>
                {
                    eventArgs = e;
                };
                updateCheck.CheckInBackground(DateTime.MinValue, UpdateCheck.VersionUnknown.ToString(), restApiUrl, updateWebPageUrl);
                updateCheck.CheckInBackground(DateTime.MinValue, UpdateCheck.VersionUnknown.ToString(), restApiUrl, updateWebPageUrl);
                wait.Set();
                updateCheck.WaitForBackgroundCheckComplete();
            }

            Assert.That(eventArgs, Is.Not.Null, "The VersionUpdate event should be called with non-null VersionEventArgs.");
            Assert.That(eventArgs.VersionUpdateStatus, Is.EqualTo(VersionUpdateStatus.NewerVersionIsAvailable), "One check should be made, indicating a newer version is available.");
            Assert.That(eventArgs.UpdateWebpageUrl, Is.EqualTo(new Uri("http://localhost/AxCrypt/Downloads.html")), "The new URL should be passed since a call is made.");
            Assert.That(calls, Is.EqualTo(1), "The web caller should only be called once.");
            Assert.That(eventArgs.Version, Is.EqualTo(newVersion), "The new version should be passed back, since one call should be made.");
        }
Exemple #11
0
        public static void TestVersionAlreadyCheckedRecently()
        {
            bool          wasCalled = false;
            FakeWebCaller webCaller = new FakeWebCaller(@"{""U"":""http://localhost/AxCrypt/Downloads.html"",""V"":""2.0.400.0"",""R"":300,""S"":0,""M"":""OK""}");

            webCaller.Calling += (object sender, EventArgs e) => { wasCalled = true; };
            SetupAssembly.FakeRuntimeEnvironment.WebCallerCreator = () =>
            {
                return(webCaller);
            };

            DateTime utcNow = DateTime.UtcNow;

            SetupAssembly.FakeRuntimeEnvironment.TimeFunction = () => { return(utcNow); };

            Version          thisVersion      = new Version(2, 0, 300, 0);
            Uri              restApiUrl       = new Uri("http://localhost/RestApi.asxh/axcrypt2version");
            Uri              updateWebPageUrl = new Uri("http://www.axantum.com/");
            VersionEventArgs eventArgs        = null;

            using (UpdateCheck updateCheck = new UpdateCheck(thisVersion))
            {
                updateCheck.VersionUpdate += (object sender, VersionEventArgs e) =>
                {
                    eventArgs = e;
                };
                updateCheck.CheckInBackground(utcNow.AddHours(-1), thisVersion.ToString(), restApiUrl, updateWebPageUrl);
                updateCheck.WaitForBackgroundCheckComplete();
            }

            Assert.That(eventArgs, Is.Not.Null, "The VersionUpdate event should be called with non-null VersionEventArgs.");
            Assert.That(eventArgs.VersionUpdateStatus, Is.EqualTo(VersionUpdateStatus.IsUpToDateOrRecentlyChecked), "No check should be made, and it is assumed this version is up to date.");
            Assert.That(eventArgs.UpdateWebpageUrl, Is.EqualTo(updateWebPageUrl), "The original URL should be passed in the event args since no call is made.");
            Assert.That(wasCalled, Is.False, "The web caller should never be called.");
            Assert.That(eventArgs.Version, Is.EqualTo(thisVersion), "The new version should not be passed back, since no call should be made.");
        }
        public static void TestDoubleDisposeAndObjectDisposedException()
        {
            SetupAssembly.FakeRuntimeEnvironment.WebCallerCreator = () =>
            {
                return new FakeWebCaller(@"{""U"":""http://localhost/AxCrypt/Downloads.html"",""V"":""2.0.307.0"",""R"":307,""S"":0,""M"":""OK""}");
            };

            DateTime utcNow = DateTime.UtcNow;
            SetupAssembly.FakeRuntimeEnvironment.TimeFunction = () => { return utcNow; };

            Version thisVersion = new Version(2, 0, 300, 0);
            Version newVersion = new Version(2, 0, 307, 0);
            Uri restApiUrl = new Uri("http://localhost/RestApi.asxh/axcrypt2version");
            Uri updateWebPageUrl = new Uri("http://www.axantum.com/");
            VersionEventArgs eventArgs = null;
            UpdateCheck updateCheck = new UpdateCheck(thisVersion);
            updateCheck.VersionUpdate += (object sender, VersionEventArgs e) =>
            {
                eventArgs = e;
            };
            updateCheck.CheckInBackground(DateTime.MinValue, UpdateCheck.VersionUnknown.ToString(), restApiUrl, updateWebPageUrl);
            updateCheck.WaitForBackgroundCheckComplete();
            updateCheck.Dispose();

            Assert.DoesNotThrow(updateCheck.Dispose);
            Assert.Throws<ObjectDisposedException>(() => { updateCheck.CheckInBackground(DateTime.MinValue, UpdateCheck.VersionUnknown.ToString(), restApiUrl, updateWebPageUrl); });
            Assert.Throws<ObjectDisposedException>(updateCheck.WaitForBackgroundCheckComplete);

            Assert.That(eventArgs, Is.Not.Null, "The VersionUpdate event should be called with non-null VersionEventArgs.");
            Assert.That(eventArgs.VersionUpdateStatus, Is.EqualTo(VersionUpdateStatus.NewerVersionIsAvailable), "The new version was newer.");
            Assert.That(eventArgs.UpdateWebpageUrl, Is.EqualTo(new Uri("http://localhost/AxCrypt/Downloads.html")), "The right URL should be passed in the event args.");
            Assert.That(eventArgs.Version, Is.EqualTo(newVersion), "The new version should be passed back.");
        }
        public static void TestExceptionDuringVersionCallButRecentlyChecked()
        {
            FakeWebCaller webCaller = new FakeWebCaller(@"{""U"":""http://localhost/AxCrypt/Downloads.html"",""V"":""2.0.400.0"",""R"":300,""S"":0,""M"":""OK""}");
            webCaller.Calling += (object sender, EventArgs e) => { throw new InvalidOperationException("Oops - a forced exception during the call."); };
            SetupAssembly.FakeRuntimeEnvironment.WebCallerCreator = () =>
            {
                return webCaller;
            };

            DateTime utcNow = DateTime.UtcNow;
            SetupAssembly.FakeRuntimeEnvironment.TimeFunction = () => { return utcNow; };

            Version thisVersion = new Version(2, 0, 300, 0);
            Uri restApiUrl = new Uri("http://localhost/RestApi.asxh/axcrypt2version");
            Uri updateWebPageUrl = new Uri("http://www.axantum.com/");
            VersionEventArgs eventArgs = null;
            using (UpdateCheck updateCheck = new UpdateCheck(thisVersion))
            {
                updateCheck.VersionUpdate += (object sender, VersionEventArgs e) =>
                {
                    eventArgs = e;
                };
                updateCheck.CheckInBackground(utcNow.AddDays(-2), UpdateCheck.VersionUnknown.ToString(), restApiUrl, updateWebPageUrl);
                updateCheck.WaitForBackgroundCheckComplete();
            }

            Assert.That(eventArgs, Is.Not.Null, "The VersionUpdate event should be called with non-null VersionEventArgs.");
            Assert.That(eventArgs.VersionUpdateStatus, Is.EqualTo(VersionUpdateStatus.ShortTimeSinceLastSuccessfulCheck), "Although the check failed, a check was recently made a short time ago.");
            Assert.That(eventArgs.UpdateWebpageUrl, Is.EqualTo(updateWebPageUrl), "The original URL should be passed since the call failed.");
            Assert.That(eventArgs.Version, Is.EqualTo(UpdateCheck.VersionUnknown), "An unknown version should be returned, since the call failed.");
        }
        public static void TestInvalidVersionReturned()
        {
            SetupAssembly.FakeRuntimeEnvironment.WebCallerCreator = () =>
            {
                return new FakeWebCaller(@"{""U"":""http://localhost/AxCrypt/Downloads.html"",""V"":""x.y.z.z"",""R"":207,""S"":0,""M"":""OK""}");
            };

            DateTime utcNow = DateTime.UtcNow;
            SetupAssembly.FakeRuntimeEnvironment.TimeFunction = () => { return utcNow; };

            Version thisVersion = new Version(2, 0, 300, 0);
            Uri restApiUrl = new Uri("http://localhost/RestApi.asxh/axcrypt2version");
            Uri updateWebPageUrl = new Uri("http://www.axantum.com/");
            VersionEventArgs eventArgs = null;
            using (UpdateCheck updateCheck = new UpdateCheck(thisVersion))
            {
                updateCheck.VersionUpdate += (object sender, VersionEventArgs e) =>
                {
                    eventArgs = e;
                };
                updateCheck.CheckInBackground(DateTime.MinValue, UpdateCheck.VersionUnknown.ToString(), restApiUrl, updateWebPageUrl);
                updateCheck.WaitForBackgroundCheckComplete();
            }

            Assert.That(eventArgs, Is.Not.Null, "The VersionUpdate event should be called even when an invalid version is returned.");
            Assert.That(eventArgs.VersionUpdateStatus, Is.EqualTo(VersionUpdateStatus.LongTimeSinceLastSuccessfulCheck), "No check has been performed previously and no new version is known.");
            Assert.That(eventArgs.UpdateWebpageUrl, Is.EqualTo(new Uri("http://localhost/AxCrypt/Downloads.html")), "The right URL should be passed in the event args.");
            Assert.That(eventArgs.Version, Is.EqualTo(UpdateCheck.VersionUnknown), "The version is not known since it could not be parsed.");
        }
        public static void TestOnlyOneCallMadeWhenCheckIsMadeWithCheckPending()
        {
            int calls = 0;
            ManualResetEvent wait = new ManualResetEvent(false);
            FakeWebCaller webCaller = new FakeWebCaller(@"{""U"":""http://localhost/AxCrypt/Downloads.html"",""V"":""2.0.400.0"",""R"":300,""S"":0,""M"":""OK""}");
            webCaller.Calling += (object sender, EventArgs e) => { wait.WaitOne(); ++calls; };
            SetupAssembly.FakeRuntimeEnvironment.WebCallerCreator = () =>
            {
                return webCaller;
            };

            DateTime utcNow = DateTime.UtcNow;
            SetupAssembly.FakeRuntimeEnvironment.TimeFunction = () => { return utcNow; };

            Version thisVersion = new Version(2, 0, 300, 0);
            Version newVersion = new Version(2, 0, 400, 0);
            Uri restApiUrl = new Uri("http://localhost/RestApi.asxh/axcrypt2version");
            Uri updateWebPageUrl = new Uri("http://www.axantum.com/");
            VersionEventArgs eventArgs = null;
            using (UpdateCheck updateCheck = new UpdateCheck(thisVersion))
            {
                updateCheck.VersionUpdate += (object sender, VersionEventArgs e) =>
                {
                    eventArgs = e;
                };
                updateCheck.CheckInBackground(DateTime.MinValue, UpdateCheck.VersionUnknown.ToString(), restApiUrl, updateWebPageUrl);
                updateCheck.CheckInBackground(DateTime.MinValue, UpdateCheck.VersionUnknown.ToString(), restApiUrl, updateWebPageUrl);
                wait.Set();
                updateCheck.WaitForBackgroundCheckComplete();
            }

            Assert.That(eventArgs, Is.Not.Null, "The VersionUpdate event should be called with non-null VersionEventArgs.");
            Assert.That(eventArgs.VersionUpdateStatus, Is.EqualTo(VersionUpdateStatus.NewerVersionIsAvailable), "One check should be made, indicating a newer version is available.");
            Assert.That(eventArgs.UpdateWebpageUrl, Is.EqualTo(new Uri("http://localhost/AxCrypt/Downloads.html")), "The new URL should be passed since a call is made.");
            Assert.That(calls, Is.EqualTo(1), "The web caller should only be called once.");
            Assert.That(eventArgs.Version, Is.EqualTo(newVersion), "The new version should be passed back, since one call should be made.");
        }
        public static void TestVersionAlreadyCheckedRecently()
        {
            bool wasCalled = false;
            FakeWebCaller webCaller = new FakeWebCaller(@"{""U"":""http://localhost/AxCrypt/Downloads.html"",""V"":""2.0.400.0"",""R"":300,""S"":0,""M"":""OK""}");
            webCaller.Calling += (object sender, EventArgs e) => { wasCalled = true; };
            SetupAssembly.FakeRuntimeEnvironment.WebCallerCreator = () =>
            {
                return webCaller;
            };

            DateTime utcNow = DateTime.UtcNow;
            SetupAssembly.FakeRuntimeEnvironment.TimeFunction = () => { return utcNow; };

            Version thisVersion = new Version(2, 0, 300, 0);
            Uri restApiUrl = new Uri("http://localhost/RestApi.asxh/axcrypt2version");
            Uri updateWebPageUrl = new Uri("http://www.axantum.com/");
            VersionEventArgs eventArgs = null;
            using (UpdateCheck updateCheck = new UpdateCheck(thisVersion))
            {
                updateCheck.VersionUpdate += (object sender, VersionEventArgs e) =>
                {
                    eventArgs = e;
                };
                updateCheck.CheckInBackground(utcNow.AddHours(-1), thisVersion.ToString(), restApiUrl, updateWebPageUrl);
                updateCheck.WaitForBackgroundCheckComplete();
            }

            Assert.That(eventArgs, Is.Not.Null, "The VersionUpdate event should be called with non-null VersionEventArgs.");
            Assert.That(eventArgs.VersionUpdateStatus, Is.EqualTo(VersionUpdateStatus.IsUpToDateOrRecentlyChecked), "No check should be made, and it is assumed this version is up to date.");
            Assert.That(eventArgs.UpdateWebpageUrl, Is.EqualTo(updateWebPageUrl), "The original URL should be passed in the event args since no call is made.");
            Assert.That(wasCalled, Is.False, "The web caller should never be called.");
            Assert.That(eventArgs.Version, Is.EqualTo(thisVersion), "The new version should not be passed back, since no call should be made.");
        }
        public static void TestVersionSameAndCheckedRecently()
        {
            SetupAssembly.FakeRuntimeEnvironment.WebCallerCreator = () =>
            {
                return new FakeWebCaller(@"{""U"":""http://localhost/AxCrypt/Downloads.html"",""V"":""2.0.300.0"",""R"":300,""S"":0,""M"":""OK""}");
            };

            DateTime utcNow = DateTime.UtcNow;
            SetupAssembly.FakeRuntimeEnvironment.TimeFunction = () => { return utcNow; };

            Version thisVersion = new Version(2, 0, 300, 0);
            Version newVersion = new Version(2, 0, 300, 0);
            Uri restApiUrl = new Uri("http://localhost/RestApi.asxh/axcrypt2version");
            Uri updateWebPageUrl = new Uri("http://www.axantum.com/");
            VersionEventArgs eventArgs = null;
            using (UpdateCheck updateCheck = new UpdateCheck(thisVersion))
            {
                updateCheck.VersionUpdate += (object sender, VersionEventArgs e) =>
                {
                    eventArgs = e;
                };
                updateCheck.CheckInBackground(utcNow.AddDays(-2), UpdateCheck.VersionUnknown.ToString(), restApiUrl, updateWebPageUrl);
                updateCheck.WaitForBackgroundCheckComplete();
            }

            Assert.That(eventArgs, Is.Not.Null, "The VersionUpdate event should be called with non-null VersionEventArgs.");
            Assert.That(eventArgs.VersionUpdateStatus, Is.EqualTo(VersionUpdateStatus.IsUpToDateOrRecentlyChecked), "The new version was the same and we checked recently.");
            Assert.That(eventArgs.UpdateWebpageUrl, Is.EqualTo(new Uri("http://localhost/AxCrypt/Downloads.html")), "The right URL should be passed in the event args.");
            Assert.That(eventArgs.Version, Is.EqualTo(newVersion), "The new version should be passed back.");
        }
        public static void TestVersionUpdatedWithInvalidVersionFormatFromServer()
        {
            SetupAssembly.FakeRuntimeEnvironment.WebCallerCreator = () =>
            {
                // The version returned has 5 components - bad!
                return new FakeWebCaller(@"{""U"":""http://localhost/AxCrypt/Downloads.html"",""V"":""2.0.307.0.0"",""R"":307,""S"":0,""M"":""OK""}");
            };

            DateTime utcNow = DateTime.UtcNow;
            SetupAssembly.FakeRuntimeEnvironment.TimeFunction = () => { return utcNow; };

            Version thisVersion = new Version(2, 0, 300, 0);
            Uri restApiUrl = new Uri("http://localhost/RestApi.asxh/axcrypt2version");
            Uri updateWebPageUrl = new Uri("http://www.axantum.com/");
            VersionEventArgs eventArgs = null;
            using (UpdateCheck updateCheck = new UpdateCheck(thisVersion))
            {
                updateCheck.VersionUpdate += (object sender, VersionEventArgs e) =>
                {
                    eventArgs = e;
                };
                updateCheck.CheckInBackground(DateTime.MinValue, UpdateCheck.VersionUnknown.ToString(), restApiUrl, updateWebPageUrl);
                updateCheck.WaitForBackgroundCheckComplete();
            }

            Assert.That(eventArgs, Is.Not.Null, "The VersionUpdate event should be called with non-null VersionEventArgs.");
            Assert.That(eventArgs.VersionUpdateStatus, Is.EqualTo(VersionUpdateStatus.LongTimeSinceLastSuccessfulCheck), "This is not a successful check, and it was DateTime.MinValue since the last.");
            Assert.That(eventArgs.UpdateWebpageUrl, Is.EqualTo(new Uri("http://localhost/AxCrypt/Downloads.html")), "The right URL should be passed in the event args.");
            Assert.That(eventArgs.Version, Is.EqualTo(UpdateCheck.VersionUnknown), "The new version has 5 components, and should be parsed as unknown.");

            SetupAssembly.FakeRuntimeEnvironment.WebCallerCreator = () =>
            {
                // The version returned is an empty string - bad!
                return new FakeWebCaller(@"{""U"":""http://localhost/AxCrypt/Downloads.html"",""V"":"""",""R"":307,""S"":0,""M"":""OK""}");
            };

            using (UpdateCheck updateCheck = new UpdateCheck(thisVersion))
            {
                updateCheck.VersionUpdate += (object sender, VersionEventArgs e) =>
                {
                    eventArgs = e;
                };
                updateCheck.CheckInBackground(DateTime.MinValue, UpdateCheck.VersionUnknown.ToString(), restApiUrl, updateWebPageUrl);
                updateCheck.WaitForBackgroundCheckComplete();
            }

            Assert.That(eventArgs, Is.Not.Null, "The VersionUpdate event should be called with non-null VersionEventArgs.");
            Assert.That(eventArgs.VersionUpdateStatus, Is.EqualTo(VersionUpdateStatus.LongTimeSinceLastSuccessfulCheck), "This is not a successful check, and it was DateTime.MinValue since the last.");
            Assert.That(eventArgs.UpdateWebpageUrl, Is.EqualTo(new Uri("http://localhost/AxCrypt/Downloads.html")), "The right URL should be passed in the event args.");
            Assert.That(eventArgs.Version, Is.EqualTo(UpdateCheck.VersionUnknown), "The new version is an empty string and should be parse as unknown.");
        }