test work

This commit is contained in:
John Smith
2023-06-18 18:47:39 -04:00
parent b8e5039251
commit b6e055e47d
21 changed files with 664 additions and 171 deletions

View File

@@ -9,12 +9,12 @@ from .conftest import simple_update_callback
@pytest.mark.asyncio
async def test_connect(api_connection):
async def test_connect(api_connection: veilid.VeilidAPI):
pass
@pytest.mark.asyncio
async def test_get_node_id(api_connection):
async def test_get_node_id(api_connection: veilid.VeilidAPI):
state = await api_connection.get_state()
node_ids = state.config.config.network.routing_table.node_id
@@ -35,7 +35,7 @@ async def test_fail_connect():
@pytest.mark.asyncio
async def test_version(api_connection):
async def test_version(api_connection: veilid.VeilidAPI):
v = await api_connection.veilid_version()
print(f"veilid_version: {v.__dict__}")
assert v.__dict__.keys() >= {"_major", "_minor", "_patch"}

View File

@@ -3,29 +3,26 @@
import pytest
import veilid
from veilid.api import CryptoSystem
import gc
@pytest.mark.asyncio
async def test_best_crypto_system(api_connection):
bcs: CryptoSystem = await api_connection.best_crypto_system()
assert await bcs.default_salt_length() == 16
async def test_best_crypto_system(api_connection: veilid.VeilidAPI):
cs: CryptoSystem = await api_connection.best_crypto_system()
async with cs:
assert await cs.default_salt_length() == 16
@pytest.mark.asyncio
async def test_get_crypto_system(api_connection):
async def test_get_crypto_system(api_connection: veilid.VeilidAPI):
cs: CryptoSystem = await api_connection.get_crypto_system(
veilid.CryptoKind.CRYPTO_KIND_VLD0
)
assert await cs.default_salt_length() == 16
# clean up handle early
del cs
async with cs:
assert await cs.default_salt_length() == 16
@pytest.mark.asyncio
async def test_get_crypto_system_invalid(api_connection):
async def test_get_crypto_system_invalid(api_connection: veilid.VeilidAPI):
with pytest.raises(veilid.VeilidAPIErrorInvalidArgument) as exc:
await api_connection.get_crypto_system(veilid.CryptoKind.CRYPTO_KIND_NONE)
@@ -35,15 +32,17 @@ async def test_get_crypto_system_invalid(api_connection):
@pytest.mark.asyncio
async def test_hash_and_verify_password(api_connection):
bcs = await api_connection.best_crypto_system()
nonce = await bcs.random_nonce()
salt = nonce.to_bytes()
async def test_hash_and_verify_password(api_connection: veilid.VeilidAPI):
cs = await api_connection.best_crypto_system()
async with cs:
nonce = await cs.random_nonce()
salt = nonce.to_bytes()
# Password match
phash = await bcs.hash_password(b"abc123", salt)
assert await bcs.verify_password(b"abc123", phash)
# Password match
phash = await cs.hash_password(b"abc123", salt)
assert await cs.verify_password(b"abc123", phash)
# Password mismatch
phash2 = await cs.hash_password(b"abc1234", salt)
assert not await cs.verify_password(b"abc12345", phash)
# Password mismatch
phash2 = await bcs.hash_password(b"abc1234", salt)
assert not await bcs.verify_password(b"abc12345", phash)

View File

@@ -0,0 +1,51 @@
# Routing context veilid tests
import veilid
import pytest
import asyncio
import json
from . import *
##################################################################
BOGUS_KEY = veilid.TypedKey.from_value(veilid.CryptoKind.CRYPTO_KIND_VLD0, veilid.PublicKey.from_bytes(b' '))
# @pytest.mark.asyncio
# async def test_get_dht_value_unopened(api_connection: veilid.VeilidAPI):
# rc = await api_connection.new_routing_context()
# async with rc:
# with pytest.raises(veilid.VeilidAPIError):
# out = await rc.get_dht_value(BOGUS_KEY, veilid.ValueSubkey(0), False)
# @pytest.mark.asyncio
# async def test_open_dht_record_nonexistent_no_writer(api_connection: veilid.VeilidAPI):
# rc = await api_connection.new_routing_context()
# async with rc:
# with pytest.raises(veilid.VeilidAPIError):
# out = await rc.open_dht_record(BOGUS_KEY, None)
# @pytest.mark.asyncio
# async def test_close_dht_record_nonexistent(api_connection: veilid.VeilidAPI):
# rc = await api_connection.new_routing_context()
# async with rc:
# with pytest.raises(veilid.VeilidAPIError):
# await rc.close_dht_record(BOGUS_KEY)
# @pytest.mark.asyncio
# async def test_delete_dht_record_nonexistent(api_connection: veilid.VeilidAPI):
# rc = await api_connection.new_routing_context()
# async with rc:
# with pytest.raises(veilid.VeilidAPIError):
# await rc.delete_dht_record(BOGUS_KEY)
# @pytest.mark.asyncio
# async def test_create_delete_dht_record_simple(api_connection: veilid.VeilidAPI):
# rc = await api_connection.new_routing_context()
# async with rc:
# rec = await rc.create_dht_record(veilid.CryptoKind.CRYPTO_KIND_VLD0, veilid.DHTSchema.dflt(1))
# await rc.close_dht_record(rec.key)
# await rc.delete_dht_record(rec.key)
# xxx make tests for tabledb api first
# xxx then make a test that creates a record, stores it in a table
# xxx then make another test that gets the keys from the table and closes/deletes them

