# Path: typtop/dbaccess.py
# def get_logging_path(username):
# def get_time():
# def entropy(typo):
#     def __str__(self):
#     def __init__(self, user, debug_mode=False):
#     def __del__(self):
#     def init_typtop(self, pw, allow_typo_login=True):
#     def reinit_typtop(self, newPw):
#     def is_typtop_init(self):
#     def getdb(self, tabname):
#     def get_db_path(self):
#     def assert_initialized(self):
#     def is_allowed(self, what):
#     def allow(self, what, how):
#     def allow_upload(self, allow):
#     def allow_login(self, allow=True):
#     def is_allowed_login(self):
#     def is_allowed_upload(self):
#     def is_real_pw(self, pw):
#     def _fill_waitlist_w_garbage(self):
#         def randomstring(k):
#     def _fill_cache_w_garbage(self):
#     def get_installation_id(self):
#     def get_last_unsent_logs_iter(self, force=False):
#     def update_last_log_sent_time(self, sent_time=0, delete_old_logs=True):
#     def insert_log(self, typo, in_cache, ts=None):
#     def _add_typo_to_waitlist(self, typo):
#     def _decrypt_n_filter_waitlist(self):
#     def get_table_size(self, table_name):
#     def get_typo_cache_size():
#     def get_pk(self):
#     def cache_insert_policy(old_t_c, new_t_c):
#     def clear_waitlist(self):
#     def check_login_count(self, update=False):
#     def check(self, pw):
#     def get_from_auxtdb(self, key):
#     def set_in_auxtdb(self, key, value):
#     def validate(self, orig_pw, typo):
#     def _update_typo_cache_by_waitlist(self, typo_cache, freq_counts):
#     def get_prompt(self):
#     def set_status(self, status):
# def check_system_status(typo_db):
# def on_correct_password(typo_db, password):
# def on_wrong_password(typo_db, password):
# def call_check(exchk_ret_val, user, password):
# class UserTypoDB(object):
#     class TypoDBError(Exception):
#     class NoneInitiatedDB(TypoDBError):
#     class CorruptedDB(TypoDBError):


import os
import json
import typtop.config as config
import typtop.dbaccess as dbaccess
import yaml
import pytest
import time
import pwd
    import stat
from typtop.dbaccess import (
    UserTypoDB, get_time, on_wrong_password,
    on_correct_password, logT, auxT,
    FREQ_COUNTS, INDEX_J, WAITLIST_SIZE,
    WAIT_LIST, pkdecrypt,
    NUMBER_OF_ENTRIES_TO_ALLOW_TYPO_LOGIN,
    logT, auxT, call_check
)
    db = start_DB()
    on_correct_password(db, get_pw())
    on_wrong_password(db, get_pw()+'1')
    s = os.stat(db.get_db_path()).st_mode
    assert not ((stat.S_IROTH | stat.S_IWOTH) & s)
    remove_DB()


def test_waitlist(isStandAlone=True):
    typoDB = start_DB()
    pwset = set(pws[:4])
    for i in range(4):
        typoDB.check(pws[i])
    typos_in_waitlist = set()
    install_id = typoDB.get_installation_id()
    for typo_ctx in typoDB.get_from_auxtdb(WAIT_LIST):
        typo_txt = pkdecrypt(typoDB._sk, typo_ctx)
        typo, ts = yaml.safe_load(typo_txt)
        if not typo.startswith(install_id):
            typos_in_waitlist.add(typo)
    assert not (typos_in_waitlist - pwset) and not (pwset - typos_in_waitlist)


def test_unintialized_exceptions():
    db = UserTypoDB(get_username(), debug_mode=True)
    assert not call_check(0, get_username(), get_pw())
    assert call_check(1, get_username(), get_pw())
    db.init_typtop(get_pw())
    assert call_check(0, get_username(), get_pw()) == 0
    assert call_check(0, get_username(), pws[1]) == 0

