Skip to content

Commit 0778daf

Browse files
committed
check if text returned is correct
1 parent 02dc404 commit 0778daf

File tree

3 files changed

+60
-12
lines changed

3 files changed

+60
-12
lines changed

checker/src/checker.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
MumbleException,
2626
)
2727
from utils import (Connection, encode_replay, decode_replay,
28-
gen_random_str, is_valid_uuid4)
28+
gen_random_str, is_valid_uuid4, get_text)
2929
from enochecker3.utils import assert_equals, assert_in
3030
from urllib.parse import urlparse
3131

@@ -63,16 +63,30 @@ async def putflag_replay(
6363

6464
con.log_debug('Get Text')
6565
era = random.choice(['Ancient', 'Medieval', 'Victorian', 'Future'])
66-
# TODO: check if text / source / book / paragraph are correct
6766
try:
68-
text = await con.client.get(f'/text?era={era}')
69-
text.raise_for_status()
70-
source = text.json().get('source')
71-
book = text.json().get('book')
72-
paragraph = text.json().get('paragraph')
67+
rs = await con.client.get(f'/text?era={era}')
68+
rs.raise_for_status()
69+
text = rs.json().get('text')
70+
source = rs.json().get('source')
71+
book = int(rs.json().get('book'))
72+
paragraph = int(rs.json().get('paragraph'))
73+
74+
loop = asyncio.get_running_loop()
75+
text_db = await loop.run_in_executor(
76+
None, lambda: get_text(era, source, book, paragraph)
77+
)
78+
7379
except Exception as e:
7480
con.log_error('Failed to get text', e)
7581

82+
if text_db is None:
83+
con.log_debug('Text from DB returned None')
84+
raise MumbleException("Failed to get text")
85+
if text_db != text:
86+
con.log_debug(f"Text from DB doesn't match. DB: {
87+
text_db} | Actual: {text}")
88+
raise MumbleException("Failed to get text")
89+
7690
time = random.choice([15, 30, 60, 120])
7791
end_time = datetime.now(timezone.utc)
7892
start_time = end_time - timedelta(seconds=time)
@@ -852,17 +866,29 @@ async def putnoise_profile(task: PutnoiseCheckerTaskMessage, db: ChainDB, logger
852866

853867
con.log_debug('Get Text')
854868
era = random.choice(['Ancient', 'Medieval', 'Victorian', 'Future'])
855-
# TODO: check if text / source / book / paragraph are correct
856869
try:
857870
res1 = await con.client.get(f'/text?era={era}')
858871
res1.raise_for_status()
859872
source = res1.json().get('source')
860-
book = res1.json().get('book')
861-
paragraph = res1.json().get('paragraph')
873+
book = int(res1.json().get('book'))
874+
paragraph = int(res1.json().get('paragraph'))
862875
text = res1.json().get('text')
876+
877+
loop = asyncio.get_running_loop()
878+
text_db = await loop.run_in_executor(
879+
None, lambda: get_text(era, source, book, paragraph)
880+
)
863881
except Exception as e:
864882
con.log_error('Failed to get text', e)
865883

884+
if text_db is None:
885+
con.log_debug('Text from DB returned None')
886+
raise MumbleException("Failed to get text")
887+
if text_db != text:
888+
con.log_debug(f"Text from DB doesn't match. DB: {
889+
text_db} | Actual: {text}")
890+
raise MumbleException("Failed to get text")
891+
866892
mode = random.choice(["time", "word"])
867893
amount_words = random.choice([15, 25, 50, 100])
868894
if mode == "time":
@@ -1332,7 +1358,6 @@ async def receive_messages():
13321358
"""
13331359

13341360

1335-
# TODO: widen the scope of this?
13361361
@checker.havoc(0)
13371362
async def havoc_text(task: HavocCheckerTaskMessage, logger: LoggerAdapter, client: AsyncClient):
13381363
logger.info(f"Havoc 0 (text) {client.base_url}")

checker/src/texts.db

1.9 MB
Binary file not shown.

checker/src/utils.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
from logging import LoggerAdapter
22
from enochecker3 import MumbleException, OfflineException
3-
from httpx import AsyncClient, ConnectTimeout, NetworkError, PoolTimeout, HTTPStatusError, RequestError, ReadTimeout
3+
from httpx import AsyncClient, ConnectTimeout, NetworkError, PoolTimeout, HTTPStatusError, ReadTimeout
44
from bs4 import BeautifulSoup
55
import string
66
import uuid
77
import secrets
88
import random
99
import base64
10+
import sqlite3
11+
from typing import Optional
1012

1113

1214
def gen_random_str(length):
@@ -77,6 +79,27 @@ def is_valid_uuid4(u: str) -> bool:
7779
return str(val) == u
7880

7981

82+
def get_text(
83+
era: str,
84+
source: str,
85+
book: int,
86+
paragraph: int
87+
) -> Optional[str]:
88+
conn = sqlite3.connect('texts.db')
89+
cursor = conn.cursor()
90+
query = """
91+
SELECT text FROM texts
92+
WHERE era = ?
93+
AND source = ?
94+
AND book = ?
95+
AND paragraph = ?
96+
"""
97+
cursor.execute(query, (era, source, book, paragraph))
98+
row = cursor.fetchone()
99+
conn.close()
100+
return row[0] if row else None
101+
102+
80103
class Connection:
81104
def __init__(
82105
self,

0 commit comments

Comments
 (0)