gitea源码

cert_test.go 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2025 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package cmd
  4. import (
  5. "path/filepath"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. )
  10. func TestCertCommand(t *testing.T) {
  11. cases := []struct {
  12. name string
  13. args []string
  14. }{
  15. {
  16. name: "RSA cert generation",
  17. args: []string{
  18. "cert-test",
  19. "--host", "localhost",
  20. "--rsa-bits", "2048",
  21. "--duration", "1h",
  22. "--start-date", "Jan 1 00:00:00 2024",
  23. },
  24. },
  25. {
  26. name: "ECDSA cert generation",
  27. args: []string{
  28. "cert-test",
  29. "--host", "localhost",
  30. "--ecdsa-curve", "P256",
  31. "--duration", "1h",
  32. "--start-date", "Jan 1 00:00:00 2024",
  33. },
  34. },
  35. {
  36. name: "mixed host, certificate authority",
  37. args: []string{
  38. "cert-test",
  39. "--host", "localhost,127.0.0.1",
  40. "--duration", "1h",
  41. "--start-date", "Jan 1 00:00:00 2024",
  42. },
  43. },
  44. }
  45. for _, c := range cases {
  46. t.Run(c.name, func(t *testing.T) {
  47. app := cmdCert()
  48. tempDir := t.TempDir()
  49. certFile := filepath.Join(tempDir, "cert.pem")
  50. keyFile := filepath.Join(tempDir, "key.pem")
  51. err := app.Run(t.Context(), append(c.args, "--out", certFile, "--keyout", keyFile))
  52. require.NoError(t, err)
  53. assert.FileExists(t, certFile)
  54. assert.FileExists(t, keyFile)
  55. })
  56. }
  57. }
  58. func TestCertCommandFailures(t *testing.T) {
  59. cases := []struct {
  60. name string
  61. args []string
  62. errMsg string
  63. }{
  64. {
  65. name: "Start Date Parsing failure",
  66. args: []string{
  67. "cert-test",
  68. "--host", "localhost",
  69. "--start-date", "invalid-date",
  70. },
  71. errMsg: "parsing time",
  72. },
  73. {
  74. name: "Unknown curve",
  75. args: []string{
  76. "cert-test",
  77. "--host", "localhost",
  78. "--ecdsa-curve", "invalid-curve",
  79. },
  80. errMsg: "unrecognized elliptic curve",
  81. },
  82. {
  83. name: "Key generation failure",
  84. args: []string{
  85. "cert-test",
  86. "--host", "localhost",
  87. "--rsa-bits", "invalid-bits",
  88. },
  89. },
  90. {
  91. name: "Missing parameters",
  92. args: []string{
  93. "cert-test",
  94. },
  95. errMsg: `"host" not set`,
  96. },
  97. }
  98. for _, c := range cases {
  99. t.Run(c.name, func(t *testing.T) {
  100. app := cmdCert()
  101. tempDir := t.TempDir()
  102. certFile := filepath.Join(tempDir, "cert.pem")
  103. keyFile := filepath.Join(tempDir, "key.pem")
  104. err := app.Run(t.Context(), append(c.args, "--out", certFile, "--keyout", keyFile))
  105. require.Error(t, err)
  106. if c.errMsg != "" {
  107. assert.ErrorContains(t, err, c.errMsg)
  108. }
  109. assert.NoFileExists(t, certFile)
  110. assert.NoFileExists(t, keyFile)
  111. })
  112. }
  113. }