summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJohn Turner <jturner.usa@gmail.com>2025-09-23 21:11:01 -0400
committerJohn Turner <jturner.usa@gmail.com>2025-09-23 21:48:01 -0400
commit83040f948f6e8264fb427b5d9cf338f46a906fe1 (patch)
treed0efab55a60a10c638df2e5b4194a060c9a85c38 /tests
parent2aa14b931f1a4fa7f465537b5310217b84615203 (diff)
downloadpypaste-83040f948f6e8264fb427b5d9cf338f46a906fe1.tar.gz
change keys to 78 bit random tokens and humanize the result
Instead of picking random words via a random choice function, we generate a 13 * key_length bit random int as the key. We humanize the key by splitting it up into 13 bit segments and using the 13 bits as an index into the word list. This allows us to store the keys in binary form which is faster and uses less space.
Diffstat (limited to 'tests')
-rwxr-xr-xtests/test_server.py23
-rwxr-xr-xtests/test_sqlite_storage.py26
2 files changed, 29 insertions, 20 deletions
diff --git a/tests/test_server.py b/tests/test_server.py
index 0ccfc5b..2b1ed2e 100755
--- a/tests/test_server.py
+++ b/tests/test_server.py
@@ -7,6 +7,7 @@ import string
import random
import aiohttp
from pathlib import Path
+from asyncio import Queue
def truncate(file: Path) -> None:
@@ -20,10 +21,21 @@ def generate_name() -> str:
return "".join(random.choice(characters) for _ in range(10))
+async def tee_pipe(proc: asyncio.subprocess.Process, queue: Queue[str]) -> None:
+ assert proc.stdout is not None
+
+ while (line := await proc.stdout.readline()) is not None:
+ sys.stderr.write(line.decode())
+ await queue.put(line.decode())
+
+
async def main() -> int:
- with tempfile.TemporaryDirectory() as tmpdir:
+ with tempfile.TemporaryDirectory(delete=False) as tmpdir:
+ print(tmpdir, file=sys.stderr)
+
socket = Path(tmpdir) / (generate_name() + ".sock")
database = Path(tmpdir) / generate_name()
+ queue: Queue[str] = Queue()
truncate(database)
@@ -38,7 +50,7 @@ async def main() -> int:
"--content-length-max-bytes",
"200000",
"--key-length",
- "3",
+ "6",
"--database",
database,
"--storage-max-bytes",
@@ -48,12 +60,9 @@ async def main() -> int:
stderr=asyncio.subprocess.STDOUT,
)
- assert proc.stdout is not None
-
- line = await proc.stdout.readline()
+ asyncio.create_task(tee_pipe(proc, queue))
- if b"starting" not in line:
- print(line, file=sys.stderr)
+ if "starting" not in await queue.get():
return 1
connection = aiohttp.UnixConnector(path=str(socket))
diff --git a/tests/test_sqlite_storage.py b/tests/test_sqlite_storage.py
index 384158c..6a93561 100755
--- a/tests/test_sqlite_storage.py
+++ b/tests/test_sqlite_storage.py
@@ -6,7 +6,7 @@ import tempfile
import aiosqlite
import string
import random
-from pypaste.server import Paste
+from pypaste.server import Paste, Key, keygen
from pypaste.server.sqlite import Sqlite
from datetime import datetime
from pathlib import Path
@@ -24,11 +24,11 @@ def generate_key() -> str:
async def test_exists_but_not_in_our_table(storage: Sqlite) -> None:
- key = generate_key()
+ key = keygen(6)
await storage.connection.execute(
- "insert into pastes values(?, ?, ?, ?)",
- (key, datetime.now().isoformat(), None, bytes()),
+ "insert into pastes values(?, ?, ?, ?, ?)",
+ (key.data, key.length, datetime.now().isoformat(), None, bytes()),
)
assert not await storage.exists(key)
@@ -36,20 +36,20 @@ async def test_exists_but_not_in_our_table(storage: Sqlite) -> None:
async def test_exists(storage: Sqlite) -> None:
dt = datetime.now()
- key = generate_key()
+ key = keygen(6)
- await storage.insert(Paste(key, dt, "test", "hello world"))
+ await storage.insert(Paste(dt, "test", "hello world"), key)
assert await storage.exists(key)
- assert not await storage.exists(generate_key())
+ assert not await storage.exists(keygen(6))
async def test_delete(storage: Sqlite) -> None:
dt = datetime.now()
- key = generate_key()
+ key = keygen(6)
- await storage.insert(Paste(key, dt, "test", "hello world"))
+ await storage.insert(Paste(dt, "test", "hello world"), key)
assert await storage.exists(key)
@@ -60,14 +60,13 @@ async def test_delete(storage: Sqlite) -> None:
async def test_insert_retrieve(storage: Sqlite) -> None:
dt = datetime.now()
- key = generate_key()
+ key = keygen(6)
- await storage.insert(Paste(key, dt, "test", "hello world"))
+ await storage.insert(Paste(dt, "test", "hello world"), key)
paste = await storage.retrieve(key)
assert paste is not None
- assert paste.key == key
assert paste.dt == dt
assert paste.syntax == "test"
assert paste.text == "hello world"
@@ -81,7 +80,8 @@ async def main() -> int:
await connection.execute(
(
"create table pastes("
- "key text,"
+ "key blob,"
+ "key_length int,"
"datetime text,"
"size int,"
"syntax text"