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

本文件承接 [5C DB audit 邊界分析](phase-five-c-db-audit-boundary-analysis.md)、[5C DB audit 實作前確認表](phase-five-c-db-audit-preimplementation-checklist.md) 與 [5C DB audit 程式 repo 結構檢查與實作前盤點](phase-five-c-db-audit-code-structure-preimplementation-checklist.md)。

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

## 0. 文件狀態

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

## 1. 5C 第一批實作範圍

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

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

## 2. 必要測試案例

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

## 3. 必跑測試命令

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

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

## 4. 停止線掃描

| 停止線 | 掃描方向 | 不得出現 |
|---|---|---|
| DB DDL / schema 變更 | 掃描 `src`、`tests`、`samples`、`docs` 的 5C 變更 | 新 audit table、`CREATE TABLE`、`ALTER TABLE`、`DROP 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 code | `Assembly.Load`、`LoadFrom`、`LoadFile`、`AssemblyLoadContext`、plugin folder scan。 |
| WebApi auth / Swagger | 掃描 WebApi 與新增檔案 | 新增 auth middleware、Swagger 設定、API route 或正式部署設定。 |
| Windows Service | 掃描 ServiceHost 與專案檔 | service installer、`UseWindowsService`、`sc.exe`、Windows Service package。 |
| Adapter public contract | 檢查 Adapters 專案 | Adapter interface、ExecuteResult 或硬體控制 contract 不得因 5C 改動。 |

## 5. 驗收證據

5C 第一批實作完成後，至少需在驗收紀錄中保留：

- 修改檔案清單。
- Application tests、Infrastructure.MySql tests 與 solution tests 結果。
- 停止線掃描結果。
- 污染字串掃描結果。
- 程式 repo `git status`、commit、push 與 Actions run。
- 文件 repo 實作與驗收紀錄、commit、push 與線上查驗。

## 6. 建議確認格式

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

```text
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 仍需後續獨立確認。
