main_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package main
  2. import (
  3. "bytes"
  4. "crypto/x509/pkix"
  5. "encoding/base64"
  6. "encoding/json"
  7. "net/http"
  8. "net/http/httptest"
  9. "strings"
  10. "testing"
  11. )
  12. func TestIndexRoute(t *testing.T) {
  13. router := newRouter()
  14. recorder := httptest.NewRecorder()
  15. request := httptest.NewRequest(http.MethodGet, "/", nil)
  16. router.ServeHTTP(recorder, request)
  17. if recorder.Code != http.StatusOK {
  18. t.Fatalf("expected 200, got %d", recorder.Code)
  19. }
  20. }
  21. func TestTLSGenerateRoute(t *testing.T) {
  22. router := newRouter()
  23. recorder := httptest.NewRecorder()
  24. payload, err := json.Marshal(map[string]any{
  25. "commonName": "example.local",
  26. "organization": "Example Corp",
  27. "organizationalUnit": "Engineering",
  28. "locality": "Charlotte Amalie",
  29. "state": "St Thomas",
  30. "country": "VI",
  31. "dnsNames": "example.local,www.example.local",
  32. "validDays": 30,
  33. "keySize": 3072,
  34. })
  35. if err != nil {
  36. t.Fatalf("marshal payload: %v", err)
  37. }
  38. request := httptest.NewRequest(http.MethodPost, "/api/tls/generate", bytes.NewReader(payload))
  39. request.Header.Set("Content-Type", "application/json")
  40. router.ServeHTTP(recorder, request)
  41. if recorder.Code != http.StatusOK {
  42. t.Fatalf("expected 200, got %d: %s", recorder.Code, recorder.Body.String())
  43. }
  44. var response map[string]any
  45. if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
  46. t.Fatalf("unmarshal response: %v", err)
  47. }
  48. for _, key := range []string{"certificatePem", "privateKeyPem", "publicKeyPem", "csrPem"} {
  49. value, _ := response[key].(string)
  50. if !strings.Contains(value, "BEGIN") {
  51. t.Fatalf("expected %s to contain PEM data", key)
  52. }
  53. }
  54. zipBase64, _ := response["zipBase64"].(string)
  55. zipFilename, _ := response["zipFilename"].(string)
  56. if zipFilename == "" {
  57. t.Fatal("expected zipFilename to be present")
  58. }
  59. zipBytes, err := base64.StdEncoding.DecodeString(zipBase64)
  60. if err != nil {
  61. t.Fatalf("decode zipBase64: %v", err)
  62. }
  63. if len(zipBytes) == 0 {
  64. t.Fatal("expected zip archive bytes")
  65. }
  66. if keySize, ok := response["keySize"].(float64); !ok || keySize != 3072 {
  67. t.Fatalf("expected keySize 3072, got %#v", response["keySize"])
  68. }
  69. }
  70. func TestSSLCheckRejectsEmptyURL(t *testing.T) {
  71. router := newRouter()
  72. recorder := httptest.NewRecorder()
  73. payload, err := json.Marshal(map[string]any{
  74. "url": "",
  75. })
  76. if err != nil {
  77. t.Fatalf("marshal payload: %v", err)
  78. }
  79. request := httptest.NewRequest(http.MethodPost, "/api/ssl/check", bytes.NewReader(payload))
  80. request.Header.Set("Content-Type", "application/json")
  81. router.ServeHTTP(recorder, request)
  82. if recorder.Code != http.StatusBadRequest {
  83. t.Fatalf("expected 400, got %d: %s", recorder.Code, recorder.Body.String())
  84. }
  85. }
  86. func TestPEMCheckRoute(t *testing.T) {
  87. router := newRouter()
  88. recorder := httptest.NewRecorder()
  89. subject := pkix.Name{
  90. CommonName: "example.local",
  91. Organization: []string{"Example Corp"},
  92. OrganizationalUnit: []string{"Engineering"},
  93. }
  94. certPEM, keyPEM, _, csrPEM, _, err := generateCertificate(subject, []string{"example.local"}, 30, 2048)
  95. if err != nil {
  96. t.Fatalf("generate certificate: %v", err)
  97. }
  98. payload, err := json.Marshal(map[string]any{
  99. "pem": string(certPEM) + "\n" + string(csrPEM) + "\n" + string(keyPEM),
  100. })
  101. if err != nil {
  102. t.Fatalf("marshal payload: %v", err)
  103. }
  104. request := httptest.NewRequest(http.MethodPost, "/api/pem/check", bytes.NewReader(payload))
  105. request.Header.Set("Content-Type", "application/json")
  106. router.ServeHTTP(recorder, request)
  107. if recorder.Code != http.StatusOK {
  108. t.Fatalf("expected 200, got %d: %s", recorder.Code, recorder.Body.String())
  109. }
  110. var response map[string]any
  111. if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
  112. t.Fatalf("unmarshal response: %v", err)
  113. }
  114. if count, ok := response["count"].(float64); !ok || count != 3 {
  115. t.Fatalf("expected 3 blocks, got %#v", response["count"])
  116. }
  117. }