|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/zeromicro/go-zero/core/stringx"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
certContent = `-----BEGIN CERTIFICATE-----
|
|
|
|
MIIDazCCAlOgAwIBAgIUEg9GVO2oaPn+YSmiqmFIuAo10WIwDQYJKoZIhvcNAQEM
|
|
|
|
BQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM
|
|
|
|
GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAgFw0yMzAzMTExMzIxMjNaGA8yMTIz
|
|
|
|
MDIxNTEzMjEyM1owRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUx
|
|
|
|
ITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcN
|
|
|
|
AQEBBQADggEPADCCAQoCggEBALplXlWsIf0O/IgnIplmiZHKGnxyfyufyE2FBRNk
|
|
|
|
OofRqbKuPH8GNqbkvZm7N29fwTDAQ+mViAggCkDht4hOzoWJMA7KYJt8JnTSWL48
|
|
|
|
M1lcrpc9DL2gszC/JF/FGvyANbBtLklkZPFBGdHUX14pjrT937wqPtm+SqUHSvRT
|
|
|
|
B7bmwmm2drRcmhpVm98LSlV7uQ2EgnJgsLjBPITKUejLmVLHfgX0RwQ2xIpX9pS4
|
|
|
|
FCe1BTacwl2gGp7Mje7y4Mfv3o0ArJW6Tuwbjx59ZXwb1KIP71b7bT04AVS8ZeYO
|
|
|
|
UMLKKuB5UR9x9Rn6cLXOTWBpcMVyzDgrAFLZjnE9LPUolZMCAwEAAaNRME8wHwYD
|
|
|
|
VR0jBBgwFoAUeW8w8pmhncbRgTsl48k4/7wnfx8wCQYDVR0TBAIwADALBgNVHQ8E
|
|
|
|
BAM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Z3G0YE7JePJOP+8J38fMB8GA1UdIwQYMBaAFHlvMPKZoZ3G
|
|
|
|
0YE7JePJOP+8J38fMA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQEMBQADggEB
|
|
|
|
AMX8dNulADOo9uQgBMyFb9TVra7iY0zZjzv4GY5XY7scd52n6CnfAPvYBBDnTr/O
|
|
|
|
BgNp5jaujb4+9u/2qhV3f9n+/3WOb2CmPehBgVSzlXqHeQ9lshmgwZPeem2T+8Tm
|
|
|
|
Nnc/xQnsUfCFszUDxpkr55+aLVM22j02RWqcZ4q7TAaVYL+kdFVMc8FoqG/0ro6A
|
|
|
|
BjE/Qn0Nn7ciX1VUjDt8l+k7ummPJTmzdi6i6E4AwO9dzrGNgGJ4aWL8cC6xYcIX
|
|
|
|
goVIRTFeONXSDno/oPjWHpIPt7L15heMpKBHNuzPkKx2YVqPHE5QZxWfS+Lzgx+Q
|
|
|
|
E2oTTM0rYKOZ8p6000mhvKI=
|
|
|
|
-----END CERTIFICATE-----`
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccount(t *testing.T) {
|
|
|
|
endpoints := []string{
|
|
|
|
"192.168.0.2:2379",
|
|
|
|
"192.168.0.3:2379",
|
|
|
|
"192.168.0.4:2379",
|
|
|
|
}
|
|
|
|
username := "foo" + stringx.Rand()
|
|
|
|
password := "bar"
|
|
|
|
anotherPassword := "any"
|
|
|
|
|
|
|
|
_, ok := GetAccount(endpoints)
|
|
|
|
assert.False(t, ok)
|
|
|
|
|
|
|
|
AddAccount(endpoints, username, password)
|
|
|
|
account, ok := GetAccount(endpoints)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, username, account.User)
|
|
|
|
assert.Equal(t, password, account.Pass)
|
|
|
|
|
|
|
|
AddAccount(endpoints, username, anotherPassword)
|
|
|
|
account, ok = GetAccount(endpoints)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, username, account.User)
|
|
|
|
assert.Equal(t, anotherPassword, account.Pass)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTLSMethods(t *testing.T) {
|
|
|
|
certFile := createTempFile(t, []byte(certContent))
|
|
|
|
defer os.Remove(certFile)
|
|
|
|
keyFile := createTempFile(t, []byte(keyContent))
|
|
|
|
defer os.Remove(keyFile)
|
|
|
|
caFile := createTempFile(t, []byte(caContent))
|
|
|
|
defer os.Remove(caFile)
|
|
|
|
|
|
|
|
assert.NoError(t, AddTLS([]string{"foo"}, certFile, keyFile, caFile, false))
|
|
|
|
cfg, ok := GetTLS([]string{"foo"})
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.NotNil(t, cfg)
|
|
|
|
|
|
|
|
assert.Error(t, AddTLS([]string{"bar"}, "bad-file", keyFile, caFile, false))
|
|
|
|
assert.Error(t, AddTLS([]string{"bar"}, certFile, keyFile, "bad-file", false))
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTempFile(t *testing.T, body []byte) string {
|
|
|
|
tmpFile, err := os.CreateTemp(os.TempDir(), "go-unit-*.tmp")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpFile.Close()
|
|
|
|
if err = os.WriteFile(tmpFile.Name(), body, os.ModePerm); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmpFile.Name()
|
|
|
|
}
|