5C DB audit 實作測試案例與停止線稽核表

返回文件索引

本文件承接 5C DB audit 邊界分析5C DB audit 實作前確認表5C DB audit 程式 repo 結構檢查與實作前盤點

此表只整理 5C 第一批程式實作後應驗證的測試案例、掃描項目與驗收證據。使用者尚未確認 5C 七項前,不修改程式 repo。

0. 文件狀態

項目內容
文件狀態已建立,等待 5C 七項確認後使用
建立日期2026-06-07
對應節點第五階段 5C DB audit
程式 repohs-device-control-template
程式 branchpoc/nmodbus-tcp
程式基準1f635d6
是否授權程式修改尚未;需等待 5C 七項皆同意

1. 5C 第一批實作範圍

類型預計檔案驗收重點
Audit modelsrc/HS.DeviceControl.Application/Auditing/AuditEventRecord.cs只保存安全摘要欄位,不保存 raw payload 或敏感資訊。
Audit writer contractsrc/HS.DeviceControl.Application/Auditing/IAuditEventWriter.cs建立 Application 層 contract,不綁定 MySQL、DDL 或正式 DB。
Fake writersrc/HS.DeviceControl.Application/Auditing/InMemoryAuditEventWriter.cs僅供測試與本機流程驗證,不寫入真實 DB。
Error codessrc/HS.DeviceControl.Application/Common/ApplicationErrorCodes.cs補足 audit request invalid、sensitive content rejected、writer failed 等標準錯誤碼。
Teststests/HS.DeviceControl.Application.Tests覆蓋安全欄位、敏感資訊阻擋、fake writer 行為與停止線。

本批不建立 MySQL writer、不新增 audit table、不執行 DDL / ALTER TABLE / formal Apply,也不保存 connection string、secret、token、正式 IP、正式 port、完整路徑或正式帳號。

2. 必要測試案例

#測試主題建議測試名稱預期結果
1建立安全 audit eventAuditEventRecord_ShouldKeepSafeSummaryFields保留 event type、subject、decision、correlation id、operator summary、UTC 建立時間與 message summary。
2必填欄位防呆AuditEventRecord_ShouldRejectMissingRequiredFields缺少 event type、subject、decision 或 correlation id 時回傳標準錯誤或丟出明確例外。
3敏感資訊阻擋AuditEventRecord_ShouldRejectSensitiveContentsecret、token、connection string、完整路徑、正式 IP、正式 port、raw payload 不得進入 audit record。
4fake writer 成功寫入InMemoryAuditEventWriter_ShouldStoreSafeEventssafe audit event 可寫入 in-memory collection,並回傳成功結果。
5fake writer 拒絕無效資料InMemoryAuditEventWriter_ShouldRejectInvalidEventsnull 或不完整 event 不得被保存,需回傳標準錯誤。
6fake writer 不外洩內部集合InMemoryAuditEventWriter_ShouldReturnReadOnlySnapshot查詢結果不可讓呼叫端直接改動 writer 內部狀態。
7不依賴 MySQLAuditWriterContract_ShouldNotDependOnMySqlInfrastructureApplication audit contract 不引用 MySQL infrastructure 型別。
8停止線掃描AuditImplementation_ShouldNotIntroduceDbOrServiceBoundaries未出現 DB DDL、真實 DB 寫入、外部 DLL 載入、auth / Swagger 或 Windows Service 擴張。

3. 必跑測試命令

驗證項目命令預期
Application testsdotnet test tests\HS.DeviceControl.Application.Tests\HS.DeviceControl.Application.Tests.csproj --no-restore全部通過,需包含 5C audit tests。
Infrastructure.MySql testsdotnet test tests\HS.DeviceControl.Infrastructure.MySql.Tests\HS.DeviceControl.Infrastructure.MySql.Tests.csproj --no-restore既有 MySQL 邊界不被破壞。
Solution testsdotnet test HS.DeviceControl.sln --no-restore全 solution 通過。
Commit 前檢查git diff --checkgit diff --cached --check無 whitespace / patch 格式問題。

.NET 5.0 EOL warning 若只來自既有 target framework,可記錄為既有限制;不得把測試失敗視為可忽略警告。

4. 停止線掃描

停止線掃描方向不得出現
DB DDL / schema 變更掃描 srctestssamplesdocs 的 5C 變更新 audit table、CREATE TABLEALTER TABLEDROP TABLE、formal Apply。
真實 DB 寫入檢查 5C 新增類別與測試MySQL writer、manual DB write、正式 DB cleanup、正式 DB read/write 驗證。
敏感資訊保存檢查 audit model、writer 與測試資料secret、token、password、connection string、完整路徑、正式 IP、正式 port、raw payload 被保存。
外部 DLL 載入掃描 production codeAssembly.LoadLoadFromLoadFileAssemblyLoadContext、plugin folder scan。
WebApi auth / Swagger掃描 WebApi 與新增檔案新增 auth middleware、Swagger 設定、API route 或正式部署設定。
Windows Service掃描 ServiceHost 與專案檔service installer、UseWindowsServicesc.exe、Windows Service package。
Adapter public contract檢查 Adapters 專案Adapter interface、ExecuteResult 或硬體控制 contract 不得因 5C 改動。

5. 驗收證據

6. 建議確認格式

若要進入 5C 第一批程式實作,請先回覆:

5C DB audit 實作前確認:
1. 同意
2. 同意
3. 同意
4. 同意
5. 同意
6. 同意
7. 同意

確認後可開始 audit contract / fake writer 實作;DB audit table、MySQL writer、DDL / ALTER TABLE、formal Apply、真實 DB 寫入、WebApi auth / Swagger 與 Windows Service 仍需後續獨立確認。