Ejemplo n.º 1
0
 def __init__(self, represented_directory, *a, **k):
     super(GentleRootDir, self).__init__(self, *a, **k)
     self._represented_dir = os.path.abspath(represented_directory)
     self._pointer_generation_namespace = random()
     self._last_reset_t = time.time() - 3600
     self.gentle_reset()
Ejemplo n.º 2
0
def test(data_store):
    """
    Test a data store (must be empty).

    Example:
    >>> from gentle_tp_da92 import test_data_store, memory_based
    >>> test_data_store.test(memory_based.GentleDataStore())
    'PASS'
    """
    c_db = data_store.content_db
    p_db = data_store.pointer_db

    assert c_db.find() == []
    assert p_db.find() == []

    p = "0b3bef2bde9575c8b3251c3c92a4f99f6b770cc8f6e73509a0276e08b2cb9573"
    assert p not in c_db
    assert p not in p_db

    string = "Hello YOU"
    c = c_db + string
    assert c == sha256(string).hexdigest()
    assert c in c_db
    assert c not in p_db

    p_db[p] = c
    assert p not in c_db
    assert p in p_db
    assert p_db[p] == c

    for db in (c_db, p_db):
        try:
            "" in db  # should raise an exception
        except:
            assert True
        else:
            assert False

    assert p_db.find(p[:2]) == [p]
    assert c_db.find(c[:2]) == [c]

    string = "Second content"
    old_c = c
    c = c_db + string
    assert c == sha256(string).hexdigest()
    assert c in c_db

    p_db[p] = c
    assert p in p_db
    assert p_db[p] == c
    assert sorted(c_db.find()) == sorted([old_c, c])
    assert p_db.find() == [p]

    del p_db[p]
    assert p not in p_db
    assert sorted(c_db.find()) == sorted([old_c, c])
    assert p_db.find() == []

    # Randomized testing
    import random, os
    random_data = []
    for i in range(300):
        random_data.append(os.urandom(random.randrange(3000)))
    for i, rnd in enumerate(random_data):
        assert len(c_db.find()) == i + 2
        c = c_db + rnd
        assert len(c_db.find()) == i + 3
        assert c in c_db
        assert c not in p_db
        assert c == sha256(rnd).hexdigest()
        assert c_db[c] == rnd
        assert c_db.find(c) == [c]

        try:
            c[:-1] in c_db  # should raise an exception
        except:
            assert True
        else:
            assert False

        p = os.urandom(32).encode("hex")
        p_db[p] = c
        assert p in p_db
        assert p_db[p] == c
        assert p not in c_db
        assert p_db.find(p) == [p]
        p = utilities.random()
        p_db[p] = c
        assert p in p_db
        assert p_db[p] == c
        assert p not in c_db
        assert p_db.find(p) == [p]

        try:
            p[:-1] in p_db  # should raise an exception
        except:
            assert True
        else:
            assert False

    return "PASS"