gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package setting
  4. import (
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func Test_parsePostgreSQLHostPort(t *testing.T) {
  9. tests := map[string]struct {
  10. HostPort string
  11. Host string
  12. Port string
  13. }{
  14. "host-port": {
  15. HostPort: "127.0.0.1:1234",
  16. Host: "127.0.0.1",
  17. Port: "1234",
  18. },
  19. "no-port": {
  20. HostPort: "127.0.0.1",
  21. Host: "127.0.0.1",
  22. Port: "5432",
  23. },
  24. "ipv6-port": {
  25. HostPort: "[::1]:1234",
  26. Host: "::1",
  27. Port: "1234",
  28. },
  29. "ipv6-no-port": {
  30. HostPort: "[::1]",
  31. Host: "::1",
  32. Port: "5432",
  33. },
  34. "unix-socket": {
  35. HostPort: "/tmp/pg.sock:1234",
  36. Host: "/tmp/pg.sock",
  37. Port: "1234",
  38. },
  39. "unix-socket-no-port": {
  40. HostPort: "/tmp/pg.sock",
  41. Host: "/tmp/pg.sock",
  42. Port: "5432",
  43. },
  44. }
  45. for k, test := range tests {
  46. t.Run(k, func(t *testing.T) {
  47. t.Log(test.HostPort)
  48. host, port := parsePostgreSQLHostPort(test.HostPort)
  49. assert.Equal(t, test.Host, host)
  50. assert.Equal(t, test.Port, port)
  51. })
  52. }
  53. }
  54. func Test_getPostgreSQLConnectionString(t *testing.T) {
  55. tests := []struct {
  56. Host string
  57. User string
  58. Passwd string
  59. Name string
  60. SSLMode string
  61. Output string
  62. }{
  63. {
  64. Host: "", // empty means default
  65. Output: "postgres://:@127.0.0.1:5432?sslmode=",
  66. },
  67. {
  68. Host: "/tmp/pg.sock",
  69. User: "testuser",
  70. Passwd: "space space !#$%^^%^```-=?=",
  71. Name: "gitea",
  72. SSLMode: "false",
  73. Output: "postgres://testuser:space%20space%20%21%23$%25%5E%5E%25%5E%60%60%60-=%3F=@:5432/gitea?host=%2Ftmp%2Fpg.sock&sslmode=false",
  74. },
  75. {
  76. Host: "/tmp/pg.sock:6432",
  77. User: "testuser",
  78. Passwd: "pass",
  79. Name: "gitea",
  80. SSLMode: "false",
  81. Output: "postgres://testuser:pass@:6432/gitea?host=%2Ftmp%2Fpg.sock&sslmode=false",
  82. },
  83. {
  84. Host: "localhost",
  85. User: "pgsqlusername",
  86. Passwd: "I love Gitea!",
  87. Name: "gitea",
  88. SSLMode: "true",
  89. Output: "postgres://pgsqlusername:I%20love%20Gitea%21@localhost:5432/gitea?sslmode=true",
  90. },
  91. {
  92. Host: "localhost:1234",
  93. User: "user",
  94. Passwd: "pass",
  95. Name: "gitea?param=1",
  96. Output: "postgres://user:pass@localhost:1234/gitea?param=1&sslmode=",
  97. },
  98. }
  99. for _, test := range tests {
  100. connStr := getPostgreSQLConnectionString(test.Host, test.User, test.Passwd, test.Name, test.SSLMode)
  101. assert.Equal(t, test.Output, connStr)
  102. }
  103. }