test(phase-0): 确认基线全绿并预置迭代验收测试契约
This commit is contained in:
+421
@@ -0,0 +1,421 @@
|
||||
package com.nanri.aiimage.modules.evidence;
|
||||
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import com.nanri.aiimage.common.security.AdminAuthSupport;
|
||||
import com.nanri.aiimage.common.security.DeviceSessionPolicy;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* AC-7/AC-13/AC-14(R3):证据件上云后的服务端落点与访问面契约。
|
||||
*
|
||||
* <p>Pending Phase 3 激活。Phase 0 预置的判据(接缝名是判据的一部分,改名必须同步改本文件
|
||||
* 并在蓝图 §5 登记):</p>
|
||||
*
|
||||
* <ul>
|
||||
* <li>Flyway {@code V182+} 新增 {@code biz_task_evidence}:task_id/uid/evidence_type/source/
|
||||
* object_key/byte_size/content_type/sha256/created_at,按 task_id 关联但**不建外键**;</li>
|
||||
* <li>{@code application.yml} 的 {@code evidence:} 块显式落 bucket(独立桶,不得为
|
||||
* nanri-ai-images)、retention-days(必须有数字默认值)、key-prefix;</li>
|
||||
* <li>证据读取端点落在 {@code /api/admin/task-evidence/**}(files/content/download/delete),
|
||||
* 每个方法都走私有 {@code requireSuperAdmin(HttpServletRequest)}(对齐
|
||||
* {@code AdminDeviceLogController:167-175});</li>
|
||||
* <li>守卫行为三样本:无 token/bad token → 401、非超管 → 403、超管 → 放行;伪造 uid 由
|
||||
* files 端点的 uid 归属校验兜住(源码判据);</li>
|
||||
* <li>证据模块日志零命中 presign / 直链(AC-13);对象 key 带 UUID(铁律 10)。</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>四样本中的「合法 JWT 200」在 Phase 3 由真机/集成用例补齐(本文件钉守卫放行分支,
|
||||
* 蓝图 Phase 3 准出另见 {@code 鉴权四样本真机留痕})。</p>
|
||||
*/
|
||||
@Disabled("Pending Phase 3: 证据件上云与服务端落点(AC-7/AC-13/AC-14)")
|
||||
class EvidencePhase3ContractTest {
|
||||
|
||||
private static final Path MODULE = Path.of("").toAbsolutePath();
|
||||
private static final Path MAIN_JAVA = MODULE.resolve("src/main/java");
|
||||
private static final Path DB_DIR = MODULE.resolve("src/main/resources/db");
|
||||
private static final Path APP_YML = MODULE.resolve("src/main/resources/application.yml");
|
||||
private static final String CONTROLLER_FQCN =
|
||||
"com.nanri.aiimage.modules.evidence.controller.AdminTaskEvidenceController";
|
||||
private static final String EVIDENCE_PATH = "/api/admin/task-evidence";
|
||||
|
||||
@Test
|
||||
void 迁移必须新增证据表且字段与索引齐全() throws IOException {
|
||||
List<Path> migrations = new ArrayList<>();
|
||||
try (Stream<Path> stream = Files.list(DB_DIR)) {
|
||||
stream.filter(path -> path.getFileName().toString().matches("V\\d+__.*\\.sql"))
|
||||
.filter(path -> versionOf(path.getFileName().toString()) >= 182)
|
||||
.forEach(migrations::add);
|
||||
}
|
||||
assertFalse(migrations.isEmpty(), "AC-7:本迭代必须新增 V182+ 迁移(当前最高 V181)");
|
||||
|
||||
StringBuilder joined = new StringBuilder();
|
||||
for (Path migration : migrations) {
|
||||
joined.append(read(migration)).append('\n');
|
||||
}
|
||||
String all = joined.toString();
|
||||
Matcher table = Pattern.compile("(?is)create\\s+table[^;]*biz_task_evidence[^;]*;")
|
||||
.matcher(all);
|
||||
assertTrue(table.find(), "AC-7:V182+ 必须建 biz_task_evidence 表(蓝图 §3.3)");
|
||||
String ddl = table.group();
|
||||
for (String column : List.of("task_id", "uid", "evidence_type", "source", "object_key",
|
||||
"byte_size", "content_type", "sha256", "created_at")) {
|
||||
assertTrue(Pattern.compile("(?i)\\b" + column + "\\b").matcher(ddl).find(),
|
||||
"AC-7:证据表缺列 " + column);
|
||||
}
|
||||
assertFalse(ddl.toUpperCase().contains("FOREIGN KEY"),
|
||||
"AC-7:证据表按 task_id 关联但不建外键(全库唯一 FK 是 V80:47)");
|
||||
assertTrue(Pattern.compile("(?i)(index|key)\\s+\\w+\\s*\\(\\s*task_id").matcher(ddl).find(),
|
||||
"AC-7:必须有 task_id 索引(按任务关联查询)");
|
||||
assertTrue(Pattern.compile("(?i)(index|key)\\s+\\w+\\s*\\(\\s*uid").matcher(ddl).find(),
|
||||
"AC-7:必须有 uid 索引(数据范围/清理)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 证据桶与保留期必须显式落在applicationYml() throws IOException {
|
||||
String yaml = read(APP_YML);
|
||||
String block = yamlBlock(yaml, "evidence");
|
||||
assertNotNull(block, "AC-13:application.yml 必须有 evidence: 配置块(不许只落代码默认值)");
|
||||
|
||||
String bucket = scalar(block, "bucket");
|
||||
assertNotNull(bucket, "AC-13:必须配置证据桶");
|
||||
String bucketDefault = defaultValue(bucket);
|
||||
assertNotNull(bucketDefault, "AC-13:证据桶不得只有变量无默认值(部署漏配即隐形)");
|
||||
assertFalse(bucketDefault.contains("nanri-ai-images"),
|
||||
"AC-13:证据必须落独立桶,不得复用业务桶 nanri-ai-images");
|
||||
|
||||
String retention = scalar(block, "retention-days");
|
||||
assertNotNull(retention, "AC-13:必须配置保留天数");
|
||||
String retentionDefault = defaultValue(retention);
|
||||
assertNotNull(retentionDefault, "AC-13:保留天数必须有确定的数字默认值(天数要进判据)");
|
||||
assertTrue(Integer.parseInt(retentionDefault) >= 1, "AC-13:保留天数必须为正整数");
|
||||
|
||||
assertNotNull(scalar(block, "key-prefix"), "AC-13:必须配置对象 key 前缀");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 证据读取端点必须全部走super访问面() throws IOException {
|
||||
Path controller = evidenceController();
|
||||
String source = read(controller);
|
||||
assertTrue(source.contains(EVIDENCE_PATH),
|
||||
"AC-13:证据读取控制器必须落在 " + EVIDENCE_PATH);
|
||||
|
||||
List<String> bodies = mappedMethodBodies(source);
|
||||
assertTrue(bodies.size() >= 4,
|
||||
"AC-13:证据读取至少要有 files/content/download/delete 四个端点,实扫 " + bodies.size());
|
||||
for (String body : bodies) {
|
||||
assertTrue(body.contains("requireSuperAdmin"),
|
||||
"AC-13:证据读取端点必须逐个走 requireSuperAdmin(非 super 一律 403)\n" + body);
|
||||
}
|
||||
assertTrue(source.contains("isSuper"),
|
||||
"AC-13:超管判定必须复用 DeviceSessionPolicy.isSuper 单出口");
|
||||
assertFalse(source.toLowerCase().contains("permitall"),
|
||||
"AC-13:证据读取端点不得 permitAll");
|
||||
|
||||
for (String endpoint : List.of("\"/files\"", "\"/content\"", "\"/download\"", "\"/delete\"")) {
|
||||
assertTrue(source.contains(endpoint), "AC-13:缺端点 " + endpoint);
|
||||
}
|
||||
|
||||
String filesBody = bodies.get(0);
|
||||
assertTrue(filesBody.contains("uid"),
|
||||
"AC-13:列表端点必须支持按 uid 收窄(伪造 uid 的归属校验入口)");
|
||||
assertTrue(filesBody.contains("operator") || filesBody.contains("getId"),
|
||||
"AC-13:uid 过滤必须与当前操作者归属对撞(防伪造 uid 越权读)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 守卫行为三样本_无token坏token401_非超管403_超管放行() throws Exception {
|
||||
Object controller = newEvidenceController();
|
||||
AdminAuthSupport auth = authSupportOf(controller);
|
||||
Method guard = superAdminGuard(controller.getClass());
|
||||
HttpServletRequest request = mock(HttpServletRequest.class);
|
||||
|
||||
when(auth.requireAdmin(any(HttpServletRequest.class)))
|
||||
.thenThrow(new BusinessException(401, "未登录"));
|
||||
Throwable noToken = invoke(guard, controller, request);
|
||||
assertInstanceOf(BusinessException.class, noToken, "AC-13:无 token 必须 401");
|
||||
assertEquals(Integer.valueOf(401), ((BusinessException) noToken).getCode());
|
||||
|
||||
when(auth.requireAdmin(any(HttpServletRequest.class)))
|
||||
.thenThrow(new BusinessException(401, "登录已过期,请重新登录"));
|
||||
Throwable badToken = invoke(guard, controller, request);
|
||||
assertInstanceOf(BusinessException.class, badToken, "AC-13:坏 token 必须 401");
|
||||
assertEquals(Integer.valueOf(401), ((BusinessException) badToken).getCode());
|
||||
|
||||
AdminUserEntity normalAdmin = new AdminUserEntity();
|
||||
normalAdmin.setId(2L);
|
||||
normalAdmin.setUsername("ops-admin");
|
||||
normalAdmin.setRole("admin");
|
||||
normalAdmin.setIsAdmin(1);
|
||||
when(auth.requireAdmin(any(HttpServletRequest.class))).thenReturn(normalAdmin);
|
||||
when(auth.currentRole(normalAdmin)).thenReturn("admin");
|
||||
Throwable forbidden = invoke(guard, controller, request);
|
||||
assertInstanceOf(BusinessException.class, forbidden, "AC-13:非超管必须 403");
|
||||
assertEquals(Integer.valueOf(403), ((BusinessException) forbidden).getCode());
|
||||
|
||||
AdminUserEntity superAdmin = new AdminUserEntity();
|
||||
superAdmin.setId(1L);
|
||||
superAdmin.setUsername("root");
|
||||
superAdmin.setRole(DeviceSessionPolicy.SUPER_ADMIN);
|
||||
superAdmin.setIsAdmin(1);
|
||||
when(auth.requireAdmin(any(HttpServletRequest.class))).thenReturn(superAdmin);
|
||||
when(auth.currentRole(superAdmin)).thenReturn(DeviceSessionPolicy.SUPER_ADMIN);
|
||||
Object returned = guard.invoke(controller, request);
|
||||
assertSame(superAdmin, returned, "AC-13:超管必须放行并返回操作者");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 登记必须按taskId关联且对象key带uuid() throws IOException {
|
||||
String all = evidenceSources();
|
||||
assertTrue(Pattern.compile("(?i)task[_-]?id").matcher(all).find(),
|
||||
"AC-7:证据登记/落表必须按 taskId 关联");
|
||||
assertTrue(all.contains("objectKey") || all.contains("object_key"),
|
||||
"AC-7:登记必须记录 objectKey");
|
||||
assertTrue(all.contains("UUID"),
|
||||
"AC-13(铁律 10):对象 key 必须带 UUID");
|
||||
assertTrue(all.contains("sha256") || all.contains("SHA-256"),
|
||||
"AC-7:登记必须带摘要(防篡改/去重)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 证据模块日志零命中presign与直链() throws IOException {
|
||||
List<Path> files = new ArrayList<>();
|
||||
try (Stream<Path> stream = Files.walk(MAIN_JAVA)) {
|
||||
stream.filter(path -> path.toString().endsWith(".java"))
|
||||
.filter(EvidencePhase3ContractTest::isEvidenceSource)
|
||||
.forEach(files::add);
|
||||
}
|
||||
assertFalse(files.isEmpty(), "AC-13:未找到证据模块源码(命名/落点未按契约)");
|
||||
List<String> leaks = new ArrayList<>();
|
||||
for (Path file : files) {
|
||||
for (String line : read(file).split("\\R")) {
|
||||
String lower = line.toLowerCase();
|
||||
boolean logLine = lower.contains("log.info") || lower.contains("log.warn")
|
||||
|| lower.contains("log.error") || lower.contains("log.debug");
|
||||
boolean urlLike = lower.contains("presign") || lower.contains("uploadurl")
|
||||
|| lower.contains("fileurl") || lower.contains("http://")
|
||||
|| lower.contains("https://");
|
||||
if (logLine && urlLike) {
|
||||
leaks.add(file.getFileName() + ": " + line.trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
assertTrue(leaks.isEmpty(), "AC-13:证据模块日志输出了 presign/直链:\n" + String.join("\n", leaks));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- helpers
|
||||
|
||||
private static Path evidenceController() throws IOException {
|
||||
List<Path> hits = new ArrayList<>();
|
||||
try (Stream<Path> stream = Files.walk(MAIN_JAVA)) {
|
||||
stream.filter(path -> path.toString().endsWith(".java"))
|
||||
.filter(path -> {
|
||||
try {
|
||||
return read(path).contains(EVIDENCE_PATH);
|
||||
} catch (IOException ex) {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
.forEach(hits::add);
|
||||
}
|
||||
assertFalse(hits.isEmpty(),
|
||||
"AC-13:未找到 " + EVIDENCE_PATH + " 的控制器源码(Phase 3 落点未按契约)");
|
||||
return hits.get(0);
|
||||
}
|
||||
|
||||
private static boolean isEvidenceSource(Path path) {
|
||||
try {
|
||||
String source = read(path);
|
||||
return source.contains("task-evidence") || source.contains("TaskEvidence")
|
||||
|| source.contains("biz_task_evidence");
|
||||
} catch (IOException ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static String evidenceSources() throws IOException {
|
||||
List<Path> files = new ArrayList<>();
|
||||
try (Stream<Path> stream = Files.walk(MAIN_JAVA)) {
|
||||
stream.filter(path -> path.toString().endsWith(".java"))
|
||||
.filter(EvidencePhase3ContractTest::isEvidenceSource)
|
||||
.forEach(files::add);
|
||||
}
|
||||
assertFalse(files.isEmpty(), "AC-7:未找到证据模块源码");
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (Path file : files) {
|
||||
builder.append(read(file)).append('\n');
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
private static Object newEvidenceController() throws Exception {
|
||||
Class<?> controllerClass = Class.forName(CONTROLLER_FQCN);
|
||||
Constructor<?> constructor = Arrays.stream(controllerClass.getDeclaredConstructors())
|
||||
.max(Comparator.comparingInt(candidate -> candidate.getParameterCount()))
|
||||
.orElseThrow();
|
||||
Class<?>[] types = constructor.getParameterTypes();
|
||||
Object[] args = new Object[types.length];
|
||||
for (int index = 0; index < types.length; index++) {
|
||||
if (types[index].isPrimitive()) {
|
||||
fail("AC-13:控制器构造器不应有原始类型参数: " + types[index]);
|
||||
}
|
||||
args[index] = AdminAuthSupport.class.isAssignableFrom(types[index])
|
||||
? mock(AdminAuthSupport.class)
|
||||
: mock(types[index]);
|
||||
}
|
||||
constructor.setAccessible(true);
|
||||
return constructor.newInstance(args);
|
||||
}
|
||||
|
||||
private static AdminAuthSupport authSupportOf(Object controller) throws IllegalAccessException {
|
||||
for (java.lang.reflect.Field field : controller.getClass().getDeclaredFields()) {
|
||||
if (AdminAuthSupport.class.isAssignableFrom(field.getType())) {
|
||||
field.setAccessible(true);
|
||||
return (AdminAuthSupport) field.get(controller);
|
||||
}
|
||||
}
|
||||
fail("AC-13:证据控制器必须注入 AdminAuthSupport(与 AdminDeviceLogController 同口径)");
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Method superAdminGuard(Class<?> controllerClass) {
|
||||
for (Method method : controllerClass.getDeclaredMethods()) {
|
||||
if (method.getName().equals("requireSuperAdmin")
|
||||
&& method.getParameterCount() == 1
|
||||
&& method.getParameterTypes()[0].isAssignableFrom(HttpServletRequest.class)) {
|
||||
method.setAccessible(true);
|
||||
return method;
|
||||
}
|
||||
}
|
||||
throw new AssertionError(
|
||||
"AC-13:证据控制器必须含私有 requireSuperAdmin(HttpServletRequest)(对齐 AdminDeviceLogController:167-175)");
|
||||
}
|
||||
|
||||
private static Throwable invoke(Method method, Object target, Object... args) throws Exception {
|
||||
try {
|
||||
method.invoke(target, args);
|
||||
return null;
|
||||
} catch (InvocationTargetException ex) {
|
||||
return ex.getTargetException();
|
||||
}
|
||||
}
|
||||
|
||||
private static List<String> mappedMethodBodies(String source) {
|
||||
String[] lines = source.split("\\R");
|
||||
Pattern mapping = Pattern.compile("@(Get|Post|Put|Delete|Patch)Mapping");
|
||||
List<String> bodies = new ArrayList<>();
|
||||
for (int index = 0; index < lines.length; index++) {
|
||||
if (!mapping.matcher(lines[index]).find()) {
|
||||
continue;
|
||||
}
|
||||
int cursor = index;
|
||||
while (cursor < lines.length && !lines[cursor].contains("{") && !lines[cursor].contains(";")) {
|
||||
cursor++;
|
||||
}
|
||||
if (cursor >= lines.length || lines[cursor].contains(";")) {
|
||||
continue;
|
||||
}
|
||||
int depth = 0;
|
||||
StringBuilder body = new StringBuilder();
|
||||
for (int line = cursor; line < lines.length; line++) {
|
||||
for (char ch : lines[line].toCharArray()) {
|
||||
if (ch == '{') {
|
||||
depth++;
|
||||
} else if (ch == '}') {
|
||||
depth--;
|
||||
}
|
||||
}
|
||||
body.append(lines[line]).append('\n');
|
||||
if (depth == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
bodies.add(body.toString());
|
||||
index = cursor;
|
||||
}
|
||||
return bodies;
|
||||
}
|
||||
|
||||
private static String yamlBlock(String yaml, String key) {
|
||||
String[] lines = yaml.split("\\R");
|
||||
Pattern header = Pattern.compile("^(\\s*)" + Pattern.quote(key) + ":\\s*$");
|
||||
int start = -1;
|
||||
int indent = -1;
|
||||
for (int index = 0; index < lines.length; index++) {
|
||||
Matcher matcher = header.matcher(lines[index]);
|
||||
if (matcher.matches()) {
|
||||
start = index;
|
||||
indent = matcher.group(1).length();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (start < 0) {
|
||||
return null;
|
||||
}
|
||||
StringBuilder block = new StringBuilder();
|
||||
for (int index = start + 1; index < lines.length; index++) {
|
||||
String line = lines[index];
|
||||
if (line.trim().isEmpty() || line.trim().startsWith("#")) {
|
||||
block.append(line).append('\n');
|
||||
continue;
|
||||
}
|
||||
int currentIndent = line.length() - line.replaceAll("^\\s+", "").length();
|
||||
if (currentIndent <= indent) {
|
||||
break;
|
||||
}
|
||||
block.append(line).append('\n');
|
||||
}
|
||||
return block.toString();
|
||||
}
|
||||
|
||||
private static String scalar(String block, String key) {
|
||||
Matcher matcher = Pattern.compile("(?m)^\\s*" + Pattern.quote(key) + ":\\s*(\\S.*)$")
|
||||
.matcher(block);
|
||||
return matcher.find() ? matcher.group(1).trim() : null;
|
||||
}
|
||||
|
||||
private static String defaultValue(String value) {
|
||||
Matcher env = Pattern.compile("^\\$\\{[^:}]+:([^}]*)\\}$").matcher(value);
|
||||
if (env.matches()) {
|
||||
String fallback = env.group(1).trim();
|
||||
return fallback.isEmpty() ? null : fallback;
|
||||
}
|
||||
return value.startsWith("${") ? null : value;
|
||||
}
|
||||
|
||||
private static long versionOf(String fileName) {
|
||||
Matcher matcher = Pattern.compile("^V(\\d+)__").matcher(fileName);
|
||||
return matcher.find() ? Long.parseLong(matcher.group(1)) : -1;
|
||||
}
|
||||
|
||||
private static String read(Path path) throws IOException {
|
||||
return Files.readString(path);
|
||||
}
|
||||
}
|
||||
+134
@@ -0,0 +1,134 @@
|
||||
package com.nanri.aiimage.modules.file.service;
|
||||
|
||||
import com.nanri.aiimage.config.StorageProperties;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.FileTime;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* AC-10(R5):stale 分片会话必须由周期任务回收,不能等人 init 时顺手清。
|
||||
*
|
||||
* <p>Pending Phase 4 激活。今天 {@code ChunkedUploadService.cleanupStaleSessions()} 的
|
||||
* 唯一调用点在 {@code init()} 里(:86),没有任何 {@code @Scheduled};断掉的上传会永久占盘,
|
||||
* 而 {@code LocalTempCleanupService} 的射程只有 source/result/transientPayload,
|
||||
* {@code chunk-uploads} 不在其列。</p>
|
||||
*
|
||||
* <p>接缝契约(改名必须同步改本文件并登记蓝图 §5):</p>
|
||||
* <ul>
|
||||
* <li>回收入口挂在 {@link ChunkedUploadService} 上,方法名不限,但必须有
|
||||
* {@link Scheduled} 注解——**摘掉注解即判红**(本文件第一条断言就是它);</li>
|
||||
* <li>「无人 init 也会清」:本用例先造 stale + fresh 两个会话,回拨 stale 的目录时间后
|
||||
* <b>不再 init</b>,直接反射调用被注解的周期方法;</li>
|
||||
* <li>近 N 分钟有片写入的会话不得误清(fresh 会话必须原样保留)。</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>分片会话只有盘上 meta/part,无 DB 行;证据表的 DB 行保留期判据见
|
||||
* {@code EvidencePhase3ContractTest}(AC-13 配置块)。</p>
|
||||
*/
|
||||
@Disabled("Pending Phase 4: 服务端残骸回收补洞(AC-10)")
|
||||
class ChunkedUploadReclaimContractTest {
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
private ChunkedUploadService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
StorageProperties properties = new StorageProperties();
|
||||
properties.setLocalTempDir(tempDir.toString());
|
||||
properties.setCleanupEnabled(true);
|
||||
service = new ChunkedUploadService(properties);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 无人init也必须由周期任务回收stale会话() throws Exception {
|
||||
List<Method> scheduled = scheduledMethods();
|
||||
assertFalse(scheduled.isEmpty(),
|
||||
"AC-10:ChunkedUploadService 上必须挂 @Scheduled 回收方法(摘掉注解即判红)");
|
||||
|
||||
ChunkedUploadService.ChunkSession stale = service.init("stale.bin", 2, 10, "evidence");
|
||||
ChunkedUploadService.ChunkSession fresh = service.init("fresh.bin", 2, 10, "evidence");
|
||||
Path staleDir = sessionDir(stale.uploadId());
|
||||
Path freshDir = sessionDir(fresh.uploadId());
|
||||
assertTrue(Files.isDirectory(staleDir));
|
||||
|
||||
// 回拨 stale 的目录时间(cleanupStaleSessions 以目录 mtime 判龄),fresh 保持当前时间
|
||||
Instant old = Instant.now().minus(30, ChronoUnit.HOURS);
|
||||
Files.setLastModifiedTime(staleDir, FileTime.from(old));
|
||||
|
||||
for (Method method : scheduled) {
|
||||
method.setAccessible(true);
|
||||
try {
|
||||
method.invoke(service);
|
||||
} catch (InvocationTargetException ex) {
|
||||
throw new AssertionError("AC-10:周期回收方法自身抛异常", ex.getTargetException());
|
||||
}
|
||||
}
|
||||
|
||||
assertFalse(Files.exists(staleDir),
|
||||
"AC-10:stale 会话仍在盘上(今天只有 init 时才清,断掉的上传永久占盘)");
|
||||
assertTrue(Files.isDirectory(freshDir),
|
||||
"AC-10:近 N 分钟有写入的会话被误清(用户大档会从 0 重传)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 统一清理必须覆盖chunkUploads目录() throws Exception {
|
||||
Path chunkDir = tempDir.resolve("chunk-uploads").resolve("c".repeat(32));
|
||||
Files.createDirectories(chunkDir);
|
||||
Path part = chunkDir.resolve("part-0");
|
||||
Files.writeString(part, "x");
|
||||
Instant old = Instant.now().minus(10, ChronoUnit.DAYS);
|
||||
setTimes(part, old);
|
||||
setTimes(chunkDir, old);
|
||||
|
||||
StorageProperties properties = new StorageProperties();
|
||||
properties.setLocalTempDir(tempDir.toString());
|
||||
properties.setCleanupEnabled(true);
|
||||
properties.setSourceRetentionHours(24);
|
||||
properties.setResultRetentionHours(24);
|
||||
properties.setTransientPayloadRetentionHours(72);
|
||||
new LocalTempCleanupService(properties).cleanupLocalTempDir();
|
||||
|
||||
assertFalse(Files.exists(chunkDir),
|
||||
"AC-10:chunk-uploads 残骸未纳入统一清理(LocalTempCleanupService 射程外的洞)");
|
||||
}
|
||||
|
||||
private List<Method> scheduledMethods() {
|
||||
List<Method> methods = new ArrayList<>();
|
||||
for (Class<?> type = ChunkedUploadService.class; type != null && type != Object.class;
|
||||
type = type.getSuperclass()) {
|
||||
Arrays.stream(type.getDeclaredMethods())
|
||||
.filter(method -> method.isAnnotationPresent(Scheduled.class))
|
||||
.forEach(methods::add);
|
||||
}
|
||||
return methods;
|
||||
}
|
||||
|
||||
private Path sessionDir(String uploadId) {
|
||||
return tempDir.resolve("chunk-uploads").resolve(uploadId);
|
||||
}
|
||||
|
||||
private static void setTimes(Path path, Instant instant) throws Exception {
|
||||
FileTime time = FileTime.from(instant);
|
||||
Files.getFileAttributeView(path,
|
||||
java.nio.file.attribute.BasicFileAttributeView.class)
|
||||
.setTimes(time, time, null);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user