61 lines
1.4 KiB
Go
61 lines
1.4 KiB
Go
package proxy
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestEnsureLocalCAGeneratesUniquePair(t *testing.T) {
|
|
root := t.TempDir()
|
|
cert1, err := EnsureLocalCA(root)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := filepath.Join(root, "data", "proxy-ca.crt")
|
|
if cert1 != want {
|
|
t.Fatalf("cert path: got %q want %q", cert1, want)
|
|
}
|
|
key1 := filepath.Join(root, "data", "proxy-ca.key")
|
|
if _, err := os.Stat(key1); err != nil {
|
|
t.Fatal("missing key", err)
|
|
}
|
|
hash1, err := AndroidCAHashFile(cert1)
|
|
if err != nil || hash1 == "" {
|
|
t.Fatalf("hash: %q %v", hash1, err)
|
|
}
|
|
|
|
// Second call reuses the same pair.
|
|
cert2, err := EnsureLocalCA(root)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cert2 != cert1 {
|
|
t.Fatalf("expected reuse, got %q vs %q", cert2, cert1)
|
|
}
|
|
|
|
// A different install root gets a different CA.
|
|
rootB := t.TempDir()
|
|
certB, err := EnsureLocalCA(rootB)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
hashB, _ := AndroidCAHashFile(certB)
|
|
if hashB == hash1 {
|
|
t.Fatal("expected different installs to get different CA hashes")
|
|
}
|
|
}
|
|
|
|
func TestFindLocalCAIgnoresCertWithoutKey(t *testing.T) {
|
|
root := t.TempDir()
|
|
dir := filepath.Join(root, "apps", "proxy")
|
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "rteproxy-ca.crt"), []byte("not-a-real-cert"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p := FindLocalCA(root); p != "" {
|
|
t.Fatalf("expected empty without key, got %q", p)
|
|
}
|
|
}
|