def test_utils_01_load_files_skip_dir(self): ''' UTL-01 | load_files: skip directory if -r not specified ''' sys.argv = ['shayfara', '-e', '--no-recursive', 'test/dirtest'] args = opts.getopts() expected = [] ret = utils.load_files(args) return self.assertEqual(ret, expected)
def test_utils_02_load_files_skip_dir(self): ''' UTL-02 | load_files: skip directory if not exists ''' sys.argv = ['shayfara', '-e', 'foo/'] args = opts.getopts() expected = [] ret = utils.load_files(args) return self.assertEqual(ret, expected)
def test_utils_10_get_password_command_line(self): ''' UTL-10 | get_password: from command line ''' sys.argv = ['shayfara', '-e', 'test/dirtest/file0', '-P', 'mypassword'] args = opts.getopts() expected = 'mypassword' ret = utils.get_password(args) return self.assertEqual(ret, expected)
def test_utils_13_get_password_user_prompt(self, input): ''' UTL-13 | get_password: from user prompt ''' sys.argv = ['shayfara', '-e', 'test/dirtest/file0'] args = opts.getopts() expected = 'mypassword' ret = utils.get_password(args) return self.assertEqual(ret, expected)
def test_utils_08_get_output_file_dec(self): ''' UTL-08 | get_output_file: default decrypt extension: .dec ''' sys.argv = ['shayfara', '-d', 'test/dirtest/file0'] args = opts.getopts() ifile = args.FILES[0] expected = '%s.dec' % ifile ret = utils.get_output_file(ifile, args) return self.assertEqual(ret, expected)
def test_utils_06_check_file_access_access(self): ''' UTL-06 | check_file_access: file access ok, return 0 ''' sys.argv = ['shayfara', '-e', 'test/dirtest/file0'] args = opts.getopts() ifile = args.FILES[0] expected = 0 ret = utils.check_file_access(ifile) return self.assertEqual(ret, expected)
def test_utils_05_check_file_access_no_access(self): ''' UTL-05 | check_file_access: file with no access, return 1 ''' sys.argv = ['shayfara', '-e', '/etc/motd'] args = opts.getopts() ifile = args.FILES[0] expected = 1 ret = utils.check_file_access(ifile) return self.assertEqual(ret, expected)
def test_utils_04_load_files_access_write_error(self): ''' UTL-04 | load_files: exit if access error ''' sys.argv = ['shayfara', '-e', '/etc/motd'] args = opts.getopts() expected = 1 with self.assertRaises(SystemExit) as cm: utils.load_files(args) return self.assertEqual(cm.exception.code, expected)
def test_utils_14_get_password_user_prompt(self, input): ''' UTL-14 | get_password: from user prompt, not matching''' sys.argv = ['shayfara', '-e', 'test/dirtest/file0'] args = opts.getopts() expected = 1 with self.assertRaises(SystemExit) as cm: utils.get_password(args) return self.assertEqual(cm.exception.code, expected)
def test_utils_09_get_output_file(self): ''' UTL-09 | get_output_file: user defined extension: .foo ''' sys.argv = ['shayfara', '-e', 'test/dirtest/file0', '-E', 'foo'] args = opts.getopts() ifile = args.FILES[0] expected = '%s.foo' % ifile ret = utils.get_output_file(ifile, args) return self.assertEqual(ret, expected)
def test_plugins_local_12_exists(self): ''' LOC-12 | local: exists - file does not exist ''' filename = 'test/dirtest/foo' sys.argv = ['shayfara', '-e', filename] args = opts.getopts() # test if file already exists ret = self.plugin.exists(filename, args) return self.assertEqual(ret, filename)
def test_msg_01_info(self): ''' MSG-01 | info message ''' sys.argv = ['shayfara', '-e', 'test/dirtest/file0'] args = opts.getopts() message = 'test message' expected = '%s\n' % message ret = msg.info(message, args) return self.assertEqual(ret, expected)
def test_msg_02_infov_verbose(self): ''' MSG-02 | infov message in verbose mode ''' sys.argv = ['shayfara', '-e', 'test/dirtest/file0', '-v'] args = opts.getopts() message = 'test message' expected = '%s\n' % message ret = msg.infov(message, args) return self.assertEqual(ret, expected)
def test_msg_05_errn(self): ''' MSG-05 | error message ''' sys.argv = ['shayfara', '-e', 'test/dirtest/file0'] args = opts.getopts() message = 'test message' expected = 'ERROR: %s\n' % message ret = msg.errn(message, args) return self.assertEqual(ret, expected)
def test_plugins_local_10_exists(self): ''' LOC-10 | local: exists - file exists, return None ''' filename = 'test/dirtest/file0' sys.argv = ['shayfara', '-e', filename] args = opts.getopts() # test if file already exists ret = self.plugin.exists(filename, args) return self.assertEqual(ret, None)
def test_msg_03_infov_no_verbose(self): ''' MSG-03 | infov message no verbose (should be silent) ''' sys.argv = ['shayfara', '-e', 'test/dirtest/file0'] args = opts.getopts() message = 'test message' expected = '' ret = msg.infov(message, args) return self.assertEqual(ret, expected)
def test_plugins_local_11_exists_force(self): ''' LOC-11 | local: exists - file exists, force replace ''' filename = 'test/dirtest/file0' sys.argv = ['shayfara', '-e', '-f', filename] args = opts.getopts() # test if file already exists ret = self.plugin.exists(filename, args) return self.assertEqual(ret, filename)
def test_msg_06_info(self): ''' MSG-06 | warning message ''' sys.argv = ['shayfara', '-e', 'test/dirtest/file0'] args = opts.getopts() message = 'test message' expected = 'WARNING: %s\n' % message ret = msg.warn(message, args) return self.assertEqual(ret, expected)
def test_msg_04_errx(self): ''' MSG-04 | errorx exit message ''' sys.argv = ['shayfara', '-e', 'test/dirtest/file0'] args = opts.getopts() message = 'test message' expected = 1 with self.assertRaises(SystemExit) as cm: msg.errx(message, args) return self.assertEqual(cm.exception.code, expected)
def test_utils_11_get_password_file_not_exists(self): ''' UTL-11 | get_password: from password file, not exists, exit ''' sys.argv = ['shayfara', '-e', 'test/dirtest/file0', '-p', 'tests/passwordfoo'] args = opts.getopts() expected = 1 with self.assertRaises(SystemExit) as cm: utils.get_password(args) return self.assertEqual(cm.exception.code, expected)
def test_utils_15_get_output_file_dec(self): ''' UTL-15 | get_output_file: directory and in-place should have no extension ''' sys.argv = ['shayfara', '-d', 'test/dirtest/file0', '-D', '/tmp/foo/', '--in-place'] args = opts.getopts() ifile = args.FILES[0] expected = ifile ret = utils.get_output_file(ifile, args) return self.assertEqual(ret, expected)
def test_utils_16_updatedir(self): ''' UTL-16 | local: updatedir - success ''' directory = '/tmp/foo' sys.argv = ['shayfara', '-e', '-D', directory, 'test/dirtest/'] args = opts.getopts() # get directory file list files = utils.load_files(args) files.sort() expected = '%s/dir1/file1' % directory # update directory ret = utils.updatedir(files[0], args.dest_dir, args.FILES[0]) return self.assertEqual(ret, expected)
def test_ciphers_01_scrypt_encrypt(self): ''' CIP-01 | scrypt: encrypt file ''' with tempfile.NamedTemporaryFile(delete=False) as f: f.write(b'Some data') filename = f.name sys.argv = ['shayfara', '-e', '-p', 'test/passwordfile', filename] args = opts.getopts() expected = 'Some data' # get password password = utils.get_password(args) # encrypt data read from file ret = scrypt.encrypt_file(password, filename) os.remove(filename) return self.assertNotEqual(ret, expected)
def test_ciphers_02_scrypt_decrypt_success(self): ''' CIP-02 | scrypt: encrypt then decrypt file ''' with tempfile.NamedTemporaryFile(delete=False) as f: f.write(b'Some data') inputfile = f.name sys.argv = ['shayfara', '-e', '-p', 'test/passwordfile', inputfile] args = opts.getopts() expected = b'Some data' # get password password = utils.get_password(args) # encrypt data read from file ret = scrypt.encrypt_file(password, inputfile) with tempfile.NamedTemporaryFile(delete=False) as f: f.write(ret) encfile = f.name # decrypt data from above encrypted file ret = scrypt.decrypt_file(password, encfile) os.remove(inputfile) os.remove(encfile) return self.assertEqual(ret, expected)
def test_opts_02_default_output_plugin(self): ''' OPT-02 | default output plug-in is local ''' sys.argv = ['shayfara', '-e', 'test/dirtest/file0', '-v'] ret = vars(opts.getopts())['plugin'] return self.assertIn('local', ret)
def test_opts_01_default_cipher(self): ''' OPT-01 | default cipher is simplecrypt ''' sys.argv = ['shayfara', '-e', 'test/dirtest/file0', '-v'] ret = vars(opts.getopts())['cipher'] return self.assertIn('simplecrypt', ret)