|
6 | 6 | from unittest.mock import patch |
7 | 7 |
|
8 | 8 | from pydoll import exceptions |
9 | | -from pydoll.utils import decode_base64_to_bytes, get_browser_ws_address, validate_browser_paths |
| 9 | +from pydoll.utils import ( |
| 10 | + clean_script_for_analysis, |
| 11 | + decode_base64_to_bytes, |
| 12 | + get_browser_ws_address, |
| 13 | + has_return_outside_function, |
| 14 | + is_script_already_function, |
| 15 | + validate_browser_paths, |
| 16 | +) |
10 | 17 |
|
11 | 18 |
|
12 | 19 | class TestUtils: |
@@ -228,3 +235,169 @@ def test_validate_browser_paths_mixed_valid_invalid(self): |
228 | 235 | result = validate_browser_paths(paths) |
229 | 236 | assert result == valid_path |
230 | 237 |
|
| 238 | + |
| 239 | +class TestDecodeBase64ToBytes: |
| 240 | + """Test decode_base64_to_bytes function.""" |
| 241 | + |
| 242 | + def test_decode_base64_to_bytes_valid_input(self): |
| 243 | + """Test decoding valid base64 string.""" |
| 244 | + base64_string = 'SGVsbG8gV29ybGQ=' # "Hello World" in base64 |
| 245 | + result = decode_base64_to_bytes(base64_string) |
| 246 | + assert result == b'Hello World' |
| 247 | + |
| 248 | + def test_decode_base64_to_bytes_empty_string(self): |
| 249 | + """Test decoding empty base64 string.""" |
| 250 | + result = decode_base64_to_bytes('') |
| 251 | + assert result == b'' |
| 252 | + |
| 253 | + |
| 254 | +class TestValidateBrowserPaths: |
| 255 | + """Test validate_browser_paths function.""" |
| 256 | + |
| 257 | + def test_validate_browser_paths_valid_path(self, tmp_path): |
| 258 | + """Test with valid executable path.""" |
| 259 | + # Create a temporary executable file |
| 260 | + executable = tmp_path / "browser" |
| 261 | + executable.write_text("#!/bin/bash\necho 'browser'") |
| 262 | + executable.chmod(0o755) |
| 263 | + |
| 264 | + result = validate_browser_paths([str(executable)]) |
| 265 | + assert result == str(executable) |
| 266 | + |
| 267 | + def test_validate_browser_paths_invalid_paths(self): |
| 268 | + """Test with invalid paths.""" |
| 269 | + from pydoll.exceptions import InvalidBrowserPath |
| 270 | + |
| 271 | + with pytest.raises(InvalidBrowserPath): |
| 272 | + validate_browser_paths(['/nonexistent/path', '/another/invalid/path']) |
| 273 | + |
| 274 | + |
| 275 | +class TestScriptAnalysisFunctions: |
| 276 | + """Test JavaScript script analysis functions.""" |
| 277 | + |
| 278 | + def test_clean_script_for_analysis_removes_comments(self): |
| 279 | + """Test that comments are removed from script.""" |
| 280 | + script = ''' |
| 281 | + // This is a line comment |
| 282 | + var x = 5; |
| 283 | + /* This is a block comment */ |
| 284 | + return x; |
| 285 | + ''' |
| 286 | + |
| 287 | + result = clean_script_for_analysis(script) |
| 288 | + |
| 289 | + assert '// This is a line comment' not in result |
| 290 | + assert '/* This is a block comment */' not in result |
| 291 | + assert 'var x = 5;' in result |
| 292 | + assert 'return x;' in result |
| 293 | + |
| 294 | + def test_clean_script_for_analysis_removes_strings(self): |
| 295 | + """Test that string literals are removed from script.""" |
| 296 | + script = ''' |
| 297 | + var message = "This string contains return statement"; |
| 298 | + var another = 'Another string with return'; |
| 299 | + var template = `Template literal with return`; |
| 300 | + return "actual return"; |
| 301 | + ''' |
| 302 | + |
| 303 | + result = clean_script_for_analysis(script) |
| 304 | + |
| 305 | + assert 'This string contains return statement' not in result |
| 306 | + assert 'Another string with return' not in result |
| 307 | + assert 'Template literal with return' not in result |
| 308 | + assert 'return ""' in result # String replaced with empty quotes |
| 309 | + |
| 310 | + def test_is_script_already_function_regular_function(self): |
| 311 | + """Test detection of regular function declaration.""" |
| 312 | + script = 'function() { console.log("test"); }' |
| 313 | + assert is_script_already_function(script) is True |
| 314 | + |
| 315 | + def test_is_script_already_function_arrow_function(self): |
| 316 | + """Test detection of arrow function.""" |
| 317 | + script = '() => { console.log("test"); }' |
| 318 | + assert is_script_already_function(script) is True |
| 319 | + |
| 320 | + def test_is_script_already_function_with_parameters(self): |
| 321 | + """Test detection of function with parameters.""" |
| 322 | + script = 'function(a, b) { return a + b; }' |
| 323 | + assert is_script_already_function(script) is True |
| 324 | + |
| 325 | + def test_is_script_already_function_not_function(self): |
| 326 | + """Test detection when script is not a function.""" |
| 327 | + script = 'console.log("test"); return "value";' |
| 328 | + assert is_script_already_function(script) is False |
| 329 | + |
| 330 | + def test_is_script_already_function_with_whitespace(self): |
| 331 | + """Test detection with leading/trailing whitespace.""" |
| 332 | + script = ' function() { test(); } ' |
| 333 | + assert is_script_already_function(script) is True |
| 334 | + |
| 335 | + def test_has_return_outside_function_simple_return(self): |
| 336 | + """Test detection of simple return statement.""" |
| 337 | + script = 'return document.title;' |
| 338 | + assert has_return_outside_function(script) is True |
| 339 | + |
| 340 | + def test_has_return_outside_function_no_return(self): |
| 341 | + """Test when script has no return statement.""" |
| 342 | + script = 'console.log("test"); var x = 5;' |
| 343 | + assert has_return_outside_function(script) is False |
| 344 | + |
| 345 | + def test_has_return_outside_function_return_inside_function(self): |
| 346 | + """Test when return is inside a function.""" |
| 347 | + script = ''' |
| 348 | + function getTitle() { |
| 349 | + return document.title; |
| 350 | + } |
| 351 | + getTitle(); |
| 352 | + ''' |
| 353 | + assert has_return_outside_function(script) is False |
| 354 | + |
| 355 | + def test_has_return_outside_function_mixed_returns(self): |
| 356 | + """Test with both inside and outside returns.""" |
| 357 | + script = ''' |
| 358 | + function inner() { |
| 359 | + return "inner"; |
| 360 | + } |
| 361 | + return "outer"; |
| 362 | + ''' |
| 363 | + assert has_return_outside_function(script) is True |
| 364 | + |
| 365 | + def test_has_return_outside_function_already_function(self): |
| 366 | + """Test when script is already a function.""" |
| 367 | + script = 'function() { return "test"; }' |
| 368 | + assert has_return_outside_function(script) is False |
| 369 | + |
| 370 | + def test_has_return_outside_function_with_comments(self): |
| 371 | + """Test with comments containing 'return'.""" |
| 372 | + script = ''' |
| 373 | + // This comment has return in it |
| 374 | + var message = "This string has return in it"; |
| 375 | + /* This block comment also has return */ |
| 376 | + return "actual return"; |
| 377 | + ''' |
| 378 | + assert has_return_outside_function(script) is True |
| 379 | + |
| 380 | + def test_has_return_outside_function_nested_braces(self): |
| 381 | + """Test with nested braces and complex structure.""" |
| 382 | + script = ''' |
| 383 | + if (true) { |
| 384 | + var obj = { |
| 385 | + method: function() { |
| 386 | + return "nested"; |
| 387 | + } |
| 388 | + }; |
| 389 | + } |
| 390 | + return "outside"; |
| 391 | + ''' |
| 392 | + assert has_return_outside_function(script) is True |
| 393 | + |
| 394 | + def test_has_return_outside_function_arrow_function(self): |
| 395 | + """Test with arrow function containing return.""" |
| 396 | + script = ''' |
| 397 | + var func = () => { |
| 398 | + return "arrow"; |
| 399 | + }; |
| 400 | + func(); |
| 401 | + ''' |
| 402 | + assert has_return_outside_function(script) is False |
| 403 | + |
0 commit comments