示例#1
0
    def connection(self):
        if not self.dbc:
            if self.path != ":memory:":
                self.path = system.check_database_path(self.path, logging.error)
            logging.debug("* Database: %s", self.path)
            self.dbc = sqlite3.connect(self.path)

            #
            # To avoid the need to map at hand columns in
            # a row with the sql schema, which is as error
            # prone as driving drunk.
            #
            self.dbc.row_factory = sqlite3.Row

            #
            # Migrate MUST be before table creation.  This
            # is safe because table creation always uses
            # the IF NOT EXISTS clause.  And this is needed
            # because otherwise we cannot migrate archived
            # databases (whose version number is old).
            # The exception is the config table which must
            # be present because migrate() looks at it.
            #
            table_config.create(self.dbc)

            migrate.migrate(self.dbc)
            migrate2.migrate(self.dbc)

            table_speedtest.create(self.dbc)
            table_geoloc.create(self.dbc)
            table_bittorrent.create(self.dbc)
            table_log.create(self.dbc)

        return self.dbc
示例#2
0
    def test_works(self):
        ''' Make sure that update_settings() works '''

        connection = sqlite3.connect(':memory:')
        table_config.create(connection)

        privacy.update_settings(
            connection, {
                'privacy.informed': 4,
                'privacy.can_collect': 5,
                'privacy.can_publish': 6,
                'foo': 'bar',
            })

        content = table_config.dictionarize(connection)

        # Make sure foo: bar was not added
        self.assertEqual(
            sorted(content.keys()),
            sorted([
                'uuid', 'version', 'privacy.informed', 'privacy.can_collect',
                'privacy.can_publish'
            ]))

        # Make sure settings were added correctly
        self.assertEqual(content['privacy.informed'], '4')
        self.assertEqual(content['privacy.can_collect'], '5')
        self.assertEqual(content['privacy.can_publish'], '6')
示例#3
0
    def test_works(self):
        ''' Make sure that update_settings() works '''

        connection = sqlite3.connect(':memory:')
        table_config.create(connection)

        privacy.update_settings(connection, {
                                             'privacy.informed': 4,
                                             'privacy.can_collect': 5,
                                             'privacy.can_publish': 6,
                                             'foo': 'bar',
                                            })

        content = table_config.dictionarize(connection)

        # Make sure foo: bar was not added
        self.assertEqual(sorted(content.keys()), sorted([
                                                         'uuid', 'version',
                                                         'privacy.informed',
                                                         'privacy.can_collect',
                                                         'privacy.can_publish'
                                                        ]))

        # Make sure settings were added correctly
        self.assertEqual(content['privacy.informed'], '4')
        self.assertEqual(content['privacy.can_collect'], '5')
        self.assertEqual(content['privacy.can_publish'], '6')
示例#4
0
    def test_failure(self):
        ''' Make sure test_settings() returns 1 for a bad database '''

        connection = sqlite3.connect(':memory:')
        table_config.create(connection)
        table_config.update(connection, {
                                         'privacy.informed': 1,
                                         'privacy.can_collect': 1,
                                         'privacy.can_publish': 0
                                        }.iteritems())

        self.assertEqual(privacy.test_settings(connection), 1)
示例#5
0
    def test_failure(self):
        ''' Make sure test_settings() returns 1 for a bad database '''

        connection = sqlite3.connect(':memory:')
        table_config.create(connection)
        table_config.update(
            connection, {
                'privacy.informed': 1,
                'privacy.can_collect': 1,
                'privacy.can_publish': 0
            }.iteritems())

        self.assertEqual(privacy.test_settings(connection), 1)
示例#6
0
    def test_works(self):
        ''' Make sure print_settings() works '''

        connection = sqlite3.connect(':memory:')
        table_config.create(connection)

        privacy.update_settings(
            connection, {
                'privacy.informed': 4,
                'privacy.can_collect': 5,
                'privacy.can_publish': 6,
            })

        self.assertEqual(privacy.print_settings(connection, ':memory:'), 0)
示例#7
0
    def test_works(self):
        ''' Make sure print_settings() works '''

        connection = sqlite3.connect(':memory:')
        table_config.create(connection)

        privacy.update_settings(connection, {
                                             'privacy.informed': 4,
                                             'privacy.can_collect': 5,
                                             'privacy.can_publish': 6,
                                            })

        self.assertEqual(privacy.print_settings(connection,
          ':memory:'), 0)
示例#8
0
    def connection(self):
        ''' Return connection to database '''
        if not self.dbc:
            database_xxx.linux_fixup_databasedir()
            if self.path != ":memory:":
                self.path = system.check_database_path(self.path)

            logging.debug("* Database: %s", self.path)
            self.dbc = sqlite3.connect(self.path)

            #
            # To avoid the need to map at hand columns in
            # a row with the sql schema, which is as error
            # prone as driving drunk.
            #
            self.dbc.row_factory = sqlite3.Row

            #
            # On POSIX systems, neubot (initially) runs as root, to ensure that
            # database location, ownership and permissions are OK (as well as
            # to bind privileged ports).  But neubot can also be started by
            # normal users.  In this case, mark the database as readonly since
            # write operation are going to raise exceptions.
            #
            if not system.has_enough_privs():
                logging.warning('database: opening database in readonly mode')
                self.readonly = True
                return self.dbc

            #
            # Migrate MUST be before table creation.  This
            # is safe because table creation always uses
            # the IF NOT EXISTS clause.  And this is needed
            # because otherwise we cannot migrate archived
            # databases (whose version number is old).
            # The exception is the config table which must
            # be present because migrate() looks at it.
            #
            table_config.create(self.dbc)

            migrate.migrate(self.dbc)
            migrate2.migrate(self.dbc)

            table_speedtest.create(self.dbc)
            table_geoloc.create(self.dbc)
            table_bittorrent.create(self.dbc)
            table_log.create(self.dbc)
            table_raw.create(self.dbc)

        return self.dbc
