public void TestIpv4() { IDeviceSpecsValidator deviceSpecsValidator = ServiceProvider.GetService <IDeviceSpecsValidator>(); Assert.True(deviceSpecsValidator.IsValidIPv4Address("122.33.5.56")); Assert.True(deviceSpecsValidator.IsValidIPv4Address("127.0.0.1")); Assert.False(deviceSpecsValidator.IsValidIPv4Address(null)); Assert.False(deviceSpecsValidator.IsValidIPv4Address("")); Assert.False(deviceSpecsValidator.IsValidIPv4Address("1")); Assert.False(deviceSpecsValidator.IsValidIPv4Address("12.4")); Assert.False(deviceSpecsValidator.IsValidIPv4Address("12.7.4")); Assert.False(deviceSpecsValidator.IsValidIPv4Address("12.563.21.3")); Assert.False(deviceSpecsValidator.IsValidIPv4Address("256.0.21.56")); Assert.False(deviceSpecsValidator.IsValidIPv4Address("f1ac:5242:1b9b:fcf1:de3a:2391:f8e8:fd5a")); }
private GatewayDto GetInputModel() { GatewayDto gatewayDto = new GatewayDto(); Console.Write("\nSerial number: "); gatewayDto.SerialNumber = Console.ReadLine(); while (string.IsNullOrEmpty(gatewayDto.SerialNumber)) { Console.WriteLine("\nSerial number must not be empty"); Console.Write("\nSerial number: "); gatewayDto.SerialNumber = Console.ReadLine(); } Console.Write("\nFirmware version: "); gatewayDto.FirmwareVersion = Console.ReadLine(); if (string.IsNullOrEmpty(gatewayDto.FirmwareVersion)) { gatewayDto.FirmwareVersion = null; } Console.Write("\nState: "); gatewayDto.State = Console.ReadLine(); if (string.IsNullOrEmpty(gatewayDto.State)) { gatewayDto.State = null; } Console.Write("\nIP: "); gatewayDto.IP = Console.ReadLine(); if (string.IsNullOrEmpty(gatewayDto.IP)) { gatewayDto.IP = null; } if (gatewayDto.IP != null) { while (!_deviceSpecsValidator.IsValidIPv4Address(gatewayDto.IP) && !_deviceSpecsValidator.IsValidIPv6Address(gatewayDto.IP)) { Console.WriteLine("\nInvalid IP address"); Console.Write("\nIP: "); gatewayDto.IP = Console.ReadLine(); } } Console.Write("\nPort: "); string portInput = Console.ReadLine(); if (!string.IsNullOrEmpty(portInput)) { bool validPort = int.TryParse(portInput, out int port) && _deviceSpecsValidator.IsValidPort(port); while (!validPort) { Console.WriteLine("\nInvalid port number"); Console.Write("\nPort: "); portInput = Console.ReadLine(); validPort = int.TryParse(portInput, out port) && _deviceSpecsValidator.IsValidPort(port); } gatewayDto.Port = port; } Console.WriteLine("Registering...\n"); return(gatewayDto); }