Exemplo n.º 1
0
    def test_database_import(self):
        """Check that consensuses are parsed and imported to the db properly.

        Import a directory of consnensuses using the databaser
        function, and also import them using a naive but simple
        way. Then cross-check the results to see that they match.
        """

        # Import the consensus directory to the database using databaser
        db_conn, db_cursor = sqlite_db.init_db(SQLITE_DB_FILE, SQLITE_DB_SCHEMA)
        databaser.import_consensus_dir_to_db(db_cursor, TEST_CONSENSUSES_DIR, False)
        db_conn.commit()

        # Now parse the same consensus directory with the naive algorithm.
        guards_dict = parse_consensuses_naive_way(TEST_CONSENSUSES_DIR)

        # Now assert that results match.
        # FIrst of all, check that the right number of consensuses were parsed
        db_cursor.execute("SELECT count(*) FROM consensus")
        consensuses_read_n = int(db_cursor.fetchone()[0])
        self.assertEquals(consensuses_read_n, 4)

        # Now get the list of guards and their guardiness from the
        # database, and compare it with the naive guards dictionary.
        db_cursor.execute("SELECT (SELECT identity FROM relay WHERE relay_id=guarddata.relay_id), count(*) FROM guarddata GROUP BY relay_id;")
        guardiness_list = db_cursor.fetchall()

        self.assertEquals(len(guardiness_list), len(guards_dict))

        # Individually for each guard make sure the times_seen is the same.
        for guard_fpr, times_seen in guardiness_list:
            # The silly parsing function should have seen this fpr
            self.assertIn(guard_fpr, guards_dict)
            # Make sure that the same number of guard observations were found
            self.assertEquals(guards_dict[guard_fpr], times_seen)
Exemplo n.º 2
0
    def test_guardfraction_from_db(self):
        """Test that the guardfraction script understands the database correctly."""

        # Initialize the database and populate it with some test data
        db_conn, db_cursor = sqlite_db.init_db(SQLITE_DB_FILE,
                                               SQLITE_DB_SCHEMA)
        populate_db_helper(db_cursor)
        db_conn.commit()

        # Now read the database using the guardfraction script.
        # (don't care about dates. that's for the next test to worry about
        guards, consensuses_read_n = guardfraction.read_db_file(
            db_conn, db_cursor, 999)

        # Now make sure that guardfraction understood the correct data.

        self.assertEquals(consensuses_read_n, 3)
        self.assertEquals(len(guards.guards), 4)
        # Check the times_seen for each guard.
        for guard_fpr, guard in guards.guards.items():
            if guard_fpr == GUARD_1_FPR:
                self.assertEquals(guard.times_seen, 3)
            elif guard_fpr == GUARD_2_FPR:
                self.assertEquals(guard.times_seen, 2)
            elif guard_fpr == GUARD_3_FPR:
                self.assertEquals(guard.times_seen, 1)
            elif guard_fpr == GUARD_4_FPR:
                self.assertEquals(guard.times_seen, 1)
            else:
                self.assertTrue(False)  # Unknown guard!

        db_conn.close()
Exemplo n.º 3
0
    def test_output_file(self):
        """
        Using the test database again, test that the guardfraction
        script spits out the correct guardfraction output file.
        """

        # Initialize the database and populate it with some test data
        db_conn, db_cursor = sqlite_db.init_db(SQLITE_DB_FILE,
                                               SQLITE_DB_SCHEMA)
        populate_db_helper(db_cursor)
        db_conn.commit()

        # Read the database
        guards, consensuses_read_n = guardfraction.read_db_file(
            db_conn, db_cursor, 999)

        # Make a tempfile to save the output file.
        temp_file, temp_path = tempfile.mkstemp()
        os.close(temp_file)
        guards.write_output_file(temp_path, 999, consensuses_read_n)

        # Open the output file and validate its contents.
        with open(temp_path) as test_fd:
            lines = test_fd.readlines()

            # One line for the file version, one line for date, one
            # line for n-inputs and 4 guards.
            self.assertEquals(len(lines), 7)

            self.assertEquals(lines[0][:26], "guardfraction-file-version")
            self.assertEquals(lines[1][:10], "written-at")

            # Test the n-inputs line
            self.assertEquals(lines[2], "n-inputs 3 999 23976\n")

            # Test guard lines
            self.assertIn(
                "guard-seen AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 100 3\n",
                lines[3:])
            self.assertIn(
                "guard-seen BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB 67 2\n",
                lines[3:])
            self.assertIn(
                "guard-seen CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC 33 1\n",
                lines[3:])
            self.assertIn(
                "guard-seen DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD 33 1\n",
                lines[3:])

        db_conn.close()
Exemplo n.º 4
0
def main():
    """Read some consensus files and make a guard activity sqlite3 database."""

    # Enable this if you want debug logs
    # logging.getLogger("").setLevel(logging.DEBUG)

    # Parse CLI
    args = parse_cmd_args()

    # Make sure a directory was provided.
    if not os.path.isdir(args.consensus_dir):
        logging.error("%s is not a directory!", args.consensus_dir)
        sys.exit(2)

    # Unwrap CLI arguments
    db_file = args.db_file
    schema_file = args.schema_file
    consensus_dir = args.consensus_dir
    delete_imported = args.delete_imported
    first_time = args.first_time

    # If there is no database file, assume that this is our first time
    # getting run.
    if not os.path.exists(db_file):
        first_time = True

    # Initialize sqlite3 database.
    db_conn, db_cursor = sqlite_db.init_db(db_file,
                                           schema_file if first_time else None)

    # Parse all consensus files
    import_consensus_dir_to_db(db_cursor, consensus_dir, delete_imported)

    logging.info("Done! Wrote database file at %s.", db_file)

    # Commit database changes and close the file. We are done!
    db_conn.commit()
    db_conn.close()
Exemplo n.º 5
0
def main():
    """
    Read an sqlite3 database and output guardfraction data.
    """

    # Enable this if you want debug logs
    # logging.getLogger("").setLevel(logging.DEBUG)

    # Parse CLI
    args = parse_cmd_args()

    output_file = args.output
    max_days = args.max_days
    db_file = args.db_file
    delete_expired = args.delete_expired
    list_missing = args.list_missing

    # Make sure that max_days is a positive integer but not too
    # positive. The maximum value is currently set to 5 years.
    if max_days <= 0 or max_days > (5 * 12 * 30):
        logging.warning("Bad max_days value (%d)", max_days)
        sys.exit(2)

    # Read database file and calculate guardfraction
    db_conn, db_cursor = sqlite_db.init_db(db_file)

    # Just print missing consensuses and bail
    if list_missing:
        print_missing_consensuses(db_conn, db_cursor, max_days)
        sys.exit(1)

    # Make sure that our clock is not horribly desynchronized.
    try:
        check_clock_correctness(db_cursor)
    except DesynchronizedClock, err:
        logging.warning("Clock issue (%s). Exiting.", err)
        sys.exit(1)