# Path: typtop/dbaccess.py
# def get_logging_path(username):
# def get_time():
# def entropy(typo):
#     def __str__(self):
#     def __init__(self, user, debug_mode=False):
#     def __del__(self):
#     def init_typtop(self, pw, allow_typo_login=True):
#     def reinit_typtop(self, newPw):
#     def is_typtop_init(self):
#     def getdb(self, tabname):
#     def get_db_path(self):
#     def assert_initialized(self):
#     def is_allowed(self, what):
#     def allow(self, what, how):
#     def allow_upload(self, allow):
#     def allow_login(self, allow=True):
#     def is_allowed_login(self):
#     def is_allowed_upload(self):
#     def is_real_pw(self, pw):
#     def _fill_waitlist_w_garbage(self):
#         def randomstring(k):
#     def _fill_cache_w_garbage(self):
#     def get_installation_id(self):
#     def get_last_unsent_logs_iter(self, force=False):
#     def update_last_log_sent_time(self, sent_time=0, delete_old_logs=True):
#     def insert_log(self, typo, in_cache, ts=None):
#     def _add_typo_to_waitlist(self, typo):
#     def _decrypt_n_filter_waitlist(self):
#     def get_table_size(self, table_name):
#     def get_typo_cache_size():
#     def get_pk(self):
#     def cache_insert_policy(old_t_c, new_t_c):
#     def clear_waitlist(self):
#     def check_login_count(self, update=False):
#     def check(self, pw):
#     def get_from_auxtdb(self, key):
#     def set_in_auxtdb(self, key, value):
#     def validate(self, orig_pw, typo):
#     def _update_typo_cache_by_waitlist(self, typo_cache, freq_counts):
#     def get_prompt(self):
#     def set_status(self, status):
# def check_system_status(typo_db):
# def on_correct_password(typo_db, password):
# def on_wrong_password(typo_db, password):
# def call_check(exchk_ret_val, user, password):
# class UserTypoDB(object):
#     class TypoDBError(Exception):
#     class NoneInitiatedDB(TypoDBError):
#     class CorruptedDB(TypoDBError):


# Path: tests/test_dbaccess.py
import os
import json
import typtop.config as config
import typtop.dbaccess as dbaccess
import yaml
import pytest
import time
import pwd
    import stat
from typtop.dbaccess import (
    UserTypoDB, get_time, on_wrong_password,
    on_correct_password, logT, auxT,
    FREQ_COUNTS, INDEX_J, WAITLIST_SIZE,
    WAIT_LIST, pkdecrypt,
    NUMBER_OF_ENTRIES_TO_ALLOW_TYPO_LOGIN,
    logT, auxT, call_check
)

    db = start_DB()
    on_correct_password(db, get_pw())
    on_wrong_password(db, get_pw()+'1')
    s = os.stat(db.get_db_path()).st_mode
    assert not ((stat.S_IROTH | stat.S_IWOTH) & s)
    remove_DB()


def test_waitlist(isStandAlone=True):
    typoDB = start_DB()
    pwset = set(pws[:4])
    for i in range(4):
        typoDB.check(pws[i])
    typos_in_waitlist = set()
    install_id = typoDB.get_installation_id()
    for typo_ctx in typoDB.get_from_auxtdb(WAIT_LIST):
        typo_txt = pkdecrypt(typoDB._sk, typo_ctx)
        typo, ts = yaml.safe_load(typo_txt)
        if not typo.startswith(install_id):
            typos_in_waitlist.add(typo)
    assert not (typos_in_waitlist - pwset) and not (pwset - typos_in_waitlist)


def test_unintialized_exceptions():
    db = UserTypoDB(get_username(), debug_mode=True)
    assert not call_check(0, get_username(), get_pw())
    assert call_check(1, get_username(), get_pw())
    db.init_typtop(get_pw())
    assert call_check(0, get_username(), get_pw()) == 0
    assert call_check(0, get_username(), pws[1]) == 0

    assert call_check(0, get_username(), get_pw()) == 0