diff --git a/06_linux_ex2/README b/06_linux_ex2/README index 8489ffb1..62f42731 100644 --- a/06_linux_ex2/README +++ b/06_linux_ex2/README @@ -1,2 +1,58 @@ -name@example.com +doron.dollev@gmail.com +### Processes handling + +**(Q1)** A user started a process and logged out from the terminal. Which command he used if the process still running in the background: + +* nohup + + +**(Q2)** The `kill` command always terminates a process. + +* False + + +**(Q3)** Which command could be used to know how many processes are running in the background terminal session? + +* jobs + + +**(Q4)** Given a terminal session with long process running in it, how will you ask this process to terminate? + +* CTRL+c + + +**(Q5)** Given a terminal session with long process running in it, how will you ask this process the stop? + +* CTRL+z + + +**(Q6)** How would you run the `sleep 10` command as a foreground process? + +* sleep 10 + + +**(Q7)** Which of the following command would deliver a SIGTERM to the `xscreensaver` process? + +* None of the above + + +**(Q8)** Which of the following would deliver a SIGKILL to the `xscreensaver` command? + +* kill -9 4846 + + +**(Q9)** Which of the following would send a SIGCHLD (signal number 17) to the `ssh-agent` process? + +* kill -CHLD 4828 + + +**(Q10)** Which key pressed within the `top` command allows the user to send a signal to a process? + +* k + + +**(Q11 - easy 5 points bonus)** Open a new terminal session and type the command `python`. Then send a SIGINT signal using your keyboard. What best describes + how the python process responds to the SIGINT signal? + +* The program has implemented a custom signal handler for the SIGINT signal. diff --git a/06_linux_ex2/tlsHandshake.sh b/06_linux_ex2/tlsHandshake.sh index e69de29b..49180fe5 100644 --- a/06_linux_ex2/tlsHandshake.sh +++ b/06_linux_ex2/tlsHandshake.sh @@ -0,0 +1,46 @@ +#!/bin/bash +echo "Initializing random number generator..." +openssl rand -out masterKey.txt -base64 32 +if [ $? -gt 0 ] +then + echo "couldn't generate key 32 based" + exit 1 +fi +curl -X POST -H "Content-Type: application/json" -d '{"clientVersion":"3.2","message":"Client Hello"}' http://devops-jan22-1273001359.eu-north-1.elb.amazonaws.com:8080/clienthello | jq -r '.serverCert,.sessionID' >tmp.txt +STATUS=$? +if [ $STATUS -gt 0 ] +then + echo "run \"man curl|jq\" end check for details for error status number $STATUS" + # for example: "error 20 at 0 depth lookup: unable to get local issuer certificate" + # This is Eve that is the reason error 20 is unlisted in curl + exit 1 +fi +# Instead could be used /clienthello -o (>) tmp.txt and use twice jq -r on cert.pem and sessionID +head --lines=-1 tmp.txt>cert.pem +export SESSION_ID=$( tail -1 tmp.txt ) +rm -f tmp.txt +export VERIFICATION_RESULT=$( openssl verify -CAfile cert-ca-aws.pem cert.pem ) + +if [ "$VERIFICATION_RESULT" != "cert.pem: OK" ] +then + echo "Server Certificate is invalid." + exit 1 +fi + +MASTER_KEY=$( openssl smime -encrypt -aes-256-cbc -in masterKey.txt -outform DER cert.pem | base64 -w 0 ) +curl -X POST -H 'Content-Type: application/json' -d '{"sessionID": "'$SESSION_ID'","masterKey": "'$MASTER_KEY'","sampleMessage": "Hi server, please encrypt me and send to client!"}' http://devops-jan22-1273001359.eu-north-1.elb.amazonaws.com:8080/keyexchange | jq -r '.encryptedSampleMessage' > encSampleMsg.txt +STATUS=$? +if [ $STATUS -gt 0 ] +then + echo "run \"man curl|jq\" end check for details for error status number $STATUS" + exit 1 +fi +cat encSampleMsg.txt | base64 -d > encSampleMsgReady.txt +openssl enc -d -aes-256-cbc -pbkdf2 -kfile masterKey.txt -in encSampleMsgReady.txt -out decrypted_secret.txt +export DECRYPTED_SAMPLE_MESSAGE=`cat decrypted_secret.txt` +if [ "$DECRYPTED_SAMPLE_MESSAGE" != "Hi server, please encrypt me and send to client!" ]; then + echo "Server symmetric encryption using the exchanged master-key has failed." + exit 1 +else + echo "Client-Server TLS handshake has been completed successfully" +fi \ No newline at end of file diff --git a/python_katas/kata_1/questions.py b/python_katas/kata_1/questions.py index 9ad97b58..a7e7fea4 100644 --- a/python_katas/kata_1/questions.py +++ b/python_katas/kata_1/questions.py @@ -5,11 +5,7 @@ def sum_of_element(elements): :param elements: list of integers :return: Return int - the sum of all elements. """ - s = 0 - for num in elements: - s = s + num - - return s + return sum(elements) def verbing(word): @@ -28,7 +24,15 @@ def verbing(word): :param word: str :return: Return the resulting string. """ - return None + my_str = word + if len(my_str) < 3: + return my_str + elif my_str[-3:] == 'ing': + my_str = my_str[:-3] + my_str += 'ly' + else: + my_str += 'ing' + return my_str def words_concatenation(words): @@ -43,14 +47,15 @@ def words_concatenation(words): :param words: list of str :return: Return the resulting string. """ - return None + my_lst = ' '.join(words) + return my_lst def reverse_words_concatenation(words): """ 1 Kata - Given a list of words, write a program that concatenates the words in a reverse way + Given a list of words, write a program that concatenates the words in a reverse way (both words and each word itself) For example: reverse_words_concatenation(['take', 'me', 'home']) returns 'home me take' @@ -58,14 +63,17 @@ def reverse_words_concatenation(words): :param words: list of str :return: Return the resulting string. """ - return None + strings = words + strings.reverse() + my_lst = words_concatenation(strings) + return my_lst def is_unique_string(some_str): """ 2 Kata - Given a string, the function returns True if all characters in the string are unique, False otherwise + Given a string, the function returns True is all characters in the string are unique, False otherwise e.g 'abcd' -> True @@ -75,10 +83,14 @@ def is_unique_string(some_str): :param some_str: :return: bool """ - return None + if len(set(some_str)) == len(some_str): + return True + else: + return False def list_diff(elements): + """ 1 Kata @@ -86,27 +98,38 @@ def list_diff(elements): reduces by its previous one. The first element should be None e.g. - [1, 2, 3, 4, 7, 11] -> [None, 1, 1, 1, 3, 4] + [1, 2, 3, 4, 7, 11] -> [None, 1, 1, 3, 4] [] -> [] [1, 5, 0, 4, 1, 1, 1] -> [None, 4, -5, 4, -3, 0, 0] :param elements: list of integers :return: the diff list """ - return None + if len(elements) > 1: + diff_list = [elements[i] - elements[i - 1] for i in range(1, len(elements))] + diff_list.insert(0, None) + return diff_list + elif len(elements) > 0: + return [None] + else: + return [] def prime_number(num): """ - 1 Kata + 1 Kata - Check if the given number is prime or not. + Check if the given number is prime or not. - hint: use the built-in function "range" - :param num: the number to check - :return: bool. True if prime, else False - """ - return None + hint: use the built-in function "range" + :param num: the number to check + :return: bool. True if prime, else False + """ + if num > 1: + for i in range(2, num): + if (num % i) == 0: + return False + return True def palindrome_num(num): @@ -122,7 +145,12 @@ def palindrome_num(num): :param num: int :return: bool. True is palindrome, else False """ - return None + my_num = str(num) + my_rnum = my_num[::-1] + if my_num == my_rnum: + return True + else: + return False def pair_match(men, women): @@ -158,14 +186,14 @@ def pair_match(men, women): def bad_average(a, b, c): """ - 1 Kata + 1 Kata: fixed This function gets 3 numbers and calculates the average. There is a mistake in the following implementation, you are required to fix it :return: """ - return a + b + c / 3 + return (a + b + c) / 3 def best_student(grades): diff --git a/python_katas/kata_1/test.py b/python_katas/kata_1/test.py index 8ee82d09..e940a353 100644 --- a/python_katas/kata_1/test.py +++ b/python_katas/kata_1/test.py @@ -16,10 +16,15 @@ class TestSumOfElements(unittest.TestCase): """ 1 Katas """ + + def sum_of_element(elements): + return sum(elements) + def test_empty_list(self): lst = [] self.assertEqual(questions.sum_of_element(lst), 0) + def test_integers_list(self): lst = [1, 2, 3, 4, 5] self.assertEqual(questions.sum_of_element(lst), 15) @@ -58,9 +63,21 @@ class TestReverseWordsConcatenation(unittest.TestCase): 1 Katas """ - def test_sample(self): - # your code here - pass + def reverse_original_example(self): + lst = ['take', 'me', 'home'] + self.assertEqual(questions.reverse_words_concatenation(lst), 0) + + def reverse_empty_list(self): + lst = [] + self.assertEqual(questions.reverse_words_concatenation(lst), 0) + + def reverse_one_string(self): + lst = ['me'] + self.assertEqual(questions.reverse_words_concatenation(lst), 0) + + def reverse_same_strings(self): + lst = ['me', 'me', 'me'] + self.assertEqual(questions.reverse_words_concatenation(lst), 0) class TestIsUniqueString(unittest.TestCase): @@ -78,9 +95,7 @@ class TestListDiff(unittest.TestCase): 1 Katas """ - def test_sample(self): - # your code here - pass + class TestPrimeNumber(unittest.TestCase): @@ -88,9 +103,29 @@ class TestPrimeNumber(unittest.TestCase): 1 Katas """ - def test_sample(self): - # your code here - pass + def test_prime_1(self): + number = 7 + self.assertEqual(questions.prime_number(number), True) + + def test_prime_2(self): + number = 17 + self.assertEqual(questions.prime_number(number), True) + + def test_prime_3(self): + number = 13 + self.assertEqual(questions.prime_number(number), True) + + def test_not_prime_1(self): + number = 1 + self.assertEqual(questions.prime_number(number), False) + + def test_not_prime_2(self): + number = -20 + self.assertEqual(questions.prime_number(number), False) + + def test_not_prime_3(self): + number = 2.4 + self.assertEqual(questions.prime_number(number), False) class TestPalindromeNum(unittest.TestCase): @@ -128,9 +163,46 @@ class TestBestStudent(unittest.TestCase): 1 Katas """ - def test_sample(self): - # your code here - pass + def best_student_original_example(self): + + dict1 = { + "Ben": 78, + "Hen": 88, + "Natan": 99, + "Efraim": 65, + "Rachel": 95 + } + self.assertEqual(questions.best_student(lst), 0) + + def best_student_over_grades(self): + dict1 = { + "Ben": 178, + "Hen": 188, + "Natan": 299, + "Efraim": 365, + "Rachel": -95 + } + self.assertEqual(questions.best_student(lst), 0) + + def best_student_same_gardes(self): + dict1 = { + "Ben": 88, + "Hen": 88, + "Natan": 88, + "Efraim": 88, + "Rachel": 88 + } + self.assertEqual(questions.best_student(lst), 0) + + def best_student_float_grades(self): + dict1 = { + "Ben": 7.5, + "Hen": 8, + "Natan": 9, + "Efraim": 5.5, + "Rachel": 9.1 + } + self.assertEqual(questions.best_student(lst), 0) class TestPrintDictAsTable(unittest.TestCase): @@ -138,10 +210,6 @@ class TestPrintDictAsTable(unittest.TestCase): 1 Katas """ - def test_sample(self): - # your code here - pass - class TestMergeDicts(unittest.TestCase): """ @@ -158,10 +226,13 @@ class TestSevenBoom(unittest.TestCase): 1 Katas """ - def test_sample(self): - # your code here - pass + def test_if_contains_7(self): + n = 7 + self.assertEqual(questions.seven_boom(n), [7]) + def test_if_modulo_7(self): + n = 30 + self.assertEqual(questions.seven_boom(n), [7, 14, 17, 21, 27, 28]) class TestCaesarCipher(unittest.TestCase): """