def test_out_equals_expected(self): self.set_up() self.assertTrue( file_cmp("test_files/expected_out.dat", "test_files/output.dat")) self.assertFalse( file_cmp("test_files/bad_out.dat", "test_files/output.dat")) del_file("test_files/output.dat")
def test_rule_file(self): self.clear_folders() prepare_filetree(self.plain_folder, ''' filename_sync: 1 filename_not_sync: 2 filename_not_sync_encrypted: 3 ''') dot_folder = os.path.join(self.plain_folder, ".syncrypto") dot_folder_check = os.path.join(self.plain_folder_check, ".syncrypto") if not os.path.exists(dot_folder): os.mkdir(dot_folder) if not os.path.exists(dot_folder_check): os.mkdir(dot_folder_check) with open(os.path.join(dot_folder, "rules"), 'wb') as f: f.write(b'exclude: name match *_not_sync') with open(os.path.join(dot_folder_check, "rules"), 'wb') as f: f.write(b'exclude: name match *_encrypted') self.cli(["--password-file", self.password_file, self.encrypted_folder, self.plain_folder]) self.assertTrue(os.path.exists( os.path.join(self.encrypted_folder, "_syncrypto", "rules"))) self.cli(["--password-file", self.password_file, self.encrypted_folder, self.plain_folder_check]) self.assertTrue(file_cmp(os.path.join(dot_folder, "rules"), os.path.join(dot_folder_check, "rules"))) cmp_result = self.tree_cmp(self.plain_folder, self.plain_folder_check) self.assertEqual(sorted(cmp_result.left_only), ["filename_not_sync", "filename_not_sync_encrypted"]) self.assertEqual(len(cmp_result.right_only), 0) self.assertEqual(len(cmp_result.diff_files), 0)
def test_encrypt_file_no_out_file(self): self.clear_folders() prepare_filetree( self.plain_folder, ''' test_simple_file: hello ext.txt: with extension ''') self.cli([ "--password-file", self.password_file, "--encrypt-file", os.path.join(self.plain_folder, "test_simple_file") ]) self.assertTrue( os.path.exists( os.path.join(self.plain_folder, "test_simple_file.encrypted"))) self.cli([ "--password-file", self.password_file, "--decrypt-file", os.path.join(self.plain_folder, "test_simple_file.encrypted"), "--out-file", os.path.join(self.plain_folder, "test_simple_file_decrypted") ]) self.assertTrue( file_cmp( os.path.join(self.plain_folder, "test_simple_file_decrypted"), os.path.join(self.plain_folder, "test_simple_file"), False)) self.cli([ "--password-file", self.password_file, "--encrypt-file", os.path.join(self.plain_folder, "ext.txt") ]) self.assertTrue( os.path.exists(os.path.join(self.plain_folder, "ext.encrypted.txt"))) self.cli([ "--password-file", self.password_file, "--decrypt-file", os.path.join(self.plain_folder, "ext.encrypted.txt"), "--out-file", os.path.join(self.plain_folder, "ext.decrypted.txt") ]) self.assertTrue( file_cmp(os.path.join(self.plain_folder, "ext.decrypted.txt"), os.path.join(self.plain_folder, "ext.txt"), False))
def can_restore(self): """Checks if the config can be restored""" active = self.active_file if active is None: active = Rofi.create_default_path() if not exists(active): return False backup = self.active_backup if backup is None: backup = "%s.bak" % active if not exists(backup): return False return not file_cmp(active, backup)
def test_encrypt_file_no_out_file(self): self.clear_folders() prepare_filetree(self.plain_folder, ''' test_simple_file: hello ext.txt: with extension ''') self.cli(["--password-file", self.password_file, "--encrypt-file", os.path.join(self.plain_folder, "test_simple_file")]) self.assertTrue(os.path.exists( os.path.join(self.plain_folder, "test_simple_file.encrypted"))) self.cli([ "--password-file", self.password_file, "--decrypt-file", os.path.join(self.plain_folder, "test_simple_file.encrypted"), "--out-file", os.path.join(self.plain_folder, "test_simple_file_decrypted") ]) self.assertTrue( file_cmp( os.path.join(self.plain_folder, "test_simple_file_decrypted"), os.path.join(self.plain_folder, "test_simple_file"), False)) self.cli(["--password-file", self.password_file, "--encrypt-file", os.path.join(self.plain_folder, "ext.txt")]) self.assertTrue(os.path.exists( os.path.join(self.plain_folder, "ext.encrypted.txt"))) self.cli([ "--password-file", self.password_file, "--decrypt-file", os.path.join(self.plain_folder, "ext.encrypted.txt"), "--out-file", os.path.join(self.plain_folder, "ext.decrypted.txt") ]) self.assertTrue( file_cmp( os.path.join(self.plain_folder, "ext.decrypted.txt"), os.path.join(self.plain_folder, "ext.txt"), False))
def test_rule_file(self): self.clear_folders() prepare_filetree( self.plain_folder, ''' filename_sync: 1 filename_not_sync: 2 filename_not_sync_encrypted: 3 ''') dot_folder = os.path.join(self.plain_folder, ".syncrypto") dot_folder_check = os.path.join(self.plain_folder_check, ".syncrypto") if not os.path.exists(dot_folder): os.mkdir(dot_folder) if not os.path.exists(dot_folder_check): os.mkdir(dot_folder_check) with open(os.path.join(dot_folder, "rules"), 'wb') as f: f.write(b'exclude: name match *_not_sync') with open(os.path.join(dot_folder_check, "rules"), 'wb') as f: f.write(b'exclude: name match *_encrypted') self.cli([ "--password-file", self.password_file, self.encrypted_folder, self.plain_folder ]) self.assertTrue( os.path.exists( os.path.join(self.encrypted_folder, "_syncrypto", "rules"))) self.cli([ "--password-file", self.password_file, self.encrypted_folder, self.plain_folder_check ]) self.assertTrue( file_cmp(os.path.join(dot_folder, "rules"), os.path.join(dot_folder_check, "rules"))) cmp_result = self.tree_cmp(self.plain_folder, self.plain_folder_check) self.assertEqual(sorted(cmp_result.left_only), ["filename_not_sync", "filename_not_sync_encrypted"]) self.assertEqual(len(cmp_result.right_only), 0) self.assertEqual(len(cmp_result.diff_files), 0)
def tree_cmp(left, right, pathname="", ignores=None): is_dir_left = left is not None and os.path.isdir(left) is_dir_right = right is not None and os.path.isdir(right) exists_left = left is not None and os.path.exists(left) exists_right = right is not None and os.path.exists(right) cmp_result = TreeCmpResult() if is_dir_left and is_dir_right: sub_files = list(set(os.listdir(left) + os.listdir(right))) for sub_file in sub_files: if sub_file == "." or sub_file == "..": continue ignore = False if ignores is not None: for pattern in ignores: if fnmatch(sub_file, pattern): ignore = True break if ignore: continue if pathname == "": sub_pathname = sub_file else: sub_pathname = pathname + "/" + sub_file sub_cmp = \ tree_cmp(os.path.join(left, sub_file), os.path.join(right, sub_file), sub_pathname) cmp_result.left_only += sub_cmp.left_only cmp_result.right_only += sub_cmp.right_only cmp_result.diff_files += sub_cmp.diff_files elif is_dir_left: if not exists_right: cmp_result.left_only.append(pathname) else: cmp_result.diff_files.append(pathname) sub_files = os.listdir(left) for sub_file in sub_files: if sub_file == "." or sub_file == "..": continue ignore = False if ignores is not None: for pattern in ignores: if fnmatch(sub_file, pattern): ignore = True break if ignore: continue if pathname == "": sub_pathname = sub_file else: sub_pathname = pathname + "/" + sub_file sub_cmp = \ tree_cmp(os.path.join(left, sub_file), None, sub_pathname) cmp_result.left_only += sub_cmp.left_only elif is_dir_right: if not exists_left: cmp_result.right_only.append(pathname) else: cmp_result.diff_files.append(pathname) sub_files = os.listdir(right) for sub_file in sub_files: if sub_file == "." or sub_file == "..": continue if pathname == "": sub_pathname = sub_file else: sub_pathname = pathname + "/" + sub_file sub_cmp = \ tree_cmp(None, os.path.join(right, sub_file), sub_pathname) cmp_result.right_only += sub_cmp.right_only elif exists_left and exists_right: if not file_cmp(left, right, False): cmp_result.diff_files.append(pathname) elif exists_left: cmp_result.left_only.append(pathname) elif exists_right: cmp_result.right_only.append(pathname) return cmp_result
def tree_cmp(left, right, pathname="", ignores=None): is_dir_left = left is not None and os.path.isdir(left) is_dir_right = right is not None and os.path.isdir(right) exists_left = left is not None and os.path.exists(left) exists_right = right is not None and os.path.exists(right) cmp_result = TreeCmpResult() if is_dir_left and is_dir_right: sub_files = list(set(os.listdir(left)+os.listdir(right))) for sub_file in sub_files: if sub_file == "." or sub_file == "..": continue ignore = False if ignores is not None: for pattern in ignores: if fnmatch(sub_file, pattern): ignore = True break if ignore: continue if pathname == "": sub_pathname = sub_file else: sub_pathname = pathname+"/"+sub_file sub_cmp = \ tree_cmp(os.path.join(left, sub_file), os.path.join(right, sub_file), sub_pathname) cmp_result.left_only += sub_cmp.left_only cmp_result.right_only += sub_cmp.right_only cmp_result.diff_files += sub_cmp.diff_files elif is_dir_left: if not exists_right: cmp_result.left_only.append(pathname) else: cmp_result.diff_files.append(pathname) sub_files = os.listdir(left) for sub_file in sub_files: if sub_file == "." or sub_file == "..": continue ignore = False if ignores is not None: for pattern in ignores: if fnmatch(sub_file, pattern): ignore = True break if ignore: continue if pathname == "": sub_pathname = sub_file else: sub_pathname = pathname+"/"+sub_file sub_cmp = \ tree_cmp(os.path.join(left, sub_file), None, sub_pathname) cmp_result.left_only += sub_cmp.left_only elif is_dir_right: if not exists_left: cmp_result.right_only.append(pathname) else: cmp_result.diff_files.append(pathname) sub_files = os.listdir(right) for sub_file in sub_files: if sub_file == "." or sub_file == "..": continue if pathname == "": sub_pathname = sub_file else: sub_pathname = pathname+"/"+sub_file sub_cmp = \ tree_cmp(None, os.path.join(right, sub_file), sub_pathname) cmp_result.right_only += sub_cmp.right_only elif exists_left and exists_right: if not file_cmp(left, right, False): cmp_result.diff_files.append(pathname) elif exists_left: cmp_result.left_only.append(pathname) elif exists_right: cmp_result.right_only.append(pathname) return cmp_result