Skip to content

Commit

Permalink
tests/runes: per restriction test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ShahanaFarooqui authored and rustyrussell committed Sep 14, 2023
1 parent b46df7c commit 7a2008f
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions tests/test_runes.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,81 @@ def test_createrune(node_factory):
params=params)['valid'] is True


def check_rune_per_restriction(l1, rune_to_test, per_sec):
assert "last_used" not in l1.rpc.showrunes(rune=rune_to_test)['runes'][0]

before = time.time()
checkrune_result_1 = l1.rpc.checkrune(nodeid=l1.info['id'],
rune=rune_to_test,
method='getinfo',
params={})

show_nano_rune = l1.rpc.showrunes(rune=rune_to_test)['runes'][0]
after = time.time()

assert checkrune_result_1['valid'] is True
assert before < show_nano_rune['last_used'] < after

# cannot use same rune till "per_sec" seconds
with pytest.raises(RpcError, match='Not permitted:') as exc_info:
l1.rpc.checkrune(nodeid=l1.info['id'],
rune=rune_to_test,
method="listpeers",
params={})

assert exc_info.value.error['code'] == 0x5de
assert exc_info.value.error['message'] == 'Not permitted: too soon'
assert l1.rpc.showrunes(rune=rune_to_test)['runes'][0]['last_used'] == show_nano_rune['last_used']

time.sleep(per_sec)

# rune should again be valid after "per_sec" seconds
checkrune_result_3 = l1.rpc.checkrune(nodeid=l1.info['id'],
rune=rune_to_test,
method='listpeers',
params={})

assert checkrune_result_3['valid'] is True
assert show_nano_rune['last_used'] <= l1.rpc.showrunes(rune=rune_to_test)['runes'][0]['last_used'] <= time.time()


def test_createrune_per_restriction(node_factory):
l1 = node_factory.get_node()

# Fixme: changing restriction to 5 seconds throws RPC error "Not Permitted: malformed per"
# Fixme: Other "check_rune_per_restriction" do not throw error 1502 but return { valid: true }

# 1 sec = 1,000,000,000 nanoseconds (nsec)
rune_per_nano_sec = l1.rpc.createrune(restrictions=[["per=1000000000nsec"]])['rune']
assert rune_per_nano_sec == 'Bl0V_vkVkGr4h356JbCMCcoDyyKE8djkoQ2156iPB509MCZwZXI9MTAwMDAwMDAwMG5zZWM='
check_rune_per_restriction(l1, rune_per_nano_sec, 1)

# 1 sec = 1,000,000 microseconds (usec)
rune_per_micro_sec = l1.rpc.createrune(restrictions=[["per=100000usec"]])['rune']
assert rune_per_micro_sec == 'VL4i1yI2kFdNDtnmqQzoLD_yQRDkYtwGKbMJe7vS-qA9MSZwZXI9MTAwMDAwdXNlYw=='
# check_rune_per_restriction(l1, rune_per_micro_sec, 1)

# 1 sec = 1,000 milliseconds (msec)
rune_per_milli_sec = l1.rpc.createrune(restrictions=[["per=1000msec"]])['rune']
assert rune_per_milli_sec == 'EzVpQwjYe2aoNQiRa4_s7FJtomD3kWzx7lusMpzA59w9MiZwZXI9MTAwMG1zZWM='
# check_rune_per_restriction(l1, rune_per_milli_sec, 1)

# 1 sec
rune_per_sec = l1.rpc.createrune(restrictions=[["per=1sec"]])['rune']
assert rune_per_sec == 'IGc8a24J5WKYAM7I2zsOG4UJfCopXhnkMjhDB-0xAGo9MyZwZXI9MXNlYw=='
# check_rune_per_restriction(l1, rune_per_sec, 1)

# default (sec)
rune_per_default = l1.rpc.createrune(restrictions=[["per=1"]])['rune']
assert rune_per_default == 'NrM7go6C4qzfRQDkUSv1DtRroJWSKqdjIOuvGS4TLFE9NCZwZXI9MQ=='
# check_rune_per_restriction(l1, rune_per_default, 1)

# 1 minute
rune_per_min = l1.rpc.createrune(restrictions=[["per=1min"]])['rune']
assert rune_per_min == 'ZfWDjFa7wTiadUWOjwpztSClfiubwVusxxUEtoLtCBk9NSZwZXI9MW1pbg=='
# check_rune_per_restriction(l1, rune_per_min, 60)


def test_showrunes(node_factory):
l1 = node_factory.get_node()
rune1 = l1.rpc.createrune()
Expand Down

0 comments on commit 7a2008f

Please sign in to comment.