2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
package mssql
|
2017-04-06 13:33:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2023-03-17 14:52:54 +00:00
|
|
|
_ "github.com/denisenkom/go-mssqldb"
|
2018-04-03 00:46:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/logging"
|
|
|
|
"github.com/hashicorp/vault/sdk/physical"
|
2017-04-06 13:33:49 +00:00
|
|
|
)
|
|
|
|
|
2023-03-17 14:52:54 +00:00
|
|
|
// TestInvalidIdentifier checks validity of an identifier
|
|
|
|
func TestInvalidIdentifier(t *testing.T) {
|
|
|
|
testcases := map[string]bool{
|
|
|
|
"name": true,
|
|
|
|
"_name": true,
|
|
|
|
"Name": true,
|
|
|
|
"#name": false,
|
|
|
|
"?Name": false,
|
|
|
|
"9name": false,
|
|
|
|
"@name": false,
|
|
|
|
"$name": false,
|
|
|
|
" name": false,
|
|
|
|
"n ame": false,
|
|
|
|
"n4444444": true,
|
|
|
|
"_4321098765": true,
|
|
|
|
"_##$$@@__": true,
|
|
|
|
"_123name#@": true,
|
|
|
|
"name!": false,
|
|
|
|
"name%": false,
|
|
|
|
"name^": false,
|
|
|
|
"name&": false,
|
|
|
|
"name*": false,
|
|
|
|
"name(": false,
|
|
|
|
"name)": false,
|
|
|
|
"nåame": true,
|
|
|
|
"åname": true,
|
|
|
|
"name'": false,
|
|
|
|
"nam`e": false,
|
|
|
|
"пример": true,
|
|
|
|
"_#Āā@#$_ĂĄąćĈĉĊċ": true,
|
|
|
|
"ÛÜÝÞßàáâ": true,
|
|
|
|
"豈更滑a23$#@": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, expected := range testcases {
|
|
|
|
if !isInvalidIdentifier(i) != expected {
|
|
|
|
t.Fatalf("unexpected identifier %s: expected validity %v", i, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
func TestMSSQLBackend(t *testing.T) {
|
2017-04-06 13:33:49 +00:00
|
|
|
server := os.Getenv("MSSQL_SERVER")
|
|
|
|
if server == "" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
database := os.Getenv("MSSQL_DB")
|
|
|
|
if database == "" {
|
|
|
|
database = "test"
|
|
|
|
}
|
|
|
|
|
|
|
|
table := os.Getenv("MSSQL_TABLE")
|
|
|
|
if table == "" {
|
|
|
|
table = "test"
|
|
|
|
}
|
|
|
|
|
2019-04-02 09:42:30 +00:00
|
|
|
schema := os.Getenv("MSSQL_SCHEMA")
|
|
|
|
if schema == "" {
|
|
|
|
schema = "test"
|
|
|
|
}
|
2019-05-20 22:15:05 +00:00
|
|
|
|
2017-04-06 13:33:49 +00:00
|
|
|
username := os.Getenv("MSSQL_USERNAME")
|
|
|
|
password := os.Getenv("MSSQL_PASSWORD")
|
|
|
|
|
|
|
|
// Run vault tests
|
2018-04-03 00:46:59 +00:00
|
|
|
logger := logging.NewVaultLogger(log.Debug)
|
2017-04-06 13:33:49 +00:00
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
b, err := NewMSSQLBackend(map[string]string{
|
2017-04-06 13:33:49 +00:00
|
|
|
"server": server,
|
|
|
|
"database": database,
|
|
|
|
"table": table,
|
2019-04-02 09:42:30 +00:00
|
|
|
"schema": schema,
|
2017-04-06 13:33:49 +00:00
|
|
|
"username": username,
|
|
|
|
"password": password,
|
2017-08-03 17:24:27 +00:00
|
|
|
}, logger)
|
2017-04-06 13:33:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create new backend: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2017-08-03 17:24:27 +00:00
|
|
|
mssql := b.(*MSSQLBackend)
|
2017-04-06 13:33:49 +00:00
|
|
|
_, err := mssql.client.Exec("DROP TABLE " + mssql.dbTable)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to drop table: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
physical.ExerciseBackend(t, b)
|
|
|
|
physical.ExerciseBackend_ListPrefix(t, b)
|
2017-04-06 13:33:49 +00:00
|
|
|
}
|
2019-03-13 04:10:09 +00:00
|
|
|
|
|
|
|
func TestMSSQLBackend_schema(t *testing.T) {
|
|
|
|
server := os.Getenv("MSSQL_SERVER")
|
|
|
|
if server == "" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
database := os.Getenv("MSSQL_DB")
|
|
|
|
if database == "" {
|
|
|
|
database = "test"
|
|
|
|
}
|
|
|
|
|
|
|
|
table := os.Getenv("MSSQL_TABLE")
|
|
|
|
if table == "" {
|
|
|
|
table = "test"
|
|
|
|
}
|
|
|
|
|
2019-04-02 09:42:30 +00:00
|
|
|
schema := os.Getenv("MSSQL_SCHEMA")
|
|
|
|
if schema == "" {
|
|
|
|
schema = "test"
|
|
|
|
}
|
2019-05-20 22:15:05 +00:00
|
|
|
|
2017-04-06 13:33:49 +00:00
|
|
|
username := os.Getenv("MSSQL_USERNAME")
|
|
|
|
password := os.Getenv("MSSQL_PASSWORD")
|
|
|
|
|
|
|
|
// Run vault tests
|
2018-04-03 00:46:59 +00:00
|
|
|
logger := logging.NewVaultLogger(log.Debug)
|
2017-04-06 13:33:49 +00:00
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
b, err := NewMSSQLBackend(map[string]string{
|
2017-04-06 13:33:49 +00:00
|
|
|
"server": server,
|
|
|
|
"database": database,
|
2019-05-20 22:15:05 +00:00
|
|
|
"schema": schema,
|
2017-04-06 13:33:49 +00:00
|
|
|
"table": table,
|
|
|
|
"username": username,
|
|
|
|
"password": password,
|
2017-08-03 17:24:27 +00:00
|
|
|
}, logger)
|
2017-04-06 13:33:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create new backend: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2017-08-03 17:24:27 +00:00
|
|
|
mssql := b.(*MSSQLBackend)
|
2017-04-06 13:33:49 +00:00
|
|
|
_, err := mssql.client.Exec("DROP TABLE " + mssql.dbTable)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to drop table: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
physical.ExerciseBackend(t, b)
|
|
|
|
physical.ExerciseBackend_ListPrefix(t, b)
|
2017-04-06 13:33:49 +00:00
|
|
|
}
|