손상 ZIP 복구 표본과 기대 해시 검증을 추가함
- 작성자
- 코노모리 카논
- 작성 시각
- 커밋
384417036d1223fd33fee259fe13c3e6c4354123- 부모
- 23eb52d
변경된 파일 11개
+649-0| @@ -0,0 +1,20 @@ | ||
| 1 | 추가: from pathlib import Path | |
| 2 | 추가: import subprocess | |
| 3 | 추가: import sys | |
| 4 | 추가: import unittest | |
| 5 | 추가: | |
| 6 | 추가: | |
| 7 | 추가: class ZipRecoveryFixtureTests(unittest.TestCase): | |
| 8 | 추가: def test_corpus_and_recovered_output_checks(self): | |
| 9 | 추가: verifier = Path(__file__).resolve().parents[1] / "zip-recovery-fixtures" / "verify.py" | |
| 10 | 추가: result = subprocess.run( | |
| 11 | 추가: [sys.executable, "-B", str(verifier)], | |
| 12 | 추가: capture_output=True, | |
| 13 | 추가: text=True, | |
| 14 | 추가: timeout=30, | |
| 15 | 추가: ) | |
| 16 | 추가: self.assertEqual(result.returncode, 0, result.stdout + result.stderr) | |
| 17 | 추가: | |
| 18 | 추가: | |
| 19 | 추가: if __name__ == "__main__": | |
| 20 | 추가: unittest.main() |
| @@ -0,0 +1,38 @@ | ||
| 1 | 추가: # ZIP 복구 표본 | |
| 2 | 추가: | |
| 3 | 추가: PLANS 8.10의 네 손상 종류를 자체 생성한 작은 ZIP으로 구분함. 정상 대조군 1개와 손상 표본 5개를 포함함. 실제 복구기 구현이나 제품 검증 결과가 아니라 복구 수용 시험용 입력과 기대값임. UI 변경 없음. | |
| 4 | 추가: | |
| 5 | 추가: ## 표본과 기대값 | |
| 6 | 추가: | |
| 7 | 추가: 정상 원본 `control.zip`은 순서대로 `hello.txt`, `한글/자료.txt`, `empty.txt`, `empty-dir/`, `middle.bin`, `after.txt`를 포함함. Store와 Deflate, 한글 경로, 빈 파일과 빈 폴더를 함께 검사함. 항목별 원본 길이, SHA-256, CRC32, 압축 방식과 바이트 위치는 `expectations.json`의 `source_entries`에 기록함. | |
| 8 | 추가: | |
| 9 | 추가: - `missing-directory.zip`: 중앙 디렉터리 시작부터 잘라낸 헤더 손상임. 로컬 헤더와 본문이 완전한 6개 항목을 원본 해시대로 복구해야 함. | |
| 10 | 추가: - `broken-eocd.zip`: EOCD 서명 4바이트만 바꾼 헤더 손상임. 6개 항목을 원본 해시대로 복구해야 함. | |
| 11 | 추가: - `middle-truncated.zip`: 다섯 번째 항목 `middle.bin`의 본문 중간에서 잘라냄. 앞의 4개 항목만 복구하고 `middle.bin`을 정상 복구로 표시하면 안 됨. 뒤의 `after.txt`는 이름과 존재 자체를 손상본에서 관측할 수 없음. | |
| 12 | 추가: - `body-corrupt.zip`: 중앙 디렉터리를 제거하고 `middle.bin` 본문의 1바이트를 뒤집음. CRC가 틀린 `middle.bin`을 제외한 5개 항목을 해시대로 복구해야 함. 뒤의 정상 `after.txt`까지 확인하여 손상 항목 이후의 탐색을 검증함. 중앙 디렉터리 제거는 일반 열기의 실패 후 복구 경로에 도달시키기 위한 별도 손상임. | |
| 13 | 추가: - `unrecoverable.zip`: 첫 로컬 헤더의 20바이트만 남김. 검증 가능한 항목이 없으므로 복구본을 게시하거나 빈 ZIP 생성을 성공으로 표시하면 안 됨. | |
| 14 | 추가: | |
| 15 | 추가: `oracle_states`는 원본을 알고 있는 시험의 판정임. `verified`만 복구본에 포함하고, `truncated`, `integrity_failed`, `not_observable`은 제외함. 이는 작업자 프로토콜의 enum 이름을 제안하는 것이 아님. 특히 중앙 목록이 없는 표본에서 앱이 사라진 항목의 이름이나 정확한 유실 개수를 알아낼 수 있다고 가정하지 않음. 복구된 파일들의 CRC와 해시가 일치해도 원본 전체의 완전성을 증명하지 못하므로 경고를 유지해야 함. | |
| 16 | 추가: | |
| 17 | 추가: 모든 실행은 손상 입력을 그대로 두고 별도 경로에 복구본을 게시해야 함. 실행 전후 입력 SHA-256을 manifest의 값과 비교하고, 검증 실패 항목의 부분 출력이 게시되지 않았는지 검사해야 함. 경고, 종료 상태, 취소, 저장 실패, 링크와 경로 제한, 자원 제한은 이후 실제 작업자 통합 시험에서 별도로 확인해야 함. 이 묶음은 그 검증을 대신하지 않음. | |
| 18 | 추가: | |
| 19 | 추가: ## 실행 | |
| 20 | 추가: | |
| 21 | 추가: 저장소 루트 기준임. 검증은 파일을 풀거나 제품 작업자를 실행하지 않음. | |
| 22 | 추가: | |
| 23 | 추가: ```sh | |
| 24 | 추가: python3 -B tests/zip-recovery-fixtures/generate.py --check | |
| 25 | 추가: python3 -B tests/zip-recovery-fixtures/verify.py | |
| 26 | 추가: python3 -B -m unittest discover -s tests/ci -p test_zip_recovery_fixtures.py | |
| 27 | 추가: ``` | |
| 28 | 추가: | |
| 29 | 추가: 기존 `tests/ci/run_rust.py local`의 Python 시험 탐색에도 연결함. 검증기는 정상본을 Python zipfile로 읽고 손상 연산, 고정 입력 해시, 각 로컬 항목의 CRC와 SHA-256, 불완전한 항목의 배제를 검사함. 로컬 항목 검사는 manifest의 알려진 위치를 사용하므로 범용 복구 알고리즘이나 손상 탐색 성공의 근거가 아님. 생성기는 고정 시간과 순서 및 Python 표준 라이브러리를 사용하며, `--check`는 현재 환경에서 커밋된 바이트의 재현성을 확인함. | |
| 30 | 추가: | |
| 31 | 추가: 실제 작업자가 복구본을 만든 뒤 아래처럼 항목 집합, 파일과 폴더 구분, 해시를 검사할 수 있음. 출력은 지정된 작업 폴더 안에 두어야 함. | |
| 32 | 추가: | |
| 33 | 추가: ```sh | |
| 34 | 추가: python3 -B tests/zip-recovery-fixtures/verify.py \ | |
| 35 | 추가: --recovered missing-directory.zip /home/user/ssd-2/projects/zipelo/tests/work/zip-recovery/recovered.zip | |
| 36 | 추가: ``` | |
| 37 | 추가: | |
| 38 | 추가: 이 명령은 fixture 입력 해시도 확인하지만 작업자의 다른 입력 사본이나 UI 경고까지 검증하지 않음. `control.zip`은 정상 열기의 대조군이며 자동 복구 대상이 아님. 표본 갱신이 필요할 때만 `generate.py`를 `--check` 없이 실행하여 이 폴더의 ZIP과 manifest를 재생성함. |
바이너리 파일이라 Diff를 표시하지 않아요.
파일 내려받기바이너리 파일이라 Diff를 표시하지 않아요.
파일 내려받기바이너리 파일이라 Diff를 표시하지 않아요.
파일 내려받기| @@ -0,0 +1,288 @@ | ||
| 1 | 추가: { | |
| 2 | 추가: "schema_version": 1, | |
| 3 | 추가: "worker_execution_verified": false, | |
| 4 | 추가: "description": "자체 생성 표본임. oracle_states는 원본을 아는 시험의 기대값이며 제품 보고 계약이 아님", | |
| 5 | 추가: "source_entries": [ | |
| 6 | 추가: { | |
| 7 | 추가: "name": "hello.txt", | |
| 8 | 추가: "kind": "file", | |
| 9 | 추가: "bytes": 26, | |
| 10 | 추가: "sha256": "29020da72fa4030c58e0303f558b922f6ed5b6766600e67d79e0abf13971f9da", | |
| 11 | 추가: "crc32": "aecbd261", | |
| 12 | 추가: "method": 0, | |
| 13 | 추가: "local_offset": 0, | |
| 14 | 추가: "data_offset": 39, | |
| 15 | 추가: "compressed_bytes": 26 | |
| 16 | 추가: }, | |
| 17 | 추가: { | |
| 18 | 추가: "name": "한글/자료.txt", | |
| 19 | 추가: "kind": "file", | |
| 20 | 추가: "bytes": 432, | |
| 21 | 추가: "sha256": "3fd2605633caf264a9cf9eb1fc23abc4a60b3191fc808a6a0f71863c64690be5", | |
| 22 | 추가: "crc32": "24d6d829", | |
| 23 | 추가: "method": 8, | |
| 24 | 추가: "local_offset": 65, | |
| 25 | 추가: "data_offset": 112, | |
| 26 | 추가: "compressed_bytes": 37 | |
| 27 | 추가: }, | |
| 28 | 추가: { | |
| 29 | 추가: "name": "empty.txt", | |
| 30 | 추가: "kind": "file", | |
| 31 | 추가: "bytes": 0, | |
| 32 | 추가: "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", | |
| 33 | 추가: "crc32": "00000000", | |
| 34 | 추가: "method": 0, | |
| 35 | 추가: "local_offset": 149, | |
| 36 | 추가: "data_offset": 188, | |
| 37 | 추가: "compressed_bytes": 0 | |
| 38 | 추가: }, | |
| 39 | 추가: { | |
| 40 | 추가: "name": "empty-dir/", | |
| 41 | 추가: "kind": "directory", | |
| 42 | 추가: "bytes": 0, | |
| 43 | 추가: "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", | |
| 44 | 추가: "crc32": "00000000", | |
| 45 | 추가: "method": 0, | |
| 46 | 추가: "local_offset": 188, | |
| 47 | 추가: "data_offset": 228, | |
| 48 | 추가: "compressed_bytes": 0 | |
| 49 | 추가: }, | |
| 50 | 추가: { | |
| 51 | 추가: "name": "middle.bin", | |
| 52 | 추가: "kind": "file", | |
| 53 | 추가: "bytes": 1024, | |
| 54 | 추가: "sha256": "785b0751fc2c53dc14a4ce3d800e69ef9ce1009eb327ccf458afe09c242c26c9", | |
| 55 | 추가: "crc32": "b70b4c26", | |
| 56 | 추가: "method": 0, | |
| 57 | 추가: "local_offset": 228, | |
| 58 | 추가: "data_offset": 268, | |
| 59 | 추가: "compressed_bytes": 1024 | |
| 60 | 추가: }, | |
| 61 | 추가: { | |
| 62 | 추가: "name": "after.txt", | |
| 63 | 추가: "kind": "file", | |
| 64 | 추가: "bytes": 39, | |
| 65 | 추가: "sha256": "7ea5f891c2d1be8db73893862b433f6680e33e11a7246baeea0addd8d296975c", | |
| 66 | 추가: "crc32": "f77deae0", | |
| 67 | 추가: "method": 8, | |
| 68 | 추가: "local_offset": 1292, | |
| 69 | 추가: "data_offset": 1331, | |
| 70 | 추가: "compressed_bytes": 41 | |
| 71 | 추가: } | |
| 72 | 추가: ], | |
| 73 | 추가: "samples": [ | |
| 74 | 추가: { | |
| 75 | 추가: "file": "control.zip", | |
| 76 | 추가: "category": "control", | |
| 77 | 추가: "bytes": 1734, | |
| 78 | 추가: "sha256": "f780e050079a18994de13da479e355312bc5bb23f14320f50f7d862df0184343", | |
| 79 | 추가: "mutations_from_control": [], | |
| 80 | 추가: "expected": { | |
| 81 | 추가: "publish_archive": false, | |
| 82 | 추가: "warning_required": false, | |
| 83 | 추가: "must_not_claim_complete_original": false, | |
| 84 | 추가: "must_preserve_source_sha256": "f780e050079a18994de13da479e355312bc5bb23f14320f50f7d862df0184343", | |
| 85 | 추가: "verified_entries": [ | |
| 86 | 추가: "hello.txt", | |
| 87 | 추가: "한글/자료.txt", | |
| 88 | 추가: "empty.txt", | |
| 89 | 추가: "empty-dir/", | |
| 90 | 추가: "middle.bin", | |
| 91 | 추가: "after.txt" | |
| 92 | 추가: ], | |
| 93 | 추가: "excluded_entries": [], | |
| 94 | 추가: "oracle_states": { | |
| 95 | 추가: "hello.txt": "verified", | |
| 96 | 추가: "한글/자료.txt": "verified", | |
| 97 | 추가: "empty.txt": "verified", | |
| 98 | 추가: "empty-dir/": "verified", | |
| 99 | 추가: "middle.bin": "verified", | |
| 100 | 추가: "after.txt": "verified" | |
| 101 | 추가: } | |
| 102 | 추가: } | |
| 103 | 추가: }, | |
| 104 | 추가: { | |
| 105 | 추가: "file": "missing-directory.zip", | |
| 106 | 추가: "category": "header_damage", | |
| 107 | 추가: "bytes": 1372, | |
| 108 | 추가: "sha256": "e461b3ce3cf1e1147405869c9fae5392897da76c34625f57f2bc67e0082171ba", | |
| 109 | 추가: "mutations_from_control": [ | |
| 110 | 추가: { | |
| 111 | 추가: "operation": "truncate", | |
| 112 | 추가: "offset": 1372 | |
| 113 | 추가: } | |
| 114 | 추가: ], | |
| 115 | 추가: "expected": { | |
| 116 | 추가: "publish_archive": true, | |
| 117 | 추가: "warning_required": true, | |
| 118 | 추가: "must_not_claim_complete_original": true, | |
| 119 | 추가: "must_preserve_source_sha256": "e461b3ce3cf1e1147405869c9fae5392897da76c34625f57f2bc67e0082171ba", | |
| 120 | 추가: "verified_entries": [ | |
| 121 | 추가: "hello.txt", | |
| 122 | 추가: "한글/자료.txt", | |
| 123 | 추가: "empty.txt", | |
| 124 | 추가: "empty-dir/", | |
| 125 | 추가: "middle.bin", | |
| 126 | 추가: "after.txt" | |
| 127 | 추가: ], | |
| 128 | 추가: "excluded_entries": [], | |
| 129 | 추가: "oracle_states": { | |
| 130 | 추가: "hello.txt": "verified", | |
| 131 | 추가: "한글/자료.txt": "verified", | |
| 132 | 추가: "empty.txt": "verified", | |
| 133 | 추가: "empty-dir/": "verified", | |
| 134 | 추가: "middle.bin": "verified", | |
| 135 | 추가: "after.txt": "verified" | |
| 136 | 추가: } | |
| 137 | 추가: } | |
| 138 | 추가: }, | |
| 139 | 추가: { | |
| 140 | 추가: "file": "broken-eocd.zip", | |
| 141 | 추가: "category": "header_damage", | |
| 142 | 추가: "bytes": 1734, | |
| 143 | 추가: "sha256": "e878201237d799ce7a5883552dbb6d2213a7c3c62bc3765123f449577e6bc880", | |
| 144 | 추가: "mutations_from_control": [ | |
| 145 | 추가: { | |
| 146 | 추가: "operation": "replace", | |
| 147 | 추가: "offset": 1712, | |
| 148 | 추가: "hex": "42414421" | |
| 149 | 추가: } | |
| 150 | 추가: ], | |
| 151 | 추가: "expected": { | |
| 152 | 추가: "publish_archive": true, | |
| 153 | 추가: "warning_required": true, | |
| 154 | 추가: "must_not_claim_complete_original": true, | |
| 155 | 추가: "must_preserve_source_sha256": "e878201237d799ce7a5883552dbb6d2213a7c3c62bc3765123f449577e6bc880", | |
| 156 | 추가: "verified_entries": [ | |
| 157 | 추가: "hello.txt", | |
| 158 | 추가: "한글/자료.txt", | |
| 159 | 추가: "empty.txt", | |
| 160 | 추가: "empty-dir/", | |
| 161 | 추가: "middle.bin", | |
| 162 | 추가: "after.txt" | |
| 163 | 추가: ], | |
| 164 | 추가: "excluded_entries": [], | |
| 165 | 추가: "oracle_states": { | |
| 166 | 추가: "hello.txt": "verified", | |
| 167 | 추가: "한글/자료.txt": "verified", | |
| 168 | 추가: "empty.txt": "verified", | |
| 169 | 추가: "empty-dir/": "verified", | |
| 170 | 추가: "middle.bin": "verified", | |
| 171 | 추가: "after.txt": "verified" | |
| 172 | 추가: } | |
| 173 | 추가: } | |
| 174 | 추가: }, | |
| 175 | 추가: { | |
| 176 | 추가: "file": "middle-truncated.zip", | |
| 177 | 추가: "category": "middle_truncation", | |
| 178 | 추가: "bytes": 780, | |
| 179 | 추가: "sha256": "552a613fef94076591c0243cf43e24a576a1b9588cf51522c2bab44160f30eab", | |
| 180 | 추가: "mutations_from_control": [ | |
| 181 | 추가: { | |
| 182 | 추가: "operation": "truncate", | |
| 183 | 추가: "offset": 780 | |
| 184 | 추가: } | |
| 185 | 추가: ], | |
| 186 | 추가: "expected": { | |
| 187 | 추가: "publish_archive": true, | |
| 188 | 추가: "warning_required": true, | |
| 189 | 추가: "must_not_claim_complete_original": true, | |
| 190 | 추가: "must_preserve_source_sha256": "552a613fef94076591c0243cf43e24a576a1b9588cf51522c2bab44160f30eab", | |
| 191 | 추가: "verified_entries": [ | |
| 192 | 추가: "hello.txt", | |
| 193 | 추가: "한글/자료.txt", | |
| 194 | 추가: "empty.txt", | |
| 195 | 추가: "empty-dir/" | |
| 196 | 추가: ], | |
| 197 | 추가: "excluded_entries": [ | |
| 198 | 추가: "middle.bin", | |
| 199 | 추가: "after.txt" | |
| 200 | 추가: ], | |
| 201 | 추가: "oracle_states": { | |
| 202 | 추가: "hello.txt": "verified", | |
| 203 | 추가: "한글/자료.txt": "verified", | |
| 204 | 추가: "empty.txt": "verified", | |
| 205 | 추가: "empty-dir/": "verified", | |
| 206 | 추가: "middle.bin": "truncated", | |
| 207 | 추가: "after.txt": "not_observable" | |
| 208 | 추가: } | |
| 209 | 추가: } | |
| 210 | 추가: }, | |
| 211 | 추가: { | |
| 212 | 추가: "file": "body-corrupt.zip", | |
| 213 | 추가: "category": "payload_corruption", | |
| 214 | 추가: "bytes": 1372, | |
| 215 | 추가: "sha256": "8989c2bb0b7f2d3c05adc2b8c9e5a305ebe8c4c939b7cd1f08b8a906a1c44849", | |
| 216 | 추가: "mutations_from_control": [ | |
| 217 | 추가: { | |
| 218 | 추가: "operation": "truncate", | |
| 219 | 추가: "offset": 1372 | |
| 220 | 추가: }, | |
| 221 | 추가: { | |
| 222 | 추가: "operation": "xor", | |
| 223 | 추가: "offset": 285, | |
| 224 | 추가: "value": 128 | |
| 225 | 추가: } | |
| 226 | 추가: ], | |
| 227 | 추가: "expected": { | |
| 228 | 추가: "publish_archive": true, | |
| 229 | 추가: "warning_required": true, | |
| 230 | 추가: "must_not_claim_complete_original": true, | |
| 231 | 추가: "must_preserve_source_sha256": "8989c2bb0b7f2d3c05adc2b8c9e5a305ebe8c4c939b7cd1f08b8a906a1c44849", | |
| 232 | 추가: "verified_entries": [ | |
| 233 | 추가: "hello.txt", | |
| 234 | 추가: "한글/자료.txt", | |
| 235 | 추가: "empty.txt", | |
| 236 | 추가: "empty-dir/", | |
| 237 | 추가: "after.txt" | |
| 238 | 추가: ], | |
| 239 | 추가: "excluded_entries": [ | |
| 240 | 추가: "middle.bin" | |
| 241 | 추가: ], | |
| 242 | 추가: "oracle_states": { | |
| 243 | 추가: "hello.txt": "verified", | |
| 244 | 추가: "한글/자료.txt": "verified", | |
| 245 | 추가: "empty.txt": "verified", | |
| 246 | 추가: "empty-dir/": "verified", | |
| 247 | 추가: "middle.bin": "integrity_failed", | |
| 248 | 추가: "after.txt": "verified" | |
| 249 | 추가: } | |
| 250 | 추가: } | |
| 251 | 추가: }, | |
| 252 | 추가: { | |
| 253 | 추가: "file": "unrecoverable.zip", | |
| 254 | 추가: "category": "unrecoverable", | |
| 255 | 추가: "bytes": 20, | |
| 256 | 추가: "sha256": "688bb3935cffb5a6686a912a4bccc75b8a99fa63f25b98dc6ddee6cfd9a752d9", | |
| 257 | 추가: "mutations_from_control": [ | |
| 258 | 추가: { | |
| 259 | 추가: "operation": "truncate", | |
| 260 | 추가: "offset": 20 | |
| 261 | 추가: } | |
| 262 | 추가: ], | |
| 263 | 추가: "expected": { | |
| 264 | 추가: "publish_archive": false, | |
| 265 | 추가: "warning_required": true, | |
| 266 | 추가: "must_not_claim_complete_original": true, | |
| 267 | 추가: "must_preserve_source_sha256": "688bb3935cffb5a6686a912a4bccc75b8a99fa63f25b98dc6ddee6cfd9a752d9", | |
| 268 | 추가: "verified_entries": [], | |
| 269 | 추가: "excluded_entries": [ | |
| 270 | 추가: "hello.txt", | |
| 271 | 추가: "한글/자료.txt", | |
| 272 | 추가: "empty.txt", | |
| 273 | 추가: "empty-dir/", | |
| 274 | 추가: "middle.bin", | |
| 275 | 추가: "after.txt" | |
| 276 | 추가: ], | |
| 277 | 추가: "oracle_states": { | |
| 278 | 추가: "hello.txt": "not_observable", | |
| 279 | 추가: "한글/자료.txt": "not_observable", | |
| 280 | 추가: "empty.txt": "not_observable", | |
| 281 | 추가: "empty-dir/": "not_observable", | |
| 282 | 추가: "middle.bin": "not_observable", | |
| 283 | 추가: "after.txt": "not_observable" | |
| 284 | 추가: } | |
| 285 | 추가: } | |
| 286 | 추가: } | |
| 287 | 추가: ] | |
| 288 | 추가: } |
| @@ -0,0 +1,127 @@ | ||
| 1 | 추가: import argparse | |
| 2 | 추가: import hashlib | |
| 3 | 추가: import io | |
| 4 | 추가: import json | |
| 5 | 추가: from pathlib import Path | |
| 6 | 추가: import struct | |
| 7 | 추가: import zipfile | |
| 8 | 추가: | |
| 9 | 추가: | |
| 10 | 추가: ROOT = Path(__file__).resolve().parent | |
| 11 | 추가: SOURCES = [ | |
| 12 | 추가: ("hello.txt", b"Zipelo recovery reference\n", zipfile.ZIP_STORED), | |
| 13 | 추가: ("한글/자료.txt", "복구할 한글 데이터\n".encode("utf-8") * 16, zipfile.ZIP_DEFLATED), | |
| 14 | 추가: ("empty.txt", b"", zipfile.ZIP_STORED), | |
| 15 | 추가: ("empty-dir/", b"", zipfile.ZIP_STORED), | |
| 16 | 추가: ("middle.bin", bytes(range(256)) * 4, zipfile.ZIP_STORED), | |
| 17 | 추가: ("after.txt", b"Intact entry after the damaged payload\n", zipfile.ZIP_DEFLATED), | |
| 18 | 추가: ] | |
| 19 | 추가: | |
| 20 | 추가: | |
| 21 | 추가: def sha256(data): | |
| 22 | 추가: return hashlib.sha256(data).hexdigest() | |
| 23 | 추가: | |
| 24 | 추가: | |
| 25 | 추가: def corpus(): | |
| 26 | 추가: stream = io.BytesIO() | |
| 27 | 추가: with zipfile.ZipFile(stream, "w") as archive: | |
| 28 | 추가: for name, data, method in SOURCES: | |
| 29 | 추가: info = zipfile.ZipInfo(name, date_time=(2020, 1, 2, 3, 4, 6)) | |
| 30 | 추가: info.create_system = 3 | |
| 31 | 추가: info.external_attr = (0o40755 if name.endswith("/") else 0o100644) << 16 | |
| 32 | 추가: if name.endswith("/"): | |
| 33 | 추가: info.external_attr |= 0x10 | |
| 34 | 추가: info.compress_type = method | |
| 35 | 추가: archive.writestr(info, data, compresslevel=6) | |
| 36 | 추가: original = stream.getvalue() | |
| 37 | 추가: with zipfile.ZipFile(io.BytesIO(original)) as archive: | |
| 38 | 추가: directory_offset = archive.start_dir | |
| 39 | 추가: entries = [] | |
| 40 | 추가: for info, (_, data, _) in zip(archive.infolist(), SOURCES): | |
| 41 | 추가: name_size, extra_size = struct.unpack_from("<HH", original, info.header_offset + 26) | |
| 42 | 추가: data_offset = info.header_offset + 30 + name_size + extra_size | |
| 43 | 추가: entries.append({ | |
| 44 | 추가: "name": info.filename, | |
| 45 | 추가: "kind": "directory" if info.is_dir() else "file", | |
| 46 | 추가: "bytes": len(data), | |
| 47 | 추가: "sha256": sha256(data), | |
| 48 | 추가: "crc32": f"{info.CRC:08x}", | |
| 49 | 추가: "method": info.compress_type, | |
| 50 | 추가: "local_offset": info.header_offset, | |
| 51 | 추가: "data_offset": data_offset, | |
| 52 | 추가: "compressed_bytes": info.compress_size, | |
| 53 | 추가: }) | |
| 54 | 추가: middle = entries[4] | |
| 55 | 추가: cut = middle["data_offset"] + middle["compressed_bytes"] // 2 | |
| 56 | 추가: corrupt_offset = middle["data_offset"] + 17 | |
| 57 | 추가: corrupt = bytearray(original[:directory_offset]) | |
| 58 | 추가: corrupt[corrupt_offset] ^= 0x80 | |
| 59 | 추가: bad_eocd = bytearray(original) | |
| 60 | 추가: bad_eocd[-22:-18] = b"BAD!" | |
| 61 | 추가: cases = [ | |
| 62 | 추가: ("control.zip", "control", original, [], ["verified"] * 6), | |
| 63 | 추가: ("missing-directory.zip", "header_damage", original[:directory_offset], | |
| 64 | 추가: [{"operation": "truncate", "offset": directory_offset}], ["verified"] * 6), | |
| 65 | 추가: ("broken-eocd.zip", "header_damage", bytes(bad_eocd), | |
| 66 | 추가: [{"operation": "replace", "offset": len(original) - 22, "hex": "42414421"}], | |
| 67 | 추가: ["verified"] * 6), | |
| 68 | 추가: ("middle-truncated.zip", "middle_truncation", original[:cut], | |
| 69 | 추가: [{"operation": "truncate", "offset": cut}], | |
| 70 | 추가: ["verified"] * 4 + ["truncated", "not_observable"]), | |
| 71 | 추가: ("body-corrupt.zip", "payload_corruption", bytes(corrupt), | |
| 72 | 추가: [{"operation": "truncate", "offset": directory_offset}, | |
| 73 | 추가: {"operation": "xor", "offset": corrupt_offset, "value": 128}], | |
| 74 | 추가: ["verified"] * 4 + ["integrity_failed", "verified"]), | |
| 75 | 추가: ("unrecoverable.zip", "unrecoverable", original[:20], | |
| 76 | 추가: [{"operation": "truncate", "offset": 20}], ["not_observable"] * 6), | |
| 77 | 추가: ] | |
| 78 | 추가: files = {} | |
| 79 | 추가: samples = [] | |
| 80 | 추가: for name, category, data, mutations, states in cases: | |
| 81 | 추가: verified = [entry["name"] for entry, state in zip(entries, states) if state == "verified"] | |
| 82 | 추가: samples.append({ | |
| 83 | 추가: "file": name, | |
| 84 | 추가: "category": category, | |
| 85 | 추가: "bytes": len(data), | |
| 86 | 추가: "sha256": sha256(data), | |
| 87 | 추가: "mutations_from_control": mutations, | |
| 88 | 추가: "expected": { | |
| 89 | 추가: "publish_archive": bool(verified) and category != "control", | |
| 90 | 추가: "warning_required": category != "control", | |
| 91 | 추가: "must_not_claim_complete_original": category != "control", | |
| 92 | 추가: "must_preserve_source_sha256": sha256(data), | |
| 93 | 추가: "verified_entries": verified, | |
| 94 | 추가: "excluded_entries": [entry["name"] for entry, state in zip(entries, states) | |
| 95 | 추가: if state != "verified"], | |
| 96 | 추가: "oracle_states": dict(zip((entry["name"] for entry in entries), states)), | |
| 97 | 추가: }, | |
| 98 | 추가: }) | |
| 99 | 추가: files[name] = data | |
| 100 | 추가: manifest = { | |
| 101 | 추가: "schema_version": 1, | |
| 102 | 추가: "worker_execution_verified": False, | |
| 103 | 추가: "description": "자체 생성 표본임. oracle_states는 원본을 아는 시험의 기대값이며 제품 보고 계약이 아님", | |
| 104 | 추가: "source_entries": entries, | |
| 105 | 추가: "samples": samples, | |
| 106 | 추가: } | |
| 107 | 추가: files["expectations.json"] = (json.dumps(manifest, ensure_ascii=False, indent=4) + "\n").encode() | |
| 108 | 추가: return files | |
| 109 | 추가: | |
| 110 | 추가: | |
| 111 | 추가: def main(): | |
| 112 | 추가: parser = argparse.ArgumentParser() | |
| 113 | 추가: parser.add_argument("--check", action="store_true") | |
| 114 | 추가: args = parser.parse_args() | |
| 115 | 추가: files = corpus() | |
| 116 | 추가: if args.check: | |
| 117 | 추가: for name, data in files.items(): | |
| 118 | 추가: if (ROOT / name).read_bytes() != data: | |
| 119 | 추가: raise SystemExit(f"재생성 불일치: {name}") | |
| 120 | 추가: print(f"재생성 일치: {len(files) - 1}개 ZIP과 기대값") | |
| 121 | 추가: else: | |
| 122 | 추가: for name, data in files.items(): | |
| 123 | 추가: (ROOT / name).write_bytes(data) | |
| 124 | 추가: | |
| 125 | 추가: | |
| 126 | 추가: if __name__ == "__main__": | |
| 127 | 추가: main() |
바이너리 파일이라 Diff를 표시하지 않아요.
파일 내려받기바이너리 파일이라 Diff를 표시하지 않아요.
파일 내려받기바이너리 파일이라 Diff를 표시하지 않아요.
파일 내려받기| @@ -0,0 +1,176 @@ | ||
| 1 | 추가: import argparse | |
| 2 | 추가: import hashlib | |
| 3 | 추가: import io | |
| 4 | 추가: import json | |
| 5 | 추가: from pathlib import Path | |
| 6 | 추가: import struct | |
| 7 | 추가: import unittest | |
| 8 | 추가: import zipfile | |
| 9 | 추가: import zlib | |
| 10 | 추가: | |
| 11 | 추가: from generate import ROOT, SOURCES, corpus | |
| 12 | 추가: | |
| 13 | 추가: | |
| 14 | 추가: MANIFEST = json.loads((ROOT / "expectations.json").read_text(encoding="utf-8")) | |
| 15 | 추가: ENTRIES = {entry["name"]: entry for entry in MANIFEST["source_entries"]} | |
| 16 | 추가: SAMPLES = {sample["file"]: sample for sample in MANIFEST["samples"]} | |
| 17 | 추가: | |
| 18 | 추가: | |
| 19 | 추가: def digest(data): | |
| 20 | 추가: return hashlib.sha256(data).hexdigest() | |
| 21 | 추가: | |
| 22 | 추가: | |
| 23 | 추가: def local_payload(data, entry): | |
| 24 | 추가: offset = entry["local_offset"] | |
| 25 | 추가: if data[offset:offset + 4] != b"PK\x03\x04" or len(data) < offset + 30: | |
| 26 | 추가: raise ValueError("완전한 로컬 헤더 없음") | |
| 27 | 추가: header = struct.unpack_from("<4s5H3I2H", data, offset) | |
| 28 | 추가: method, crc, packed, size, name_size, extra_size = ( | |
| 29 | 추가: header[3], header[6], header[7], header[8], header[9], header[10] | |
| 30 | 추가: ) | |
| 31 | 추가: start = offset + 30 + name_size + extra_size | |
| 32 | 추가: end = start + packed | |
| 33 | 추가: if end > len(data): | |
| 34 | 추가: raise ValueError("본문 절단") | |
| 35 | 추가: payload = data[start:end] | |
| 36 | 추가: if method == zipfile.ZIP_DEFLATED: | |
| 37 | 추가: decoder = zlib.decompressobj(-15) | |
| 38 | 추가: payload = decoder.decompress(payload) + decoder.flush() | |
| 39 | 추가: if not decoder.eof or decoder.unused_data or decoder.unconsumed_tail: | |
| 40 | 추가: raise ValueError("DEFLATE 경계 불일치") | |
| 41 | 추가: elif method != zipfile.ZIP_STORED: | |
| 42 | 추가: raise ValueError("표본에 없는 압축 방법") | |
| 43 | 추가: if len(payload) != size or zlib.crc32(payload) != crc: | |
| 44 | 추가: raise ValueError("본문 크기 또는 CRC 불일치") | |
| 45 | 추가: return payload | |
| 46 | 추가: | |
| 47 | 추가: | |
| 48 | 추가: def verify_recovered(sample_name, recovered): | |
| 49 | 추가: sample = SAMPLES[sample_name] | |
| 50 | 추가: if not sample["expected"]["publish_archive"]: | |
| 51 | 추가: raise ValueError("이 표본은 복구본을 게시하면 안 됨") | |
| 52 | 추가: source = (ROOT / sample_name).read_bytes() | |
| 53 | 추가: if digest(source) != sample["sha256"]: | |
| 54 | 추가: raise ValueError("손상 원본이 변경됨") | |
| 55 | 추가: with zipfile.ZipFile(recovered) as archive: | |
| 56 | 추가: names = archive.namelist() | |
| 57 | 추가: expected = sample["expected"]["verified_entries"] | |
| 58 | 추가: if len(names) != len(set(names)) or set(names) != set(expected): | |
| 59 | 추가: raise ValueError("복구 항목 집합 불일치") | |
| 60 | 추가: for info in archive.infolist(): | |
| 61 | 추가: reference = ENTRIES[info.filename] | |
| 62 | 추가: if info.file_size != reference["bytes"]: | |
| 63 | 추가: raise ValueError("복구 내용 크기 불일치") | |
| 64 | 추가: with archive.open(info) as member: | |
| 65 | 추가: payload = member.read(reference["bytes"] + 1) | |
| 66 | 추가: if info.is_dir() != (reference["kind"] == "directory"): | |
| 67 | 추가: raise ValueError("파일과 폴더 종류 불일치") | |
| 68 | 추가: if len(payload) != reference["bytes"] or digest(payload) != reference["sha256"]: | |
| 69 | 추가: raise ValueError("복구 내용 해시 불일치") | |
| 70 | 추가: | |
| 71 | 추가: | |
| 72 | 추가: class CorpusTests(unittest.TestCase): | |
| 73 | 추가: def test_fixture_hashes_and_categories(self): | |
| 74 | 추가: self.assertEqual({sample["category"] for sample in SAMPLES.values()}, { | |
| 75 | 추가: "control", "header_damage", "middle_truncation", "payload_corruption", "unrecoverable" | |
| 76 | 추가: }) | |
| 77 | 추가: for name, sample in SAMPLES.items(): | |
| 78 | 추가: with self.subTest(name=name): | |
| 79 | 추가: data = (ROOT / name).read_bytes() | |
| 80 | 추가: self.assertEqual(len(data), sample["bytes"]) | |
| 81 | 추가: self.assertEqual(digest(data), sample["sha256"]) | |
| 82 | 추가: self.assertEqual(digest(data), sample["expected"]["must_preserve_source_sha256"]) | |
| 83 | 추가: | |
| 84 | 추가: def test_regeneration_matches_committed_bytes(self): | |
| 85 | 추가: for name, data in corpus().items(): | |
| 86 | 추가: self.assertEqual((ROOT / name).read_bytes(), data, name) | |
| 87 | 추가: | |
| 88 | 추가: def test_control_with_independent_zipfile_reader(self): | |
| 89 | 추가: with zipfile.ZipFile(ROOT / "control.zip") as archive: | |
| 90 | 추가: self.assertIsNone(archive.testzip()) | |
| 91 | 추가: self.assertEqual(archive.namelist(), list(ENTRIES)) | |
| 92 | 추가: for name, expected, _ in SOURCES: | |
| 93 | 추가: self.assertEqual(archive.read(name), expected) | |
| 94 | 추가: self.assertEqual(digest(expected), ENTRIES[name]["sha256"]) | |
| 95 | 추가: | |
| 96 | 추가: def test_exact_damage_operations(self): | |
| 97 | 추가: original = (ROOT / "control.zip").read_bytes() | |
| 98 | 추가: for name, sample in SAMPLES.items(): | |
| 99 | 추가: damaged = bytearray(original) | |
| 100 | 추가: for operation in sample["mutations_from_control"]: | |
| 101 | 추가: offset = operation["offset"] | |
| 102 | 추가: if operation["operation"] == "truncate": | |
| 103 | 추가: del damaged[offset:] | |
| 104 | 추가: elif operation["operation"] == "replace": | |
| 105 | 추가: replacement = bytes.fromhex(operation["hex"]) | |
| 106 | 추가: damaged[offset:offset + len(replacement)] = replacement | |
| 107 | 추가: elif operation["operation"] == "xor": | |
| 108 | 추가: damaged[offset] ^= operation["value"] | |
| 109 | 추가: else: | |
| 110 | 추가: self.fail("알 수 없는 손상 연산") | |
| 111 | 추가: self.assertEqual(bytes(damaged), (ROOT / name).read_bytes(), name) | |
| 112 | 추가: | |
| 113 | 추가: def test_damage_requires_recovery(self): | |
| 114 | 추가: for name, sample in SAMPLES.items(): | |
| 115 | 추가: if sample["category"] == "control": | |
| 116 | 추가: continue | |
| 117 | 추가: with self.subTest(name=name), self.assertRaises(zipfile.BadZipFile): | |
| 118 | 추가: zipfile.ZipFile(ROOT / name) | |
| 119 | 추가: | |
| 120 | 추가: def test_verified_entries_have_intact_local_payloads(self): | |
| 121 | 추가: for name, sample in SAMPLES.items(): | |
| 122 | 추가: data = (ROOT / name).read_bytes() | |
| 123 | 추가: for entry_name in sample["expected"]["verified_entries"]: | |
| 124 | 추가: with self.subTest(sample=name, entry=entry_name): | |
| 125 | 추가: payload = local_payload(data, ENTRIES[entry_name]) | |
| 126 | 추가: self.assertEqual(digest(payload), ENTRIES[entry_name]["sha256"]) | |
| 127 | 추가: | |
| 128 | 추가: def test_excluded_entries_are_not_verified(self): | |
| 129 | 추가: for name, sample in SAMPLES.items(): | |
| 130 | 추가: data = (ROOT / name).read_bytes() | |
| 131 | 추가: for entry_name in sample["expected"]["excluded_entries"]: | |
| 132 | 추가: with self.subTest(sample=name, entry=entry_name), self.assertRaises(ValueError): | |
| 133 | 추가: local_payload(data, ENTRIES[entry_name]) | |
| 134 | 추가: counts = [len(sample["expected"]["verified_entries"]) for sample in SAMPLES.values()] | |
| 135 | 추가: self.assertEqual(counts, [6, 6, 6, 4, 5, 0]) | |
| 136 | 추가: self.assertEqual(SAMPLES["middle-truncated.zip"]["expected"]["oracle_states"]["after.txt"], | |
| 137 | 추가: "not_observable") | |
| 138 | 추가: | |
| 139 | 추가: def test_reference_recovered_output_checker(self): | |
| 140 | 추가: for name, sample in SAMPLES.items(): | |
| 141 | 추가: if not sample["expected"]["publish_archive"]: | |
| 142 | 추가: continue | |
| 143 | 추가: stream = io.BytesIO() | |
| 144 | 추가: with zipfile.ZipFile(stream, "w") as archive: | |
| 145 | 추가: for entry_name, payload, _ in SOURCES: | |
| 146 | 추가: if entry_name in sample["expected"]["verified_entries"]: | |
| 147 | 추가: archive.writestr(entry_name, payload) | |
| 148 | 추가: stream.seek(0) | |
| 149 | 추가: verify_recovered(name, stream) | |
| 150 | 추가: | |
| 151 | 추가: def test_checker_rejects_missing_wrong_and_extra_entries(self): | |
| 152 | 추가: for mutation in ("missing", "wrong", "extra"): | |
| 153 | 추가: stream = io.BytesIO() | |
| 154 | 추가: with zipfile.ZipFile(stream, "w") as archive: | |
| 155 | 추가: for index, (name, payload, _) in enumerate(SOURCES): | |
| 156 | 추가: if mutation == "missing" and index == 0: | |
| 157 | 추가: continue | |
| 158 | 추가: archive.writestr(name, b"wrong" if mutation == "wrong" and index == 0 else payload) | |
| 159 | 추가: if mutation == "extra": | |
| 160 | 추가: archive.writestr("unexpected.txt", b"extra") | |
| 161 | 추가: stream.seek(0) | |
| 162 | 추가: with self.subTest(mutation=mutation), self.assertRaises(ValueError): | |
| 163 | 추가: verify_recovered("missing-directory.zip", stream) | |
| 164 | 추가: with self.assertRaises(ValueError): | |
| 165 | 추가: verify_recovered("unrecoverable.zip", io.BytesIO()) | |
| 166 | 추가: | |
| 167 | 추가: | |
| 168 | 추가: if __name__ == "__main__": | |
| 169 | 추가: parser = argparse.ArgumentParser() | |
| 170 | 추가: parser.add_argument("--recovered", nargs=2, metavar=("SAMPLE", "OUTPUT_ZIP")) | |
| 171 | 추가: args = parser.parse_args() | |
| 172 | 추가: if args.recovered: | |
| 173 | 추가: verify_recovered(args.recovered[0], Path(args.recovered[1])) | |
| 174 | 추가: print("복구본 항목과 해시 일치") | |
| 175 | 추가: else: | |
| 176 | 추가: unittest.main(argv=[__file__], verbosity=2) |