|
2 | 2 | import os |
3 | 3 | from unittest.mock import patch, MagicMock |
4 | 4 |
|
| 5 | +import aiomysql |
5 | 6 | import pytest |
| 7 | +from pymysql.constants import CLIENT |
6 | 8 | from pymysql.err import OperationalError |
7 | 9 |
|
8 | 10 |
|
@@ -81,3 +83,33 @@ async def test_load_warnings(cursor, table_local_file): |
81 | 83 | with warnings.catch_warnings(record=True) as w: |
82 | 84 | await cursor.execute(sql) |
83 | 85 | assert "Incorrect integer value" in str(w[-1].message) |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.run_loop |
| 89 | +async def test_load_local_disabled(mysql_params, table_local_file): |
| 90 | + # By setting the client flag, the server will be informed that we support |
| 91 | + # loading local files. This validates that the client side check catches |
| 92 | + # the server attempting to read files from us without having this |
| 93 | + # explicitly enabled on the connection. The local_infile parameter sets |
| 94 | + # the client flag, but not the other way round. |
| 95 | + params = mysql_params.copy() |
| 96 | + params["local_infile"] = False |
| 97 | + if "client_flag" in params: |
| 98 | + params["client_flag"] |= CLIENT.LOCAL_FILES |
| 99 | + else: |
| 100 | + params["client_flag"] = CLIENT.LOCAL_FILES |
| 101 | + |
| 102 | + async with aiomysql.connect(**params) as conn: |
| 103 | + async with conn.cursor() as cursor: |
| 104 | + # Test load local infile with a valid file |
| 105 | + filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), |
| 106 | + 'fixtures', |
| 107 | + 'load_local_data.txt') |
| 108 | + with pytest.raises( |
| 109 | + RuntimeError, |
| 110 | + match="Received LOAD_LOCAL packet but local_infile option is false", |
| 111 | + ): |
| 112 | + await cursor.execute( |
| 113 | + ("LOAD DATA LOCAL INFILE '{0}' INTO TABLE " + |
| 114 | + "test_load_local FIELDS TERMINATED BY ','").format(filename) |
| 115 | + ) |
0 commit comments