def test_model_one_stroke(self):
     print("Testing by training model with one keystroke")
     user = self.kda.getUser(TestAuthenticator.validUser[0])
     for functionName in TestAuthenticator.validFunction:
         detector = KeystrokeDynamicAttacker.getDetector(functionName)
         detector.trainModel(user.getStrokes(1))
         self.assertEqual(detector.evaluate(user.getStrokes(1)[0]), 1)
 def test_invalid_type_population(self):
     print("Test spoofing using a nonsensical string ")
     user = self.kda.getUser(TestAuthenticator.validUser[0])
     functionName = TestAuthenticator.validFunction[0]
     detector = KeystrokeDynamicAttacker.getDetector(functionName)
     detector.trainModel(user.getStrokes(1))
     with self.assertRaises(TypeError):
         spoofer = KeystrokeSpoofer("Something between 2 and 7 ", detector)
         spoofer.createSpoof()
 def test_floating_population(self):
     print("Test spoofing using a fraction as a population ")
     user = self.kda.getUser(TestAuthenticator.validUser[0])
     functionName = TestAuthenticator.validFunction[0]
     detector = KeystrokeDynamicAttacker.getDetector(functionName)
     detector.trainModel(user.getStrokes(1))
     with self.assertWarns(UserWarning):
         spoofer = KeystrokeSpoofer(20.3423, detector)
         spoofer.createSpoof()
 def test_zero_population(self):
     print("Testing by trying to setting population to zero")
     user = self.kda.getUser(TestAuthenticator.validUser[0])
     functionName = TestAuthenticator.validFunction[0]
     detector = KeystrokeDynamicAttacker.getDetector(functionName)
     detector.trainModel(user.getStrokes(1))
     with self.assertRaises(ValueError):
         spoofer = KeystrokeSpoofer(0, detector)
         spoofer.createSpoof()
 def test_positive_population(self):
     print("Testing model using a valid population")
     user = self.kda.getUser(TestAuthenticator.validUser[0])
     functionName = TestAuthenticator.validFunction[0]
     detector = KeystrokeDynamicAttacker.getDetector(functionName)
     detector.trainModel(user.getStrokes(1))
     print(user.getStrokes(1))
     spoofer = KeystrokeSpoofer(100, detector)
     self.assertTrue(len(spoofer.createSpoof()) > 0)
 def test_keystroke_similarity(self):
     print("Testing similarity rating on evaluators")
     user = self.kda.getUser(TestAuthenticator.validUser[0])
     fakeKeystroke = np.full(31, 50)
     for functionName in TestAuthenticator.validFunction:
         detector = KeystrokeDynamicAttacker.getDetector(functionName)
         detector.trainModel(user.getTrainingVector())
         # Fake should be less similar then the real user keystroke
         self.assertTrue(
             detector.evaluate(fakeKeystroke) < detector.evaluate(
                 user.getStrokes(1)[0]))
 def setUp(self):
     self.kda = KeystrokeDynamicAttacker(
         "Resources/DSL-StrongPasswordData.csv")