View File

@@ -12,11 +12,15 @@ from .conftest import server_info
@pytest.mark.asyncio
async def test_routing_contexts(api_connection):
async def test_routing_contexts(api_connection: veilid.VeilidAPI):
rc = await api_connection.new_routing_context()
rcp = await rc.with_privacy()
rcps = await rcp.with_sequencing(veilid.Sequencing.ENSURE_ORDERED)
await rcps.with_custom_privacy(veilid.Stability.RELIABLE)
async with rc:
rcp = await rc.with_privacy(release = False)
async with rcp:
rcps = await rcp.with_sequencing(veilid.Sequencing.ENSURE_ORDERED, release = False)
async with rcps:
rcpscp = await rcps.with_custom_privacy(veilid.Stability.RELIABLE, release = False)
await rcpscp.release()
@pytest.mark.asyncio
@@ -38,24 +42,25 @@ async def test_routing_context_app_message_loopback():
# make a routing context that uses a safety route
rc = await (await api.new_routing_context()).with_privacy()
async with rc:
# make a new local private route
prl, blob = await api.new_private_route()
# make a new local private route
prl, blob = await api.new_private_route()
# import it as a remote route as well so we can send to it
prr = await api.import_remote_private_route(blob)
# import it as a remote route as well so we can send to it
prr = await api.import_remote_private_route(blob)
# send an app message to our own private route
message = b"abcd1234"
await rc.app_message(prr, message)
# send an app message to our own private route
message = b"abcd1234"
await rc.app_message(prr, message)
# we should get the same message back
update: veilid.VeilidUpdate = await asyncio.wait_for(
app_message_queue.get(), timeout=10
)
# we should get the same message back
update: veilid.VeilidUpdate = await asyncio.wait_for(
app_message_queue.get(), timeout=10
)
assert isinstance(update.detail, veilid.VeilidAppMessage)
assert update.detail.message == message
assert isinstance(update.detail, veilid.VeilidAppMessage)
assert update.detail.message == message
@pytest.mark.asyncio
@@ -74,37 +79,33 @@ async def test_routing_context_app_call_loopback():
# make a routing context that uses a safety route
rc = await (await api.new_routing_context()).with_privacy()
async with rc:
# make a new local private route
prl, blob = await api.new_private_route()
# make a new local private route
prl, blob = await api.new_private_route()
# import it as a remote route as well so we can send to it
prr = await api.import_remote_private_route(blob)
# import it as a remote route as well so we can send to it
prr = await api.import_remote_private_route(blob)
# send an app message to our own private route
request = b"abcd1234"
app_call_task = asyncio.create_task(
rc.app_call(prr, request), name="app call task"
)
# send an app message to our own private route
request = b"abcd1234"
app_call_task = asyncio.create_task(
rc.app_call(prr, request), name="app call task"
)
# we should get the same request back
update: veilid.VeilidUpdate = await asyncio.wait_for(
app_call_queue.get(), timeout=10
)
appcall = update.detail
# we should get the same request back
update: veilid.VeilidUpdate = await asyncio.wait_for(
app_call_queue.get(), timeout=10
)
appcall = update.detail
assert isinstance(appcall, veilid.VeilidAppCall)
assert appcall.message == request
assert isinstance(appcall, veilid.VeilidAppCall)
assert appcall.message == request
# now we reply to the request
reply = b"qwer5678"
await api.app_call_reply(appcall.call_id, reply)
# now we reply to the request
reply = b"qwer5678"
# TK: OperationId use to be a subclass of `int`. When I wrapped `appcall.call_id` in int(),
# this failed JSON schema validation, which defines `call_id` as a string. Maybe that was a
# typo, and OperationId is really *supposed* to be a str? Alternatively, perhaps the
# signature of `app_call_reply` is wrong and it's supposed to take a type other than
# OperationId?
await api.app_call_reply(OperationId(appcall.call_id), reply)
# now we should get the reply from the call
result = await app_call_task
assert result == reply
# now we should get the reply from the call
result = await app_call_task
assert result == reply

