mirror of
https://github.com/xweiba/location-spoofer.git
synced 2026-09-25 16:11:54 +08:00
feat: improve runtime setup and guidance
This commit is contained in:
+2
-2
@@ -31,8 +31,8 @@ func generateCA() (certPEM, keyPEM []byte, err error) {
|
||||
template := x509.Certificate{
|
||||
SerialNumber: serialNumber,
|
||||
Subject: pkix.Name{
|
||||
Organization: []string{"WLOC"},
|
||||
CommonName: "WLOC CA " + time.Now().In(time.FixedZone("CST", 8*3600)).Format("2006.01.02 15:04"),
|
||||
Organization: []string{"Location Spoofer"},
|
||||
CommonName: "Location Spoofer CA",
|
||||
},
|
||||
NotBefore: time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC),
|
||||
NotAfter: time.Date(2045, 1, 1, 0, 0, 0, 0, time.UTC),
|
||||
|
||||
+28
-11
@@ -15,17 +15,16 @@ func TestGenerateCA(t *testing.T) {
|
||||
if len(certPEM) == 0 || len(keyPEM) == 0 {
|
||||
t.Fatal("empty CA output")
|
||||
}
|
||||
block, _ := pem.Decode(certPEM)
|
||||
if block == nil {
|
||||
t.Fatal("invalid cert PEM")
|
||||
}
|
||||
cert, err := x509.ParseCertificate(block.Bytes)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cert := parseCertificatePEM(t, certPEM)
|
||||
if !cert.IsCA {
|
||||
t.Fatal("certificate is not a CA")
|
||||
}
|
||||
if cert.Subject.CommonName != "Location Spoofer CA" {
|
||||
t.Fatalf("unexpected CA common name: %q", cert.Subject.CommonName)
|
||||
}
|
||||
if len(cert.Subject.Organization) != 1 || cert.Subject.Organization[0] != "Location Spoofer" {
|
||||
t.Fatalf("unexpected CA organization: %v", cert.Subject.Organization)
|
||||
}
|
||||
if time.Until(cert.NotAfter).Hours() < 360*24 {
|
||||
t.Fatal("CA validity is too short")
|
||||
}
|
||||
@@ -34,16 +33,34 @@ func TestGenerateCA(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateCAUsesUniquePrivateKeys(t *testing.T) {
|
||||
_, firstKey, err := generateCA()
|
||||
func TestGenerateCAUsesUniquePrivateKeysAndSerialNumbers(t *testing.T) {
|
||||
firstCertPEM, firstKey, err := generateCA()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, secondKey, err := generateCA()
|
||||
secondCertPEM, secondKey, err := generateCA()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(firstKey) == string(secondKey) {
|
||||
t.Fatal("generated CA private keys must not be deterministic")
|
||||
}
|
||||
firstCert := parseCertificatePEM(t, firstCertPEM)
|
||||
secondCert := parseCertificatePEM(t, secondCertPEM)
|
||||
if firstCert.SerialNumber.Cmp(secondCert.SerialNumber) == 0 {
|
||||
t.Fatal("generated CA serial numbers must not be deterministic")
|
||||
}
|
||||
}
|
||||
|
||||
func parseCertificatePEM(t *testing.T, certPEM []byte) *x509.Certificate {
|
||||
t.Helper()
|
||||
block, _ := pem.Decode(certPEM)
|
||||
if block == nil {
|
||||
t.Fatal("invalid cert PEM")
|
||||
}
|
||||
cert, err := x509.ParseCertificate(block.Bytes)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return cert
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user