public void WhenCallingConnectedAndExceptionShouldReturnFalse()
        {
            var networkInterface = new NetworkInterface();
            NetworkInterface.NetworkInterfaces = new INetworkInterface[] { networkInterface };

            Assert.False(this.ConnectionInfo.Connected);
        }
        public void WhenNetworkInterfaceWithIpExistsShouldReturnFalseForHasConnection()
        {
            var networkInterface = new NetworkInterface();
            networkInterface.CurrentIpAddress = new System.Net.IPAddress(0);
            NetworkInterface.NetworkInterfaces = new INetworkInterface[] { networkInterface };

            Assert.False(this.ConnectionInfo.Connected);
        }
        public void WhenCallingMobileAndOnlyWifiAvailableShouldReturnFalse()
        {
            var networkInterface = new NetworkInterface();
            networkInterface.CurrentIpAddress = new System.Net.IPAddress(42);
            networkInterface.Name = "wifi";
            NetworkInterface.NetworkInterfaces = new INetworkInterface[] { networkInterface };

            Assert.False(this.ConnectionInfo.Mobile);
        }
        public void WhenCallingMobileAndOnlyGPRSAvailableButNotConnectedShouldReturnFalse()
        {
            var networkInterface = new NetworkInterface();
            networkInterface.CurrentIpAddress = new System.Net.IPAddress(0);
            networkInterface.Name = "cellular line";
            NetworkInterface.NetworkInterfaces = new INetworkInterface[] { networkInterface };

            Assert.False(this.ConnectionInfo.Mobile);
        }
        public void WhenCallingPostWithVaidUrlShouldSucceedUsingHttpsAndAcceptAllPolicy()
        {
            var networkInterface = new NetworkInterface();
            networkInterface.CurrentIpAddress = new IPAddress(42);
            NetworkInterface.NetworkInterfaces = new INetworkInterface[] { networkInterface };

            var httpClient = new HttpClient(new AcceptAllCertificatePolicy());
            var message = new Message(new Uri("https://httpbin.org/post"), "{ \"lat\":51.527516, \"lng\":-0.715806, \"utc\":1362065860 }");
            message.Headers.Add("content-type", "application/json");
            message.Headers.Add("param1", "Value1");
            message.Headers.Add("param2", "Value2");
            message.Headers.Add("param3", "Value3");
            var result = httpClient.Post(message);

            Assert.Equal(Result.Ok, result);
        }
        public void WhenCallingPostWithInvalidURLSchemeShouldReturnPermanentError()
        {
            var networkInterface = new NetworkInterface();
            networkInterface.CurrentIpAddress = new IPAddress(42);
            NetworkInterface.NetworkInterfaces = new INetworkInterface[] { networkInterface };

            var httpClient = new HttpClient(null);
            var message = new Message(new Uri("test://httpbin.org/post"), "{ \"lat\":51.527516, \"lng\":-0.715806, \"utc\":1362065860 }");
            message.Headers.Add("content-type", "application/json");
            message.Headers.Add("param1", "Value1");
            message.Headers.Add("param2", "Value2");
            message.Headers.Add("param3", "Value3");
            var result = httpClient.Post(message);

            Assert.Equal(Result.PermanentError, result);
        }
        public GivenAForwardService()
        {
            NetworkInterface.NetworkInterfaces = new INetworkInterface[0];
            this.Preferences = new Preferences();
            this.Preferences.ResetCellularDataBlackout();
            this.Preferences.ResetWlanBlackoutRange();
            this.Preferences.ResetWwanBlackoutRange();
            this.Preferences.ResetRoamingBlackout();
            this.Preferences.ResetOnChargeBlackout();
            this.Preferences.ResetOffChargeBlackout();

            var networkInterface = new NetworkInterface();
            networkInterface.CurrentIpAddress = new IPAddress(42);
            networkInterface.Name = "wifi";

            NetworkInterface.NetworkInterfaces = new INetworkInterface[] { networkInterface };

            this.PersistentStore = new SQLiteRepository();
            this.HttpClient = new Mock<IHttpClient>();
            this.NetworkStateService = new Mock<INetworkStateService>();
            this.PeriodicBackoutCheck = new Mock<IPeriodicBackoutCheck>();
            this.Logger = new Mock<IReynaLogger>();

            this.WaitHandle = new AutoResetEventAdapter(false);

            File.Delete(this.DatabasePath);
            this.HttpClient.Setup(c => c.Post(It.IsAny<IMessage>()))
                .Returns(Result.Ok);

            this.PersistentStore.Initialise();

            this.PeriodicBackoutCheck.Setup(p => p.IsTimeElapsed("ForwardService", 100))
                .Returns(true);

            this.ForwardService = new ForwardService(this.PersistentStore, this.HttpClient.Object, this.NetworkStateService.Object, this.WaitHandle, 100, 0, false, this.Logger.Object);
            this.ForwardService.PeriodicBackoutCheck = this.PeriodicBackoutCheck.Object;

            Microsoft.Win32.Registry.LocalMachine.DeleteSubKey(@"Software\Reyna\PeriodicBackoutCheck", false);
            Microsoft.Win32.Registry.LocalMachine.DeleteSubKey(@"Software\Reyna", false);
        }
        public void WhenCallingWifiAndUsingBluetoothShouldReturnFalse()
        {
            var usbNetworkInterface = new NetworkInterface();
            usbNetworkInterface.Name = "SS1VNDIS1";

            var lanNetworkInterface = new NetworkInterface();
            lanNetworkInterface.Speed = 10000000;

            NetworkInterface.NetworkInterfaces = new INetworkInterface[] { usbNetworkInterface, lanNetworkInterface };
            Assert.False(this.ConnectionInfo.Wifi);
        }
        public void WhenCallingWifiAndNotUsingWifiShouldReturnFalse()
        {
            var usbNetworkInterface = new NetworkInterface();
            usbNetworkInterface.Name = "USB Cable";

            var lanNetworkInterface = new NetworkInterface();
            lanNetworkInterface.Speed = 10000000;

            NetworkInterface.NetworkInterfaces = new INetworkInterface[] { usbNetworkInterface, lanNetworkInterface };
            Assert.False(this.ConnectionInfo.Wifi);
        }
        public void WhenCallingWifiAndMoreThanOneNetworkInterfaceExistsAndNoneIsWirelessNetworkInterface()
        {
            var usbNetworkInterface = new NetworkInterface();
            usbNetworkInterface.Name = "USB Cable";

            var lanNetworkInterface = new NetworkInterface();
            lanNetworkInterface.Speed = 10000000;

            var wirelessInterface = new NetworkInterface();
            wirelessInterface.Name = "WIRELESS";

            NetworkInterface.NetworkInterfaces = new INetworkInterface[] { usbNetworkInterface, lanNetworkInterface, wirelessInterface };

            Assert.True(this.ConnectionInfo.Wifi);
        }
        public void WhenCallingWifiAndMoreThanOneNetworkInterfaceExistsAndFirstOneIsUSBSkipItAndUseFirstWireless()
        {
            var networkInterface = new NetworkInterface();
            networkInterface.Name = "USB Cable";
            var wirelessInterface = new WirelessNetworkInterface();

            NetworkInterface.NetworkInterfaces = new INetworkInterface[] { networkInterface, wirelessInterface };

            Assert.True(this.ConnectionInfo.Wifi);
        }
        public void WhenCallingWifiAndGPRSNetworkInterfaceExistsShouldReturnFalse(string name)
        {
            var networkInterface = new NetworkInterface();
            networkInterface.Name = name;
            NetworkInterface.NetworkInterfaces = new INetworkInterface[] { networkInterface };

            Assert.False(this.ConnectionInfo.Wifi);
        }
        public void WhenCallingWifiAndLanNetworkInterfaceExistsShouldReturnFalse(int speed)
        {
            var networkInterface = new NetworkInterface();
            networkInterface.Speed = speed;
            NetworkInterface.NetworkInterfaces = new INetworkInterface[] { networkInterface };

            Assert.False(this.ConnectionInfo.Wifi);
        }
        public void WhenCallingPostAndConnectionNotMobile()
        {
            var networkInterface = new NetworkInterface();
            networkInterface.CurrentIpAddress = null;
            networkInterface.Name = "cellular line";
            var from = new Time();
            var to = new Time(from.MinuteOfDay + 1);
            this.Preferences.SetCellularDataBlackout(new TimeRange(from, to));

            ConnectionInfo connectionInfo = new ConnectionInfo();

            Assert.False(connectionInfo.Mobile);
        }
        public void WhenCallingStartAndNotConnectedShouldNotCallPostOnHttpClientOrDeleteMessage()
        {
            var networkInterface = new NetworkInterface();
            networkInterface.CurrentIpAddress = null;
            NetworkInterface.NetworkInterfaces = new INetworkInterface[] { networkInterface };

            var message = this.CreateMessage();
            this.ForwardService.Start();

            this.PersistentStore.Add(message);
            Thread.Sleep(200);

            Assert.NotNull(this.PersistentStore.Get());
            this.HttpClient.Verify(c => c.Post(It.IsAny<IMessage>()), Times.Never());
        }
        public void WhenCallingCanSendShouldReturnTrue()
        {
            var networkInterface = new NetworkInterface();
            networkInterface.CurrentIpAddress = new IPAddress(42);
            NetworkInterface.NetworkInterfaces = new INetworkInterface[] { networkInterface };

            Assert.True(HttpClient.CanSend() == Result.Ok);
        }
        public void WhenCallingCanSendAndOffChargeBlackoutAndIsChargingReturnOk()
        {
            var networkInterface = new NetworkInterface();
            networkInterface.CurrentIpAddress = new IPAddress(42);
            networkInterface.Name = "wifi";
            NetworkInterface.NetworkInterfaces = new INetworkInterface[] { networkInterface };

            NativeMethods.ACLineStatus = 1;
            NativeMethods.SystemPowerStatusExResult = 1;
            this.Preferences.SetOffChargeBlackout(true);

            Assert.True(HttpClient.CanSend() == Result.Ok);
        }
        public void WhenCallingCanSendAndNotRoamingBlackoutReturnOk()
        {
            var networkInterface = new NetworkInterface();
            networkInterface.CurrentIpAddress = new IPAddress(42);
            networkInterface.Name = "cellular line";
            NetworkInterface.NetworkInterfaces = new INetworkInterface[] { networkInterface };

            this.Preferences.SetRoamingBlackout(false);
            var registry = new Registry();
            registry.SetDWord(Microsoft.Win32.Registry.LocalMachine, "System\\State\\Phone", "Status", 0x200 | 0x32);

            Assert.True(HttpClient.CanSend() == Result.Ok);
            Microsoft.Win32.Registry.LocalMachine.DeleteSubKeyTree("System\\State\\Phone");
        }
        public void WhenCallingCanSendAndOutsideWwanBlackoutReturnOk()
        {
            var networkInterface = new NetworkInterface();
            networkInterface.CurrentIpAddress = new IPAddress(42);
            networkInterface.Name = "cellular line";
            NetworkInterface.NetworkInterfaces = new INetworkInterface[] { networkInterface };
            this.Preferences.SetWwanBlackoutRange("02:00-02:01");

            Assert.True(HttpClient.CanSend() == Result.Ok);
        }
        public void WhenCallingCanSendAndInsideWlanBlackoutReturnBlackout()
        {
            var networkInterface = new NetworkInterface();
            networkInterface.CurrentIpAddress = new IPAddress(42);
            networkInterface.Name = "wifi";
            NetworkInterface.NetworkInterfaces = new INetworkInterface[] { networkInterface };
            this.Preferences.SetWlanBlackoutRange("00:00-23:59");

            Assert.True(HttpClient.CanSend() == Result.Blackout);
        }
        public void WhenCallingMobileAndThrowsShouldReturnFalse()
        {
            var networkInterface = new NetworkInterface();
            networkInterface.Name = "cellular line";

            NetworkInterface.NetworkInterfaces = new INetworkInterface[] { networkInterface };

            Assert.False(this.ConnectionInfo.Mobile);
        }
        public void WhenCallingPostAndConnectionIsNotGPRSAndBlackoutShouldReturnBlackout()
        {
            var networkInterface = new NetworkInterface();
            networkInterface.CurrentIpAddress = new IPAddress(42);
            networkInterface.Name = "wifi";
            var from = new Time();
            var to = new Time(from.MinuteOfDay + 1);
            this.Preferences.SetCellularDataBlackout(new TimeRange(from, to));

            NetworkInterface.NetworkInterfaces = new INetworkInterface[] { networkInterface };

            var httpClient = new HttpClient(new AcceptAllCertificatePolicy());
            var message = new Message(new Uri("https://httpbin.org/post"), "{ \"lat\":51.527516, \"lng\":-0.715806, \"utc\":1362065860 }");
            message.Headers.Add("content-type", "application/json");
            message.Headers.Add("param1", "Value1");
            message.Headers.Add("param2", "Value2");
            message.Headers.Add("param3", "Value3");
            var result = httpClient.Post(message);

            Assert.Equal(Result.Ok, result);
        }
        public void WhenCallingMobileAndGPRSAndWifiConnectedShouldReturnFalse()
        {
            var networkInterface = new NetworkInterface();
            networkInterface.CurrentIpAddress = new System.Net.IPAddress(43);
            networkInterface.Name = "cellular line";

            var wifi = new NetworkInterface();
            wifi.CurrentIpAddress = new System.Net.IPAddress(43);
            wifi.Name = "wifi";

            NetworkInterface.NetworkInterfaces = new INetworkInterface[] { networkInterface, wifi };

            Assert.False(this.ConnectionInfo.Mobile);
        }
        public void WhenCallingStartAndItIsBlackoutShouldNotCallPostOnHttpClientOrDeleteMessage()
        {
            var networkInterface = new NetworkInterface();
            networkInterface.CurrentIpAddress = new IPAddress(42);
            networkInterface.Name = "cellular line";
            var from = new Time();
            var to = new Time(from.MinuteOfDay + 1);
            this.Preferences.SetCellularDataBlackout(new TimeRange(from, to));
            NetworkInterface.NetworkInterfaces = new INetworkInterface[] { networkInterface };

            var message = this.CreateMessage();
            this.ForwardService.Start();

            this.PersistentStore.Add(message);
            Thread.Sleep(200);

            Assert.NotNull(this.PersistentStore.Get());
            this.HttpClient.Verify(c => c.Post(It.IsAny<IMessage>()), Times.Never());
        }