github.com/nakagami/firebirdsql@v0.9.10/utils_test.go (about)

     1  /*******************************************************************************
     2  The MIT License (MIT)
     3  
     4  Copyright (c) 2013-2019 Hajime Nakagami
     5  
     6  Permission is hereby granted, free of charge, to any person obtaining a copy of
     7  this software and associated documentation files (the "Software"), to deal in
     8  the Software without restriction, including without limitation the rights to
     9  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
    10  the Software, and to permit persons to whom the Software is furnished to do so,
    11  subject to the following conditions:
    12  
    13  The above copyright notice and this permission notice shall be included in all
    14  copies or substantial portions of the Software.
    15  
    16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
    18  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
    19  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
    20  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    21  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    22  *******************************************************************************/
    23  
    24  package firebirdsql
    25  
    26  import (
    27  	"testing"
    28  )
    29  
    30  func TestDSNParse(t *testing.T) {
    31  	var testDSNs = []struct {
    32  		dsn            string
    33  		addr           string
    34  		dbName         string
    35  		user           string
    36  		passwd         string
    37  		role           string
    38  		authPluginName string
    39  		wireCrypt      string
    40  	}{
    41  		{"user:password@localhost:3000/dbname", "localhost:3000", "dbname", "user", "password", "", "Srp256", "true"},
    42  		{"user:password@localhost/dbname", "localhost:3050", "dbname", "user", "password", "", "Srp256", "true"},
    43  		{"user:password@localhost/dir/dbname", "localhost:3050", "/dir/dbname", "user", "password", "", "Srp256", "true"},
    44  		{"user:password@localhost/c:\\fbdata\\database.fdb", "localhost:3050", "c:\\fbdata\\database.fdb", "user", "password", "", "Srp256", "true"},
    45  		{"user:password@localhost/c:/fbdata/database.fdb", "localhost:3050", "c:/fbdata/database.fdb", "user", "password", "", "Srp256", "true"},
    46  		{"user:password@localhost/dbname?role=role", "localhost:3050", "dbname", "user", "password", "role", "Srp256", "true"},
    47  		{"user:password@localhost:3000/c:/fbdata/database.fdb?role=role&wire_crypt=false", "localhost:3000", "c:/fbdata/database.fdb", "user", "password", "role", "Srp256", "false"},
    48  		{"firebird://user:password@localhost:3000/dbname", "localhost:3000", "dbname", "user", "password", "", "Srp256", "true"},
    49  		{"firebird://user:%21p%40ssword%3F@localhost:3050/dbname", "localhost:3050", "dbname", "user", "!p@ssword?", "", "Srp256", "true"},
    50  	}
    51  
    52  	for _, d := range testDSNs {
    53  		dsn, err := parseDSN(d.dsn)
    54  		if dsn == nil {
    55  			t.Fatal("parse DSN fail. firebirdDsn is nil.")
    56  		}
    57  		if err != nil {
    58  			t.Fatal(err)
    59  		}
    60  		if dsn.addr != d.addr {
    61  			t.Errorf("parse DSN fail:%s(%s != %s)", d.dsn, dsn.addr, d.addr)
    62  		}
    63  		if dsn.dbName != d.dbName {
    64  			t.Errorf("parse DSN fail:%s(%s != %s)", d.dsn, dsn.dbName, d.dbName)
    65  		}
    66  		if dsn.user != d.user {
    67  			t.Errorf("parse DSN fail:%s(%s != %s)", d.dsn, dsn.user, d.user)
    68  		}
    69  		if dsn.passwd != d.passwd {
    70  			t.Errorf("parse DSN fail:%s(%s != %s)", d.dsn, dsn.passwd, d.passwd)
    71  		}
    72  		if dsn.options["role"] != d.role {
    73  			t.Errorf("parse DSN fail:%s(%s != %s)", d.dsn, dsn.options["role"], d.role)
    74  		}
    75  		if dsn.options["auth_plugin_name"] != d.authPluginName {
    76  			t.Errorf("parse DSN fail:%s(%s != %s)", d.dsn, dsn.options["auth_plugin_name"], d.authPluginName)
    77  		}
    78  		if dsn.options["wire_crypt"] != d.wireCrypt {
    79  			t.Errorf("parse DSN fail:%s(%v != %v)", d.dsn, dsn.options["wire_crypt"], d.wireCrypt)
    80  		}
    81  	}
    82  
    83  	_, err := parseDSN("something wrong")
    84  	if err == nil {
    85  		t.Fatalf("Error Not occured")
    86  	}
    87  	_, err = parseDSN("SomethingWrongConnectionString")
    88  	if err == nil {
    89  		t.Fatalf("Error Not occured")
    90  	}
    91  
    92  }