github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/core/ledger/util/couchdb/couchdbutil_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package couchdb
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/hyperledger/fabric/common/ledger/testutil"
    24  	"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
    25  )
    26  
    27  //Unit test of couch db util functionality
    28  func TestCreateCouchDBConnectionAndDB(t *testing.T) {
    29  	if ledgerconfig.IsCouchDBEnabled() {
    30  
    31  		database := "testcreatecouchdbconnectionanddb"
    32  		cleanup(database)
    33  		defer cleanup(database)
    34  		//create a new connection
    35  		couchInstance, err := CreateCouchInstance(couchDBDef.URL, couchDBDef.Username, couchDBDef.Password,
    36  			couchDBDef.MaxRetries, couchDBDef.MaxRetriesOnStartup, couchDBDef.RequestTimeout)
    37  		testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to CreateCouchInstance"))
    38  
    39  		_, err = CreateCouchDatabase(*couchInstance, database)
    40  		testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to CreateCouchDatabase"))
    41  	}
    42  
    43  }
    44  
    45  //Unit test of couch db util functionality
    46  func TestCreateCouchDBSystemDBs(t *testing.T) {
    47  	if ledgerconfig.IsCouchDBEnabled() {
    48  
    49  		database := "testcreatecouchdbsystemdb"
    50  		cleanup(database)
    51  		defer cleanup(database)
    52  
    53  		//create a new connection
    54  		couchInstance, err := CreateCouchInstance(couchDBDef.URL, couchDBDef.Username, couchDBDef.Password,
    55  			couchDBDef.MaxRetries, couchDBDef.MaxRetriesOnStartup, couchDBDef.RequestTimeout)
    56  
    57  		testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to CreateCouchInstance"))
    58  
    59  		err = CreateSystemDatabasesIfNotExist(*couchInstance)
    60  		testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to create system databases"))
    61  
    62  		db := CouchDatabase{CouchInstance: *couchInstance, DBName: "_users"}
    63  
    64  		//Retrieve the info for the new database and make sure the name matches
    65  		dbResp, _, errdb := db.GetDatabaseInfo()
    66  		testutil.AssertNoError(t, errdb, fmt.Sprintf("Error when trying to retrieve _users database information"))
    67  		testutil.AssertEquals(t, dbResp.DbName, "_users")
    68  
    69  		db = CouchDatabase{CouchInstance: *couchInstance, DBName: "_replicator"}
    70  
    71  		//Retrieve the info for the new database and make sure the name matches
    72  		dbResp, _, errdb = db.GetDatabaseInfo()
    73  		testutil.AssertNoError(t, errdb, fmt.Sprintf("Error when trying to retrieve _replicator database information"))
    74  		testutil.AssertEquals(t, dbResp.DbName, "_replicator")
    75  
    76  		db = CouchDatabase{CouchInstance: *couchInstance, DBName: "_global_changes"}
    77  
    78  		//Retrieve the info for the new database and make sure the name matches
    79  		dbResp, _, errdb = db.GetDatabaseInfo()
    80  		testutil.AssertNoError(t, errdb, fmt.Sprintf("Error when trying to retrieve _global_changes database information"))
    81  		testutil.AssertEquals(t, dbResp.DbName, "_global_changes")
    82  
    83  	}
    84  
    85  }
    86  func TestDatabaseMapping(t *testing.T) {
    87  
    88  	//create a new instance and database object using a database name mixed case
    89  	databaseName, err := mapAndValidateDatabaseName("testDB")
    90  	testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to map database name"))
    91  	testutil.AssertEquals(t, databaseName, "testdb")
    92  
    93  	//create a new instance and database object using a database name with numerics
    94  	databaseName, err = mapAndValidateDatabaseName("test1234DB")
    95  	testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to map database name"))
    96  	testutil.AssertEquals(t, databaseName, "test1234db")
    97  
    98  	//create a new instance and database object using a database name with special characters
    99  	databaseName, err = mapAndValidateDatabaseName("test1234_$(),+-/~!@#%^&*[]{}.")
   100  	testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to map database name"))
   101  	testutil.AssertEquals(t, databaseName, "test1234_$(),+-/_____________")
   102  
   103  	//create a new instance and database object using a database name with special characters
   104  	databaseName, err = mapAndValidateDatabaseName("5test1234")
   105  	testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to map database name"))
   106  	testutil.AssertEquals(t, databaseName, "db_5test1234")
   107  
   108  	//create a new instance and database object using an empty string
   109  	_, err = mapAndValidateDatabaseName("")
   110  	testutil.AssertError(t, err, fmt.Sprintf("Error should have been thrown for an invalid name"))
   111  
   112  	_, err = mapAndValidateDatabaseName("A12345678901234567890123456789012345678901234" +
   113  		"56789012345678901234567890123456789012345678901234567890123456789012345678901234567890" +
   114  		"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456" +
   115  		"78901234567890123456789012345678901234567890")
   116  	testutil.AssertError(t, err, fmt.Sprintf("Error should have been thrown for an invalid name"))
   117  
   118  }