class TestAuthenticator(unittest.TestCase):
    validFunction = [
        "Euclidean", "Euclidean normed", "Manhattan", "Manhattan scaled"
    ]
    invalidFunction = "I am not a function"
    validUser = ["s022", "s033"]
    invalidUser = "******"

    def setUp(self):
        self.kda = KeystrokeDynamicAttacker(
            "Resources/DSL-StrongPasswordData.csv")

    def test_keystroke_similarity(self):
        print("Testing similarity rating on evaluators")
        user = self.kda.getUser(TestAuthenticator.validUser[0])
        fakeKeystroke = np.full(31, 50)
        for functionName in TestAuthenticator.validFunction:
            detector = KeystrokeDynamicAttacker.getDetector(functionName)
            detector.trainModel(user.getTrainingVector())
            # Fake should be less similar then the real user keystroke
            self.assertTrue(
                detector.evaluate(fakeKeystroke) < detector.evaluate(
                    user.getStrokes(1)[0]))

    # test the model using one keystroke as it should give us back a perfect match
    def test_model_one_stroke(self):
        print("Testing by training model with one keystroke")
        user = self.kda.getUser(TestAuthenticator.validUser[0])
        for functionName in TestAuthenticator.validFunction:
            detector = KeystrokeDynamicAttacker.getDetector(functionName)
            detector.trainModel(user.getStrokes(1))
            self.assertEqual(detector.evaluate(user.getStrokes(1)[0]), 1)

    # Test positive value as population
    def test_positive_population(self):
        print("Testing model using a valid population")
        user = self.kda.getUser(TestAuthenticator.validUser[0])
        functionName = TestAuthenticator.validFunction[0]
        detector = KeystrokeDynamicAttacker.getDetector(functionName)
        detector.trainModel(user.getStrokes(1))
        print(user.getStrokes(1))
        spoofer = KeystrokeSpoofer(100, detector)
        self.assertTrue(len(spoofer.createSpoof()) > 0)

    # Test positive value as population
    def test_negative_population(self):
        print("Testing by trying to set a negative population")
        user = self.kda.getUser(TestAuthenticator.validUser[0])
        functionName = TestAuthenticator.validFunction[0]
        detector = KeystrokeDynamicAttacker.getDetector(functionName)
        detector.trainModel(user.getStrokes(1))
        with self.assertRaises(ValueError):
            spoofer = KeystrokeSpoofer(-100, detector)
            spoofer.createSpoof()

    # Test with 0 as a population
    def test_zero_population(self):
        print("Testing by trying to setting population to zero")
        user = self.kda.getUser(TestAuthenticator.validUser[0])
        functionName = TestAuthenticator.validFunction[0]
        detector = KeystrokeDynamicAttacker.getDetector(functionName)
        detector.trainModel(user.getStrokes(1))
        with self.assertRaises(ValueError):
            spoofer = KeystrokeSpoofer(0, detector)
            spoofer.createSpoof()

    def test_floating_population(self):
        print("Test spoofing using a fraction as a population ")
        user = self.kda.getUser(TestAuthenticator.validUser[0])
        functionName = TestAuthenticator.validFunction[0]
        detector = KeystrokeDynamicAttacker.getDetector(functionName)
        detector.trainModel(user.getStrokes(1))
        with self.assertWarns(UserWarning):
            spoofer = KeystrokeSpoofer(20.3423, detector)
            spoofer.createSpoof()

    def test_invalid_type_population(self):
        print("Test spoofing using a nonsensical string ")
        user = self.kda.getUser(TestAuthenticator.validUser[0])
        functionName = TestAuthenticator.validFunction[0]
        detector = KeystrokeDynamicAttacker.getDetector(functionName)
        detector.trainModel(user.getStrokes(1))
        with self.assertRaises(TypeError):
            spoofer = KeystrokeSpoofer("Something between 2 and 7 ", detector)
            spoofer.createSpoof()

    def test_valid_keystroke_getter(self):
        print("Test to get make sure keystroke getter works ")
        user = self.kda.getUser(TestAuthenticator.validUser[0])
        self.assertEqual(len(user.getStrokes(10)), 10)

    def test_negative_keystroke_getter(self):
        print("Try getting back negative keystrokes ")
        user = self.kda.getUser(TestAuthenticator.validUser[0])
        with self.assertWarns(UserWarning):
            self.assertEqual(len(user.getStrokes(-100)), 0)

    def test_out_of_bound_keystroke_getter(self):
        print("Try getting more keystrokes then is available ")
        user = self.kda.getUser(TestAuthenticator.validUser[0])
        with self.assertWarns(UserWarning):
            self.assertEqual(len(user.getStrokes(1000)), 400)

    def test_making_children(self):
        print("Seeing the result of making kids ")
        # make an ultra-fit parent with [0, 0, 0] as keystroke
        parent1 = Keystroke(np.zeros(3), 1)
        # make an ultra-fit parent with [5, 5, 5] as keystroke
        parent2 = Keystroke(np.full(3, 5), 1)
        perfectKid = np.full(3, 5)
        # 10000 is being dramatic because there is a 5% chance of mutation
        # But, with perfects who are perfectly fit we should have no mutation
        for i in range(10000):
            self.assertTrue((parent1.makeChild(parent2) - perfectKid).all())
            self.assertTrue((parent2.makeChild(parent1) - perfectKid).all())

    def test_invalid_user_main(self):
        print("Test main with an invalid user ")
        with self.assertRaises(ValueError):
            self.kda.spoofUser(TestAuthenticator.invalidUser,
                               TestAuthenticator.validFunction[0], 20)

    def test_invalid_spoof_function_main(self):
        print("Test main with invalid function for evaluation ")
        with self.assertRaises(ValueError):
            self.kda.spoofUser(TestAuthenticator.validUser[0],
                               TestAuthenticator.invalidFunction, 20)

    def test_invalid_classify_function_main(self):
        print("Test main with invalid function for evaluation ")
        with self.assertRaises(ValueError):
            self.kda.classifyUsers(TestAuthenticator.invalidFunction)

    # test detection of obvious goats
    def testGoat(self):
        print("Test if classifier can detect goats amidst group of sheep")
        sheep = AnimalCreator.create_sheep(200)
        goat = AnimalCreator.create_goat(2)
        # add the goats to the sheep
        sheep.update(goat)
        detector = self.kda.getDetector("Manhattan")
        classifier = Classifier(sheep, detector, True)
        sheepIDs, lambsIDs, goatsIDs, wolvesIDs = classifier.classifyUser()
        self.assertEqual(sheepIDs, 200)
        # self.assertEqual(lambsIDs, 0) # can't guarantee
        self.assertEqual(goatsIDs, 2)
        # self.assertEqual(wolvesIDs, 0)

    # test detection of obvious wolf and lambs
    def test_wolf_and_lamb(self):
        print("Test if classifier can detect lambs and wolves")
        sheep = AnimalCreator.create_sheep(400)
        animal = AnimalCreator.create_lambs_and_wolves(2)
        goat = AnimalCreator.create_goat(5)
        # add the animals to the sheep
        animal.update(sheep)
        animal.update(goat)
        detector = self.kda.getDetector("Manhattan")
        classifier = Classifier(animal, detector, True)
        sheepIDs, lambsIDs, goatsIDs, wolvesIDs = classifier.classifyUser()
        self.assertTrue(400 <= sheepIDs <= 406)
        self.assertEqual(lambsIDs, 4)
        self.assertEqual(goatsIDs, 5)
        self.assertEqual(wolvesIDs, 2)