View File

@@ -0,0 +1,127 @@
# TableDB veilid tests
import pytest
import veilid
from veilid.api import CryptoSystem
TEST_DB = "__pytest_db"
TEST_NONEXISTENT_DB = "__pytest_nonexistent_db"
@pytest.mark.asyncio
async def test_delete_table_db_nonexistent(api_connection: veilid.VeilidAPI):
deleted = await api_connection.delete_table_db(TEST_NONEXISTENT_DB)
assert not deleted
@pytest.mark.asyncio
async def test_open_delete_table_db(api_connection: veilid.VeilidAPI):
# delete test db if it exists
await api_connection.delete_table_db(TEST_DB)
tdb = await api_connection.open_table_db(TEST_DB, 1)
async with tdb:
# delete should fail since it is still open
with pytest.raises(veilid.VeilidAPIErrorGeneric) as exc:
await api_connection.delete_table_db(TEST_DB)
# drop the db
# now delete should succeed
deleted = await api_connection.delete_table_db(TEST_DB)
assert deleted
@pytest.mark.asyncio
async def test_open_twice_table_db(api_connection: veilid.VeilidAPI):
# delete test db if it exists
await api_connection.delete_table_db(TEST_DB)
tdb = await api_connection.open_table_db(TEST_DB, 1)
tdb2 = await api_connection.open_table_db(TEST_DB, 1)
# delete should fail because open
with pytest.raises(veilid.VeilidAPIErrorGeneric) as exc:
await api_connection.delete_table_db(TEST_DB)
await tdb.release()
# delete should fail because open
with pytest.raises(veilid.VeilidAPIErrorGeneric) as exc:
await api_connection.delete_table_db(TEST_DB)
await tdb2.release()
# delete should now succeed
deleted = await api_connection.delete_table_db(TEST_DB)
assert deleted
@pytest.mark.asyncio
async def test_open_twice_table_db_store_load(api_connection: veilid.VeilidAPI):
# delete test db if it exists
await api_connection.delete_table_db(TEST_DB)
tdb = await api_connection.open_table_db(TEST_DB, 1)
async with tdb:
tdb2 = await api_connection.open_table_db(TEST_DB, 1)
async with tdb2:
# store into first db copy
await tdb.store(b"asdf", b"1234")
# load from second db copy
assert await tdb.load(b"asdf") == b"1234"
# delete should now succeed
deleted = await api_connection.delete_table_db(TEST_DB)
assert deleted
@pytest.mark.asyncio
async def test_open_twice_table_db_store_delete_load(api_connection: veilid.VeilidAPI):
# delete test db if it exists
await api_connection.delete_table_db(TEST_DB)
tdb = await api_connection.open_table_db(TEST_DB, 1)
async with tdb:
tdb2 = await api_connection.open_table_db(TEST_DB, 1)
async with tdb2:
# store into first db copy
await tdb.store(b"asdf", b"1234")
# delete from second db copy and clean up
await tdb2.delete(b"asdf")
# load from first db copy
assert await tdb.load(b"asdf") == None
# delete should now succeed
deleted = await api_connection.delete_table_db(TEST_DB)
assert deleted
@pytest.mark.asyncio
async def test_resize_table_db(api_connection: veilid.VeilidAPI):
# delete test db if it exists
await api_connection.delete_table_db(TEST_DB)
tdb = await api_connection.open_table_db(TEST_DB, 1)
async with tdb:
# reopen the db with more columns should fail if it is already open
with pytest.raises(veilid.VeilidAPIErrorGeneric) as exc:
await api_connection.open_table_db(TEST_DB, 2)
tdb2 = await api_connection.open_table_db(TEST_DB, 2)
async with tdb2:
# write something to second column
await tdb2.store(b"qwer", b"5678", col = 1)
# reopen the db with fewer columns
tdb = await api_connection.open_table_db(TEST_DB, 1)
async with tdb:
# Should fail access to second column
with pytest.raises(veilid.VeilidAPIErrorGeneric) as exc:
await tdb.load(b"qwer", col = 1)
# Should succeed with access to second column
assert await tdb2.load(b"qwer", col = 1) == b"5678"
# now delete should succeed
deleted = await api_connection.delete_table_db(TEST_DB)
assert deleted