-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Expand file tree
/
Copy pathtest_sqlalchemy.py
More file actions
69 lines (53 loc) · 2.04 KB
/
Copy pathtest_sqlalchemy.py
File metadata and controls
69 lines (53 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
"""
Copyright (c) 2006-2026 sqlmap developers (https://sqlmap.org)
See the file 'LICENSE' for copying permission
The SQLAlchemy '-d' connector wrapper (lib/utils/sqlalchemy.py). The absolute
SQLite path must map to 'sqlite:///' + abspath: an extra slash yields the db
'//path' (tolerated only on Linux by accident) and, on Windows, a broken
'/C:\\...' that fails to open.
"""
import os
import sqlite3
import sys
import tempfile
import unittest
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from _testutils import bootstrap
bootstrap()
try:
import sqlalchemy as _sa
_HAVE_SA = hasattr(_sa, "dialects")
except ImportError:
_HAVE_SA = False
from lib.core.data import conf
from lib.utils.sqlalchemy import SQLAlchemy
@unittest.skipUnless(_HAVE_SA, "SQLAlchemy not installed")
class TestSQLAlchemySqlitePath(unittest.TestCase):
_KEYS = ("direct", "dbmsUser", "dbmsPass", "hostname", "port", "dbmsDb")
def setUp(self):
self._saved = dict((k, conf.get(k)) for k in self._KEYS)
def tearDown(self):
for k, v in self._saved.items():
conf[k] = v
def test_absolute_sqlite_path_opens_correct_file(self):
d = tempfile.mkdtemp(prefix="sqlmap-sa-test")
dbfile = os.path.join(d, "target.db")
con = sqlite3.connect(dbfile)
con.execute("CREATE TABLE t (x TEXT)")
con.execute("INSERT INTO t VALUES ('secret')")
con.commit()
con.close()
conf.direct = "sqlite://%s" % dbfile
conf.dbmsUser = conf.dbmsPass = None
conf.hostname = None
conf.port = None
conf.dbmsDb = dbfile
sa = SQLAlchemy(dialect="sqlite")
sa.connect()
# the reformatted URL must resolve to the exact absolute file (not '//...path')
self.assertEqual(_sa.engine.url.make_url(sa.address).database, os.path.abspath(dbfile))
# and end-to-end it must read from that file
self.assertEqual(sa.select("SELECT x FROM t"), [("secret",)])
if __name__ == "__main__":
unittest.main(verbosity=2)