def test003_build_sha256_path_raises_IrmaValueError(self): sha = str() with self.assertRaises(IrmaValueError) as context: module.build_sha256_path(sha) self.assertEqual(str(context.exception), "too much prefix for file storage") self.assertTrue(module.config.get_samples_storage_path.called) self.assertFalse(module.os.path.join.called) self.assertFalse(module.os.path.exists.called) self.assertFalse(module.os.makedirs.called)
def test004_build_sha256_path_raises_IrmaFileSystemError(self): sha = "1234567890" module.os.path.isdir.return_value = False with self.assertRaises(IrmaFileSystemError) as context: module.build_sha256_path(sha) self.assertEquals(str(context.exception), "storage path is not a directory") self.assertTrue(module.config.get_samples_storage_path.called) self.assertEqual(module.os.path.join.call_count, 3) self.assertEqual(module.os.path.exists.call_count, 1) self.assertFalse(module.os.makedirs.called)
def _new_file(fileobj, session): sha256 = sha256sum(fileobj) # split files between subdirs path = build_sha256_path(sha256) try: # The file exists log.debug("try opening file with sha256: %s", sha256) file = File.load_from_sha256(sha256, session) if file.path is None: log.debug("file sample missing writing it") save_to_file(fileobj, path) file.path = path except IrmaDatabaseResultNotFound: # It doesn't time = compat.timestamp() sha1 = sha1sum(fileobj) md5 = md5sum(fileobj) # determine file mimetype magic = Magic() # magic only deal with buffer # feed it with a 4MB buffer mimetype = magic.from_buffer(fileobj.read(2**22)) size = save_to_file(fileobj, path) log.debug( "not present, saving, sha256 %s sha1 %s" "md5 %s size %s mimetype: %s", sha256, sha1, md5, size, mimetype) file = File(sha256, sha1, md5, size, mimetype, path, time, time) session.add(file) return file
def test001_build_sha256_path_ok(self): sha = "1234567890" result = module.build_sha256_path(sha) self.assertTrue(module.config.get_samples_storage_path.called) self.assertEqual(module.os.path.join.call_count, 4) self.assertEqual(module.os.path.exists.call_count, 1) self.assertEqual(module.os.path.join.call_args, ((module.os.path.join(), sha), )) self.assertFalse(module.os.makedirs.called) self.assertEqual(result, module.os.path.join())
def test001_build_sha256_path_ok(self): sha = "1234567890" result = module.build_sha256_path(sha) self.assertTrue(module.config.get_samples_storage_path.called) self.assertEqual(module.os.path.join.call_count, 4) self.assertEqual(module.os.path.exists.call_count, 1) self.assertEqual(module.os.path.join.call_args, ((module.os.path.join(), sha),)) self.assertFalse(module.os.makedirs.called) self.assertEqual(result, module.os.path.join())