public override async Task OnExecuteAsync(ProgramOptions pOptions, CommandOptions cOptions, CancellationToken cToken)
        {
            Console.WriteLine("Scanning for web ports...");

            if (!Enum.TryParse(pOptions.Status, out PortStatus portStatus))
            {
                Console.WriteLine("Invalid port status.");
                return;
            }

            try
            {
                var portScanner = new WebPortScanner();
                var settings    = new ScanProperties(pOptions.MinPort, pOptions.MaxPort, portStatus);

                var scanResult = await portScanner.ScanAsync(settings, cToken);

                new PortStatusPrinter().PrintTable(scanResult);

                Console.WriteLine("\nDone.");
            }
            catch (Exception e)
            {
                Console.WriteLine($"Command 'webPort', ran into exception: {e.Message}");
            }
        }
Example #2
0
        public void Test_InvalidPortScanRange(int minPort, int maxPort)
        {
            var wpScanner = new WebPortScanner();

            Assert.ThrowsAsync <ArgumentException>(async() =>
            {
                var scanProperties = new ScanProperties
                {
                    MinPort = minPort,
                    MaxPort = maxPort
                };

                await wpScanner.ScanAsync(scanProperties, CancellationToken.None);
            });
        }
Example #3
0
        public void Test_MaxPortLimit()
        {
            var wpScanner = new WebPortScanner();

            Assert.ThrowsAsync <ArgumentOutOfRangeException>(async() =>
            {
                var scanProperties = new ScanProperties
                {
                    MinPort = 1,
                    MaxPort = 65536
                };

                await wpScanner.ScanAsync(scanProperties, CancellationToken.None);
            });
        }
Example #4
0
        public void Test_ValidPortScanRange(int minPort, int maxPort)
        {
            var wpScanner  = new WebPortScanner();
            var totalPorts = (maxPort - minPort) + 1;

            var scanProperties = new ScanProperties
            {
                MinPort = minPort,
                MaxPort = maxPort
            };

            var actual = wpScanner.ScanAsync(scanProperties, CancellationToken.None)
                         .GetAwaiter()
                         .GetResult()
                         .Count();

            Assert.Equal(totalPorts, actual);
        }
        public void Test_PortScanRangeEquals(int minPort, int maxPort)
        {
            var wpScanner = new WebPortScanner();
            var cancellationTokenSource = new CancellationTokenSource();
            var cToken = cancellationTokenSource.Token;

            IList <IPrintablePortStatus> sResult = default;

            try
            {
                var task = wpScanner.ScanAsync(new ScanProperties(minPort, maxPort, PortStatus.Any), cToken);
                sResult = task.Result.ToList();
            }
            catch (Exception e)
            {
                Assert.True(false, e.Message);
            }

            Assert.Equal((maxPort - minPort) + 1, sResult.Count);
        }
        public void Test_ValidPortScanRange()
        {
            var wpScanner = new WebPortScanner();
            var cancellationTokenSource = new CancellationTokenSource();
            var cToken = cancellationTokenSource.Token;

            try
            {
                var task   = wpScanner.ScanAsync(new ScanProperties(15, 30, PortStatus.Any), cToken);
                var result = task.Result;
            }
            catch (Exception e)
            {
                Assert.True(false, e.Message);
            }
            finally
            {
                cancellationTokenSource.Dispose();
            }

            Assert.True(true);
        }
        public void Test_ValidPortStatus(PortStatus status)
        {
            const int minPort = 0;
            const int maxPort = 3000;

            var wpScanner = new WebPortScanner();
            var cancellationTokenSource = new CancellationTokenSource();
            var cToken = cancellationTokenSource.Token;

            IList <IPrintablePortStatus> sResult = default;

            try
            {
                var task = wpScanner.ScanAsync(new ScanProperties(minPort, maxPort, status), cToken);
                sResult = task.Result.ToList();
            }
            catch (Exception e)
            {
                Assert.True(false, e.Message);
            }
            finally
            {
                cancellationTokenSource.Dispose();
            }

            bool statusOk;

            if (status == PortStatus.Any)
            {
                statusOk = ((maxPort - minPort) + 1) == sResult.Count;
            }
            else
            {
                var portStatusString = status.ToString();
                statusOk = sResult.All(x => x.GetStatusString() == portStatusString);
            }

            Assert.True(statusOk);
        }
        public void Test_InvalidPortScanRange()
        {
            var wpScanner = new WebPortScanner();
            var cancellationTokenSource = new CancellationTokenSource();
            var cToken = cancellationTokenSource.Token;

            try
            {
                Assert.ThrowsAsync <ArgumentException>(async() =>
                {
                    var scanProperties = new ScanProperties(30, 15, PortStatus.Any);
                    await wpScanner.ScanAsync(scanProperties, cToken);
                });
            }
            catch (Exception e)
            {
                Assert.True(false, e.Message);
            }
            finally
            {
                cancellationTokenSource.Dispose();
            }
        }
        public void Test_MinPortLimit()
        {
            var webScanner = new WebPortScanner();
            var cancellationTokenSource = new CancellationTokenSource();
            var cToken = cancellationTokenSource.Token;

            try
            {
                Assert.ThrowsAsync <ArgumentOutOfRangeException>(async() =>
                {
                    var scanProperties = new ScanProperties(-1, 100, PortStatus.Any);
                    await webScanner.ScanAsync(scanProperties, cToken);
                });
            }
            catch (Exception e)
            {
                Assert.True(false, e.Message);
            }
            finally
            {
                cancellationTokenSource.Dispose();
            }
        }