From 12334dcd3f6461e51caec5aaf2e296bded8af4f6 Mon Sep 17 00:00:00 2001 From: kb1lqc Date: Sun, 11 Mar 2018 00:02:29 -0800 Subject: [PATCH 01/10] Adding checkUserInput() per #30 to start validating user input * Added checkUserInput() * Checked callsign * Checked ID * Checked IP Address --- faradayio_cli/faradayio_cli.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/faradayio_cli/faradayio_cli.py b/faradayio_cli/faradayio_cli.py index 5b25131..11fb2dd 100644 --- a/faradayio_cli/faradayio_cli.py +++ b/faradayio_cli/faradayio_cli.py @@ -7,6 +7,7 @@ import threading import time import pytun +import ipaddress from faradayio.faraday import Monitor from faradayio.faraday import SerialTestClass @@ -47,6 +48,36 @@ def setupArgparse(): # Parse and return arguments return parser.parse_args() +def checkUserInput(args): + """Checks user input for validity + + Args: + args: argparse arguments + + """ + # Check callsign + # Expect a string + if not isinstance(args.callsign, str): + raise TypeError + # Callsigns are at most seven characters long + if len(args.callsign) > 7: + raise ValueError + + # Check ID + # Expect and integer + if not isinstance(args.id, int): + raise TypeError + # Expect a value between 0-255 + if args.id < 0 or args.id > 255: + raise ValueError + + # Check IP Address + # Expect a string + if not isinstance(args.addr, str): + raise TypeError + # Expect an IP address that is valid + ipaddress.IPv4Address(args.addr) + def setupSerialPort(loopback, port, baud, readtimeout, writetimeout): """Sets up serial port by connecting to phsyical or software port. @@ -95,6 +126,8 @@ def main(): except argparse.ArgumentError as error: raise SystemExit(error) + checkUserInput(args) + # Setup serial port try: serialPort = setupSerialPort(loopback=args.loopback, From 9e84abf6c40829349caabb87b3bb1aa139ec25d1 Mon Sep 17 00:00:00 2001 From: kb1lqc Date: Sun, 11 Mar 2018 00:47:17 -0800 Subject: [PATCH 02/10] Added baud rate user input value checking per #30 * Added baud rate value and type checking * Changed argparse input type to integer --- faradayio_cli/faradayio_cli.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/faradayio_cli/faradayio_cli.py b/faradayio_cli/faradayio_cli.py index 11fb2dd..7e3f102 100644 --- a/faradayio_cli/faradayio_cli.py +++ b/faradayio_cli/faradayio_cli.py @@ -32,7 +32,7 @@ def setupArgparse(): # Optional arguments parser.add_argument("--addr", default="10.0.0.1", help="Set IP Address of TUN adapter (Faraday Radio)") - parser.add_argument("-b", "--baud", default="115200", + parser.add_argument("-b", "--baud", default=115200, type=int, help="Set serial port baud rate") parser.add_argument("-l", "--loopback", action="store_true", help="Use software loopback serial port") @@ -78,6 +78,17 @@ def checkUserInput(args): # Expect an IP address that is valid ipaddress.IPv4Address(args.addr) + # Check Baud Rate + # Expect and integer + if not isinstance(args.baud, int): + raise TypeError + # Expect and integer that is a standard serial value + # Should be able to use argparse choices too + baudrate = [50, 75, 110, 134, 150, 200, 600, 1200, 1800, 2400, 4800, 9600, + 19200, 38400, 57600, 115200, 230400, 460800, 500000, 57600, + 921600] + if args.baud not in baudrate: + raise ValueError def setupSerialPort(loopback, port, baud, readtimeout, writetimeout): """Sets up serial port by connecting to phsyical or software port. From 1df4b66c9a829bd04136be53f653f6c7943883ca Mon Sep 17 00:00:00 2001 From: kb1lqc Date: Sun, 11 Mar 2018 10:48:21 -0700 Subject: [PATCH 03/10] Added check for loopback type * Expect boolean on `l`, `--loopback` option --- faradayio_cli/faradayio_cli.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/faradayio_cli/faradayio_cli.py b/faradayio_cli/faradayio_cli.py index 7e3f102..71c7165 100644 --- a/faradayio_cli/faradayio_cli.py +++ b/faradayio_cli/faradayio_cli.py @@ -90,6 +90,12 @@ def checkUserInput(args): if args.baud not in baudrate: raise ValueError + # Check loopback True/False + # Expect a boolean + if not isinstance(args.loopback, bool): + raise TypeError + + def setupSerialPort(loopback, port, baud, readtimeout, writetimeout): """Sets up serial port by connecting to phsyical or software port. From 90af4fe2734f97f362c272a4732e450eeaf7e963 Mon Sep 17 00:00:00 2001 From: kb1lqc Date: Sun, 11 Mar 2018 11:06:20 -0700 Subject: [PATCH 04/10] Added MTU size check * Convert mtu to int at argparse * Check mtu for int * Bheck for mtu >= 68 bytes * Check for mtu <= 65535 bytes --- faradayio_cli/faradayio_cli.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/faradayio_cli/faradayio_cli.py b/faradayio_cli/faradayio_cli.py index 71c7165..45961d0 100644 --- a/faradayio_cli/faradayio_cli.py +++ b/faradayio_cli/faradayio_cli.py @@ -36,7 +36,7 @@ def setupArgparse(): help="Set serial port baud rate") parser.add_argument("-l", "--loopback", action="store_true", help="Use software loopback serial port") - parser.add_argument("-m", "--mtu", default=1500, + parser.add_argument("-m", "--mtu", default=1500, type=int, help="Set Maximum Transmission Unit (MTU)") parser.add_argument("-p", "--port", default="/dev/ttyUSB0", help="Physical serial port of radio") @@ -95,6 +95,14 @@ def checkUserInput(args): if not isinstance(args.loopback, bool): raise TypeError + # Check Maximum Transmission Unit (MTU) + # Expect and integer + if not isinstance(args.mtu, int): + raise TypeError + # Expect a value between 68-65535 per RFC 791 + if not 68 <= args.mtu <= 65535: + raise ValueError("--mtu must be between 68 and 65535 bytes") + def setupSerialPort(loopback, port, baud, readtimeout, writetimeout): """Sets up serial port by connecting to phsyical or software port. From ac022604d381ae676161c8ce31de504bba1b798b Mon Sep 17 00:00:00 2001 From: kb1lqc Date: Sun, 11 Mar 2018 11:15:07 -0700 Subject: [PATCH 05/10] Added exception information per #30 exceptions * Went back and added helpful information for each exception where possible to indicate to user what happened. * Updated some value checking to be more pythonic --- faradayio_cli/faradayio_cli.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/faradayio_cli/faradayio_cli.py b/faradayio_cli/faradayio_cli.py index 45961d0..6686a71 100644 --- a/faradayio_cli/faradayio_cli.py +++ b/faradayio_cli/faradayio_cli.py @@ -58,50 +58,50 @@ def checkUserInput(args): # Check callsign # Expect a string if not isinstance(args.callsign, str): - raise TypeError + raise TypeError("callsign must be a string") # Callsigns are at most seven characters long - if len(args.callsign) > 7: - raise ValueError + if not 3 <= len(args.callsign) <= 7: + raise ValueError("callsign must be between 3 and 7 characters long") # Check ID # Expect and integer if not isinstance(args.id, int): - raise TypeError + raise TypeError("id must be an integer") # Expect a value between 0-255 - if args.id < 0 or args.id > 255: - raise ValueError + if not 0 <= args.id <= 255: + raise ValueError("id must be between 0 and 255") # Check IP Address # Expect a string if not isinstance(args.addr, str): - raise TypeError + raise TypeError("IP address must be a string") # Expect an IP address that is valid ipaddress.IPv4Address(args.addr) # Check Baud Rate # Expect and integer if not isinstance(args.baud, int): - raise TypeError + raise TypeError("baud rate must be an integer") # Expect and integer that is a standard serial value # Should be able to use argparse choices too baudrate = [50, 75, 110, 134, 150, 200, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 500000, 57600, 921600] if args.baud not in baudrate: - raise ValueError + raise ValueError("baud rate must be a standard value per --help") # Check loopback True/False # Expect a boolean if not isinstance(args.loopback, bool): - raise TypeError + raise TypeError("loopback must be a boolean") # Check Maximum Transmission Unit (MTU) # Expect and integer if not isinstance(args.mtu, int): - raise TypeError + raise TypeError("mtu must be an integer") # Expect a value between 68-65535 per RFC 791 if not 68 <= args.mtu <= 65535: - raise ValueError("--mtu must be between 68 and 65535 bytes") + raise ValueError("mtu must be between 68 and 65535 bytes") def setupSerialPort(loopback, port, baud, readtimeout, writetimeout): From 34943831179b0718c4c84afd8769b1be5771a2c7 Mon Sep 17 00:00:00 2001 From: kb1lqc Date: Sun, 11 Mar 2018 11:17:47 -0700 Subject: [PATCH 06/10] Added serial port path check per #30 * Check that the serial port path is a string --- faradayio_cli/faradayio_cli.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/faradayio_cli/faradayio_cli.py b/faradayio_cli/faradayio_cli.py index 6686a71..372d520 100644 --- a/faradayio_cli/faradayio_cli.py +++ b/faradayio_cli/faradayio_cli.py @@ -103,6 +103,11 @@ def checkUserInput(args): if not 68 <= args.mtu <= 65535: raise ValueError("mtu must be between 68 and 65535 bytes") + # Check serial port path value + # Expect a string + if not isinstance(args.port, str): + raise TypeError("serial port path must be a string") + def setupSerialPort(loopback, port, baud, readtimeout, writetimeout): """Sets up serial port by connecting to phsyical or software port. From 03b6067aaaadd1d3f7ca59733beeaf042e9e9511 Mon Sep 17 00:00:00 2001 From: kb1lqc Date: Sun, 11 Mar 2018 11:36:18 -0700 Subject: [PATCH 07/10] Added pyserial read timeout checks per #30 * Check for integer and NoneType values, however, Nonetype is currently impossible to get per design of argparse options * Check for integer value creater than or equal to zero --- faradayio_cli/faradayio_cli.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/faradayio_cli/faradayio_cli.py b/faradayio_cli/faradayio_cli.py index 372d520..443455c 100644 --- a/faradayio_cli/faradayio_cli.py +++ b/faradayio_cli/faradayio_cli.py @@ -108,6 +108,19 @@ def checkUserInput(args): if not isinstance(args.port, str): raise TypeError("serial port path must be a string") + # Check timeout + # Expect and integer + if not isinstance(args.timeout, int): + if not isinstance(args.timeout, type(None)): + # TODO: Likely cannot be NoneType yet per argparse design + raise TypeError("id must be an integer or Nonetype") + # Expect a value between 68-65535 per RFC 791 + if isinstance(args.timeout, int): + if not 0 <= args.mtu + raise ValueError("read timeout must be a positive value") + + + def setupSerialPort(loopback, port, baud, readtimeout, writetimeout): """Sets up serial port by connecting to phsyical or software port. From 4c587ac192003173f11cd93657d5f3fe5806b4f5 Mon Sep 17 00:00:00 2001 From: kb1lqc Date: Sun, 11 Mar 2018 11:38:09 -0700 Subject: [PATCH 08/10] Added write timeout check per #30, fixed wording errors in read timeout * Added writetimeout checks similar to readtimeout * Fixed read timeout notes to remove copy/paste errors --- faradayio_cli/faradayio_cli.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/faradayio_cli/faradayio_cli.py b/faradayio_cli/faradayio_cli.py index 443455c..ea2a75c 100644 --- a/faradayio_cli/faradayio_cli.py +++ b/faradayio_cli/faradayio_cli.py @@ -113,12 +113,23 @@ def checkUserInput(args): if not isinstance(args.timeout, int): if not isinstance(args.timeout, type(None)): # TODO: Likely cannot be NoneType yet per argparse design - raise TypeError("id must be an integer or Nonetype") - # Expect a value between 68-65535 per RFC 791 + raise TypeError("read timeout must be an integer or Nonetype") + # Expect a value greater than zero if isinstance(args.timeout, int): if not 0 <= args.mtu raise ValueError("read timeout must be a positive value") + # Check write timeout + # Expect and integer + if not isinstance(args.writetimeout, int): + if not isinstance(args.writetimeout, type(None)): + # TODO: Likely cannot be NoneType yet per argparse design + raise TypeError("write timeout must be an integer or Nonetype") + # Expect a value greater than zero + if isinstance(args.writetimeout, int): + if not 0 <= args.mtu + raise ValueError("write timeout must be a positive value") + From 29425a049da68900e5a071f9f4e8c75d22b51679 Mon Sep 17 00:00:00 2001 From: kb1lqc Date: Sun, 11 Mar 2018 11:41:21 -0700 Subject: [PATCH 09/10] Fixed incorrect variable names and if statements * Used copy paste erros with mtu arguments being checked for timeouts * Fixed if checks which were missing ending colon --- faradayio_cli/faradayio_cli.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/faradayio_cli/faradayio_cli.py b/faradayio_cli/faradayio_cli.py index ea2a75c..570e081 100644 --- a/faradayio_cli/faradayio_cli.py +++ b/faradayio_cli/faradayio_cli.py @@ -116,7 +116,7 @@ def checkUserInput(args): raise TypeError("read timeout must be an integer or Nonetype") # Expect a value greater than zero if isinstance(args.timeout, int): - if not 0 <= args.mtu + if not 0 <= args.timeout: raise ValueError("read timeout must be a positive value") # Check write timeout @@ -127,7 +127,7 @@ def checkUserInput(args): raise TypeError("write timeout must be an integer or Nonetype") # Expect a value greater than zero if isinstance(args.writetimeout, int): - if not 0 <= args.mtu + if not 0 <= args.writetimeout: raise ValueError("write timeout must be a positive value") From 43565ef6a26d05fad1bb296b1826574772712d36 Mon Sep 17 00:00:00 2001 From: kb1lqc Date: Sun, 11 Mar 2018 11:44:25 -0700 Subject: [PATCH 10/10] Fixed linting errors found with flake8 --- faradayio_cli/faradayio_cli.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/faradayio_cli/faradayio_cli.py b/faradayio_cli/faradayio_cli.py index 570e081..d8737a1 100644 --- a/faradayio_cli/faradayio_cli.py +++ b/faradayio_cli/faradayio_cli.py @@ -48,6 +48,7 @@ def setupArgparse(): # Parse and return arguments return parser.parse_args() + def checkUserInput(args): """Checks user input for validity @@ -131,8 +132,6 @@ def checkUserInput(args): raise ValueError("write timeout must be a positive value") - - def setupSerialPort(loopback, port, baud, readtimeout, writetimeout): """Sets up serial port by connecting to phsyical or software port.