<commit_before>from unittest import TestCase
from functional_test import FunctionalTest
import subprocess
import tempfile
from source_navigation_steps import SourceNavigationSteps
import os
import getpass

class SubmissionNotInMemoryTest(TestCase, FunctionalTest, SourceNavigationSteps):

    def setUp(self):
        self.devnull = open('/dev/null', 'r')
        FunctionalTest.setUp(self)

    def tearDown(self):
        FunctionalTest.tearDown(self)

    def _memory_dump(self, pid):
        core_dump_base_name = '/tmp/core_dump'
        core_dump_file_name = core_dump_base_name + '.' + pid
        try:
            subprocess.call(["sudo", "gcore", "-o", core_dump_base_name, pid], stdout=self.devnull, stderr=self.devnull)
            subprocess.call(["sudo", "chown", getpass.getuser(), core_dump_file_name])
            with open(core_dump_file_name, 'r') as fp:
                return fp.read()
        finally:
            os.remove(core_dump_file_name)

    def test_message_is_not_retained_in_memory(self):
        self._source_visits_source_homepage()
        self._source_chooses_to_submit_documents()
        self._source_continues_to_submit_page()
        self._source_submits_a_message()

        source_server_pid = str(self.source_process.pid)

        self.assertFalse(self.secret_message in self._memory_dump(source_server_pid))

    def test_file_upload_is_not_retained_in_memory(self):
        self._source_visits_source_homepage()
        self._source_chooses_to_submit_documents()
        self._source_continues_to_submit_page()
        self._source_submits_a_file()

        source_server_pid = str(self.source_process.pid)

        self.assertFalse(self.secret_message in self._memory_dump(source_server_pid))
<commit_msg>make tests fail with the number of occurences of the secret_message in memory<commit_after>from unittest import TestCase
from functional_test import FunctionalTest
import subprocess
import tempfile
from source_navigation_steps import SourceNavigationSteps
import os
import getpass
import re

class SubmissionNotInMemoryTest(TestCase, FunctionalTest, SourceNavigationSteps):

    def setUp(self):
        self.devnull = open('/dev/null', 'r')
        FunctionalTest.setUp(self)

    def tearDown(self):
        FunctionalTest.tearDown(self)

    def _memory_dump(self, pid):
        core_dump_base_name = '/tmp/core_dump'
        core_dump_file_name = core_dump_base_name + '.' + pid
        try:
            subprocess.call(["sudo", "gcore", "-o", core_dump_base_name, pid], stdout=self.devnull, stderr=self.devnull)
            subprocess.call(["sudo", "chown", getpass.getuser(), core_dump_file_name])
            with open(core_dump_file_name, 'r') as fp:
                return fp.read()
        finally:
            os.remove(core_dump_file_name)

    def _num_strings_in(self, needle, haystack):
        return sum(1 for _ in re.finditer(re.escape(needle), haystack))

    def test_message_is_not_retained_in_memory(self):
        self._source_visits_source_homepage()
        self._source_chooses_to_submit_documents()
        self._source_continues_to_submit_page()
        self._source_submits_a_message()

        source_server_pid = str(self.source_process.pid)

        memory_dump = self._memory_dump(source_server_pid)
        secrets_in_memory = self._num_strings_in(self.secret_message, memory_dump)

        self.assertLess(secrets_in_memory, 1)

    def test_file_upload_is_not_retained_in_memory(self):
        self._source_visits_source_homepage()
        self._source_chooses_to_submit_documents()
        self._source_continues_to_submit_page()
        self._source_submits_a_file()

        source_server_pid = str(self.source_process.pid)

        memory_dump = self._memory_dump(source_server_pid)
        secrets_in_memory = self._num_strings_in(self.secret_message, memory_dump)

        self.assertLess(secrets_in_memory, 1)

{'fraction_non_alphanumeric': 0.08452868852459017, 'fraction_numerical': 0.0007684426229508197, 'mean_word_length': 3.687875150060024, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4545608', 'n_tokens_mistral': 1248, 'n_tokens_neox': 1208, 'n_words': 200}