Example #1
0
    def testIsVotingAllowed_DisableHasFlaggedChecks(self):
        blockables = test_utils.CreateSantaBlockables(26)
        bundle = test_utils.CreateSantaBundle(bundle_binaries=blockables)

        # Flag one of the binaries.
        blockables[0].flagged = True
        blockables[0].put()

        with self.LoggedInUser():
            # Ensure that the normal call succeeds in finding the flagged binary.
            allowed, reason = bundle.IsVotingAllowed()
            self.assertFalse(allowed)
            self.assertEqual(
                constants.VOTING_PROHIBITED_REASONS.FLAGGED_BINARY, reason)

            # In a transaction, the 26 searched blockables should exceed the allowed
            # limit of 25.
            with self.assertRaises(db.BadRequestError):
                ndb.transaction(
                    lambda: bundle.IsVotingAllowed(enable_flagged_checks=True),
                    xg=True)

            # With the checks disabled, IsVotingAllowed shouldn't raise an exception.
            def Test():
                allowed, reason = bundle.IsVotingAllowed(
                    enable_flagged_checks=False)
                self.assertTrue(allowed)
                self.assertIsNone(reason)

            ndb.transaction(Test, xg=True)
Example #2
0
 def testIsVotingAllowed_CallTheSuper(self):
     bundle = test_utils.CreateSantaBundle()
     with self.LoggedInUser():
         with mock.patch.object(santa.base.Blockable,
                                'IsVotingAllowed') as mock_method:
             bundle.IsVotingAllowed()
             self.assertTrue(mock_method.called)
Example #3
0
    def testUserGetOwnEventWithContext_Bundle(self):
        """Getting an event of the requesting user's by id."""
        bundle = test_utils.CreateSantaBundle(
            bundle_binaries=[self.santa_blockable1])
        event = test_utils.CreateSantaEvent(
            self.santa_blockable1,
            bundle_key=bundle.key,
            executing_user=self.user_1.nickname,
            event_type=constants.EVENT_TYPE.ALLOW_UNKNOWN,
            host_id=self.santa_host1.key.id(),
            last_blocked_dt=datetime.datetime(2015, 3, 1, 1, 0, 0),
            first_blocked_dt=datetime.datetime(2015, 3, 1, 1, 0, 0),
            parent=utils.ConcatenateKeys(self.user_1.key, self.santa_host1.key,
                                         bundle.key))
        test_utils.CreateVote(bundle, user_email=self.user_1.email)

        params = {'withContext': 'true'}
        with self.LoggedInUser(user=self.user_1):
            response = self.testapp.get(self.ROUTE % event.key.urlsafe(),
                                        params)

        output = response.json

        self.assertIn('application/json', response.headers['Content-type'])
        self.assertIsInstance(output, dict)
        self.assertEqual(output['event']['id'], event.key.id())
        self.assertEqual(output['host']['id'], output['event']['hostId'])
        bundle_key = ndb.Key(urlsafe=output['event']['bundleKey'])
        self.assertEqual(output['blockable']['id'], bundle_key.id())
        self.assertIsNotNone(output['vote'])
Example #4
0
    def testUser_GetOwnEvent_SantaBundle(self):
        bundle = test_utils.CreateSantaBundle(
            bundle_binaries=[self.santa_blockable1])

        event = test_utils.CreateSantaEvent(
            self.santa_blockable1,
            bundle_key=bundle.key,
            executing_user=self.user_1.nickname,
            event_type=constants.EVENT_TYPE.BLOCK_UNKNOWN,
            host_id=self.santa_host1.key.id(),
            last_blocked_dt=datetime.datetime(2015, 3, 1, 1, 0, 0),
            first_blocked_dt=datetime.datetime(2015, 3, 1, 1, 0, 0),
            parent=utils.ConcatenateKeys(self.user_1.key, self.santa_host1.key,
                                         bundle.key))

        with self.LoggedInUser(user=self.user_1):
            response = self.testapp.get(self.ROUTE % bundle.key.id())

        output = response.json

        self.assertIn('application/json', response.headers['Content-type'])
        self.assertIsInstance(output, dict)

        self.assertEqual(output['id'], event.key.id())
        self.assertEqual(output['hostId'], event.host_id)
Example #5
0
    def testResetsState(self):
        bundle = test_utils.CreateSantaBundle(uploaded_dt=None)
        bundle.ResetState()

        self.assertTaskCount(constants.TASK_QUEUE.BQ_PERSISTENCE, 1)
        self.RunDeferredTasks(constants.TASK_QUEUE.BQ_PERSISTENCE)
        self.assertEntityCount(bigquery.BundleRow, 1)
