|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Tests for the remote sandbox path resolver in BaseEnvironment.""" |
| 16 | + |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | +from google.adk.environment._base_environment import resolve_sandbox_path |
| 20 | +import pytest |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.parametrize( |
| 24 | + ('path', 'expected'), |
| 25 | + [ |
| 26 | + ('notes.txt', '/workspace/notes.txt'), |
| 27 | + ('sub/nested/file.txt', '/workspace/sub/nested/file.txt'), |
| 28 | + ('./notes.txt', '/workspace/notes.txt'), |
| 29 | + # '..' segments that stay inside the workspace are normalized away. |
| 30 | + ('sub/../notes.txt', '/workspace/notes.txt'), |
| 31 | + ('.', '/workspace'), |
| 32 | + ], |
| 33 | +) |
| 34 | +def test_resolve_sandbox_path_anchors_relative_paths(path, expected): |
| 35 | + assert resolve_sandbox_path(path, '/workspace') == expected |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.parametrize( |
| 39 | + 'path', |
| 40 | + [ |
| 41 | + '..', |
| 42 | + '../outside.txt', |
| 43 | + 'subdir/../../etc/passwd', |
| 44 | + 'a/../../b', |
| 45 | + ], |
| 46 | +) |
| 47 | +def test_resolve_sandbox_path_rejects_relative_escape(path): |
| 48 | + with pytest.raises(ValueError, match='escapes working directory'): |
| 49 | + resolve_sandbox_path(path, '/workspace') |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.parametrize('path', ['/etc/passwd', '/workspace/../../etc/passwd']) |
| 53 | +def test_resolve_sandbox_path_passes_absolute_paths_through(path): |
| 54 | + """Absolute paths follow the BaseEnvironment contract and pass through.""" |
| 55 | + assert resolve_sandbox_path(path, '/workspace') == path |
| 56 | + |
| 57 | + |
| 58 | +def test_resolve_sandbox_path_accepts_pathlike(): |
| 59 | + assert resolve_sandbox_path(Path('notes.txt'), '/workspace') == ( |
| 60 | + '/workspace/notes.txt' |
| 61 | + ) |
0 commit comments