2023-12-31 01:19:53 +05:30
|
|
|
#!/bin/env /usr/bin/python3
|
|
|
|
#
|
|
|
|
# Copyright (C) 2023 Aravinth Manivannan <realaravinth@batsense.net>
|
2023-12-31 01:28:59 +05:30
|
|
|
#
|
2023-12-31 01:19:53 +05:30
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
2023-12-31 01:28:59 +05:30
|
|
|
#
|
2023-12-31 01:19:53 +05:30
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
2023-12-31 01:28:59 +05:30
|
|
|
#
|
2023-12-31 01:19:53 +05:30
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
from asyncio import sleep
|
|
|
|
import json
|
|
|
|
|
|
|
|
from dcache import grpc_add_pow, grpc_get_pow, grpc_delete_pow
|
|
|
|
|
|
|
|
# 1. Check duplicate pow
|
|
|
|
# 2. Create pow
|
|
|
|
# 3. Read non-existent pow
|
|
|
|
# 4. Read pow
|
|
|
|
# 5. Read expired pow
|
|
|
|
|
2023-12-31 01:28:59 +05:30
|
|
|
|
2023-12-31 01:19:53 +05:30
|
|
|
def add_pow(captcha, pow):
|
2023-12-31 01:28:59 +05:30
|
|
|
"""Add pow to"""
|
|
|
|
try:
|
2023-12-31 01:19:53 +05:30
|
|
|
res = grpc_add_pow(captcha, pow)
|
|
|
|
return res
|
|
|
|
except Exception as e:
|
|
|
|
return e
|
2023-12-31 01:28:59 +05:30
|
|
|
|
|
|
|
|
2023-12-31 01:19:53 +05:30
|
|
|
def get_pow_from(captcha, pow):
|
2023-12-31 01:28:59 +05:30
|
|
|
"""Add pow to"""
|
|
|
|
try:
|
2023-12-31 01:19:53 +05:30
|
|
|
res = grpc_get_pow(captcha, pow)
|
2023-12-31 01:28:59 +05:30
|
|
|
if res.HasField("result"):
|
2023-12-31 01:19:53 +05:30
|
|
|
return res.result
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
except Exception as e:
|
|
|
|
return e
|
|
|
|
|
2023-12-31 01:28:59 +05:30
|
|
|
|
2023-12-31 01:19:53 +05:30
|
|
|
def delete_pow(captcha, pow):
|
2023-12-31 01:28:59 +05:30
|
|
|
"""Add pow to"""
|
|
|
|
try:
|
2023-12-31 01:19:53 +05:30
|
|
|
grpc_delete_pow(pow)
|
|
|
|
except Exception as e:
|
|
|
|
return e
|
|
|
|
|
2023-12-31 01:28:59 +05:30
|
|
|
|
2023-12-31 01:19:53 +05:30
|
|
|
async def add_pow_works():
|
|
|
|
"""Test: Add pow"""
|
|
|
|
try:
|
|
|
|
key = "add_pow"
|
|
|
|
pow_name = "add_pow_pow"
|
|
|
|
|
|
|
|
add_pow(key, pow_name)
|
|
|
|
stored_pow = get_pow_from(key, pow_name)
|
|
|
|
assert stored_pow.difficulty_factor == 500
|
|
|
|
assert stored_pow.duration == 5
|
|
|
|
print("[*] Add pow works")
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
raise e
|
|
|
|
|
2023-12-31 01:28:59 +05:30
|
|
|
|
2023-12-31 01:19:53 +05:30
|
|
|
async def pow_ttl_works():
|
|
|
|
"""Test: pow TTL"""
|
|
|
|
try:
|
|
|
|
key = "ttl_pow"
|
|
|
|
pow_name = "ttl_pow_pow"
|
|
|
|
|
|
|
|
add_pow(key, pow_name)
|
|
|
|
await sleep(5 + 2)
|
|
|
|
|
|
|
|
error = get_pow_from(key, pow_name)
|
|
|
|
assert error is None
|
|
|
|
|
|
|
|
print("[*] pow TTL works")
|
|
|
|
except Exception as e:
|
|
|
|
raise e
|
|
|
|
|
|
|
|
|
|
|
|
async def pow_doesnt_exist():
|
|
|
|
"""Test: Non-existent pow"""
|
|
|
|
try:
|
|
|
|
pow_name = "nonexistent_pow"
|
|
|
|
key = "nonexistent_pow_key"
|
|
|
|
|
|
|
|
error = get_pow_from(key, pow_name)
|
|
|
|
assert error is None
|
|
|
|
|
|
|
|
print("[*] pow Doesn't Exist works")
|
|
|
|
except Exception as e:
|
|
|
|
raise e
|
|
|
|
|
|
|
|
|
|
|
|
async def delete_pow_works():
|
|
|
|
"""Test: Delete pows"""
|
|
|
|
try:
|
|
|
|
pow_name = "delete_pow"
|
|
|
|
key = "delete_pow_key"
|
2023-12-31 01:28:59 +05:30
|
|
|
# pow = get_pow(pow_name)
|
2023-12-31 01:19:53 +05:30
|
|
|
|
|
|
|
add_pow(key, pow_name)
|
|
|
|
delete_pow(key, pow_name)
|
|
|
|
error = get_pow_from(key, pow_name)
|
|
|
|
assert error is None
|
|
|
|
|
|
|
|
print("[*] Delete pow works")
|
|
|
|
except Exception as e:
|
|
|
|
raise e
|