From c135603ed0fd1ccdba844b698ec7a3999221769b Mon Sep 17 00:00:00 2001 From: fdai7207 Date: Sat, 28 Jan 2023 21:38:46 +0100 Subject: [PATCH 01/16] implement unit tests for the functions agePermission() and checkIfInteger() --- build-project.sh | 5 +- project.yml | 101 +++++++++++++++++++++++++++++ src/main.c | 2 +- src/mainMenu.c | 42 +++++++++++++ src/mainMenu.h | 13 ++++ test/support/.gitkeep | 0 test/test_mainMenu.c | 143 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 303 insertions(+), 3 deletions(-) mode change 100644 => 100755 build-project.sh create mode 100644 project.yml create mode 100644 src/mainMenu.c create mode 100644 src/mainMenu.h create mode 100644 test/support/.gitkeep create mode 100644 test/test_mainMenu.c diff --git a/build-project.sh b/build-project.sh old mode 100644 new mode 100755 index c75f560..4247c59 --- a/build-project.sh +++ b/build-project.sh @@ -1,5 +1,6 @@ clear +ceedling test:all cd src/ -gcc main.c +gcc main.c mainMenu.c ./a.out -rm a.out \ No newline at end of file +rm a.out diff --git a/project.yml b/project.yml new file mode 100644 index 0000000..e253248 --- /dev/null +++ b/project.yml @@ -0,0 +1,101 @@ +--- + +# Notes: +# Sample project C code is not presently written to produce a release artifact. +# As such, release build options are disabled. +# This sample, therefore, only demonstrates running a collection of unit tests. + +:project: + :use_exceptions: FALSE + :use_test_preprocessor: TRUE + :use_auxiliary_dependencies: TRUE + :build_root: build +# :release_build: TRUE + :test_file_prefix: test_ + :which_ceedling: gem + :ceedling_version: 0.31.1 + :default_tasks: + - test:all + +#:test_build: +# :use_assembly: TRUE + +#:release_build: +# :output: MyApp.out +# :use_assembly: FALSE + +:environment: + +:extension: + :executable: .out + +:paths: + :test: + - +:test/** + - -:test/support + :source: + - src/** + :support: + - test/support + :libraries: [] + +:defines: + # in order to add common defines: + # 1) remove the trailing [] from the :common: section + # 2) add entries to the :common: section (e.g. :test: has TEST defined) + :common: &common_defines [] + :test: + - *common_defines + - TEST + :test_preprocess: + - *common_defines + - TEST + +:cmock: + :mock_prefix: mock_ + :when_no_prototypes: :warn + :enforce_strict_ordering: TRUE + :plugins: + - :ignore + - :callback + :treat_as: + uint8: HEX8 + uint16: HEX16 + uint32: UINT32 + int8: INT8 + bool: UINT8 + +# Add -gcov to the plugins list to make sure of the gcov plugin +# You will need to have gcov and gcovr both installed to make it work. +# For more information on these options, see docs in plugins/gcov +:gcov: + :reports: + - HtmlDetailed + :gcovr: + :html_medium_threshold: 75 + :html_high_threshold: 90 + +#:tools: +# Ceedling defaults to using gcc for compiling, linking, etc. +# As [:tools] is blank, gcc will be used (so long as it's in your system path) +# See documentation to configure a given toolchain for use + +# LIBRARIES +# These libraries are automatically injected into the build process. Those specified as +# common will be used in all types of builds. Otherwise, libraries can be injected in just +# tests or releases. These options are MERGED with the options in supplemental yaml files. +:libraries: + :placement: :end + :flag: "-l${1}" + :path_flag: "-L ${1}" + :system: [] # for example, you might list 'm' to grab the math library + :test: [] + :release: [] + +:plugins: + :load_paths: + - "#{Ceedling.load_path}" + :enabled: + - stdout_pretty_tests_report + - module_generator +... diff --git a/src/main.c b/src/main.c index a6b9b24..3f845e6 100644 --- a/src/main.c +++ b/src/main.c @@ -3,4 +3,4 @@ int main() { return 0; -} \ No newline at end of file +} diff --git a/src/mainMenu.c b/src/mainMenu.c new file mode 100644 index 0000000..54d20ad --- /dev/null +++ b/src/mainMenu.c @@ -0,0 +1,42 @@ +#include"mainMenu.h" + + +bool agePermission(int age){ + + if(age >= 18) + { + + return true; + + } + + else + { + + return false; + + } + +} + +bool checkIfInteger(char* input){ + + char *end_pointer; + + strtol(input, &end_pointer, 10); + + if (end_pointer == input || *end_pointer != '\0') + { + + return false; + + } + + else + { + + return true; + + } +} + diff --git a/src/mainMenu.h b/src/mainMenu.h new file mode 100644 index 0000000..61192be --- /dev/null +++ b/src/mainMenu.h @@ -0,0 +1,13 @@ +#ifndef MAINMENU_H_ +#define MAINMENU_H_ + +#include +#include +#include +#include + +bool agePermission(int age); +bool checkIfInteger(char* userInput); + +#endif + diff --git a/test/support/.gitkeep b/test/support/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test/test_mainMenu.c b/test/test_mainMenu.c new file mode 100644 index 0000000..515237d --- /dev/null +++ b/test/test_mainMenu.c @@ -0,0 +1,143 @@ +#ifdef TEST + +#include "unity.h" + +#include "mainMenu.h" + +void setUp(void) +{ +} + +void tearDown(void) +{ +} +void test_agePermissionValidAge(void) +{ + //Test case : 0 + + //Arrange + + int validAge[83]; + + bool validAgeResult[83]; + + int j=0; + + for(int i =18;i<101;i++){ + + validAge[j]= i; + j++; + } + + //Act + + for(int i=0;i<83;i++){ + + validAgeResult[i] = agePermission(validAge[i]); + } + + //Assert + + for(int i=0;i<83;i++){ + + TEST_ASSERT_TRUE(validAgeResult[i]); + + } + +} + +void test_agePermissionInvalidAge(void) +{ + + //Test case : 1 + + //Arrange + + int invalidAge[117]; + + bool invalidAgeResult[117]; + + int j=0; + + for(int i =-100;i<18;i++){ + + invalidAge[j]= i; + j++; + } + + //Act + + for(int i=0;i<117;i++){ + + invalidAgeResult[i] = agePermission(invalidAge[i]); + + } + + //Assert + + for(int i=0;i<117;i++){ + + TEST_ASSERT_FALSE(invalidAgeResult[i]); + + } + +} + +void test_IsInteger(void) +{ + + //Arrange + + char* inputIsInteger[] = {"-10000000","-2000000","-354698","-66667","-7878","-987","-64","-5","0","1","2","10","201","333","4321","56974","698751","7878989","88954621" }; + + bool inputIsIntegerResult[19]; + + //Act + + for(int i=0;i<19;i++) + { + + inputIsIntegerResult[i] = checkIfInteger(inputIsInteger[i]); + + } + + //Assert + + for(int i=0;i<19;i++) + { + + TEST_ASSERT_TRUE(inputIsIntegerResult[i]); + + } + +} + +void test_IsNotInteger(void) +{ + + //Arrange + + char* inputIsNotInteger[] = {"0.15","3.141592653589793238","5.3254f","-6.264","-7878.3261","foo","Bar","FIZZ","buzZ","joHN","jAnE","foo-bar","3,15","2k13",""," ","-","+","/*-+.,/=" }; + + bool inputIsNotIntegerResult[19]; + + //Act + + for(int i=0;i<19;i++) + { + + inputIsNotIntegerResult[i] = checkIfInteger(inputIsNotInteger[i]); + + } + + //Assert + + for(int i=0;i<19;i++) + { + + TEST_ASSERT_FALSE(inputIsNotIntegerResult[i]); + + } + +} +#endif // TEST From dcee631e6b743f9d08c96bf0945cc97291f714ff Mon Sep 17 00:00:00 2001 From: fdai7207 Date: Sun, 29 Jan 2023 00:00:06 +0100 Subject: [PATCH 02/16] implement the functions ageInput() and showMenu() to execute mainMenu.c . --- build-project.sh | 6 ++--- src/main.c | 4 +++- src/mainMenu.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ src/mainMenu.h | 3 +++ 4 files changed, 68 insertions(+), 4 deletions(-) diff --git a/build-project.sh b/build-project.sh index 4247c59..ea712c3 100755 --- a/build-project.sh +++ b/build-project.sh @@ -1,6 +1,6 @@ clear ceedling test:all cd src/ -gcc main.c mainMenu.c -./a.out -rm a.out +gcc -o main main.c mainMenu.c +./main +rm main diff --git a/src/main.c b/src/main.c index 3f845e6..b37734e 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,8 @@ -#include +#include "mainMenu.h" int main() { + + ageInput(); return 0; } diff --git a/src/mainMenu.c b/src/mainMenu.c index 54d20ad..9cd19c0 100644 --- a/src/mainMenu.c +++ b/src/mainMenu.c @@ -39,4 +39,63 @@ bool checkIfInteger(char* input){ } } +void ageInput(){ + char input[20]; + char* input_pointer; + + long age; + + printf("\nPlease specify your age : "); + scanf("%s",input); + + + while (true) + { + age = strtol(input,&input_pointer,10); + + if(checkIfInteger(input) == true && agePermission(age)== true) + { + age = strtol(input,&input_pointer,10); + + printf("Access granted!\n\n\n\n"); + + showMenu(); + + break; + + } + + else if(checkIfInteger(input) == true && agePermission(age)== false) + { + + printf("You should be at least 18 years old to create a bank account!\n"); + + break; + + } + + else + { + printf("input invalid! try again!\n"); + + scanf("%s",input); + + } + + } + +} + + +void showMenu(){ + + printf("\t\t\t\t\t\t\t Welcome to Bank Manager!"); + printf("\n\n\n\n\t\t\t\t\t\tPlease select one of the following functions!"); + printf("\n\n\n\n\t\t\t\t\t\t ->Login as an existing costumer."); + printf("\n\n\t\t\t\t\t\t ->Register as a new costumer."); + printf("\n\n\t\t\t\t\t\t ->Login as an Employee."); + printf("\n\n\t\t\t\t\t\t\t\t ->Exit.\n"); + printf("\n\n\n\n\n Selection :\n"); + +} diff --git a/src/mainMenu.h b/src/mainMenu.h index 61192be..ccc7d9c 100644 --- a/src/mainMenu.h +++ b/src/mainMenu.h @@ -9,5 +9,8 @@ bool agePermission(int age); bool checkIfInteger(char* userInput); +void ageInput(); +void showMenu(); + #endif From cf756de8b96281c35aa2efb3dd8912357c69c3b2 Mon Sep 17 00:00:00 2001 From: fdai7207 Date: Sun, 29 Jan 2023 00:25:24 +0100 Subject: [PATCH 03/16] refactoring: changed some variable names, made code more readable and enhanced some functions --- src/mainMenu.c | 59 ++++++++++++++++++-------------------------------- 1 file changed, 21 insertions(+), 38 deletions(-) diff --git a/src/mainMenu.c b/src/mainMenu.c index 9cd19c0..dd249fc 100644 --- a/src/mainMenu.c +++ b/src/mainMenu.c @@ -1,62 +1,44 @@ #include"mainMenu.h" -bool agePermission(int age){ +bool agePermission(int age) +{ - if(age >= 18) - { - - return true; - - } - - else - { - - return false; - - } + return age >= 18; } -bool checkIfInteger(char* input){ - - char *end_pointer; - - strtol(input, &end_pointer, 10); - - if (end_pointer == input || *end_pointer != '\0') - { +bool checkIfInteger(char* userInput) +{ - return false; + char *endPointer; - } + /*the endPointer points to the first non-integer character signaled to stop conversion*/ - else - { + strtol(userInput, &endPointer, 10); - return true; + return !(endPointer == userInput || *endPointer != '\0'); - } } -void ageInput(){ +void ageInput() +{ - char input[20]; - char* input_pointer; + char* userInput = malloc(20*sizeof(char*)); + char* userInputPointer; long age; printf("\nPlease specify your age : "); - scanf("%s",input); + scanf("%s",userInput); while (true) { - age = strtol(input,&input_pointer,10); + age = strtol(userInput,&userInputPointer,10); - if(checkIfInteger(input) == true && agePermission(age)== true) + if((checkIfInteger(userInput))&& (agePermission(age))) { - age = strtol(input,&input_pointer,10); + //age = strtol(userInput,&userInputPointer,10); printf("Access granted!\n\n\n\n"); @@ -66,7 +48,7 @@ void ageInput(){ } - else if(checkIfInteger(input) == true && agePermission(age)== false) + else if((checkIfInteger(userInput)) && !(agePermission(age))) { printf("You should be at least 18 years old to create a bank account!\n"); @@ -79,7 +61,7 @@ void ageInput(){ { printf("input invalid! try again!\n"); - scanf("%s",input); + scanf("%s",userInput); } @@ -88,7 +70,8 @@ void ageInput(){ } -void showMenu(){ +void showMenu() +{ printf("\t\t\t\t\t\t\t Welcome to Bank Manager!"); printf("\n\n\n\n\t\t\t\t\t\tPlease select one of the following functions!"); From 8543ec2ec2705a60a59b5e5b92891ce30602985f Mon Sep 17 00:00:00 2001 From: fdai7207 Date: Sun, 29 Jan 2023 01:08:46 +0100 Subject: [PATCH 04/16] refactoring: removed redundant code in the unit tests to improve readability and maintainability. --- test/test_mainMenu.c | 142 +++++++++++++++++++------------------------ 1 file changed, 64 insertions(+), 78 deletions(-) diff --git a/test/test_mainMenu.c b/test/test_mainMenu.c index 515237d..eb93bb7 100644 --- a/test/test_mainMenu.c +++ b/test/test_mainMenu.c @@ -6,138 +6,124 @@ void setUp(void) { + } void tearDown(void) { + } + void test_agePermissionValidAge(void) { //Test case : 0 - //Arrange - - int validAge[83]; - - bool validAgeResult[83]; + /*Arrange*/ - int j=0; + int Age = 18; - for(int i =18;i<101;i++){ + bool validAgeResult[83]; - validAge[j]= i; - j++; - } - - //Act + /*Act*/ - for(int i=0;i<83;i++){ - - validAgeResult[i] = agePermission(validAge[i]); + for(int i =0;i<83;i++){ + + validAgeResult[i]= Age + i; } - - //Assert + + + /*Assert*/ for(int i=0;i<83;i++){ - - TEST_ASSERT_TRUE(validAgeResult[i]); - - } + TEST_ASSERT_TRUE(validAgeResult[i]); + + } + } void test_agePermissionInvalidAge(void) { //Test case : 1 - - //Arrange - + + /*Arrange*/ + int invalidAge[117]; - + bool invalidAgeResult[117]; - - int j=0; - - for(int i =-100;i<18;i++){ - - invalidAge[j]= i; - j++; + + + for(int i =-100;i<18;i++) + { + + invalidAge[i+100]= i; } - - //Act - - for(int i=0;i<117;i++){ - + + /*Act*/ + + for(int i=0;i<117;i++) + { + invalidAgeResult[i] = agePermission(invalidAge[i]); } - - //Assert - - for(int i=0;i<117;i++){ - + + /*Assert*/ + + for(int i=0;i<117;i++) + { + TEST_ASSERT_FALSE(invalidAgeResult[i]); - - } + + } } void test_IsInteger(void) { - - //Arrange - - char* inputIsInteger[] = {"-10000000","-2000000","-354698","-66667","-7878","-987","-64","-5","0","1","2","10","201","333","4321","56974","698751","7878989","88954621" }; - bool inputIsIntegerResult[19]; + //test case 0 - //Act - - for(int i=0;i<19;i++) - { + /*Arrange*/ - inputIsIntegerResult[i] = checkIfInteger(inputIsInteger[i]); - - } + char* inputIsInteger[] = {"-10000000","-2000000","-354698","-66667","-7878","-987","-64","-5","0","1","2","10","201","333","4321","56974","698751","7878989","88954621" }; + + bool inputIsIntegerExpected = true; - //Assert + /*Act and Assert*/ for(int i=0;i<19;i++) { + bool inputIsIntegerResult = checkIfInteger(inputIsInteger[i]); - TEST_ASSERT_TRUE(inputIsIntegerResult[i]); + TEST_ASSERT_EQUAL(inputIsIntegerExpected,inputIsIntegerResult); - } + } } void test_IsNotInteger(void) { + //test case 1 + + /*Arrange*/ - //Arrange - char* inputIsNotInteger[] = {"0.15","3.141592653589793238","5.3254f","-6.264","-7878.3261","foo","Bar","FIZZ","buzZ","joHN","jAnE","foo-bar","3,15","2k13",""," ","-","+","/*-+.,/=" }; - - bool inputIsNotIntegerResult[19]; - - //Act - - for(int i=0;i<19;i++) - { - - inputIsNotIntegerResult[i] = checkIfInteger(inputIsNotInteger[i]); - - } - - //Assert - + + bool inputIsNotIntegerExpected = false; + + /*Act and Assert*/ + + for(int i=0;i<19;i++) { - - TEST_ASSERT_FALSE(inputIsNotIntegerResult[i]); - + bool inputIsNotIntegerResult = checkIfInteger(inputIsNotInteger[i]); + + TEST_ASSERT_EQUAL(inputIsNotIntegerExpected,inputIsNotIntegerResult); + } + } #endif // TEST From ceccdd60a9c47576822201d15f245325391a29f0 Mon Sep 17 00:00:00 2001 From: fdai7207 Date: Mon, 30 Jan 2023 15:13:57 +0100 Subject: [PATCH 05/16] implement unit tests for the function chooseOption(). --- .gitignore | 3 +- src/mainMenu.c | 20 +++++++- src/mainMenu.h | 1 + test/test_mainMenu.c | 111 ++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 126 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 496ee2c..dce188f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.DS_Store \ No newline at end of file +.DS_Store +.swp diff --git a/src/mainMenu.c b/src/mainMenu.c index dd249fc..eca4c34 100644 --- a/src/mainMenu.c +++ b/src/mainMenu.c @@ -1,4 +1,4 @@ -#include"mainMenu.h" +#include "mainMenu.h" bool agePermission(int age) @@ -20,6 +20,22 @@ bool checkIfInteger(char* userInput) return !(endPointer == userInput || *endPointer != '\0'); } + +bool chooseOption(int choiceInput) +{ + + if(choiceInput < 1 || choiceInput > 4) + { + return false; + } + + else + { + return true; + } + +} + void ageInput() { @@ -82,3 +98,5 @@ void showMenu() printf("\n\n\n\n\n Selection :\n"); } + + diff --git a/src/mainMenu.h b/src/mainMenu.h index ccc7d9c..6974d21 100644 --- a/src/mainMenu.h +++ b/src/mainMenu.h @@ -8,6 +8,7 @@ bool agePermission(int age); bool checkIfInteger(char* userInput); +bool chooseOption(int choiceInput); void ageInput(); void showMenu(); diff --git a/test/test_mainMenu.c b/test/test_mainMenu.c index eb93bb7..ea65cc2 100644 --- a/test/test_mainMenu.c +++ b/test/test_mainMenu.c @@ -26,7 +26,7 @@ void test_agePermissionValidAge(void) /*Act*/ - for(int i =0;i<83;i++){ + for(int i =0; i < 83; i++){ validAgeResult[i]= Age + i; } @@ -34,7 +34,7 @@ void test_agePermissionValidAge(void) /*Assert*/ - for(int i=0;i<83;i++){ + for(int i=0; i < 83; i++){ TEST_ASSERT_TRUE(validAgeResult[i]); @@ -54,7 +54,7 @@ void test_agePermissionInvalidAge(void) bool invalidAgeResult[117]; - for(int i =-100;i<18;i++) + for(int i =-100; i < 18; i++) { invalidAge[i+100]= i; @@ -62,7 +62,7 @@ void test_agePermissionInvalidAge(void) /*Act*/ - for(int i=0;i<117;i++) + for(int i=0; i < 117; i++) { invalidAgeResult[i] = agePermission(invalidAge[i]); @@ -71,7 +71,7 @@ void test_agePermissionInvalidAge(void) /*Assert*/ - for(int i=0;i<117;i++) + for(int i=0; i < 117; i++) { TEST_ASSERT_FALSE(invalidAgeResult[i]); @@ -93,7 +93,7 @@ void test_IsInteger(void) /*Act and Assert*/ - for(int i=0;i<19;i++) + for(int i=0; i < 19; i++) { bool inputIsIntegerResult = checkIfInteger(inputIsInteger[i]); @@ -116,7 +116,7 @@ void test_IsNotInteger(void) /*Act and Assert*/ - for(int i=0;i<19;i++) + for(int i=0; i<19; i++) { bool inputIsNotIntegerResult = checkIfInteger(inputIsNotInteger[i]); @@ -126,4 +126,101 @@ void test_IsNotInteger(void) } +void test_validChoiceInput(void) +{ + //test case 0 + + /*Arrange*/ + + int validInput[4]; + + bool validInputResult[4]; + + /*Act*/ + + for(int i = 0; i < 4; i++) + { + validInput[i] = i + 1; + } + + for(int i = 0; i < 4; i++) + { + validInputResult[i] = chooseOption(validInput[i]); + } + + /*Assert*/ + + for(int i = 0; i < 4; i++) + { + TEST_ASSERT_TRUE(validInputResult[i]); + } + +} + +void test_invalidChoiceInput_firstCase(void) +{ + // test case 1 + + /*Arrange*/ + + int invalidInput[100]; + + bool invalidInputResult[100]; + + /*Act*/ + + for(int i = -100; i < 0; i++) + { + invalidInput[i+100] = i; + } + + for(int i = 0; i < 100; i++) + { + invalidInputResult[i] = chooseOption(invalidInput[i]); + } + + /*Assert*/ + + for(int i = 0; i < 100; i++) + { + TEST_ASSERT_FALSE(invalidInputResult[i]); + } + +} + +void test_invalidChoiceInput_secondCase(void) +{ + // test case 2 + + /*Arrange*/ + + int invalidInput[100]; + + bool invalidInputResult[100]; + + /*Act*/ + + for(int i = 0; i < 100; i++) + { + invalidInput[i] = i + 5; + } + + for(int i = 0; i < 100; i++) + { + invalidInputResult[i] = chooseOption(invalidInput[i]); + } + + /*Assert*/ + + for(int i = 0; i < 100; i++) + { + TEST_ASSERT_FALSE(invalidInputResult[i]); + } + +} + + + + + #endif // TEST From ac4b64166e22e13d84cbcb54ae071125302817b5 Mon Sep 17 00:00:00 2001 From: fdai7207 Date: Mon, 30 Jan 2023 19:44:01 +0100 Subject: [PATCH 06/16] implement the function menuInput(), in order for the user to choose a banking option. --- src/mainMenu.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++- src/mainMenu.h | 1 + 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/mainMenu.c b/src/mainMenu.c index eca4c34..fdb0aa8 100644 --- a/src/mainMenu.c +++ b/src/mainMenu.c @@ -54,12 +54,13 @@ void ageInput() if((checkIfInteger(userInput))&& (agePermission(age))) { - //age = strtol(userInput,&userInputPointer,10); printf("Access granted!\n\n\n\n"); showMenu(); + menuInput(); + break; } @@ -84,6 +85,57 @@ void ageInput() } } +void menuInput() +{ + + char choiceInput[20]; + + char* choiceInputPointer; + + int selection; + + scanf("%s",choiceInput); + + while(true) + { + selection = strtol(choiceInput,&choiceInputPointer,10); + + if(chooseOption(selection) == true && checkIfInteger(choiceInput) == true) + { + switch(selection) + { + + case 1 : printf("\nLoginAsCostumer() function will be implemented here soon\n\n"); + + break; + + case 2 : printf("\ncreateCostumerAccount() function will be implemented here soon\n\n"); + + break; + + case 3 : printf("\nLoginAsEmployee() function will be implemented here soon\n\n"); + + break; + + case 4 : printf("\e[1;1H\e[2J"); + + printf("\nsee you next time !\n\n"); + + break; + + } + break; + } + + else + { + printf("Input invalid! try again!\n"); + + scanf("%s",choiceInput); + } + } + +} void showMenu() diff --git a/src/mainMenu.h b/src/mainMenu.h index 6974d21..0824d42 100644 --- a/src/mainMenu.h +++ b/src/mainMenu.h @@ -12,6 +12,7 @@ bool chooseOption(int choiceInput); void ageInput(); void showMenu(); +void menuInput(); #endif From 0e8d805605d65c8352f05aefa025ef31386a9a69 Mon Sep 17 00:00:00 2001 From: fdai7207 Date: Mon, 30 Jan 2023 20:01:49 +0100 Subject: [PATCH 07/16] refactoring: made code more readable and enhanced the menuInput() and chooseOption() functions. --- src/mainMenu.c | 70 +++++++++++++++++--------------------------------- 1 file changed, 24 insertions(+), 46 deletions(-) diff --git a/src/mainMenu.c b/src/mainMenu.c index fdb0aa8..7b60ecf 100644 --- a/src/mainMenu.c +++ b/src/mainMenu.c @@ -24,15 +24,7 @@ bool checkIfInteger(char* userInput) bool chooseOption(int choiceInput) { - if(choiceInput < 1 || choiceInput > 4) - { - return false; - } - - else - { - return true; - } + return !(choiceInput < 1 || choiceInput > 4); } @@ -50,6 +42,8 @@ void ageInput() while (true) { + /*the userInput string is changed to a number with the strtol function*/ + age = strtol(userInput,&userInputPointer,10); if((checkIfInteger(userInput))&& (agePermission(age))) @@ -85,56 +79,40 @@ void ageInput() } } + void menuInput() { - char choiceInput[20]; - char* choiceInputPointer; - int selection; - scanf("%s",choiceInput); + scanf("%s", choiceInput); + selection = strtol(choiceInput, &choiceInputPointer, 10); - while(true) + while (!checkIfInteger(choiceInput) || !chooseOption(selection)) { - selection = strtol(choiceInput,&choiceInputPointer,10); - - if(chooseOption(selection) == true && checkIfInteger(choiceInput) == true) - { - switch(selection) - { + printf("Input invalid! try again!\n"); - case 1 : printf("\nLoginAsCostumer() function will be implemented here soon\n\n"); + scanf("%s", choiceInput); - break; - - case 2 : printf("\ncreateCostumerAccount() function will be implemented here soon\n\n"); - - break; - - case 3 : printf("\nLoginAsEmployee() function will be implemented here soon\n\n"); - - break; - - case 4 : printf("\e[1;1H\e[2J"); - - printf("\nsee you next time !\n\n"); - - break; - - } - break; - } + selection = strtol(choiceInput, &choiceInputPointer, 10); + } + + switch(selection) + { + case 1 : printf("\nLoginAsCostumer() function will be implemented here soon\n\n"); + break; - else - { - printf("Input invalid! try again!\n"); + case 2 : printf("\ncreateCostumerAccount() function will be implemented here soon\n\n"); + break; - scanf("%s",choiceInput); - } - } + case 3 : printf("\nLoginAsEmployee() function will be implemented here soon\n\n"); + break; + case 4 : printf("\e[1;1H\e[2J"); + printf("\nsee you next time !\n\n"); + break; + } } From eee36c1fda7192a609446d0b0e8e9d785544a21d Mon Sep 17 00:00:00 2001 From: fdai7207 Date: Tue, 31 Jan 2023 14:30:31 +0100 Subject: [PATCH 08/16] refactoring: removed redundant code in the unit tests of the chooseOption() function. --- test/test_mainMenu.c | 47 +++++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/test/test_mainMenu.c b/test/test_mainMenu.c index ea65cc2..af93eaf 100644 --- a/test/test_mainMenu.c +++ b/test/test_mainMenu.c @@ -134,25 +134,23 @@ void test_validChoiceInput(void) int validInput[4]; - bool validInputResult[4]; + bool validInputExpected = true; - /*Act*/ + for(int i = 0; i < 4; i++) { validInput[i] = i + 1; } + /*Act and Asssert*/ + for(int i = 0; i < 4; i++) { - validInputResult[i] = chooseOption(validInput[i]); - } - /*Assert*/ + bool validInputResult = chooseOption(validInput[i]); - for(int i = 0; i < 4; i++) - { - TEST_ASSERT_TRUE(validInputResult[i]); + TEST_ASSERT_EQUAL(validInputExpected,validInputResult); } } @@ -165,25 +163,24 @@ void test_invalidChoiceInput_firstCase(void) int invalidInput[100]; - bool invalidInputResult[100]; + bool invalidInputExpected = false; - /*Act*/ + - for(int i = -100; i < 0; i++) + for(int i = -100; i < 1; i++) { invalidInput[i+100] = i; } + /*Act and Assert*/ + for(int i = 0; i < 100; i++) { - invalidInputResult[i] = chooseOption(invalidInput[i]); - } - /*Assert*/ + bool invalidInputResult = chooseOption(invalidInput[i]); + + TEST_ASSERT_EQUAL(invalidInputExpected,invalidInputResult); - for(int i = 0; i < 100; i++) - { - TEST_ASSERT_FALSE(invalidInputResult[i]); } } @@ -196,27 +193,23 @@ void test_invalidChoiceInput_secondCase(void) int invalidInput[100]; - bool invalidInputResult[100]; - - /*Act*/ + bool invalidInputExpected = false; for(int i = 0; i < 100; i++) { invalidInput[i] = i + 5; } - for(int i = 0; i < 100; i++) - { - invalidInputResult[i] = chooseOption(invalidInput[i]); - } - - /*Assert*/ + /*Act and Assert*/ for(int i = 0; i < 100; i++) { - TEST_ASSERT_FALSE(invalidInputResult[i]); + bool invalidInputResult = chooseOption(invalidInput[i]); + + TEST_ASSERT_EQUAL(invalidInputExpected,invalidInputResult); } + } From 14d97220d07505bae9a4919ec24d25d20c4cb124 Mon Sep 17 00:00:00 2001 From: fdai7207 Date: Wed, 1 Feb 2023 01:25:05 +0100 Subject: [PATCH 09/16] implement unit tests for the checkEmployeeCredentials() function. --- build-project.sh | 2 +- src/employeeList.txt | 12 ++++ src/employeeLogin.c | 22 ++++++ src/employeeLogin.h | 16 +++++ src/main | Bin 0 -> 16408 bytes test/test_employeeLogin.c | 146 ++++++++++++++++++++++++++++++++++++++ test/test_mainMenu.c | 2 +- 7 files changed, 198 insertions(+), 2 deletions(-) create mode 100644 src/employeeList.txt create mode 100644 src/employeeLogin.c create mode 100644 src/employeeLogin.h create mode 100755 src/main create mode 100644 test/test_employeeLogin.c diff --git a/build-project.sh b/build-project.sh index ea712c3..420b964 100755 --- a/build-project.sh +++ b/build-project.sh @@ -1,6 +1,6 @@ clear ceedling test:all cd src/ -gcc -o main main.c mainMenu.c +gcc main.c mainMenu.c -o main ./main rm main diff --git a/src/employeeList.txt b/src/employeeList.txt new file mode 100644 index 0000000..94986be --- /dev/null +++ b/src/employeeList.txt @@ -0,0 +1,12 @@ +Atharva Atharvafdai7514 + +Can BlooMaskfdlt3817 + +Haytham TimoDLfdai7207 + +Julius Insertcatfdai7057 + +Mohamed MDfdai6618 + +Shivam Schivam007fdlt3781 + diff --git a/src/employeeLogin.c b/src/employeeLogin.c new file mode 100644 index 0000000..542526f --- /dev/null +++ b/src/employeeLogin.c @@ -0,0 +1,22 @@ +#include "employeeLogin.h" + +int checkEmployeeCredentials(char *inputUsername, char *inputPassword) +{ + + char listUsername[credentialLength]; + char listPassword[credentialLength]; + + FILE *employeeList = fopen("src/employeeList.txt", "r"); + while (fscanf(employeeList, "%s %s", listUsername, listPassword) != EOF) { + if (strcmp(inputUsername, listUsername) == 0 && strcmp(inputPassword, listPassword) == 0) { + fclose(employeeList); + return 1; + } + else if(strcmp(inputUsername, listUsername) == 0 && strcmp(inputPassword, listPassword) != 0){ + fclose(employeeList); + return 2; + } + } + fclose(employeeList); + return 0; +} diff --git a/src/employeeLogin.h b/src/employeeLogin.h new file mode 100644 index 0000000..6e4cb65 --- /dev/null +++ b/src/employeeLogin.h @@ -0,0 +1,16 @@ +#ifndef EMPLOYEELOGIN_H_ +#define EMPLOYEELOGIN_H_ + +#include +#include +#include +#include + +#define credentialLength 20 + + + +int checkEmployeeCredentials(char* inputUsername , char* inputPassword); + +#endif + diff --git a/src/main b/src/main new file mode 100755 index 0000000000000000000000000000000000000000..fad988bb3623d9069264d63cdfeaecddd9fa83f8 GIT binary patch literal 16408 zcmeHOdvH|M89y5cga{-cAP<#`LI>((Ng{**0(lVFAOQ-fSi$Sf?%iZ%vp4SEg~So1 z1zR&TmOh<2j>YM8WcnI)9EaLh5R2F<6FSxMhtsC5w3|U~och3uY=7T5=iA)9Ce!L4 z{iCxdv-dmS?|Yo@eCOVqbMHOpzNU@qiUI+_WsF!Yh&x=akUA~cQL8H;b)r^G!f~#+ zT8slfR$@wCrwF8~oGr?yB^n*wVJdV#$gY?~n)lN) z+9^|(=h`#K7{!ps96hM*n0oCfH~ke#zkgLeZP$6})RgUrCA$G_H=ylulma5Trkqa- zgMPI-pMGjUM#YdvA^dhbw4I->)%r}S4JzBtxsgxP-WF|l>>B0APj@O2Rq8k ze_ME%x9avr8;44rf2KOEVsSjyTC-$vJW>&lCDL6LU9~k8HA{jiCs-{*ptz_FI;Yle zx=9r67kz}WJ=Le-M`N7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-N&)iUU+1`@xEjoX5Nz>@qP{7ynHzYVke#$ov* zkUs+QIk|4`=8@C06E2)lbgf#uK|7?@>0qT`yW#rAPus=h~H`Cc2S;ueolTR z@IN5eJMxgRK=$^$sg7B_+BV^&NCh*B>xpH|3LE`w{GTtPI&#ezu_8v9<0;!W=*4- z?$MO(cwOlF#>N%K!nJm543CLb)xqjuWkuDJMY_t`AE%V3vEuElUqX2gKyr^xl%W+a z2OUZOYT!kpO!WEUBRj%}eE9Vm=j#HjEX_Cu#B5Ppr+6MmT2~to(*>XBN~)f<$Uk3a zj6Wdtr{&h!GX6L!BgOxdQSj0F`%~%vaM0b z6ZkbF;Cq2jEy8~<4uo~Kg_CYo(7`f$t2Ji2R+|8TcY@JqFf4|b1Y%`MX=;(%t zhclt+=Bd)I1H?n5#ZIJAHO+Nf>sy-4rcG;2>!$ja=0?ES31T*dv^2DKt1#DZ z+}u#V(cHXl-L|If=JxuAjZLudZ)&LHT?+qUKLTx#$m!BP2GfpMt|jm`QNDB)vZH!W zM4n=}-EmU(=1zLaD)7>GFLechkh$95048sKP)!pG*t)SR&B56C(w#sPin>W9*<&YVXQi5EOvsXU+#&;A z>x{c1D8me*8f8U_X+a6lUpENZ@Cr>&Gm5GkY%*}a9W(3zcOEg zir3!1FOL6@us+|%nWuS$*PiD!Obu4l)Ng+a@b@rxH49vEQ=!gfr@ z!Pslh^YMk1N}uCoH*ClJL>Lps@$>w%R@?jasV#4rqTAQ%IT72>%^|A7_Fn2cMWx~n y?NF|O2XYmDH1+0q`2I`R2gi Date: Wed, 1 Feb 2023 16:30:01 +0100 Subject: [PATCH 10/16] implement the functions employeeCredentials() and loginAsEmployee() for employees to access their accounts. --- build-project.sh | 6 +++ project.yml | 1 + src/employeeLogin.c | 89 +++++++++++++++++++++++++++++++++++--------- src/employeeLogin.h | 15 +++----- src/main | Bin 16408 -> 0 bytes src/main.c | 3 +- src/mainMenu.c | 7 ++-- src/mainMenu.h | 2 + 8 files changed, 93 insertions(+), 30 deletions(-) delete mode 100755 src/main diff --git a/build-project.sh b/build-project.sh index 420b964..5988d59 100755 --- a/build-project.sh +++ b/build-project.sh @@ -1,6 +1,12 @@ clear ceedling test:all cd src/ +cp employeeLogin.c employeeLogin.c.bak +sed -i 's/src\///g' employeeLogin.c gcc main.c mainMenu.c -o main ./main rm main +cp employeeLogin.c.bak employeeLogin.c +rm employeeLogin.c.bak +cd .. +rm -r build/ diff --git a/project.yml b/project.yml index e253248..51a5a2a 100644 --- a/project.yml +++ b/project.yml @@ -35,6 +35,7 @@ - -:test/support :source: - src/** + - src/ :support: - test/support :libraries: [] diff --git a/src/employeeLogin.c b/src/employeeLogin.c index 542526f..624d0ae 100644 --- a/src/employeeLogin.c +++ b/src/employeeLogin.c @@ -1,22 +1,77 @@ +#include "mainMenu.h" #include "employeeLogin.h" -int checkEmployeeCredentials(char *inputUsername, char *inputPassword) +extern int checkEmployeeCredentials(char *inputUsername, char *inputPassword) { - char listUsername[credentialLength]; - char listPassword[credentialLength]; - - FILE *employeeList = fopen("src/employeeList.txt", "r"); - while (fscanf(employeeList, "%s %s", listUsername, listPassword) != EOF) { - if (strcmp(inputUsername, listUsername) == 0 && strcmp(inputPassword, listPassword) == 0) { - fclose(employeeList); - return 1; - } - else if(strcmp(inputUsername, listUsername) == 0 && strcmp(inputPassword, listPassword) != 0){ - fclose(employeeList); - return 2; - } - } - fclose(employeeList); - return 0; + char listUsername[credentialLength]; + char listPassword[credentialLength]; + + FILE* employeeList = fopen("src/employeeList.txt","r"); + + if(employeeList == NULL ) + { + printf("file does not exist"); + exit(1); + } + else + { + while (fscanf(employeeList, "%s %s", listUsername, listPassword) != EOF) + { + if (strcmp(inputUsername, listUsername) == 0 && strcmp(inputPassword, listPassword) == 0) + { + fclose(employeeList); + return 1; + } + else if(strcmp(inputUsername, listUsername) == 0 && strcmp(inputPassword, listPassword) != 0) + { + fclose(employeeList); + return 2; + } + } + fclose(employeeList); + return 0; + } +} + +void employeeCredentials(char* username,char* password) +{ + printf("Enter username: "); + scanf("%s", username); + printf("Enter password: "); + scanf("%s", password); +} + +void loginAsEmployee() +{ + int counter=2; + char username[credentialLength]; + char password[credentialLength]; + + employeeCredentials(username, password); + + while(counter>0) + { + if(checkEmployeeCredentials(username, password) == 0) + { + printf("User not found\n"); + employeeCredentials(username, password); + } + else if(checkEmployeeCredentials(username, password)==2) + { + printf("Wrong Informations !\nyou have %d tries left\n",counter); + employeeCredentials(username, password); + --counter; + } + else + { + printf("User Approved\n"); + break; + } + } + if(counter==0) + { + printf("you used up all of the tries! account locked\n"); + } + } diff --git a/src/employeeLogin.h b/src/employeeLogin.h index 6e4cb65..485955f 100644 --- a/src/employeeLogin.h +++ b/src/employeeLogin.h @@ -1,16 +1,13 @@ -#ifndef EMPLOYEELOGIN_H_ -#define EMPLOYEELOGIN_H_ +#ifndef LOGINEMPLOYEE_H_ +#define LOGINEMPLOYEE_H_ -#include -#include -#include -#include #define credentialLength 20 - - -int checkEmployeeCredentials(char* inputUsername , char* inputPassword); +int checkEmployeeCredentials(char* username , char* password); +void employeeCredentials(char* username, char* password); +void loginAsEmployee(); #endif + diff --git a/src/main b/src/main deleted file mode 100755 index fad988bb3623d9069264d63cdfeaecddd9fa83f8..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16408 zcmeHOdvH|M89y5cga{-cAP<#`LI>((Ng{**0(lVFAOQ-fSi$Sf?%iZ%vp4SEg~So1 z1zR&TmOh<2j>YM8WcnI)9EaLh5R2F<6FSxMhtsC5w3|U~och3uY=7T5=iA)9Ce!L4 z{iCxdv-dmS?|Yo@eCOVqbMHOpzNU@qiUI+_WsF!Yh&x=akUA~cQL8H;b)r^G!f~#+ zT8slfR$@wCrwF8~oGr?yB^n*wVJdV#$gY?~n)lN) z+9^|(=h`#K7{!ps96hM*n0oCfH~ke#zkgLeZP$6})RgUrCA$G_H=ylulma5Trkqa- zgMPI-pMGjUM#YdvA^dhbw4I->)%r}S4JzBtxsgxP-WF|l>>B0APj@O2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k!Hzym7ic2IhAb^A~@`vB;d--aJhD)il)O5pCT zBG6SHm@{#F$>G5Ga>B2~ZwK1&et#ZWUx@iy|JWPZ>>a3!$>r-N&)iUU+1`@xEjoX5Nz>@qP{7ynHzYVke#$ov* zkUs+QIk|4`=8@C06E2)lbgf#uK|7?@>0qT`yW#rAPus=h~H`Cc2S;ueolTR z@IN5eJMxgRK=$^$sg7B_+BV^&NCh*B>xpH|3LE`w{GTtPI&#ezu_8v9<0;!W=*4- z?$MO(cwOlF#>N%K!nJm543CLb)xqjuWkuDJMY_t`AE%V3vEuElUqX2gKyr^xl%W+a z2OUZOYT!kpO!WEUBRj%}eE9Vm=j#HjEX_Cu#B5Ppr+6MmT2~to(*>XBN~)f<$Uk3a zj6Wdtr{&h!GX6L!BgOxdQSj0F`%~%vaM0b z6ZkbF;Cq2jEy8~<4uo~Kg_CYo(7`f$t2Ji2R+|8TcY@JqFf4|b1Y%`MX=;(%t zhclt+=Bd)I1H?n5#ZIJAHO+Nf>sy-4rcG;2>!$ja=0?ES31T*dv^2DKt1#DZ z+}u#V(cHXl-L|If=JxuAjZLudZ)&LHT?+qUKLTx#$m!BP2GfpMt|jm`QNDB)vZH!W zM4n=}-EmU(=1zLaD)7>GFLechkh$95048sKP)!pG*t)SR&B56C(w#sPin>W9*<&YVXQi5EOvsXU+#&;A z>x{c1D8me*8f8U_X+a6lUpENZ@Cr>&Gm5GkY%*}a9W(3zcOEg zir3!1FOL6@us+|%nWuS$*PiD!Obu4l)Ng+a@b@rxH49vEQ=!gfr@ z!Pslh^YMk1N}uCoH*ClJL>Lps@$>w%R@?jasV#4rqTAQ%IT72>%^|A7_Fn2cMWx~n y?NF|O2XYmDH1+0q`2I`R2gi #include +#define credentialLength 20 + bool agePermission(int age); bool checkIfInteger(char* userInput); bool chooseOption(int choiceInput); From 509362d0aa60dce0ea5bea5b08b9d1c3a8db68d9 Mon Sep 17 00:00:00 2001 From: fdai7207 Date: Wed, 1 Feb 2023 19:46:39 +0100 Subject: [PATCH 11/16] refactoring: changed some variable names for readability and used pointers to conserve memory in the functions CheckEmployeeCredentials() and EmployeeCredentials. --- src/.employeeLogin.c.swp | Bin 0 -> 12288 bytes src/employeeLogin.c | 55 ++++++++++++++++++++++++--------------- 2 files changed, 34 insertions(+), 21 deletions(-) create mode 100644 src/.employeeLogin.c.swp diff --git a/src/.employeeLogin.c.swp b/src/.employeeLogin.c.swp new file mode 100644 index 0000000000000000000000000000000000000000..a2975fcb31a1a48c8e9e0e9a4ff11c9c866b60ce GIT binary patch literal 12288 zcmeI2O>Epm6vw9(6_n z=2=m_G;wiuj?RotFti64yYlh(_6JWe_TwmHE?-Yv-c?U-+s-x@&Od))zJ7Lc?wO}% zXZ9R>H8(iE#N~3AOWqM(A(MK%9w(wZ9!K`L=!SldhqkvJsC}0#Y3K#{90!D#5Pyya%?B*fH7Vd`g`2Tb_6>LqTmzfn5_k@bf``FxhZ*|`Tn8KAWv~ofFbynl0z3pB1b2bI zq32)V4e&bH0$0IH;38NAO7kUPno+=Re)UHeyeZ5#j zl6M9A($0LmniEEzOdPAc9`p#q+SCh)`#uE@C9a?(@CfoYMAz%MA|kReD&o=>G; mSw&CM(Tv;SgBDFH5wBY?U?dOwM~{@ z@_&{h!I!gubhma~SzbhZG!uqVu%f)nmMU$qlJH~EH#BUrUe8zG+w2QbAUiZGognJ+ z#0zAs0#Qo2d_~a7b|w$`!b#FqYq`n#WUjt)Hj7r>DQ`%H?FxJO=}5HkDWb>ym>Lb5 zsxUP~wP8PZ#3_S>9PCCv&}`P=LVK(Ml}l=HrnS1m-qTKRvaV+SVdQ|LmQJMFK$bCO zNHv`TyH22i8ESB*%Bt)6LZ`u#Ity%NF@JcFb4FoF7*8e0aW&Dx07Cwwv&o2&qHbU~QnU@1zbRtsT4hUp&xu z^f4~-G!jWKk~C2qCCB!ISXkP#mKP@jN1~XiA{_pudUAVHg+0})+U@PC5m{oXJJs}& z8?oXfvb&)*tO)sVtRGi}PMxB`aayHXI!^zp6h~Dpv8Vf!|J{AhA?x3}K0H#pyWRN; zG39nz2}>w`~j1NQ0Ue&gB)oG+yc>gS|Qyg-49BowfTifodk!P={nu`J20xd9?~vw;h#%PVGKY fiH&;F_IqtX<-Ts|m-KFV;8B-*a!$zJ7<&H!zLM@d literal 0 HcmV?d00001 diff --git a/src/employeeLogin.c b/src/employeeLogin.c index 624d0ae..0d44ddf 100644 --- a/src/employeeLogin.c +++ b/src/employeeLogin.c @@ -4,8 +4,8 @@ extern int checkEmployeeCredentials(char *inputUsername, char *inputPassword) { - char listUsername[credentialLength]; - char listPassword[credentialLength]; + char* listUsername = malloc(credentialLength * sizeof(char*)); + char* listPassword = malloc(credentialLength * sizeof(char*)); FILE* employeeList = fopen("src/employeeList.txt","r"); @@ -15,15 +15,18 @@ extern int checkEmployeeCredentials(char *inputUsername, char *inputPassword) exit(1); } else - { + { + /*loop that checks if the two strings seperated by space exist in the employee list*/ + while (fscanf(employeeList, "%s %s", listUsername, listPassword) != EOF) { - if (strcmp(inputUsername, listUsername) == 0 && strcmp(inputPassword, listPassword) == 0) + + if (!(strcmp(inputUsername, listUsername)) && !(strcmp(inputPassword, listPassword))) { fclose(employeeList); return 1; } - else if(strcmp(inputUsername, listUsername) == 0 && strcmp(inputPassword, listPassword) != 0) + else if(!(strcmp(inputUsername, listUsername)) && strcmp(inputPassword, listPassword)) { fclose(employeeList); return 2; @@ -32,46 +35,56 @@ extern int checkEmployeeCredentials(char *inputUsername, char *inputPassword) fclose(employeeList); return 0; } + + free(inputUsername); + free(inputPassword); } -void employeeCredentials(char* username,char* password) +void employeeCredentials(char* inputUsername,char* inputPassword) { printf("Enter username: "); - scanf("%s", username); + scanf("%s", inputUsername); printf("Enter password: "); - scanf("%s", password); + scanf("%s", inputPassword); } void loginAsEmployee() { - int counter=2; - char username[credentialLength]; - char password[credentialLength]; - - employeeCredentials(username, password); + int counter=3; + char* username = malloc(credentialLength * sizeof(char*)); + char* password = malloc(credentialLength * sizeof(char*)); while(counter>0) { - if(checkEmployeeCredentials(username, password) == 0) + employeeCredentials(username, password); + + int checkCredentials = checkEmployeeCredentials(username,password); + + + if(checkCredentials == 0) { - printf("User not found\n"); - employeeCredentials(username, password); + printf("\n\nUser not found\n\n"); } - else if(checkEmployeeCredentials(username, password)==2) + else if(checkCredentials == 2) { - printf("Wrong Informations !\nyou have %d tries left\n",counter); - employeeCredentials(username, password); + printf("\n\nWrong Informations !\nyou have %d tries left\n\n",counter-1); --counter; } else { - printf("User Approved\n"); + printf("\n\nUser Approved\n\n"); break; } } + if(counter==0) { - printf("you used up all of the tries! account locked\n"); + + printf("you used up all of the tries! account locked\nPlease contact an employee of higher clearance !\n\n"); + } + free(username); + free(password); + } From be083578653005756d80ca32cad372f8287c12a7 Mon Sep 17 00:00:00 2001 From: fdai7207 Date: Wed, 1 Feb 2023 20:12:06 +0100 Subject: [PATCH 12/16] refactoring: removed redundant code from the unit test file test_employeeLogin.c, changed variable names and added som comments . --- .../.test_employeeLogin.c.swp | Bin 12288 -> 16384 bytes test/test_employeeLogin.c | 159 +++++++----------- 2 files changed, 62 insertions(+), 97 deletions(-) rename src/.employeeLogin.c.swp => test/.test_employeeLogin.c.swp (57%) diff --git a/src/.employeeLogin.c.swp b/test/.test_employeeLogin.c.swp similarity index 57% rename from src/.employeeLogin.c.swp rename to test/.test_employeeLogin.c.swp index a2975fcb31a1a48c8e9e0e9a4ff11c9c866b60ce..77752368280750880b47ea9f9ce5e66997e1bc68 100644 GIT binary patch literal 16384 zcmeI3TZ|J`7{`xzfmIh#qVhNv5*H}j3$nYq+zMU8E?X7al_+kSVP?7=ww>9`Oc!@+ z4old(g(`pRrIh)^1 zr{_E0IrGi`oar|VyQ23Gr-VCuqXcCUA?EQj(x+<)`C}O&d2zUfMz^`)WPgvB?F1#8Q#cm#L^cm#L^cm#L^cm#L^cm#L^ z{u2?frsk2oIFotK*-Se7E9&-FI>%Qz`^W0)okPCk5#SNv5#SNv5#SNv5#SNv5#SNv z5#SNv5#SNH3?m@QguITVzI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;h#%PVGKY fiH&;F_IqtX<-Ts|m-KFV;8B-*a!$zJ7<&H!zLM@d diff --git a/test/test_employeeLogin.c b/test/test_employeeLogin.c index 5cfdfdf..8e2d8de 100644 --- a/test/test_employeeLogin.c +++ b/test/test_employeeLogin.c @@ -17,130 +17,95 @@ void test_SuccessfulLoginEmployee_(void) //test case : 0 - //Arrange - char* ExistingEmployee1 = "Atharva"; - char* ExistingPassword1 = "Atharvafdai7514"; - - char* ExistingEmployee2 = "Can"; - char* ExistingPassword2 = "BlooMaskfdlt3817"; + /*Arrange*/ + + char* validEmployeesCredentials[][2] = { + {"Atharva", "Atharvafdai7514"}, + {"Can", "BlooMaskfdlt3817"}, + {"Haytham", "TimoDLfdai7207"}, + {"Julius", "Insertcatfdai7057"}, + {"Mohamed", "MDfdai6618"}, + {"Shivam", "Schivam007fdlt3781"} + }; + /*Act and Assert*/ - char* ExistingEmployee3 = "Haytham"; - char* ExistingPassword3 = "TimoDLfdai7207"; + int expected[] = {1,1,1,1,1,1}; - char* ExistingEmployee4 = "Julius"; - char* ExistingPassword4 = "Insertcatfdai7057"; + for(int i=0; i<6; i++) + { + + int result = checkEmployeeCredentials(validEmployeesCredentials[i][0], validEmployeesCredentials[i][1]); + + TEST_ASSERT_EQUAL_INT(expected[i],result); + + } - char* ExistingEmployee5 = "Mohamed"; - char* ExistingPassword5 = "MDfdai6618"; - char* ExistingEmployee6 = "Shivam"; - char* ExistingPassword6 = "Schivam007fdlt3781"; - - //Act - - int result1 = checkEmployeeCredentials(ExistingEmployee1,ExistingPassword1); - int result2 = checkEmployeeCredentials(ExistingEmployee2,ExistingPassword2); - int result3 = checkEmployeeCredentials(ExistingEmployee3,ExistingPassword3); - int result4 = checkEmployeeCredentials(ExistingEmployee4,ExistingPassword4); - int result5 = checkEmployeeCredentials(ExistingEmployee5,ExistingPassword5); - int result6 = checkEmployeeCredentials(ExistingEmployee6,ExistingPassword6); - - //Assert - TEST_ASSERT_EQUAL_INT(1,result1); - TEST_ASSERT_EQUAL_INT(1,result2); - TEST_ASSERT_EQUAL_INT(1,result3); - TEST_ASSERT_EQUAL_INT(1,result4); - TEST_ASSERT_EQUAL_INT(1,result5); - TEST_ASSERT_EQUAL_INT(1,result6); } void test_WrongInfosLoginEmployee(void) { //test case : 1 + /*Arrange*/ + + char* wrongEmployeesCredentials[][2] = { + {"Atharva", "doe"}, + {"Can", "Bar"}, + {"Haytham", "buzz"}, + {"Julius", "fizz"}, + {"Mohamed", "muster"}, + {"Shivam", "TimoDL"} + }; - //Arrange - char* wrongInfoEmployee1 = "Atharva"; - char* wrongInfoPassword1 = "doe"; - - char* wrongInfoEmployee2 = "Can"; - char* wrongInfoPassword2 = "Bar"; + /*Act and Assert*/ - char* wrongInfoEmployee3 = "Haytham"; - char* wrongInfoPassword3 = "buzz"; + int expected[] = {2,2,2,2,2,2}; - char* wrongInfoEmployee4 = "Julius"; - char* wrongInfoPassword4 = "Musterpass"; + for(int i=0; i<6; i++) + { - char* wrongInfoEmployee5 = "Mohamed"; - char* wrongInfoPassword5 = "Irgendwas"; + int result = checkEmployeeCredentials(wrongEmployeesCredentials[i][0], wrongEmployeesCredentials[i][1]); - char* wrongInfoEmployee6 = "Shivam"; - char* wrongInfoPassword6 = "John"; + TEST_ASSERT_EQUAL_INT(expected[i],result); - //Act + } - int result1 = checkEmployeeCredentials(wrongInfoEmployee1,wrongInfoPassword1); - int result2 = checkEmployeeCredentials(wrongInfoEmployee2,wrongInfoPassword2); - int result3 = checkEmployeeCredentials(wrongInfoEmployee3,wrongInfoPassword3); - int result4 = checkEmployeeCredentials(wrongInfoEmployee4,wrongInfoPassword4); - int result5 = checkEmployeeCredentials(wrongInfoEmployee5,wrongInfoPassword5); - int result6 = checkEmployeeCredentials(wrongInfoEmployee6,wrongInfoPassword6); - - //Assert +} - TEST_ASSERT_EQUAL_INT(2,result1); - TEST_ASSERT_EQUAL_INT(2,result2); - TEST_ASSERT_EQUAL_INT(2,result3); - TEST_ASSERT_EQUAL_INT(2,result4); - TEST_ASSERT_EQUAL_INT(2,result5); - TEST_ASSERT_EQUAL_INT(2,result6); -} void test_MissingLoginEmployee(void) { //test case : 2 + /*Arrange*/ + + char* missingEmployeesCredentials[][2] = { + {"John", "doe"}, + {"Jane", "Doe"}, + {"Foo", "Bar"}, + {"Fizz", "Buzz"}, + {"Musterman", "Mustermanpassword"}, + {"Musterfrau", "Musterfraupassword"} + }; + + int expected[] = {0,0,0,0,0,0}; + + /*Act and Assert*/ + + for(int i=0; i<6; i++) + { + + int result = checkEmployeeCredentials(missingEmployeesCredentials[i][0], missingEmployeesCredentials[i][1]); + + TEST_ASSERT_EQUAL_INT(expected[i],result); + + } - //Arrange - - char* missingEmployee1 = "John"; - char* missingPassword1 = "Doe"; - - char* missingEmployee2 = "Jane"; - char* missingPassword2 = "Doe"; - - char* missingEmployee3 = "Foo"; - char* missingPassword3 = "Bar"; - - char* missingEmployee4 = "Mustermann"; - char* missingPassword4 = "PassMustermann"; - - char* missingEmployee5 = "Musterfrau"; - char* missingPassword5 = "PassMusterfrau"; - - char* missingEmployee6 = "Fizz"; - char* missingPassword6 = "Buzz"; - - //Act - int result1 = checkEmployeeCredentials(missingEmployee1,missingPassword1); - int result2 = checkEmployeeCredentials(missingEmployee2,missingPassword2); - int result3 = checkEmployeeCredentials(missingEmployee3,missingPassword3); - int result4 = checkEmployeeCredentials(missingEmployee4,missingPassword4); - int result5 = checkEmployeeCredentials(missingEmployee5,missingPassword5); - int result6 = checkEmployeeCredentials(missingEmployee6,missingPassword6); - - //Assert - TEST_ASSERT_EQUAL_INT(0,result1); - TEST_ASSERT_EQUAL_INT(0,result2); - TEST_ASSERT_EQUAL_INT(0,result3); - TEST_ASSERT_EQUAL_INT(0,result4); - TEST_ASSERT_EQUAL_INT(0,result5); - TEST_ASSERT_EQUAL_INT(0,result6); - + } #endif // TEST From 46223938440dbb9a27703f4a496c38d8405b1445 Mon Sep 17 00:00:00 2001 From: fdai7207 Date: Wed, 1 Feb 2023 23:28:17 +0100 Subject: [PATCH 13/16] implement unit tests for the function employeesAccess(). --- src/employeeLogin.c | 15 ++++++++++++ src/employeeLogin.h | 7 +++++- test/.test_employeeLogin.c.swp | Bin 16384 -> 0 bytes test/test_employeeLogin.c | 41 +++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) delete mode 100644 test/.test_employeeLogin.c.swp diff --git a/src/employeeLogin.c b/src/employeeLogin.c index 0d44ddf..dfcac61 100644 --- a/src/employeeLogin.c +++ b/src/employeeLogin.c @@ -1,6 +1,21 @@ #include "mainMenu.h" #include "employeeLogin.h" +bool employeesAccess(char* employeesAccessCode) +{ + + if(strcmp(employeesAccessCode,accessKey) == 0) + { + return true; + } + + else + { + return false; + } + +} + extern int checkEmployeeCredentials(char *inputUsername, char *inputPassword) { diff --git a/src/employeeLogin.h b/src/employeeLogin.h index 485955f..dd77acc 100644 --- a/src/employeeLogin.h +++ b/src/employeeLogin.h @@ -1,10 +1,15 @@ #ifndef LOGINEMPLOYEE_H_ #define LOGINEMPLOYEE_H_ - +#define accessKey "DF9E9A8B5E" #define credentialLength 20 +#include + +bool employeesAccess(char* employeesAccessCode); + int checkEmployeeCredentials(char* username , char* password); + void employeeCredentials(char* username, char* password); void loginAsEmployee(); diff --git a/test/.test_employeeLogin.c.swp b/test/.test_employeeLogin.c.swp deleted file mode 100644 index 77752368280750880b47ea9f9ce5e66997e1bc68..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16384 zcmeI3TZ|J`7{`xzfmIh#qVhNv5*H}j3$nYq+zMU8E?X7al_+kSVP?7=ww>9`Oc!@+ z4old(g(`pRrIh)^1 zr{_E0IrGi`oar|VyQ23Gr-VCuqXcCUA?EQj(x+<)`C}O&d2zUfMz^`)WPgvB?F1#8Q#cm#L^cm#L^cm#L^cm#L^cm#L^ z{u2?frsk2oIFotK*-Se7E9&-FI>%Qz`^W0)okPCk5#SNv5#SNv5#SNv5#SNv5#SNv z5#SNv5#SNH3?m@QguITVzI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#ldlbrCP&d8=wLt&FY{GOi(H`~y z$E;=>S+`=Y6g8-Ar`c-tG}zOqYR(#t`LfoC|G{Z{IctktwJ!6VBS)>XXvj_L>g#e+ zR!gr(du6U%l)74~Xfl1UcZrKa&D0uUIS)JLe$Wwh)YIoY&u+B(pSkm$uJ0g&1k|3T PbwT>~S~ZTGDwX^R=7EM0 diff --git a/test/test_employeeLogin.c b/test/test_employeeLogin.c index 8e2d8de..e29bf5d 100644 --- a/test/test_employeeLogin.c +++ b/test/test_employeeLogin.c @@ -108,4 +108,45 @@ void test_MissingLoginEmployee(void) } +void test_validEmployeeAccessCode(void) +{ + //test case 0 + + /*Arrange*/ + + char validAccesscode[11] = "DF9E9A8B5E"; + + /*Act*/ + bool validAccessCodeResult = employeesAccess(validAccesscode); + + /*Assert*/ + + TEST_ASSERT_TRUE(validAccessCodeResult); + +} + +void test_invalidEmployeeAccessCode(void) +{ + //test case 1 + + /*Arrange*/ + + char* invalidAccessCode[] = {"15","foo","fizz","buzz","fizzbuzz","test","bankmanagement"}; + bool invalidCodeResults[7]; + + /*Act*/ + + for(int i=0;i<7;i++) + { + invalidCodeResults[i] = employeesAccess(invalidAccessCode[i]); + } + + /*Assert*/ + + for(int i=0;i<7;i++) + { + TEST_ASSERT_FALSE(invalidCodeResults[i]); + } +} + #endif // TEST From 557709bdd6fbdbabc91846daa6f83d86a675180a Mon Sep 17 00:00:00 2001 From: fdai7207 Date: Thu, 2 Feb 2023 00:53:37 +0100 Subject: [PATCH 14/16] implement the function inputEmployeeAccessCode() so that only employees can access the loginAsEmployee() function. --- build-project.sh | 1 + src/employeeLogin.c | 39 ++++++++++++++++++++++++++++++++++++++- src/employeeLogin.h | 2 +- src/mainMenu.c | 2 +- 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/build-project.sh b/build-project.sh index 5988d59..3aac8dc 100755 --- a/build-project.sh +++ b/build-project.sh @@ -1,3 +1,4 @@ +trap 'echo "Interrupted"; rm main; cp employeeLogin.c.bak employeeLogin.c; rm employeeLogin.c.bak; cd ..; rm -r build/; exit' SIGINT clear ceedling test:all cd src/ diff --git a/src/employeeLogin.c b/src/employeeLogin.c index dfcac61..6ec85db 100644 --- a/src/employeeLogin.c +++ b/src/employeeLogin.c @@ -4,7 +4,7 @@ bool employeesAccess(char* employeesAccessCode) { - if(strcmp(employeesAccessCode,accessKey) == 0) + if(strcmp(employeesAccessCode,employeeAccessKey) == 0) { return true; } @@ -55,6 +55,43 @@ extern int checkEmployeeCredentials(char *inputUsername, char *inputPassword) free(inputPassword); } +void inputEmployeeAccessCode() +{ + char accessKey[10]; + + int remainingAttempts = 10; + + + printf("\n\nPlease enter the Access key : "); + // scanf("%s",accessKey); + + while(remainingAttempts>0) + { + scanf("%s",accessKey); + + if(employeesAccess(accessKey)==true) + { + printf("\n\nAccess granted!\n\n"); + + loginAsEmployee(); + break; + + } + else + { + --remainingAttempts; + printf("\n\nAccess key didnt match! try again !\n\n"); + + } + if(remainingAttempts == 0) + { + printf("you've reached the maximum number of tries!\nplease contact an employee of a high clearance(2 or higher) \n\n"); + } + } + + +} + void employeeCredentials(char* inputUsername,char* inputPassword) { printf("Enter username: "); diff --git a/src/employeeLogin.h b/src/employeeLogin.h index dd77acc..c563e18 100644 --- a/src/employeeLogin.h +++ b/src/employeeLogin.h @@ -1,7 +1,7 @@ #ifndef LOGINEMPLOYEE_H_ #define LOGINEMPLOYEE_H_ -#define accessKey "DF9E9A8B5E" +#define employeeAccessKey "DF9E9A8B5E" #define credentialLength 20 #include diff --git a/src/mainMenu.c b/src/mainMenu.c index f9f48a2..40c553f 100644 --- a/src/mainMenu.c +++ b/src/mainMenu.c @@ -107,7 +107,7 @@ void menuInput() case 2 : printf("\ncreateCostumerAccount() function will be implemented here soon\n\n"); break; - case 3 : loginAsEmployee(); + case 3 : inputEmployeeAccessCode(); break; case 4 : printf("\e[1;1H\e[2J"); From 098ce77c87a6837bf17269fcecde5ab31a1b4606 Mon Sep 17 00:00:00 2001 From: fdai7207 Date: Thu, 2 Feb 2023 01:34:36 +0100 Subject: [PATCH 15/16] refactoring: changed some variables and some functions names for a better readability and optimized the employeesAccess() function --- src/employeeLogin.c | 38 ++++++++++++++------------------------ src/employeeLogin.h | 2 +- src/mainMenu.c | 2 +- 3 files changed, 16 insertions(+), 26 deletions(-) diff --git a/src/employeeLogin.c b/src/employeeLogin.c index 6ec85db..3182746 100644 --- a/src/employeeLogin.c +++ b/src/employeeLogin.c @@ -4,19 +4,11 @@ bool employeesAccess(char* employeesAccessCode) { - if(strcmp(employeesAccessCode,employeeAccessKey) == 0) - { - return true; - } - - else - { - return false; - } + return !(strcmp(employeesAccessCode,employeeAccessKey)); } -extern int checkEmployeeCredentials(char *inputUsername, char *inputPassword) +int checkEmployeeCredentials(char *inputUsername, char *inputPassword) { char* listUsername = malloc(credentialLength * sizeof(char*)); @@ -55,44 +47,42 @@ extern int checkEmployeeCredentials(char *inputUsername, char *inputPassword) free(inputPassword); } -void inputEmployeeAccessCode() +void getEmployeeAccessCode() { - char accessKey[10]; + char accessCode[10]; int remainingAttempts = 10; - printf("\n\nPlease enter the Access key : "); - // scanf("%s",accessKey); - while(remainingAttempts>0) + while(remainingAttempts > 0) { - scanf("%s",accessKey); + scanf("%s",accessCode); - if(employeesAccess(accessKey)==true) + if(employeesAccess(accessCode)) { - printf("\n\nAccess granted!\n\n"); + printf("\n\nAccess granted!\n\n"); loginAsEmployee(); - break; + break; } + else { - --remainingAttempts; printf("\n\nAccess key didnt match! try again !\n\n"); - + --remainingAttempts; } + if(remainingAttempts == 0) { printf("you've reached the maximum number of tries!\nplease contact an employee of a high clearance(2 or higher) \n\n"); } } - } -void employeeCredentials(char* inputUsername,char* inputPassword) +void getEmployeeCredentials(char* inputUsername,char* inputPassword) { printf("Enter username: "); scanf("%s", inputUsername); @@ -108,7 +98,7 @@ void loginAsEmployee() while(counter>0) { - employeeCredentials(username, password); + getEmployeeCredentials(username, password); int checkCredentials = checkEmployeeCredentials(username,password); diff --git a/src/employeeLogin.h b/src/employeeLogin.h index c563e18..d6f3314 100644 --- a/src/employeeLogin.h +++ b/src/employeeLogin.h @@ -10,7 +10,7 @@ bool employeesAccess(char* employeesAccessCode); int checkEmployeeCredentials(char* username , char* password); -void employeeCredentials(char* username, char* password); +void getEmployeeCredentials(char* username, char* password); void loginAsEmployee(); #endif diff --git a/src/mainMenu.c b/src/mainMenu.c index 40c553f..b2d8243 100644 --- a/src/mainMenu.c +++ b/src/mainMenu.c @@ -107,7 +107,7 @@ void menuInput() case 2 : printf("\ncreateCostumerAccount() function will be implemented here soon\n\n"); break; - case 3 : inputEmployeeAccessCode(); + case 3 : getEmployeeAccessCode(); break; case 4 : printf("\e[1;1H\e[2J"); From fdf0bcd9cafb8f6678bd5a5b5ca17cc0f5b18588 Mon Sep 17 00:00:00 2001 From: fdai7207 Date: Thu, 2 Feb 2023 03:01:53 +0100 Subject: [PATCH 16/16] refactoring: removed redundant code from the unit test of the employeeAccess() function. --- test/test_employeeLogin.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/test/test_employeeLogin.c b/test/test_employeeLogin.c index e29bf5d..8860af5 100644 --- a/test/test_employeeLogin.c +++ b/test/test_employeeLogin.c @@ -132,21 +132,16 @@ void test_invalidEmployeeAccessCode(void) /*Arrange*/ char* invalidAccessCode[] = {"15","foo","fizz","buzz","fizzbuzz","test","bankmanagement"}; - bool invalidCodeResults[7]; + bool invalidCodeExpectation = false; - /*Act*/ + /*Act and assert*/ for(int i=0;i<7;i++) { - invalidCodeResults[i] = employeesAccess(invalidAccessCode[i]); + bool invalidCodeResults = employeesAccess(invalidAccessCode[i]); + TEST_ASSERT_EQUAL(invalidCodeExpectation,invalidCodeResults); } - /*Assert*/ - - for(int i=0;i<7;i++) - { - TEST_ASSERT_FALSE(invalidCodeResults[i]); - } } #endif // TEST