Files
ViewDesignEngine/tests/plugin/upgrade_tool_smoke.cmake

131 lines
5.5 KiB
CMake

# P4-48: vde_plugin_tool upgrade <pkg> --root (verify + semver + atomic upgrade/rollback)
cmake_minimum_required(VERSION 3.20)
if(NOT DEFINED VDE_TOOL OR NOT DEFINED VDE_BINARY_DIR)
message(FATAL_ERROR "upgrade_tool_smoke: missing -DVDE_TOOL/-DVDE_BINARY_DIR")
endif()
function(run_checked)
execute_process(COMMAND ${ARGN} RESULT_VARIABLE _rc OUTPUT_VARIABLE _out ERROR_VARIABLE _err)
if(NOT _rc EQUAL 0)
message(FATAL_ERROR "command failed (${_rc}): ${ARGN}\n${_out}\n${_err}")
endif()
endfunction()
function(run_expect_fail)
execute_process(COMMAND ${ARGN} RESULT_VARIABLE _rc OUTPUT_VARIABLE _out ERROR_VARIABLE _err)
if(_rc EQUAL 0)
message(FATAL_ERROR "expected failure: ${ARGN}\n${_out}")
endif()
endfunction()
set(_root "${VDE_BINARY_DIR}/upgrade_tool_smoke")
set(_old "${_root}/plugin_old")
set(_new "${_root}/plugin_new")
set(_install "${_root}/install")
file(REMOVE_RECURSE "${_root}")
file(MAKE_DIRECTORY "${_old}" "${_new}")
file(WRITE "${_old}/vde-plugin.json" [=[
{
"id": "com.vde.test.upgradable",
"name": "Upgrade Smoke Plugin",
"version": "1.0.0",
"kind": "format",
"abi": { "major": 1, "minor": 3 },
"entry": {"windows": "upgrade_test.dll", "linux": "libupgrade_test.so"},
"capabilities": ["format:upgradable.read"],
"license": "Apache-2.0"
}
]=])
file(WRITE "${_old}/upgrade_test.dll" "OLD-DLL")
file(WRITE "${_old}/data.txt" "old-data")
file(WRITE "${_new}/vde-plugin.json" [=[
{
"id": "com.vde.test.upgradable",
"name": "Upgrade Smoke Plugin",
"version": "1.1.0",
"kind": "format",
"abi": { "major": 1, "minor": 3 },
"entry": {"windows": "upgrade_test.dll", "linux": "libupgrade_test.so"},
"capabilities": ["format:upgradable.read"],
"license": "Apache-2.0"
}
]=])
file(WRITE "${_new}/upgrade_test.dll" "NEW-DLL")
file(WRITE "${_new}/data.txt" "new-data")
run_checked("${VDE_TOOL}" package "${_old}" -o "${_root}/old.vdeplugin")
run_checked("${VDE_TOOL}" package "${_new}" -o "${_root}/new.vdeplugin")
set(_target "${_install}/com.vde.test.upgradable")
# 1) 未安装 -> upgrade 等同 install
run_checked("${VDE_TOOL}" upgrade "${_root}/new.vdeplugin" --root "${_install}")
file(READ "${_target}/vde-plugin.json" _manifest)
string(FIND "${_manifest}" "1.1.0" _pos)
if(_pos LESS 0)
message(FATAL_ERROR "upgrade install did not write 1.1.0")
endif()
file(READ "${_target}/upgrade_test.dll" _dll)
if(NOT _dll STREQUAL "NEW-DLL")
message(FATAL_ERROR "upgrade install wrote wrong dll")
endif()
# 2) 相等版本默认拒绝;--force 重装
run_expect_fail("${VDE_TOOL}" upgrade "${_root}/new.vdeplugin" --root "${_install}")
run_checked("${VDE_TOOL}" upgrade "${_root}/new.vdeplugin" --root "${_install}" --force)
# 3) 降级默认拒绝;--force 允许
run_expect_fail("${VDE_TOOL}" upgrade "${_root}/old.vdeplugin" --root "${_install}")
file(READ "${_target}/vde-plugin.json" _manifest_after_reject)
string(FIND "${_manifest_after_reject}" "1.1.0" _pos_reject)
if(_pos_reject LESS 0)
message(FATAL_ERROR "downgrade rejection modified installed version")
endif()
run_checked("${VDE_TOOL}" upgrade "${_root}/old.vdeplugin" --root "${_install}" --force)
file(READ "${_target}/vde-plugin.json" _manifest_old)
string(FIND "${_manifest_old}" "1.0.0" _pos_old)
if(_pos_old LESS 0)
message(FATAL_ERROR "--force downgrade did not install 1.0.0")
endif()
# 4) 正常升级并检查旧备份清理
run_checked("${VDE_TOOL}" upgrade "${_root}/new.vdeplugin" --root "${_install}")
file(GLOB _leftovers "${_install}/.vde-stage-*" "${_install}/com.vde.test.upgradable.vde-bak-*")
if(_leftovers)
message(FATAL_ERROR "upgrade left staging/backup dirs: ${_leftovers}")
endif()
# 5) 坏包 / 路径逃逸:必须失败且旧版本保留
set(_sha "")
string(SHA256 _sha "X")
set(_bad_archive "VDEPKG1\n{\"id\":\"com.vde.test.upgradable\",\"version\":\"9.9.9\",\"kind\":\"format\",\"files\":1}\nFILE ${_sha} 1 vde-plugin.json\nX")
file(WRITE "${_root}/bad.vdeplugin" "${_bad_archive}")
run_expect_fail("${VDE_TOOL}" upgrade "${_root}/bad.vdeplugin" --root "${_install}")
file(READ "${_target}/vde-plugin.json" _manifest_after_bad)
string(FIND "${_manifest_after_bad}" "1.1.0" _pos_bad)
if(_pos_bad LESS 0)
message(FATAL_ERROR "bad package modified installed version")
endif()
set(_evil_archive "VDEPKG1\n{\"id\":\"com.vde.test.upgradable\",\"version\":\"9.9.9\",\"kind\":\"format\",\"files\":1}\nFILE ${_sha} 1 ../evil.txt\nX")
file(WRITE "${_root}/evil.vdeplugin" "${_evil_archive}")
run_expect_fail("${VDE_TOOL}" upgrade "${_root}/evil.vdeplugin" --root "${_install}")
if(EXISTS "${_install}/../evil.txt" OR EXISTS "${_root}/evil.txt")
message(FATAL_ERROR "path escape created file")
endif()
# 6) --rollback 恢复人工备份
file(COPY "${_old}/" DESTINATION "${_install}/com.vde.test.upgradable.vde-bak-999")
run_checked("${VDE_TOOL}" upgrade "${_root}/new.vdeplugin" --root "${_install}" --rollback)
file(READ "${_target}/vde-plugin.json" _manifest_rollback)
string(FIND "${_manifest_rollback}" "1.0.0" _pos_rollback)
if(_pos_rollback LESS 0)
message(FATAL_ERROR "--rollback did not restore 1.0.0")
endif()
# 7) 用法/无备份错误
run_expect_fail("${VDE_TOOL}" upgrade "${_root}/new.vdeplugin")
file(REMOVE_RECURSE "${_install}/com.vde.test.upgradable.vde-bak-999")
run_expect_fail("${VDE_TOOL}" upgrade "${_root}/new.vdeplugin" --root "${_install}" --rollback)
file(READ "${_target}/vde-plugin.json" _manifest_final)
string(FIND "${_manifest_final}" "1.0.0" _pos_final)
if(_pos_final LESS 0)
message(FATAL_ERROR "failed rollback changed installed version")
endif()
message(STATUS "VDE_PluginToolUpgrade: PASS")