Example #6
0
    def testPersistsStateChange(self):
        bundle = test_utils.CreateSantaBundle(uploaded_dt=None)
        bundle.ChangeState(constants.STATE.SUSPECT)

        self.assertTaskCount(constants.TASK_QUEUE.BQ_PERSISTENCE, 1)
        self.RunDeferredTasks(constants.TASK_QUEUE.BQ_PERSISTENCE)
        self.assertEntityCount(bigquery.BundleRow, 1)
Example #7
0
    def setUp(self):
        app = webapp2.WSGIApplication(routes=[lookups.ROUTES])
        super(LookupsTest, self).setUp(wsgi_app=app)

        self.santa_blockable1 = test_utils.CreateSantaBlockable(
            id='eeeeffffgggghhhh',
            id_type=constants.ID_TYPE.SHA256,
            blockable_hash='eeeeffffgggghhhh',
            file_name='Mac.app',
            publisher='Arple',
            product_name='New Shiny',
            version='2.0',
            flagged=True)

        self.santa_blockable2 = test_utils.CreateSantaBlockable()

        self.santa_bundle = test_utils.CreateSantaBundle(
            id='yyyyyyyyyyyyyyyyyyyyyyyyyyyy',
            bundle_binaries=[self.santa_blockable1, self.santa_blockable2])

        self.bit9_blockable1 = test_utils.CreateBit9Binary(
            id='zzzzzzzzzzzzzzzzzzzzzzzzzzzz',
            id_type=constants.ID_TYPE.SHA256,
            file_name='spyware.exe',
            publisher='TrustUs',
            product_name='Free PC Check!',
            version='1.0')

        self.bit9_blockable2 = test_utils.CreateBit9Binary(
            id='aaaaaaaaaaaaaaaaaaaaaaaaaaaa',
            id_type=constants.ID_TYPE.SHA256,
            file_name='legit.exe',
            publisher='A Really Trustworthy Company',
            product_name='Safe To Run',
            version='1.0')
Example #8
0
    def testToDict_Package(self):
        blockable = test_utils.CreateSantaBlockable()
        bundle = test_utils.CreateSantaBundle(bundle_binaries=[blockable])
        rule = test_utils.CreateSantaRule(
            bundle.key, rule_type=constants.RULE_TYPE.PACKAGE)

        self.assertSameElements([blockable.key.id()],
                                rule.to_dict()['binary_ids'])
Example #9
0
    def testHasFlaggedBinary_Yes(self):

        bundle_binaries = test_utils.CreateSantaBlockables(10, flagged=False)
        bundle_binaries[-1].flagged = True
        bundle_binaries[-1].put()
        bundle = test_utils.CreateSantaBundle(bundle_binaries=bundle_binaries)

        self.assertTrue(bundle.HasFlaggedBinary())
Example #10
0
    def testGet_Success_NoContents(self):
        bundle = test_utils.CreateSantaBundle()

        with self.LoggedInUser():
            response = self.testapp.get(self.ROUTE % bundle.key.id())
        output = response.json

        self.assertFalse(output)
Example #11
0
    def testPackage(self):
        blockable = test_utils.CreateSantaBlockable()
        bundle = test_utils.CreateSantaBundle(bundle_binaries=[blockable])
        rule = test_utils.CreateSantaRule(
            bundle.key, rule_type=constants.RULE_TYPE.PACKAGE)

        self.assertSameElements([blockable.key.id()],
                                model_utils.GetBundleBinaryIdsForRule(rule))
Example #12
0
    def testHasFlaggedCert_No(self):

        cert = test_utils.CreateSantaCertificate(flagged=False)
        bundle_binaries = test_utils.CreateSantaBlockables(10,
                                                           cert_key=cert.key)
        bundle = test_utils.CreateSantaBundle(bundle_binaries=bundle_binaries)

        self.assertFalse(bundle.HasFlaggedCert())
Example #13
0
    def testIsVotingAllowed_BundleUpload(self):
        bundle = test_utils.CreateSantaBundle(uploaded_dt=None)
        self.assertFalse(bundle.has_been_uploaded)

        allowed, reason = bundle.IsVotingAllowed()

        self.assertFalse(allowed)
        self.assertEqual(constants.VOTING_PROHIBITED_REASONS.UPLOADING_BUNDLE,
                         reason)
Example #14
0
    def testHasFlaggedCert_Yes(self):

        cert = test_utils.CreateSantaCertificate(flagged=True)
        bundle_binaries = test_utils.CreateSantaBlockables(10)
        bundle_binaries[-1].cert_key = cert.key
        bundle_binaries[-1].put()
        bundle = test_utils.CreateSantaBundle(bundle_binaries=bundle_binaries)

        self.assertTrue(bundle.HasFlaggedCert())
Example #15
0
    def testIgnoreCalculateScoreBeforeUpload(self):
        bundle = test_utils.CreateSantaBundle(uploaded_dt=None)
        test_utils.CreateVote(bundle)

        # Trigger the SantaBundle.score ComputedProperty calculation.
        bundle.put()

        # The score should have not reflected the real score until the bundle is
        # uploaded.
        self.assertEqual(0, bundle.key.get().score)
