Files
rond-with-you/internal/fog/tilename_test.go
T
tamakyi b4c2a8452f 审计修复:后端(安全 / 数据一致性 / 性能)
audit-2026-09-21.md 里的后端条目落地。每条都配了回归守卫,并逐条做过变异验证
(注入原 bug → 确认按正确理由变红 → 还原 → 变绿)。

安全
- CSRF 只加同源 Origin/Referer 守卫(csrf.go),不动模板
- 会话 cookie 的 Secure 按请求判断;两步验证两闸(loginGuard + 验证码一次性)
- 跳转收口到 safeInternalPath;clientIP 只认 XFF 最后一段
- GPX 按属主圈定且查 RowsAffected;下载名走 downloadDisposition
- 上传口统一 MaxBytesReader;落盘后的失败路径清掉半成品

数据一致性
- 区域判定两端各过一遍再 AND(apiTrack 与 movementsWhere 对齐)
- Filter.Verdict 全域一致(下推到 visitsWhere);聚合刷新必须返回 error
- 附属数据改挂坐标键,Go 与 SQL 两套表达式逐位一致
- 脏备份的伪时间/越界坐标不再进领域模型;rows 循环统一 drainRows
- 合并与导出:writeRawRow 上抛错误、悬空引用清理、原生列补全

性能
- placeCols 相关子查询带 dataset_id 前导列;findSourceArchive 先按大小筛
- 批量勾选改 ANY($3::bigint[]);fillupCache 加上限;同一查询不再查两遍

另:新增「全新库」冒烟测试 —— 空库跑完 Migrate 再用应用侧读查询真打一遍,
迁移块排到依赖表之前这种错只有新库会炸,老库毫无症状。
2026-09-24 20:37:16 +08:00

49 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package fog
import (
"crypto/md5"
"fmt"
"testing"
)
// 瓦片编号 0..9 是合法的(tx=0, ty=0 那块就是 0),编码/解码都必须能往返。
func TestTileNameRoundtripAllIDs(t *testing.T) {
for _, id := range []int{0, 1, 5, 9, 10, 11, 99, 100, 511, 512, 262143, 1 << 20} {
name := tileName(id)
got, ok := tileIDFromName(name)
if !ok || got != id {
t.Errorf("id=%d → name=%q → (%d,%v),往返不一致", id, name, got, ok)
}
}
}
// 4 位 md5("5") 前缀 + mask1 掩码 + 2 位 M2 尾缀 = 7 字节:旧的长度守卫会放行、
// md5 校验也通过,随后在 s[len(s)-2+i] 处越界 panic(上传构造过的 .fwss 即可触发)。
func TestTileIDFromNameRejectsCraftedShortName(t *testing.T) {
name := fmt.Sprintf("%x", md5.Sum([]byte("5")))[:4] +
string(mask1[5]) + string(mask2[5]) + string(mask2[5])
if len(name) != 7 {
t.Fatalf("构造名长度应为 7,实为 %d (%q)", len(name), name)
}
if id, ok := tileIDFromName(name); ok {
t.Fatalf("构造名 %q 不应被接受(解出 id=%d)", name, id)
}
}
// 网格第一列(tx=0)的瓦片编号就是 0..9,Encode 不能 panic,且能原样解回。
func TestEncodeHandlesSingleDigitTile(t *testing.T) {
bm := make([]byte, bitmapSize)
bm[0] = 0x80
data, err := Encode([]Block{{GX: 0, GY: 0, Bitmap: bm}}, nil)
if err != nil {
t.Fatalf("Encode: %v", err)
}
blocks, cells, err := Decode(data)
if err != nil {
t.Fatalf("Decode: %v", err)
}
if len(blocks) != 1 || blocks[0].GX != 0 || blocks[0].GY != 0 || cells != 1 {
t.Fatalf("往返得到 %+v (cells=%d),期望 1 块 (0,0)、1 格", blocks, cells)
}
}