2021-06-06 14:05:14 +03:00
|
|
|
#!/bin/env /usr/bin/python3
|
|
|
|
# # Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
|
2021-06-06 09:10:30 +03:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# 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/>.
|
2021-06-06 14:32:02 +03:00
|
|
|
from asyncio import sleep
|
2021-06-06 14:05:14 +03:00
|
|
|
import sys
|
2021-06-08 16:43:57 +03:00
|
|
|
import json
|
2021-06-06 09:10:30 +03:00
|
|
|
|
2021-06-06 14:05:14 +03:00
|
|
|
from mcaptcha import register
|
2021-06-07 14:44:39 +03:00
|
|
|
import utils
|
2021-06-06 09:10:30 +03:00
|
|
|
|
2021-06-08 16:43:57 +03:00
|
|
|
r = utils.connect()
|
2021-06-07 14:44:39 +03:00
|
|
|
utils.ping(r)
|
2021-06-06 09:10:30 +03:00
|
|
|
|
|
|
|
COMMANDS = {
|
2021-06-09 17:11:58 +03:00
|
|
|
"COUNT" : "MCAPTCHA_CACHE.ADD_VISITOR",
|
|
|
|
"GET" : "MCAPTCHA_CACHE.GET",
|
2021-06-06 09:10:30 +03:00
|
|
|
}
|
|
|
|
|
2021-06-07 14:44:39 +03:00
|
|
|
def incr(key):
|
2021-06-08 16:43:57 +03:00
|
|
|
data = r.execute_command(COMMANDS["COUNT"], key)
|
|
|
|
return json.loads(data)
|
2021-06-06 09:10:30 +03:00
|
|
|
|
|
|
|
def get_count(key):
|
|
|
|
try:
|
|
|
|
count = r.execute_command(COMMANDS["GET"], key)
|
|
|
|
return int(count)
|
|
|
|
except:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def assert_count(expect, key):
|
|
|
|
count = get_count(key)
|
|
|
|
assert count == expect
|
|
|
|
|
2021-06-06 14:32:02 +03:00
|
|
|
async def incr_one_works():
|
2021-06-06 14:05:14 +03:00
|
|
|
try:
|
|
|
|
key = "incr_one"
|
2021-06-07 14:44:39 +03:00
|
|
|
register(key)
|
2021-06-06 14:05:14 +03:00
|
|
|
initial_count = get_count(key)
|
|
|
|
# incriment
|
2021-06-07 14:44:39 +03:00
|
|
|
incr(key)
|
2021-06-06 14:05:14 +03:00
|
|
|
assert_count(initial_count + 1, key)
|
|
|
|
# wait till expiry
|
2021-06-07 14:44:39 +03:00
|
|
|
await sleep(5 + 2)
|
2021-06-06 14:05:14 +03:00
|
|
|
assert_count(initial_count, key)
|
2021-06-08 16:43:57 +03:00
|
|
|
print("[*] Incr one works")
|
2021-06-06 14:05:14 +03:00
|
|
|
except Exception as e:
|
|
|
|
raise e
|
|
|
|
|
2021-06-06 09:10:30 +03:00
|
|
|
|
2021-06-06 14:32:02 +03:00
|
|
|
async def race_works():
|
2021-06-06 09:10:30 +03:00
|
|
|
key = "race_works"
|
2021-06-06 14:05:14 +03:00
|
|
|
try:
|
2021-06-07 14:44:39 +03:00
|
|
|
register(key)
|
2021-06-06 14:05:14 +03:00
|
|
|
initial_count = get_count(key)
|
|
|
|
race_num = 200
|
|
|
|
for _ in range(race_num):
|
2021-06-07 14:44:39 +03:00
|
|
|
incr(key)
|
2021-06-06 14:05:14 +03:00
|
|
|
assert_count(initial_count + race_num, key)
|
|
|
|
# wait till expiry
|
2021-06-07 14:44:39 +03:00
|
|
|
await sleep(5 + 2)
|
2021-06-06 14:05:14 +03:00
|
|
|
assert_count(initial_count, key)
|
2021-06-08 16:43:57 +03:00
|
|
|
print("[*] Race works")
|
|
|
|
except Exception as e:
|
|
|
|
raise e
|
|
|
|
|
|
|
|
|
|
|
|
async def difficulty_works():
|
|
|
|
key = "difficulty_works"
|
|
|
|
try:
|
|
|
|
register(key)
|
|
|
|
for _ in range(51):
|
|
|
|
incr(key)
|
|
|
|
|
|
|
|
data = incr(key)
|
|
|
|
assert data["difficulty_factor"] == 50
|
|
|
|
|
|
|
|
for _ in range(501):
|
|
|
|
incr(key)
|
|
|
|
data = incr(key)
|
|
|
|
assert data["difficulty_factor"] == 500
|
|
|
|
|
|
|
|
await sleep(5 + 2)
|
|
|
|
data = incr(key)
|
|
|
|
assert data["difficulty_factor"] == 50
|
|
|
|
|
|
|
|
print("[*] Difficulty factor works")
|
2021-06-06 14:05:14 +03:00
|
|
|
except Exception as e:
|
|
|
|
raise e
|