- model.SignProfile:platform(windows/ios)、证书/口令/描述文件加密落盘,库里只留元数据 - 主密钥由 app.jwt_secret 派生,目录 0700、文件 0600;响应永不回证书内容与口令 - /dev/sign-profiles 上传/列表/详情/删除;iOS 强制要求 .mobileprovision - ProtectTask 增加 sign_profile_id(上传时可指定,校验归属),为重签做准备 - 单测:加解密往返/换钥失败/篡改检测/文件权限/平台归一化
91 lines
2.2 KiB
Go
91 lines
2.2 KiB
Go
package protect
|
||
|
||
import (
|
||
"bytes"
|
||
"crypto/rand"
|
||
"os"
|
||
"path/filepath"
|
||
"testing"
|
||
)
|
||
|
||
func TestEncryptRoundTrip(t *testing.T) {
|
||
key := make([]byte, 32)
|
||
rand.Read(key)
|
||
plain := []byte("pfxpass-测试-1234")
|
||
enc, err := encryptWithKey(key, plain)
|
||
if err != nil {
|
||
t.Fatalf("加密失败: %v", err)
|
||
}
|
||
if len(enc) != len(plain)+12+16 {
|
||
t.Fatalf("密文长度异常: %d(期望 %d)", len(enc), len(plain)+28)
|
||
}
|
||
if bytes.Contains(enc, plain) {
|
||
t.Fatal("密文里出现了明文")
|
||
}
|
||
got, err := decryptWithKey(key, enc)
|
||
if err != nil {
|
||
t.Fatalf("解密失败: %v", err)
|
||
}
|
||
if !bytes.Equal(got, plain) {
|
||
t.Fatalf("往返不一致: %q", got)
|
||
}
|
||
// 换密钥必须解不开
|
||
bad := make([]byte, 32)
|
||
rand.Read(bad)
|
||
if _, err := decryptWithKey(bad, enc); err == nil {
|
||
t.Fatal("错误密钥竟然解开了")
|
||
}
|
||
// 篡改一字节必须失败
|
||
enc[len(enc)-1] ^= 0xFF
|
||
if _, err := decryptWithKey(key, enc); err == nil {
|
||
t.Fatal("篡改后的密文没被检出")
|
||
}
|
||
}
|
||
|
||
func TestWriteReadEncryptedFile(t *testing.T) {
|
||
key := make([]byte, 32)
|
||
rand.Read(key)
|
||
dir := t.TempDir()
|
||
p := filepath.Join(dir, "sub", "pass.enc")
|
||
plain := []byte("secret-pass")
|
||
|
||
enc, err := encryptWithKey(key, plain)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := os.MkdirAll(filepath.Dir(p), 0o700); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := os.WriteFile(p, enc, 0o600); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
st, err := os.Stat(p)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if st.Mode().Perm() != 0o600 {
|
||
t.Fatalf("权限应为 0600,实际 %o", st.Mode().Perm())
|
||
}
|
||
raw, _ := os.ReadFile(p)
|
||
got, err := decryptWithKey(key, raw)
|
||
if err != nil || !bytes.Equal(got, plain) {
|
||
t.Fatalf("文件往返失败: %v", err)
|
||
}
|
||
}
|
||
|
||
func TestNormalizePlatform(t *testing.T) {
|
||
cases := map[string]string{
|
||
"windows": PlatformWindows, "win": PlatformWindows, "PE": PlatformWindows,
|
||
"ios": PlatformIOS, "ipa": PlatformIOS,
|
||
}
|
||
for in, want := range cases {
|
||
got, err := NormalizePlatform(in)
|
||
if err != nil || got != want {
|
||
t.Fatalf("%s → %s (%v),期望 %s", in, got, err, want)
|
||
}
|
||
}
|
||
if _, err := NormalizePlatform("android"); err == nil {
|
||
t.Fatal("android 不该被接受(请用加壳任务类型自动判断)")
|
||
}
|
||
}
|