Пример #1
0
def main(args, keys):
    if args.mode == MODE_ENCRYPT:
        # check if destination exists
        if args.force is False and os.path.exists(args.outputfile):
            if ask_overwrite(args.outputfile) is False:
                return

        # args.inputfile is a list of files
        for f in args.inputfile:
            if not os.path.exists(f):
                raise SesameError('File doesn\'t exist at {0}'.format(f))

        encrypt(args.inputfile, args.outputfile, keys)

    elif args.mode == MODE_DECRYPT:
        # fail if input file doesn't exist
        if not os.path.exists(args.inputfile):
            raise SesameError('File doesn\'t exist at {0}'.format(args.inputfile))

        # check input not zero-length
        statinfo = os.stat(args.inputfile)
        if statinfo.st_size == 0:
            raise SesameError('Input file is zero-length')

        decrypt(args.inputfile, args.force, args.output_dir, args.try_all)
Пример #2
0
    def test_single_relative(self):
        """
        Simple auto-gen key; relative paths; deletes source file
        """
        # use only the first test file
        test_file_path = self.file_contents.keys()[0]

        with cd(self.working_dir):
            # encrypt the test file
            encrypt(
                inputfiles=[test_file_path],
                outputfile='sesame.encrypted',
                keys=[self.key],
            )

            # delete the source file
            os.remove(test_file_path)

            # decrypt the test file
            decrypt(
                inputfile='sesame.encrypted',
                keys=[self.key],
                output_dir=os.getcwd(),         # default in argparse
            )

            # ensure file has been created
            assert os.path.exists(test_file_path)

            # verify decrypted contents
            with open(test_file_path, 'r') as f:
                assert self.file_contents[test_file_path] == f.read()
Пример #3
0
    def test_single_relative_force(self):
        """
        Simple auto-gen key; relative paths; with force flag to overwrite source file
        """
        # use only the first test file
        test_file_path = self.file_contents.keys()[0]

        with cd(self.working_dir):
            # encrypt the test file
            encrypt(
                inputfiles=[test_file_path],
                outputfile='sesame.encrypted',
                keys=[self.key],
            )

            # sleep before decrypt to ensure file ctime is different
            time.sleep(1)

            # decrypt the test file
            decrypt(
                inputfile='sesame.encrypted',
                keys=[self.key],
                output_dir=os.getcwd(),         # default in argparse
                force=True,
            )

            # ensure file has been overwritten
            assert self.file_timestamps[test_file_path] < os.stat(test_file_path).st_ctime

            # verify decrypted contents
            with open(test_file_path, 'r') as f:
                assert self.file_contents[test_file_path] == f.read()
Пример #4
0
    def test_multiple_absolute(self):
        """
        Test a directory hierarchy with absolute paths
        """
        # convert the files list to absolute paths
        test_input_files = [
            os.path.join(self.working_dir, path) for path in self.file_contents.keys()
        ]

        with cd(self.working_dir):
            # encrypt all the test files
            encrypt(
                inputfiles=test_input_files,
                outputfile='sesame.encrypted',
                keys=[self.key],
            )

            # delete the source files
            for path in self.file_contents.keys():
                delete_path(path)

            # decrypt the test files
            decrypt(
                inputfile='sesame.encrypted',
                keys=[self.key],
                output_dir=os.getcwd(),         # default in argparse
            )

            for test_file_path in self.file_contents.keys():
                # the file will be extracted on the absolute path
                test_file_path_abs = os.path.join(self.working_dir, test_file_path)[1:]

                # verify decrypted contents at the absolute extracted path
                with open(test_file_path_abs, 'r') as f:
                    assert self.file_contents[test_file_path] == f.read()
Пример #5
0
    def test_multiple_relative(self):
        """
        Test a directory hierarchy with relative paths
        """
        with cd(self.working_dir):
            # encrypt all the test files
            encrypt(
                inputfiles=self.file_contents.keys(),
                outputfile='sesame.encrypted',
                keys=[self.key],
            )

            # delete the source files
            for path in self.file_contents.keys():
                delete_path(path)

            # decrypt the test files
            decrypt(
                inputfile='sesame.encrypted',
                keys=[self.key],
                output_dir=os.getcwd(),         # default in argparse
            )

            for test_file_path in self.file_contents.keys():
                # ensure files have been created
                assert os.path.exists(test_file_path)

                # verify decrypted contents
                with open(test_file_path, 'r') as f:
                    assert self.file_contents[test_file_path] == f.read()
Пример #6
0
    def test_single_absolute(self):
        """
        Simple auto-gen key; absolute paths
        """
        # use only the first test file
        test_file_path = self.file_contents.keys()[0]

        with cd(self.working_dir):
            # encrypt the test file
            encrypt(
                inputfiles=[os.path.join(self.working_dir, test_file_path)],
                outputfile=os.path.join(self.working_dir, 'sesame.encrypted'),
                keys=[self.key],
            )

            # delete the source file
            os.remove(test_file_path)

            # sleep before decrypt to ensure file ctime is different
            time.sleep(1)

            # decrypt the test file
            decrypt(
                inputfile=os.path.join(self.working_dir, 'sesame.encrypted'),
                keys=[self.key],
                output_dir=os.getcwd(),         # default in argparse
            )

            # the file will be extracted on the absolute path
            test_file_path_abs = os.path.join(self.working_dir, test_file_path)[1:]

            # verify decrypted contents at the absolute extracted path
            with open(test_file_path_abs, 'r') as f:
                assert self.file_contents[test_file_path] == f.read()
Пример #7
0
    def test_single_relative_output_dir(self):
        """
        Simple auto-gen key; relative paths; deletes source file; change output directory
        """
        # use only the first test file
        test_file_path = self.file_contents.keys()[0]

        with cd(self.working_dir):
            # encrypt the test file
            encrypt(
                inputfiles=[test_file_path],
                outputfile='sesame.encrypted',
                keys=[self.key],
            )

            # create a new temporary directory to extract into
            with make_secure_temp_directory() as output_dir:
                # decrypt the test file
                decrypt(
                    inputfile='sesame.encrypted',
                    keys=[self.key],
                    output_dir=output_dir
                )

                # ensure file has been created in the output_dir
                assert os.path.exists(os.path.join(output_dir, test_file_path))

                # verify decrypted contents
                with open(os.path.join(output_dir, test_file_path), 'r') as f:
                    assert self.file_contents[test_file_path] == f.read()
Пример #8
0
    def test_single_relative_overwrite_false(self):
        """
        Simple auto-gen key; relative paths; answer no to overwrite the source file
        """
        # use only the first test file
        test_file_path = self.file_contents.keys()[0]

        with cd(self.working_dir):
            # encrypt the test file
            encrypt(
                inputfiles=[test_file_path],
                outputfile='sesame.encrypted',
                keys=[self.key],
            )

            # sleep before decrypt to ensure file ctime is different
            time.sleep(1)

            # decrypt the test file; mock responds no to overwrite the existing file
            with mock.patch('__builtin__.raw_input', return_value='n'):
                # decrypt the test file
                decrypt(
                    inputfile='sesame.encrypted',
                    keys=[self.key],
                    output_dir=os.getcwd(),         # default in argparse
                )

            # ensure no file has been decrypted
            assert self.file_timestamps[test_file_path] == os.stat(test_file_path).st_ctime