forked from all_user/stat_center
代码提交
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
package versionSer
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var reAllIdent = regexp.MustCompile(`<key>Payload(?:Identifier|UUID)</key>[ \t]*\r?\n[ \t]*<string>([^<]*)</string>`)
|
||||
|
||||
func collectIdentity(t *testing.T, data []byte) []string {
|
||||
t.Helper()
|
||||
var out []string
|
||||
for _, m := range reAllIdent.FindAllSubmatch(data, -1) {
|
||||
out = append(out, string(m[1]))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// TestUniqueProfileIdentityMatchesTemplates 确认 uniqueProfileIdentity 的派生结果与 conf 下
|
||||
// 已改好的模板完全一致:两边用同一套规则,部署后不会再变一次标识符。
|
||||
func TestUniqueProfileIdentityMatchesTemplates(t *testing.T) {
|
||||
files, err := filepath.Glob("../../conf/*.mobileconfig")
|
||||
if err != nil || len(files) == 0 {
|
||||
t.Fatalf("找不到 mobileconfig 模板: %v", err)
|
||||
}
|
||||
|
||||
seen := make(map[string]string)
|
||||
for _, path := range files {
|
||||
name := filepath.Base(path)
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("%s 读取失败: %v", name, err)
|
||||
}
|
||||
|
||||
want := collectIdentity(t, raw)
|
||||
if len(want) == 0 {
|
||||
t.Fatalf("%s 没有解析到任何标识符", name)
|
||||
}
|
||||
got := collectIdentity(t, uniqueProfileIdentity(raw, name))
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("%s 标识符数量不一致: got %d want %d", name, len(got), len(want))
|
||||
}
|
||||
for i := range want {
|
||||
if got[i] != want[i] {
|
||||
t.Errorf("%s 第 %d 个标识符不一致:\n got %s\n want %s", name, i, got[i], want[i])
|
||||
}
|
||||
}
|
||||
|
||||
// 跨文件判重:顶层 PayloadIdentifier 相同会让 iOS 覆盖安装
|
||||
for _, v := range got {
|
||||
if prev, ok := seen[v]; ok {
|
||||
t.Errorf("标识符 %s 同时出现在 %s 和 %s,iOS 上会互相覆盖", v, prev, name)
|
||||
}
|
||||
seen[v] = name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestUniqueProfileIdentityIsStable 同一模板多次生成必须得到相同标识符,
|
||||
// 否则用户每次下载都会在桌面多出一个图标。
|
||||
func TestUniqueProfileIdentityIsStable(t *testing.T) {
|
||||
raw, err := os.ReadFile("../../conf/hgmj.mobileconfig")
|
||||
if err != nil {
|
||||
t.Skipf("模板不存在: %v", err)
|
||||
}
|
||||
a := collectIdentity(t, uniqueProfileIdentity(raw, "hgmj.mobileconfig"))
|
||||
b := collectIdentity(t, uniqueProfileIdentity(raw, "hgmj.mobileconfig"))
|
||||
for i := range a {
|
||||
if a[i] != b[i] {
|
||||
t.Fatalf("第 %d 个标识符不稳定: %s != %s", i, a[i], b[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestUniqueProfileIdentityDiffersByConfigFile 不同 configFile 必须派生出不同标识符。
|
||||
func TestUniqueProfileIdentityDiffersByConfigFile(t *testing.T) {
|
||||
raw, err := os.ReadFile("../../conf/hgmj.mobileconfig")
|
||||
if err != nil {
|
||||
t.Skipf("模板不存在: %v", err)
|
||||
}
|
||||
a := collectIdentity(t, uniqueProfileIdentity(raw, "hgmj.mobileconfig"))
|
||||
b := collectIdentity(t, uniqueProfileIdentity(raw, "newapp.mobileconfig"))
|
||||
for i := range a {
|
||||
if a[i] == b[i] {
|
||||
t.Fatalf("不同 configFile 派生出相同标识符: %s", a[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user