Test chmod command validation.
()
| 125 | |
| 126 | |
| 127 | def test_validate_chmod(): |
| 128 | """Test chmod command validation.""" |
| 129 | print("\nTesting chmod validation:\n") |
| 130 | passed = 0 |
| 131 | failed = 0 |
| 132 | |
| 133 | # Test cases: (command, should_be_allowed, description) |
| 134 | test_cases = [ |
| 135 | # Allowed cases |
| 136 | ("chmod +x init.sh", True, "basic +x"), |
| 137 | ("chmod +x script.sh", True, "+x on any script"), |
| 138 | ("chmod u+x init.sh", True, "user +x"), |
| 139 | ("chmod a+x init.sh", True, "all +x"), |
| 140 | ("chmod ug+x init.sh", True, "user+group +x"), |
| 141 | ("chmod +x file1.sh file2.sh", True, "multiple files"), |
| 142 | # Blocked cases |
| 143 | ("chmod 777 init.sh", False, "numeric mode"), |
| 144 | ("chmod 755 init.sh", False, "numeric mode 755"), |
| 145 | ("chmod +w init.sh", False, "write permission"), |
| 146 | ("chmod +r init.sh", False, "read permission"), |
| 147 | ("chmod -x init.sh", False, "remove execute"), |
| 148 | ("chmod -R +x dir/", False, "recursive flag"), |
| 149 | ("chmod --recursive +x dir/", False, "long recursive flag"), |
| 150 | ("chmod +x", False, "missing file"), |
| 151 | ] |
| 152 | |
| 153 | for cmd, should_allow, description in test_cases: |
| 154 | allowed, reason = validate_chmod_command(cmd) |
| 155 | if allowed == should_allow: |
| 156 | print(f" PASS: {cmd!r} ({description})") |
| 157 | passed += 1 |
| 158 | else: |
| 159 | expected = "allowed" if should_allow else "blocked" |
| 160 | actual = "allowed" if allowed else "blocked" |
| 161 | print(f" FAIL: {cmd!r} ({description})") |
| 162 | print(f" Expected: {expected}, Got: {actual}") |
| 163 | if reason: |
| 164 | print(f" Reason: {reason}") |
| 165 | failed += 1 |
| 166 | |
| 167 | return passed, failed |
| 168 | |
| 169 | |
| 170 | def test_validate_init_script(): |
no test coverage detected