Example #16
0
 def testToDict_CertId(self):
     santa_certificate = test_utils.CreateSantaCertificate()
     blockable = test_utils.CreateSantaBlockable(
         cert_key=santa_certificate.key)
     bundle = test_utils.CreateSantaBundle(
         main_cert_key=santa_certificate.key, bundle_binaries=[blockable])
     with self.LoggedInUser():
         dict_ = bundle.to_dict()
     self.assertTrue(dict_['has_been_uploaded'])
     self.assertEqual(santa_certificate.key.id(), dict_['cert_id'])
Example #17
0
    def testGet_Success_Bundle(self):
        test_blockables = test_utils.CreateSantaBlockables(4)
        bundle = test_utils.CreateSantaBundle(bundle_binaries=test_blockables)

        with self.LoggedInUser():
            response = self.testapp.get(self.ROUTE % bundle.key.id())
        output = response.json

        self.assertSameElements(
            (blockable.key.id() for blockable in test_blockables),
            (blockable_dict['id'] for blockable_dict in output))
Example #18
0
  def testIsVotingAllowed_HasFlaggedCert(self):
    blockable = test_utils.CreateSantaBlockable(
        cert_key=self.santa_certificate.key)
    bundle = test_utils.CreateSantaBundle(bundle_binaries=[blockable])

    with self.LoggedInUser():
      allowed, reason = bundle.IsVotingAllowed()
      self.assertTrue(allowed)

      self.santa_certificate.flagged = True
      self.santa_certificate.put()

      allowed, reason = bundle.IsVotingAllowed()
      self.assertFalse(allowed)
      self.assertEqual(constants.VOTING_PROHIBITED_REASONS.FLAGGED_CERT, reason)
Example #19
0
    def testIsVotingAllowed_HasFlaggedBinary(self):
        # First, create two unflagged binaries.
        blockables = test_utils.CreateSantaBlockables(2)
        bundle = test_utils.CreateSantaBundle(bundle_binaries=blockables)

        with self.LoggedInUser():
            allowed, reason = bundle.IsVotingAllowed()
            self.assertTrue(allowed)

            # Now flag one of the binaries.
            blockables[0].flagged = True
            blockables[0].put()

            allowed, reason = bundle.IsVotingAllowed()
            self.assertFalse(allowed)
            self.assertEqual(
                constants.VOTING_PROHIBITED_REASONS.FLAGGED_BINARY, reason)
Example #20
0
    def testGet_Success_SantaBinaryOrder(self):
        bundle = test_utils.CreateSantaBundle(binary_count=4)
        path_pairs = [('a', 'z'), ('a', 'y'), ('a/b/c', 'x'), ('a/b', 'z')]
        expected_path_order = ['a/y', 'a/z', 'a/b/z', 'a/b/c/x']
        for rel_path, file_name in path_pairs:
            binary = test_utils.CreateSantaBlockable()
            santa.SantaBundleBinary.Generate(bundle.key,
                                             binary.key,
                                             cert_key=binary.cert_key,
                                             rel_path=rel_path,
                                             file_name=file_name).put()

        with self.LoggedInUser():
            response = self.testapp.get(self.ROUTE % bundle.key.id())
        output = response.json

        self.assertListEqual(
            expected_path_order,
            [blockable_dict['fullPath'] for blockable_dict in output])
Example #21
0
 def testIsInstance(self):
     bundle = test_utils.CreateSantaBundle()
     self.assertTrue(bundle.IsInstance('Blockable'))
     self.assertTrue(bundle.IsInstance('Package'))
     self.assertTrue(bundle.IsInstance('SantaBundle'))
     self.assertFalse(bundle.IsInstance('SomethingElse'))
Example #22
0
    def testHasFlaggedBinary_No(self):

        bundle_binaries = test_utils.CreateSantaBlockables(10, flagged=False)
        bundle = test_utils.CreateSantaBundle(bundle_binaries=bundle_binaries)

        self.assertFalse(bundle.HasFlaggedBinary())
Example #23
0
 def testToDict(self):
     bundle = test_utils.CreateSantaBundle()
     with self.LoggedInUser():
         dict_ = bundle.to_dict()
     self.assertTrue(dict_['has_been_uploaded'])
     self.assertIsNone(dict_['cert_id'])
Example #24
0
 def testResetsState(self):
     bundle = test_utils.CreateSantaBundle(uploaded_dt=None)
     bundle.ResetState()
     self.assertBigQueryInsertion(constants.BIGQUERY_TABLE.BUNDLE)
Example #25
0
 def testPersistsStateChange(self):
     bundle = test_utils.CreateSantaBundle(uploaded_dt=None)
     bundle.ChangeState(constants.STATE.SUSPECT)
     self.assertBigQueryInsertion(constants.BIGQUERY_TABLE.BUNDLE)