2023-06-19 21:35:49 -05:00
|
|
|
# Routing context veilid tests
|
2023-06-18 17:47:39 -05:00
|
|
|
|
2023-06-19 21:35:49 -05:00
|
|
|
import veilid
|
|
|
|
import pytest
|
|
|
|
import asyncio
|
|
|
|
import json
|
|
|
|
from . import *
|
2023-06-18 17:47:39 -05:00
|
|
|
|
2023-06-19 21:35:49 -05:00
|
|
|
##################################################################
|
2023-07-30 15:45:20 -05:00
|
|
|
BOGUS_KEY = veilid.TypedKey.from_value(
|
|
|
|
veilid.CryptoKind.CRYPTO_KIND_VLD0, veilid.PublicKey.from_bytes(b' '))
|
|
|
|
|
2023-06-18 17:47:39 -05:00
|
|
|
|
2023-06-19 21:35:49 -05:00
|
|
|
@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)
|
2023-06-18 17:47:39 -05:00
|
|
|
|
|
|
|
|
2023-06-19 21:35:49 -05:00
|
|
|
@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)
|
2023-06-18 17:47:39 -05:00
|
|
|
|
2023-07-30 15:45:20 -05:00
|
|
|
|
2023-06-19 21:35:49 -05:00
|
|
|
@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)
|
2023-06-18 17:47:39 -05:00
|
|
|
|
2023-07-30 15:45:20 -05:00
|
|
|
|
2023-06-19 21:35:49 -05:00
|
|
|
@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)
|
2023-07-30 15:45:20 -05:00
|
|
|
|
|
|
|
|
2023-06-19 21:35:49 -05:00
|
|
|
@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:
|
2023-07-22 12:06:46 -05:00
|
|
|
rec = await rc.create_dht_record(
|
|
|
|
veilid.DHTSchema.dflt(1), veilid.CryptoKind.CRYPTO_KIND_VLD0
|
|
|
|
)
|
2023-06-19 21:35:49 -05:00
|
|
|
await rc.close_dht_record(rec.key)
|
|
|
|
await rc.delete_dht_record(rec.key)
|
2023-06-18 17:47:39 -05:00
|
|
|
|
2023-07-30 15:45:20 -05:00
|
|
|
|
2023-06-19 21:35:49 -05:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_get_dht_value_nonexistent(api_connection: veilid.VeilidAPI):
|
|
|
|
rc = await api_connection.new_routing_context()
|
|
|
|
async with rc:
|
2023-07-08 21:50:44 -05:00
|
|
|
rec = await rc.create_dht_record(veilid.DHTSchema.dflt(1))
|
2023-06-19 21:35:49 -05:00
|
|
|
assert await rc.get_dht_value(rec.key, 0, False) == None
|
|
|
|
await rc.close_dht_record(rec.key)
|
|
|
|
await rc.delete_dht_record(rec.key)
|
2023-06-18 19:57:51 -05:00
|
|
|
|
2023-07-30 15:45:20 -05:00
|
|
|
|
2023-06-19 21:35:49 -05:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_set_get_dht_value(api_connection: veilid.VeilidAPI):
|
|
|
|
rc = await api_connection.new_routing_context()
|
|
|
|
async with rc:
|
2023-07-08 21:50:44 -05:00
|
|
|
rec = await rc.create_dht_record(veilid.DHTSchema.dflt(2))
|
2023-07-30 15:45:20 -05:00
|
|
|
|
2023-06-19 21:35:49 -05:00
|
|
|
vd = await rc.set_dht_value(rec.key, 0, b"BLAH BLAH BLAH")
|
2023-07-30 15:45:20 -05:00
|
|
|
assert vd == None
|
|
|
|
|
2023-06-19 21:35:49 -05:00
|
|
|
vd2 = await rc.get_dht_value(rec.key, 0, False)
|
|
|
|
assert vd2 != None
|
2023-07-30 15:45:20 -05:00
|
|
|
|
2023-06-26 20:29:02 -05:00
|
|
|
vd3 = await rc.get_dht_value(rec.key, 0, True)
|
|
|
|
assert vd3 != None
|
|
|
|
|
2023-06-29 13:52:53 -05:00
|
|
|
vd4 = await rc.get_dht_value(rec.key, 1, False)
|
|
|
|
assert vd4 == None
|
|
|
|
|
2023-06-20 22:46:39 -05:00
|
|
|
print("vd2: {}", vd2.__dict__)
|
2023-06-26 20:29:02 -05:00
|
|
|
print("vd3: {}", vd3.__dict__)
|
2023-06-20 22:46:39 -05:00
|
|
|
|
2023-06-26 20:29:02 -05:00
|
|
|
assert vd2 == vd3
|
2023-06-18 19:57:51 -05:00
|
|
|
|
2023-06-19 21:35:49 -05:00
|
|
|
await rc.close_dht_record(rec.key)
|
|
|
|
await rc.delete_dht_record(rec.key)
|
2023-06-18 19:57:51 -05:00
|
|
|
|
2023-07-30 15:45:20 -05:00
|
|
|
|
2023-06-29 13:52:53 -05:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_open_writer_dht_value(api_connection: veilid.VeilidAPI):
|
|
|
|
rc = await api_connection.new_routing_context()
|
|
|
|
async with rc:
|
2023-07-08 21:50:44 -05:00
|
|
|
rec = await rc.create_dht_record(veilid.DHTSchema.dflt(2))
|
2023-06-29 13:52:53 -05:00
|
|
|
key = rec.key
|
|
|
|
owner = rec.owner
|
|
|
|
secret = rec.owner_secret
|
2023-06-29 21:18:45 -05:00
|
|
|
print(f"key:{key}")
|
2023-06-29 13:52:53 -05:00
|
|
|
|
2023-07-08 21:50:44 -05:00
|
|
|
cs = await api_connection.get_crypto_system(rec.key.kind())
|
2023-06-29 13:52:53 -05:00
|
|
|
async with cs:
|
|
|
|
assert await cs.validate_key_pair(owner, secret)
|
|
|
|
other_keypair = await cs.generate_key_pair()
|
|
|
|
|
|
|
|
va = b"Qwertyuiop Asdfghjkl Zxcvbnm"
|
|
|
|
vb = b"1234567890"
|
|
|
|
vc = b"!@#$%^&*()"
|
|
|
|
|
2023-06-29 21:18:45 -05:00
|
|
|
# Test subkey writes
|
2023-06-29 13:52:53 -05:00
|
|
|
vdtemp = await rc.set_dht_value(key, 1, va)
|
2023-07-30 15:45:20 -05:00
|
|
|
assert vdtemp == None
|
2023-06-29 13:52:53 -05:00
|
|
|
|
|
|
|
vdtemp = await rc.get_dht_value(key, 1, False)
|
2023-06-29 21:18:45 -05:00
|
|
|
assert vdtemp.data == va
|
|
|
|
assert vdtemp.seq == 0
|
|
|
|
assert vdtemp.writer == owner
|
2023-06-29 13:52:53 -05:00
|
|
|
|
|
|
|
vdtemp = await rc.get_dht_value(key, 0, False)
|
2023-06-29 21:18:45 -05:00
|
|
|
assert vdtemp == None
|
2023-06-29 13:52:53 -05:00
|
|
|
|
|
|
|
vdtemp = await rc.set_dht_value(key, 0, vb)
|
2023-07-30 15:45:20 -05:00
|
|
|
assert vdtemp == None
|
2023-06-29 13:52:53 -05:00
|
|
|
|
|
|
|
vdtemp = await rc.get_dht_value(key, 0, True)
|
2023-06-29 21:18:45 -05:00
|
|
|
assert vdtemp.data == vb
|
2023-06-29 13:52:53 -05:00
|
|
|
|
|
|
|
vdtemp = await rc.get_dht_value(key, 1, True)
|
2023-06-29 21:18:45 -05:00
|
|
|
assert vdtemp.data == va
|
2023-06-29 13:52:53 -05:00
|
|
|
|
|
|
|
# Equal value should not trigger sequence number update
|
|
|
|
vdtemp = await rc.set_dht_value(key, 1, va)
|
2023-07-30 15:45:20 -05:00
|
|
|
assert vdtemp == None
|
2023-06-29 13:52:53 -05:00
|
|
|
|
|
|
|
# Different value should trigger sequence number update
|
|
|
|
vdtemp = await rc.set_dht_value(key, 1, vb)
|
2023-07-30 15:45:20 -05:00
|
|
|
assert vdtemp == None
|
2023-06-29 13:52:53 -05:00
|
|
|
|
|
|
|
# Now that we initialized some subkeys
|
|
|
|
# and verified they stored correctly
|
|
|
|
# Delete things locally and reopen and see if we can write
|
|
|
|
# with the same writer key
|
|
|
|
|
|
|
|
await rc.close_dht_record(key)
|
|
|
|
await rc.delete_dht_record(key)
|
|
|
|
|
|
|
|
rec = await rc.open_dht_record(key, veilid.KeyPair.from_parts(owner, secret))
|
|
|
|
assert rec != None
|
|
|
|
assert rec.key == key
|
|
|
|
assert rec.owner == owner
|
|
|
|
assert rec.owner_secret == secret
|
|
|
|
assert rec.schema.kind == veilid.DHTSchemaKind.DFLT
|
|
|
|
assert rec.schema.o_cnt == 2
|
|
|
|
|
2023-06-29 21:18:45 -05:00
|
|
|
# Verify subkey 1 can be set before it is get but newer is available online
|
|
|
|
vdtemp = await rc.set_dht_value(key, 1, vc)
|
|
|
|
assert vdtemp != None
|
|
|
|
assert vdtemp.data == vb
|
|
|
|
assert vdtemp.seq == 1
|
|
|
|
assert vdtemp.writer == owner
|
|
|
|
|
|
|
|
# Verify subkey 1 can be set a second time and it updates because seq is newer
|
|
|
|
vdtemp = await rc.set_dht_value(key, 1, vc)
|
2023-07-30 15:45:20 -05:00
|
|
|
assert vdtemp == None
|
|
|
|
|
2023-06-29 21:18:45 -05:00
|
|
|
# Verify the network got the subkey update with a refresh check
|
2023-06-29 13:52:53 -05:00
|
|
|
vdtemp = await rc.get_dht_value(key, 1, True)
|
|
|
|
assert vdtemp != None
|
|
|
|
assert vdtemp.data == vc
|
|
|
|
assert vdtemp.seq == 2
|
|
|
|
assert vdtemp.writer == owner
|
|
|
|
|
|
|
|
# Delete things locally and reopen and see if we can write
|
|
|
|
# with a different writer key (should fail)
|
|
|
|
|
|
|
|
await rc.close_dht_record(key)
|
|
|
|
await rc.delete_dht_record(key)
|
2023-07-30 15:45:20 -05:00
|
|
|
|
2023-06-29 13:52:53 -05:00
|
|
|
rec = await rc.open_dht_record(key, other_keypair)
|
|
|
|
assert rec != None
|
|
|
|
assert rec.key == key
|
|
|
|
assert rec.owner == owner
|
|
|
|
assert rec.owner_secret == None
|
|
|
|
assert rec.schema.kind == veilid.DHTSchemaKind.DFLT
|
|
|
|
assert rec.schema.o_cnt == 2
|
|
|
|
|
|
|
|
# Verify subkey 1 can NOT be set because we have the wrong writer
|
2023-07-01 09:45:31 -05:00
|
|
|
with pytest.raises(veilid.VeilidAPIError):
|
|
|
|
vdtemp = await rc.set_dht_value(key, 1, va)
|
2023-07-30 15:45:20 -05:00
|
|
|
|
2023-06-29 13:52:53 -05:00
|
|
|
# Verify subkey 0 can NOT be set because we have the wrong writer
|
2023-07-01 09:45:31 -05:00
|
|
|
with pytest.raises(veilid.VeilidAPIError):
|
|
|
|
vdtemp = await rc.set_dht_value(key, 0, va)
|
2023-07-30 15:45:20 -05:00
|
|
|
|
2024-02-21 19:52:48 -06:00
|
|
|
# Verify subkey 0 can be set because override with the right writer
|
2024-02-21 21:00:06 -06:00
|
|
|
vdtemp = await rc.set_dht_value(key, 0, va, veilid.KeyPair.from_parts(owner, secret))
|
|
|
|
assert vdtemp == None
|
2024-02-21 19:52:48 -06:00
|
|
|
|
2023-06-29 13:52:53 -05:00
|
|
|
# Clean up
|
|
|
|
await rc.close_dht_record(key)
|
|
|
|
await rc.delete_dht_record(key)
|
2024-03-13 21:34:44 -05:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_inspect_dht_record(api_connection: veilid.VeilidAPI):
|
|
|
|
rc = await api_connection.new_routing_context()
|
|
|
|
async with rc:
|
|
|
|
rec = await rc.create_dht_record(veilid.DHTSchema.dflt(2))
|
|
|
|
|
|
|
|
vd = await rc.set_dht_value(rec.key, 0, b"BLAH BLAH BLAH")
|
|
|
|
assert vd == None
|
|
|
|
|
|
|
|
rr = await rc.inspect_dht_record(rec.key, [], veilid.DHTReportScope.LOCAL)
|
|
|
|
print("rr: {}", rr.__dict__)
|
|
|
|
assert rr.subkeys == [[0,1]]
|
|
|
|
assert rr.local_seqs == [0, 0xFFFFFFFF]
|
|
|
|
assert rr.network_seqs == []
|
|
|
|
|
|
|
|
rr2 = await rc.inspect_dht_record(rec.key, [], veilid.DHTReportScope.SYNC_GET)
|
|
|
|
print("rr2: {}", rr2.__dict__)
|
|
|
|
assert rr2.subkeys == [[0,1]]
|
|
|
|
assert rr2.local_seqs == [0, 0xFFFFFFFF]
|
|
|
|
assert rr2.network_seqs == [0, 0xFFFFFFFF]
|
|
|
|
|
|
|
|
await rc.close_dht_record(rec.key)
|
|
|
|
await rc.delete_dht_record(rec.key)
|