示例#9
0
    def connection(self):
        if not self.dbc:
            if self.path != ":memory:":
                self.path = system.check_database_path(self.path, logging.error)
            logging.debug("* Database: %s" % self.path)
            self.dbc = sqlite3.connect(self.path)
            #
            # To avoid the need to map at hand columns in
            # a row with the sql schema, which is as error
            # prone as driving drunk.
            #
            self.dbc.row_factory = sqlite3.Row
            table_config.create(self.dbc)
            table_speedtest.create(self.dbc)
            table_geoloc.create(self.dbc)
            table_bittorrent.create(self.dbc)
            table_log.create(self.dbc)
            migrate.migrate(self.dbc)

        return self.dbc
示例#10
0
    def test_legacy(self):
        ''' Make sure test_settings() returns 1 for a legacy database '''

        #
        # This case should not happen, still I want to be sure
        # an error would be reported in this case.
        #

        connection = sqlite3.connect(':memory:')
        table_config.create(connection)
        table_config.update(connection, {
                                         'privacy.informed': 1,
                                         'privacy.can_collect': 1,
                                         'privacy.can_publish': 1
                                        }.iteritems())

        # Go back to version 4.1 of the database
        connection.execute(''' UPDATE config SET name="privacy.can_share"
                               WHERE name="privacy.can_publish" ''')

        self.assertEqual(privacy.test_settings(connection), 1)
示例#11
0
    def runTest(self):

        connection = sqlite3.connect(":memory:")
        connection.row_factory = sqlite3.Row

        v, v2 = [], []
        table_config.create(connection)
        for name, value in table_config.dictionarize(connection).items():
            v.append((name, value))
        table_config.create(connection)
        for name, value in table_config.dictionarize(connection).items():
            v2.append((name, value))
        self.assertEquals(v, v2)

        table_config.update(connection, {"uuid": ""}.iteritems())
        result = table_config.dictionarize(connection)
        #
        # The version number changes as time passes and we don't
        # want to keep the test uptodate.
        #
        del result['version']
        self.assertEquals(result, {"uuid": ""})

        table_config.update(connection, {}.iteritems(), clear=True)
        self.assertEquals(table_config.dictionarize(connection), {})

        table_config.create(connection)
        print(table_config.jsonize(connection))
示例#12
0
    def runTest(self):

        connection = sqlite3.connect(":memory:")
        connection.row_factory = sqlite3.Row

        v, v2 = [], []
        table_config.create(connection)
        for name, value in table_config.dictionarize(connection).items():
            v.append((name, value))
        table_config.create(connection)
        for name, value in table_config.dictionarize(connection).items():
            v2.append((name, value))
        self.assertEquals(v, v2)

        table_config.update(connection, {"uuid": ""}.iteritems())
        result = table_config.dictionarize(connection)
        #
        # The version number changes as time passes and we don't
        # want to keep the test uptodate.
        #
        del result['version']
        self.assertEquals(result, {"uuid": ""})

        table_config.update(connection, {}.iteritems(), clear=True)
        self.assertEquals(table_config.dictionarize(connection), {})

        table_config.create(connection)
        print(table_config.jsonize(connection))
示例#13
0
    def test_legacy(self):
        ''' Make sure test_settings() returns 1 for a legacy database '''

        #
        # This case should not happen, still I want to be sure
        # an error would be reported in this case.
        #

        connection = sqlite3.connect(':memory:')
        table_config.create(connection)
        table_config.update(
            connection, {
                'privacy.informed': 1,
                'privacy.can_collect': 1,
                'privacy.can_publish': 1
            }.iteritems())

        # Go back to version 4.1 of the database
        connection.execute(''' UPDATE config SET name="privacy.can_share"
                               WHERE name="privacy.can_publish" ''')

        self.assertEqual(privacy.test_settings(connection), 1)
示例#14
0
    def runTest(self):

        connection = sqlite3.connect(":memory:")
        connection.row_factory = sqlite3.Row

        v, v2 = [], []
        table_config.create(connection)
        table_config.walk(connection, v.append)
        table_config.create(connection)
        table_config.walk(connection, v2.append)
        self.assertEquals(v, v2)

        table_config.update(connection, {"uuid": ""}.iteritems())
        self.assertEquals(table_config.dictionarize(connection),
                          {"version": "4.0", "uuid": ""})

        table_config.update(connection, {}.iteritems(), clear=True)
        self.assertEquals(table_config.dictionarize(connection), {})

        table_config.create(connection)
        print(table_config.jsonize(connection))