Robust and secure programming

Quick links

3.1

Programming

3.2.1

Data types

3.2.2

Programming concepts

3.2.3

Arithmetic operations

3.2.4

Relational operations

3.2.5

Boolean operations

3.2.6

Data structures

3.2.7

Input/output and file handling

3.2.8

String handling operations

3.2.9

Random number generation

3.2.10

Subroutines

3.2.11

Structured programming

3.2.12

Robust and secure programming

3.3.13

Classification of programming languages

 

Syllabus content

Content   Additional Information
Be able to write simple data validation routines.   Students should be able to use data validation techniques to write simple routines that check the validity of data being entered by a user. The following validation checks are examples of simple data validation routines: • checking if an entered string has a minimum length • checking if a string is empty • checking if data entered lies within a given range (eg between 1 and 10).
     
Be able to write simple authentication routines.   Students should be able to write a simple authentication routine that uses a username and password. Students will only be required to use plain text usernames and passwords (ie students will not need to encrypt the passwords).
     
Be able to select suitable test data that covers normal (typical), boundary (extreme) and erroneous data. Be able to justify the choice of test data    

 

Adapting the menu

Robust code.
“Robust programming, also called bomb-proof programming, is a style of programming that prevents abnormal termination or unexpected actions. Basically, it requires code to handle bad (invalid or absurd) inputs in a reasonable way.”

Making code robust

While the menu works, it only works if the user types in 1, 2 or 3. If they type other characters such as “r”, the program will crash, as shown below.

You need to write code that is “robust”. Robust code will collect or “trap” user input that would otherwise break the program and redirect the user to try again.

In the example above any non-digit will crash the program and any number other than 1 or 2 will exit the program.
Ideally we want the program to trap these input errors and then ask the user to try again.

Python has the keywords “try” and “except” that can be used to trap errors. Place “try” at the start of the data entry and selection process and “except” at the end. If there is any error generated by inaccurate data entry than it will be captured by this change to the code.

This technique can be used almost anywhere where data is being input by a user.
The code that follows the “except” is the code that you want the program to execute if there is an issue with data entry.

Rounded Rectangular Callout: A message is shown on screen and the show_menu() line displays the menu again.
What about the other issue of typing in a number that is outside the range of the accepted values. In this case typing 6 has the same effect as typing 3 as the “else” is a catchall selection.


In this example I have added a while statement that will run until a valid input value has been entered by the user.

Create a function to check data entry

There may be an occasion when you have to check specific numeric data that is being entered to make sure that it is in a set range of values. You can use a function for this.

For example setting the range for an input value to be say, between 5 and 25

The function has 3 input parameters, the prompt that asks the question, the minimum value and the maximum value.

Selection is set to -99 at the start to force the while statement to be executed.

The “while” statement ensures that the selection made by the user is in the range between the “minimum value” and the “maximum value” and the “try” – “except” instruction will capture any non-numeric values.

The minimum and maximum values have also been used in the error comment.

Here you can see the function in use. A value “r” has been used without the program crashing, a value (3) below the minimum and another (30) above the maximum have been rejected and the final value 12 has been accepted. Testing is always a crucial part of program development.

This also demonstrates the full range of testing required for a function or feature of your programming; always test the extreme values at the upper and lower bound, just below the lower bound and just above the upper bound (these last two values should be rejected) a typical value and a vlaue that has no merit at all such as a letter when a number is required and a number when a letter is required or punctuation when either is required.

Be able to write simple authentication routines.

The following segment of code enables a user to add a username/ password combination to a file of passwords and then check that the combination is correct when entered by comparing both the password and the username to the values stored in a file.

In your coursework you may be asked to do something similar.

decide is a variable that stores the users choice when asked do you want to add a new password? If the user elects Y (for yes obvioulsy) then the programm will ask fir a username and a password, run a double typing check (verification process that makes the user type the same thing twice to ensure that they have actually typed what they wanted and not what they thought they wanted) and then the credentials (password and username combination) are saved to a file called passwords.txt.

the variable a_or_w allows the user to decide whether or not to append the credentials (i.e. add then to the existing file) or to write the credentials (i.e replace the existing file with the new credentials)

link is the name I have chosen for the connection between this program and the passwords.txt file.

The use of +"," in the write command ensures that even if there are spaces in the credentials, each one is separated with a comma. If you wish you could change this to be other punctuation.

myfile is also the name I have chosen for the connection between this program and the passwords.txt file but in this case I am using the connection to read the file. All of the file is read in to a variable called detail that stores the credentials as a series of characters.

credential[] is an array that will store each of the credentials as alternating usernames and passwords.

word is a variable that will store each credential as it is read from the text file. To beginwith it must be set to "" (no value - the empty string).

The line for i in detail: reads the text file (now stored in detail) one character at a time. Each character is checked and if it is not a comma then it is added to the variable word. If it is a comma then the word is added to the credential array and the word is set back to the empty string.

access is a variable that stores an output string "password or username not found." that can be modified if the username is found but the password is wrong or the credentials are correct.

You are free to modify this code as you wish.

3.1 Fundamentals of algorithms

3.2 Programming

3.3 Fundamentals of data representation

3.4 Computer systems

3.5 Fundamentals of computer networks

3.6 Fundamentals of cyber security

3.7 Ethical, legal and environmental impacts of digital technology on wider society, including issues of privacy

3.8 Aspects of software development

Glossary and other links

Glossary of computing terms.

AQA 8520: The 2016 syllabus

AQA pseudocode guide