def test_name1_auth(self, mock_chall_path):
        dom = "0"

        self.handler.add_chall_msg(
            dom,
            messages.Challenge(session_id="0",
                               nonce="nonce0",
                               challenges=acme_util.DV_CHALLENGES,
                               combinations=acme_util.gen_combos(
                                   acme_util.DV_CHALLENGES)), "dummy_key")

        path = gen_path([acme_util.SIMPLE_HTTPS], acme_util.DV_CHALLENGES)
        mock_chall_path.return_value = path
        self.handler._satisfy_challenges()  # pylint: disable=protected-access

        self.assertEqual(len(self.handler.responses), 1)
        self.assertEqual(len(self.handler.responses[dom]),
                         len(acme_util.DV_CHALLENGES))
        self.assertEqual(len(self.handler.dv_c), 1)
        self.assertEqual(len(self.handler.cont_c), 1)

        # Test if statement for cont_auth perform
        self.assertEqual(self.mock_cont_auth.perform.call_count, 0)
        self.assertEqual(self.mock_dv_auth.perform.call_count, 1)

        self.assertEqual(
            self.handler.responses[dom],
            self._get_exp_response(dom, path, acme_util.DV_CHALLENGES))

        self.assertEqual(len(self.handler.dv_c[dom]), 1)
        self.assertEqual(len(self.handler.cont_c[dom]), 0)
        self.assertTrue(
            isinstance(self.handler.dv_c[dom][0].achall,
                       achallenges.SimpleHTTPS))
    def test_name1_all(self, mock_chall_path):
        dom = "0"

        combos = acme_util.gen_combos(acme_util.CHALLENGES)
        self.handler.add_chall_msg(
            dom,
            messages.Challenge(session_id=dom,
                               nonce="nonce0",
                               challenges=acme_util.CHALLENGES,
                               combinations=combos), "dummy_key")

        path = gen_path([acme_util.SIMPLE_HTTPS, acme_util.RECOVERY_TOKEN],
                        acme_util.CHALLENGES)
        mock_chall_path.return_value = path

        self.handler._satisfy_challenges()  # pylint: disable=protected-access

        self.assertEqual(len(self.handler.responses), 1)
        self.assertEqual(len(self.handler.responses[dom]),
                         len(acme_util.CHALLENGES))
        self.assertEqual(len(self.handler.dv_c), 1)
        self.assertEqual(len(self.handler.cont_c), 1)
        self.assertEqual(len(self.handler.dv_c[dom]), 1)
        self.assertEqual(len(self.handler.cont_c[dom]), 1)

        self.assertEqual(
            self.handler.responses[dom],
            self._get_exp_response(dom, path, acme_util.CHALLENGES))
        self.assertTrue(
            isinstance(self.handler.dv_c[dom][0].achall,
                       achallenges.SimpleHTTPS))
        self.assertTrue(
            isinstance(self.handler.cont_c[dom][0].achall,
                       achallenges.RecoveryToken))
    def test_name1_all(self, mock_chall_path):
        dom = "0"

        combos = acme_util.gen_combos(acme_util.CHALLENGES)
        self.handler.add_chall_msg(
            dom,
            messages.Challenge(
                session_id=dom, nonce="nonce0", challenges=acme_util.CHALLENGES,
                combinations=combos),
            "dummy_key")

        path = gen_path([acme_util.SIMPLE_HTTPS, acme_util.RECOVERY_TOKEN],
                        acme_util.CHALLENGES)
        mock_chall_path.return_value = path

        self.handler._satisfy_challenges()  # pylint: disable=protected-access

        self.assertEqual(len(self.handler.responses), 1)
        self.assertEqual(
            len(self.handler.responses[dom]), len(acme_util.CHALLENGES))
        self.assertEqual(len(self.handler.dv_c), 1)
        self.assertEqual(len(self.handler.cont_c), 1)
        self.assertEqual(len(self.handler.dv_c[dom]), 1)
        self.assertEqual(len(self.handler.cont_c[dom]), 1)

        self.assertEqual(
            self.handler.responses[dom],
            self._get_exp_response(dom, path, acme_util.CHALLENGES))
        self.assertTrue(isinstance(self.handler.dv_c[dom][0].achall,
                                   achallenges.SimpleHTTPS))
        self.assertTrue(isinstance(self.handler.cont_c[dom][0].achall,
                                   achallenges.RecoveryToken))
    def test_name5_all(self, mock_chall_path):
        combos = acme_util.gen_combos(acme_util.CHALLENGES)
        for i in xrange(5):
            self.handler.add_chall_msg(
                str(i),
                messages.Challenge(
                    session_id=str(i), nonce="nonce%d" % i,
                    challenges=acme_util.CHALLENGES, combinations=combos),
                "dummy_key")

        path = gen_path([acme_util.DVSNI, acme_util.RECOVERY_CONTACT],
                        acme_util.CHALLENGES)
        mock_chall_path.return_value = path

        self.handler._satisfy_challenges()  # pylint: disable=protected-access

        self.assertEqual(len(self.handler.responses), 5)
        for i in xrange(5):
            self.assertEqual(
                len(self.handler.responses[str(i)]), len(acme_util.CHALLENGES))
        self.assertEqual(len(self.handler.dv_c), 5)
        self.assertEqual(len(self.handler.cont_c), 5)

        for i in xrange(5):
            dom = str(i)
            self.assertEqual(
                self.handler.responses[dom],
                self._get_exp_response(dom, path, acme_util.CHALLENGES))
            self.assertEqual(len(self.handler.dv_c[dom]), 1)
            self.assertEqual(len(self.handler.cont_c[dom]), 1)

            self.assertTrue(isinstance(self.handler.dv_c[dom][0].achall,
                                       achallenges.DVSNI))
            self.assertTrue(isinstance(self.handler.cont_c[dom][0].achall,
                                       achallenges.RecoveryContact))
    def test_name5_all(self, mock_chall_path):
        challenges = acme_util.get_challenges()
        combos = acme_util.gen_combos(challenges)
        for i in xrange(5):
            self.handler.add_chall_msg(
                str(i),
                messages.Challenge(
                    session_id=str(i), nonce="nonce%d" % i,
                    challenges=challenges, combinations=combos),
                "dummy_key")

        path = gen_path(["dvsni", "recoveryContact"], challenges)
        mock_chall_path.return_value = path

        self.handler._satisfy_challenges()  # pylint: disable=protected-access

        self.assertEqual(len(self.handler.responses), 5)
        for i in xrange(5):
            self.assertEqual(
                len(self.handler.responses[str(i)]), len(challenges))
        self.assertEqual(len(self.handler.dv_c), 5)
        self.assertEqual(len(self.handler.client_c), 5)

        for i in xrange(5):
            dom = str(i)
            self.assertEqual(
                self.handler.responses[dom],
                self._get_exp_response(dom, path, challenges))
            self.assertEqual(len(self.handler.dv_c[dom]), 1)
            self.assertEqual(len(self.handler.client_c[dom]), 1)

            self.assertTrue(isinstance(self.handler.dv_c[dom][0].chall,
                                       challenge_util.DvsniChall))
            self.assertTrue(isinstance(self.handler.client_c[dom][0].chall,
                                       challenge_util.RecContactChall))
    def test_name1_auth(self, mock_chall_path):
        dom = "0"

        self.handler.add_chall_msg(
            dom,
            messages.Challenge(
                session_id="0", nonce="nonce0",
                challenges=acme_util.DV_CHALLENGES,
                combinations=acme_util.gen_combos(acme_util.DV_CHALLENGES)),
            "dummy_key")

        path = gen_path([acme_util.SIMPLE_HTTPS], acme_util.DV_CHALLENGES)
        mock_chall_path.return_value = path
        self.handler._satisfy_challenges()  # pylint: disable=protected-access

        self.assertEqual(len(self.handler.responses), 1)
        self.assertEqual(len(self.handler.responses[dom]),
                         len(acme_util.DV_CHALLENGES))
        self.assertEqual(len(self.handler.dv_c), 1)
        self.assertEqual(len(self.handler.cont_c), 1)

        # Test if statement for cont_auth perform
        self.assertEqual(self.mock_cont_auth.perform.call_count, 0)
        self.assertEqual(self.mock_dv_auth.perform.call_count, 1)

        self.assertEqual(
            self.handler.responses[dom],
            self._get_exp_response(dom, path, acme_util.DV_CHALLENGES))

        self.assertEqual(len(self.handler.dv_c[dom]), 1)
        self.assertEqual(len(self.handler.cont_c[dom]), 0)
        self.assertTrue(isinstance(self.handler.dv_c[dom][0].achall,
                                   achallenges.SimpleHTTPS))
    def test_name1_all(self, mock_chall_path):
        dom = "0"

        challenges = acme_util.get_challenges()
        combos = acme_util.gen_combos(challenges)
        self.handler.add_chall_msg(
            dom,
            messages.Challenge(session_id=dom, nonce="nonce0",
                               challenges=challenges, combinations=combos),
            "dummy_key")

        path = gen_path(["simpleHttps", "recoveryToken"], challenges)
        mock_chall_path.return_value = path

        self.handler._satisfy_challenges()  # pylint: disable=protected-access

        self.assertEqual(len(self.handler.responses), 1)
        self.assertEqual(len(self.handler.responses[dom]), len(challenges))
        self.assertEqual(len(self.handler.dv_c), 1)
        self.assertEqual(len(self.handler.client_c), 1)
        self.assertEqual(len(self.handler.dv_c[dom]), 1)
        self.assertEqual(len(self.handler.client_c[dom]), 1)

        self.assertEqual(
            self.handler.responses[dom],
            self._get_exp_response(dom, path, challenges))
        self.assertTrue(isinstance(self.handler.dv_c[dom][0].chall,
                                   challenge_util.SimpleHttpsChall))
        self.assertTrue(isinstance(self.handler.client_c[dom][0].chall,
                                   challenge_util.RecTokenChall))
    def test_name1_all(self, mock_chall_path):
        dom = "0"

        challenges = acme_util.get_challenges()
        combos = acme_util.gen_combos(challenges)
        self.handler.add_chall_msg(
            dom,
            messages.Challenge(session_id=dom,
                               nonce="nonce0",
                               challenges=challenges,
                               combinations=combos), "dummy_key")

        path = gen_path(["simpleHttps", "recoveryToken"], challenges)
        mock_chall_path.return_value = path

        self.handler._satisfy_challenges()  # pylint: disable=protected-access

        self.assertEqual(len(self.handler.responses), 1)
        self.assertEqual(len(self.handler.responses[dom]), len(challenges))
        self.assertEqual(len(self.handler.dv_c), 1)
        self.assertEqual(len(self.handler.client_c), 1)
        self.assertEqual(len(self.handler.dv_c[dom]), 1)
        self.assertEqual(len(self.handler.client_c[dom]), 1)

        self.assertEqual(self.handler.responses[dom],
                         self._get_exp_response(dom, path, challenges))
        self.assertTrue(
            isinstance(self.handler.dv_c[dom][0].chall,
                       challenge_util.SimpleHttpsChall))
        self.assertTrue(
            isinstance(self.handler.client_c[dom][0].chall,
                       challenge_util.RecTokenChall))
    def test_common_case_with_continuity(self):
        challs = (acme_util.RECOVERY_TOKEN, acme_util.RECOVERY_CONTACT,
                  acme_util.DVSNI, acme_util.SIMPLE_HTTPS)
        prefs = [challenges.RecoveryToken, challenges.DVSNI]
        combos = acme_util.gen_combos(challs)
        self.assertEqual(self._call(challs, prefs, combos), (0, 2))

        # dumb_path() trivial test
        self.assertTrue(self._call(challs, prefs, None))
    def test_common_case_with_continuity(self):
        challs = (acme_util.RECOVERY_TOKEN,
                  acme_util.RECOVERY_CONTACT,
                  acme_util.DVSNI,
                  acme_util.SIMPLE_HTTPS)
        prefs = [challenges.RecoveryToken, challenges.DVSNI]
        combos = acme_util.gen_combos(challs)
        self.assertEqual(self._call(challs, prefs, combos), (0, 2))

         # dumb_path() trivial test
        self.assertTrue(self._call(challs, prefs, None))
    def test_perform_exception_cleanup(self, mock_chall_path):
        """3 Challenge messages... fail perform... clean up."""
        # pylint: disable=protected-access
        self.mock_dv_auth.perform.side_effect = errors.LetsEncryptDvsniError

        challenges = acme_util.get_challenges()
        combos = acme_util.gen_combos(challenges)

        for i in xrange(3):
            self.handler.add_chall_msg(
                str(i),
                messages.Challenge(
                    session_id=str(i), nonce="nonce%d" % i,
                    challenges=challenges, combinations=combos),
                "dummy_key")

        mock_chall_path.side_effect = [
            gen_path(["dvsni", "proofOfPossession"], challenges),
            gen_path(["proofOfPossession"], challenges),
            gen_path(["dvsni"], challenges),
        ]

        # This may change in the future... but for now catch the error
        self.assertRaises(errors.LetsEncryptAuthHandlerError,
                          self.handler._satisfy_challenges)

        # Verify cleanup is actually run correctly
        self.assertEqual(self.mock_dv_auth.cleanup.call_count, 2)
        self.assertEqual(self.mock_client_auth.cleanup.call_count, 2)


        dv_cleanup_args = self.mock_dv_auth.cleanup.call_args_list
        client_cleanup_args = self.mock_client_auth.cleanup.call_args_list

        # Check DV cleanup
        for i in xrange(2):
            dv_chall_list = dv_cleanup_args[i][0][0]
            self.assertEqual(len(dv_chall_list), 1)
            self.assertTrue(
                isinstance(dv_chall_list[0], challenge_util.DvsniChall))


        # Check Auth cleanup
        for i in xrange(2):
            client_chall_list = client_cleanup_args[i][0][0]
            self.assertEqual(len(client_chall_list), 1)
            self.assertTrue(
                isinstance(client_chall_list[0], challenge_util.PopChall))
    def test_full_cont_server(self):
        challs = (acme_util.RECOVERY_TOKEN, acme_util.RECOVERY_CONTACT,
                  acme_util.POP, acme_util.DVSNI, acme_util.SIMPLE_HTTPS,
                  acme_util.DNS)
        # Typical webserver client that can do everything except DNS
        # Attempted to make the order realistic
        prefs = [
            challenges.RecoveryToken, challenges.ProofOfPossession,
            challenges.SimpleHTTPS, challenges.DVSNI,
            challenges.RecoveryContact
        ]
        combos = acme_util.gen_combos(challs)
        self.assertEqual(self._call(challs, prefs, combos), (0, 4))

        # Dumb path trivial test
        self.assertTrue(self._call(challs, prefs, None))
    def test_perform_exception_cleanup(self, mock_chall_path):
        """3 Challenge messages... fail perform... clean up."""
        # pylint: disable=protected-access
        self.mock_dv_auth.perform.side_effect = errors.LetsEncryptDvsniError

        challenges = acme_util.get_challenges()
        combos = acme_util.gen_combos(challenges)

        for i in xrange(3):
            self.handler.add_chall_msg(
                str(i),
                messages.Challenge(session_id=str(i),
                                   nonce="nonce%d" % i,
                                   challenges=challenges,
                                   combinations=combos), "dummy_key")

        mock_chall_path.side_effect = [
            gen_path(["dvsni", "proofOfPossession"], challenges),
            gen_path(["proofOfPossession"], challenges),
            gen_path(["dvsni"], challenges),
        ]

        # This may change in the future... but for now catch the error
        self.assertRaises(errors.LetsEncryptAuthHandlerError,
                          self.handler._satisfy_challenges)

        # Verify cleanup is actually run correctly
        self.assertEqual(self.mock_dv_auth.cleanup.call_count, 2)
        self.assertEqual(self.mock_client_auth.cleanup.call_count, 2)

        dv_cleanup_args = self.mock_dv_auth.cleanup.call_args_list
        client_cleanup_args = self.mock_client_auth.cleanup.call_args_list

        # Check DV cleanup
        for i in xrange(2):
            dv_chall_list = dv_cleanup_args[i][0][0]
            self.assertEqual(len(dv_chall_list), 1)
            self.assertTrue(
                isinstance(dv_chall_list[0], challenge_util.DvsniChall))

        # Check Auth cleanup
        for i in xrange(2):
            client_chall_list = client_cleanup_args[i][0][0]
            self.assertEqual(len(client_chall_list), 1)
            self.assertTrue(
                isinstance(client_chall_list[0], challenge_util.PopChall))
    def test_full_cont_server(self):
        challs = (acme_util.RECOVERY_TOKEN,
                  acme_util.RECOVERY_CONTACT,
                  acme_util.POP,
                  acme_util.DVSNI,
                  acme_util.SIMPLE_HTTPS,
                  acme_util.DNS)
        # Typical webserver client that can do everything except DNS
        # Attempted to make the order realistic
        prefs = [challenges.RecoveryToken,
                 challenges.ProofOfPossession,
                 challenges.SimpleHTTPS,
                 challenges.DVSNI,
                 challenges.RecoveryContact]
        combos = acme_util.gen_combos(challs)
        self.assertEqual(self._call(challs, prefs, combos), (0, 4))

        # Dumb path trivial test
        self.assertTrue(self._call(challs, prefs, None))
    def test_name5_all(self, mock_chall_path):
        combos = acme_util.gen_combos(acme_util.CHALLENGES)
        for i in xrange(5):
            self.handler.add_chall_msg(
                str(i),
                messages.Challenge(session_id=str(i),
                                   nonce="nonce%d" % i,
                                   challenges=acme_util.CHALLENGES,
                                   combinations=combos), "dummy_key")

        path = gen_path([acme_util.DVSNI, acme_util.RECOVERY_CONTACT],
                        acme_util.CHALLENGES)
        mock_chall_path.return_value = path

        self.handler._satisfy_challenges()  # pylint: disable=protected-access

        self.assertEqual(len(self.handler.responses), 5)
        for i in xrange(5):
            self.assertEqual(len(self.handler.responses[str(i)]),
                             len(acme_util.CHALLENGES))
        self.assertEqual(len(self.handler.dv_c), 5)
        self.assertEqual(len(self.handler.cont_c), 5)

        for i in xrange(5):
            dom = str(i)
            self.assertEqual(
                self.handler.responses[dom],
                self._get_exp_response(dom, path, acme_util.CHALLENGES))
            self.assertEqual(len(self.handler.dv_c[dom]), 1)
            self.assertEqual(len(self.handler.cont_c[dom]), 1)

            self.assertTrue(
                isinstance(self.handler.dv_c[dom][0].achall,
                           achallenges.DVSNI))
            self.assertTrue(
                isinstance(self.handler.cont_c[dom][0].achall,
                           achallenges.RecoveryContact))
    def test_name5_all(self, mock_chall_path):
        challenges = acme_util.get_challenges()
        combos = acme_util.gen_combos(challenges)
        for i in xrange(5):
            self.handler.add_chall_msg(
                str(i),
                messages.Challenge(session_id=str(i),
                                   nonce="nonce%d" % i,
                                   challenges=challenges,
                                   combinations=combos), "dummy_key")

        path = gen_path(["dvsni", "recoveryContact"], challenges)
        mock_chall_path.return_value = path

        self.handler._satisfy_challenges()  # pylint: disable=protected-access

        self.assertEqual(len(self.handler.responses), 5)
        for i in xrange(5):
            self.assertEqual(len(self.handler.responses[str(i)]),
                             len(challenges))
        self.assertEqual(len(self.handler.dv_c), 5)
        self.assertEqual(len(self.handler.client_c), 5)

        for i in xrange(5):
            dom = str(i)
            self.assertEqual(self.handler.responses[dom],
                             self._get_exp_response(dom, path, challenges))
            self.assertEqual(len(self.handler.dv_c[dom]), 1)
            self.assertEqual(len(self.handler.client_c[dom]), 1)

            self.assertTrue(
                isinstance(self.handler.dv_c[dom][0].chall,
                           challenge_util.DvsniChall))
            self.assertTrue(
                isinstance(self.handler.client_c[dom][0].chall,
                           challenge_util.RecContactChall))