def test_data_provider_int(self): # Full integrity Test including flask self.dp = data_provider.DataProvider(self.provider) self.dp.set_password(self.password) str_backend = StorageServer(test_dir) with self.str_app.app_context(): for r in self.sr: # check that bloom filter is empty b = str_backend.bloom self.assertNotIn(to_base64(r.get_long_hash()), b) # Check that DB empty res = str_backend.batch_get_records( [to_base64(r.get_long_hash()) for r in self.sr], "client") # Decrypt result = [ Record.from_ciphertext(json.loads(r), self.enc_keys[0]) for h, r in res ] self.assertEqual([], result) s = Session(True) with patch("requests.get", s.get), \ patch("requests.post", s.post), \ patch.object(self.dp, "_receive_ots", Mock(return_value=self.enc_keys_int[:len(self.sr)])): self.dp.store_records(self.sr) str_backend = StorageServer(test_dir) with self.str_app.app_context(): for r in self.sr: # check that records are in bloom filter b = str_backend.bloom self.assertIn(to_base64(r.get_long_hash()), b) # Check records in db res = str_backend.batch_get_records( [to_base64(r.get_long_hash()) for r in self.sr], "client") # Decrypt result = [ Record.from_ciphertext(json.loads(r), self.enc_keys[0]) for h, r in res ] for m in self.sr: self.assertIn(m, result) for r in result: self.assertIn(r, self.sr)
def batch_get_records(self, candidates: List[Record]) -> List[Record]: """ Retrieve the records for all hashes in the list :param candidates: Record objects for all candidates (hash_set) :return: List of retrieved records (decyrpted) """ log.info("4.1 Retrieve encryption keys.") start = time.monotonic() ot_indices = [] # No duplicates for r in candidates: if r.get_ot_index() not in ot_indices: ot_indices.append(r.get_ot_index()) enc_keys = self._get_enc_keys(ot_indices) # Create mapping enc_keys = dict(zip(ot_indices, enc_keys)) self.eval['key_retrieve_time'] = time.monotonic() log.info( f"4.1 - Retrieve keys took: {print_time(time.monotonic() - start)}" ) log.info("4.2 Retrieve encrypted records.") start = time.monotonic() hash_list = [to_base64(r.get_long_hash()) for r in candidates] records = self._batch_get_encrpyted_records(hash_list) if not records: self.eval['record_retrieve_time'] = time.monotonic() self.eval['decryption_time'] = time.monotonic() return [] res_list = [] self.eval['record_retrieve_time'] = time.monotonic() log.info( f"4.2 - Retrieve records: {print_time(time.monotonic() - start)}") log.info("4.3 Decrypting.") start = time.monotonic() for h, c in records: c = json.loads(c) key = enc_keys[hash_to_index(from_base64(h), config.OT_INDEX_LEN)] log.debug(f"Using key {key} for record {h}.") res_list.append(Record.from_ciphertext(c, key)) self.eval['decryption_time'] = time.monotonic() log.info( f"4.3 - Decryption took: {print_time(time.monotonic() - start)}") return res_list