|
2 | 2 |
|
3 | 3 | import os |
4 | 4 |
|
| 5 | +import pytest |
| 6 | + |
5 | 7 | from event import Event, EventType, Process |
6 | 8 | from server import FileActivityService |
| 9 | +from utils import join_path_with_filename, path_to_string |
7 | 10 |
|
8 | 11 |
|
9 | 12 | def test_setxattr( |
@@ -204,3 +207,66 @@ def test_setxattr_new_file( |
204 | 207 | ], |
205 | 208 | strict=False, |
206 | 209 | ) |
| 210 | + |
| 211 | + |
| 212 | +@pytest.mark.parametrize( |
| 213 | + 'filename', |
| 214 | + [ |
| 215 | + pytest.param('xattr.txt', id='ASCII'), |
| 216 | + pytest.param('café.txt', id='French'), |
| 217 | + pytest.param('файл.txt', id='Cyrillic'), |
| 218 | + pytest.param('测试.txt', id='Chinese'), |
| 219 | + pytest.param('🔒secure.txt', id='Emoji'), |
| 220 | + pytest.param(b'xattr\xff\xfe.txt', id='InvalidUTF8'), |
| 221 | + ], |
| 222 | +) |
| 223 | +def test_setxattr_utf8_filenames( |
| 224 | + monitored_dir: str, |
| 225 | + server: FileActivityService, |
| 226 | + filename: str | bytes, |
| 227 | +): |
| 228 | + """ |
| 229 | + Tests that xattr events are correctly tracked on files with |
| 230 | + various UTF-8 and non-UTF-8 filenames. |
| 231 | +
|
| 232 | + Args: |
| 233 | + monitored_dir: Temporary directory path for creating the test file. |
| 234 | + server: The server instance to communicate with. |
| 235 | + filename: Name of the file to create (includes UTF-8 test cases). |
| 236 | + """ |
| 237 | + fut = join_path_with_filename(monitored_dir, filename) |
| 238 | + |
| 239 | + with open(fut, 'w') as f: |
| 240 | + f.write('test') |
| 241 | + |
| 242 | + # gRPC events use lossy UTF-8 conversion, but os.setxattr |
| 243 | + # needs the original path to find the file on disk. |
| 244 | + fut_str = path_to_string(fut) |
| 245 | + |
| 246 | + process = Process.from_proc() |
| 247 | + |
| 248 | + server.wait_events( |
| 249 | + [ |
| 250 | + Event( |
| 251 | + process=process, |
| 252 | + event_type=EventType.CREATION, |
| 253 | + file=fut_str, |
| 254 | + host_path=fut_str, |
| 255 | + ), |
| 256 | + ], |
| 257 | + ) |
| 258 | + |
| 259 | + os.setxattr(fut, 'user.utf8_test', b'value') |
| 260 | + |
| 261 | + server.wait_events( |
| 262 | + [ |
| 263 | + Event( |
| 264 | + process=process, |
| 265 | + event_type=EventType.XATTR, |
| 266 | + file='', |
| 267 | + host_path=fut_str, |
| 268 | + xattr_name='user.utf8_test', |
| 269 | + ), |
| 270 | + ], |
| 271 | + strict=False, |
| 272 | + ) |
0 commit comments