code.gitea.io/gitea@v1.22.3/modules/setting/database_test.go (about)

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