github.com/kaituanwang/hyperledger@v2.0.1+incompatible/core/ledger/util/couchdb/couchdbutil_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 SPDX-License-Identifier: Apache-2.0 4 */ 5 6 package couchdb 7 8 import ( 9 "encoding/hex" 10 "testing" 11 12 "github.com/hyperledger/fabric/common/metrics/disabled" 13 "github.com/hyperledger/fabric/common/util" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 //Unit test of couch db util functionality 18 func TestCreateCouchDBConnectionAndDB(t *testing.T) { 19 20 database := "testcreatecouchdbconnectionanddb" 21 cleanup(database) 22 defer cleanup(database) 23 //create a new connection 24 couchInstance, err := CreateCouchInstance(testConfig(), &disabled.Provider{}) 25 assert.NoError(t, err, "Error when trying to CreateCouchInstance") 26 27 _, err = CreateCouchDatabase(couchInstance, database) 28 assert.NoError(t, err, "Error when trying to CreateCouchDatabase") 29 30 } 31 32 //Unit test of couch db util functionality 33 func TestNotCreateCouchGlobalChangesDB(t *testing.T) { 34 config := testConfig() 35 config.CreateGlobalChangesDB = false 36 database := "_global_changes" 37 cleanup(database) 38 defer cleanup(database) 39 40 //create a new connection 41 couchInstance, err := CreateCouchInstance(config, &disabled.Provider{}) 42 assert.NoError(t, err, "Error when trying to CreateCouchInstance") 43 44 db := CouchDatabase{CouchInstance: couchInstance, DBName: database} 45 46 //Retrieve the info for the new database and make sure the name matches 47 _, _, errdb := db.GetDatabaseInfo() 48 assert.NotNil(t, errdb) 49 } 50 51 //Unit test of couch db util functionality 52 func TestCreateCouchDBSystemDBs(t *testing.T) { 53 54 database := "testcreatecouchdbsystemdb" 55 cleanup(database) 56 defer cleanup(database) 57 58 //create a new connection 59 couchInstance, err := CreateCouchInstance(testConfig(), &disabled.Provider{}) 60 61 assert.NoError(t, err, "Error when trying to CreateCouchInstance") 62 63 err = CreateSystemDatabasesIfNotExist(couchInstance) 64 assert.NoError(t, err, "Error when trying to create system databases") 65 66 db := CouchDatabase{CouchInstance: couchInstance, DBName: "_users"} 67 68 //Retrieve the info for the new database and make sure the name matches 69 dbResp, _, errdb := db.GetDatabaseInfo() 70 assert.NoError(t, errdb, "Error when trying to retrieve _users database information") 71 assert.Equal(t, "_users", dbResp.DbName) 72 73 db = CouchDatabase{CouchInstance: couchInstance, DBName: "_replicator"} 74 75 //Retrieve the info for the new database and make sure the name matches 76 dbResp, _, errdb = db.GetDatabaseInfo() 77 assert.NoError(t, errdb, "Error when trying to retrieve _replicator database information") 78 assert.Equal(t, "_replicator", dbResp.DbName) 79 80 db = CouchDatabase{CouchInstance: couchInstance, DBName: "_global_changes"} 81 82 //Retrieve the info for the new database and make sure the name matches 83 dbResp, _, errdb = db.GetDatabaseInfo() 84 assert.NoError(t, errdb, "Error when trying to retrieve _global_changes database information") 85 assert.Equal(t, "_global_changes", dbResp.DbName) 86 87 } 88 89 func TestDatabaseMapping(t *testing.T) { 90 //create a new instance and database object using a database name mixed case 91 _, err := mapAndValidateDatabaseName("testDB") 92 assert.Error(t, err, "Error expected because the name contains capital letters") 93 94 //create a new instance and database object using a database name with special characters 95 _, err = mapAndValidateDatabaseName("test1234/1") 96 assert.Error(t, err, "Error expected because the name contains illegal chars") 97 98 //create a new instance and database object using a database name with special characters 99 _, err = mapAndValidateDatabaseName("5test1234") 100 assert.Error(t, err, "Error expected because the name starts with a number") 101 102 //create a new instance and database object using an empty string 103 _, err = mapAndValidateDatabaseName("") 104 assert.Error(t, err, "Error should have been thrown for an invalid name") 105 106 _, err = mapAndValidateDatabaseName("a12345678901234567890123456789012345678901234" + 107 "56789012345678901234567890123456789012345678901234567890123456789012345678901234567890" + 108 "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456" + 109 "78901234567890123456789012345678901234567890") 110 assert.Error(t, err, "Error should have been thrown for an invalid name") 111 112 transformedName, err := mapAndValidateDatabaseName("test.my.db-1") 113 assert.NoError(t, err, "") 114 assert.Equal(t, "test$my$db-1", transformedName) 115 } 116 117 func TestConstructMetadataDBName(t *testing.T) { 118 // Allowed pattern for chainName: [a-z][a-z0-9.-] 119 chainName := "tob2g.y-z0f.qwp-rq5g4-ogid5g6oucyryg9sc16mz0t4vuake5q557esz7sn493nf0ghch0xih6dwuirokyoi4jvs67gh6r5v6mhz3-292un2-9egdcs88cstg3f7xa9m1i8v4gj0t3jedsm-woh3kgiqehwej6h93hdy5tr4v.1qmmqjzz0ox62k.507sh3fkw3-mfqh.ukfvxlm5szfbwtpfkd1r4j.cy8oft5obvwqpzjxb27xuw6" 120 121 truncatedChainName := "tob2g.y-z0f.qwp-rq5g4-ogid5g6oucyryg9sc16mz0t4vuak" 122 assert.Equal(t, chainNameAllowedLength, len(truncatedChainName)) 123 124 // <first 50 chars (i.e., chainNameAllowedLength) of chainName> + 1 char for '(' + <64 chars for SHA256 hash 125 // (hex encoding) of untruncated chainName> + 1 char for ')' + 1 char for '_' = 117 chars 126 hash := hex.EncodeToString(util.ComputeSHA256([]byte(chainName))) 127 expectedDBName := truncatedChainName + "(" + hash + ")" + "_" 128 expectedDBNameLength := 117 129 130 constructedDBName := ConstructMetadataDBName(chainName) 131 assert.Equal(t, expectedDBNameLength, len(constructedDBName)) 132 assert.Equal(t, expectedDBName, constructedDBName) 133 } 134 135 func TestConstructedNamespaceDBName(t *testing.T) { 136 // === SCENARIO 1: chainName_ns$$coll === 137 138 // Allowed pattern for chainName: [a-z][a-z0-9.-] 139 chainName := "tob2g.y-z0f.qwp-rq5g4-ogid5g6oucyryg9sc16mz0t4vuake5q557esz7sn493nf0ghch0xih6dwuirokyoi4jvs67gh6r5v6mhz3-292un2-9egdcs88cstg3f7xa9m1i8v4gj0t3jedsm-woh3kgiqehwej6h93hdy5tr4v.1qmmqjzz0ox62k.507sh3fkw3-mfqh.ukfvxlm5szfbwtpfkd1r4j.cy8oft5obvwqpzjxb27xuw6" 140 141 // Allowed pattern for namespace and collection: [a-zA-Z0-9_-] 142 ns := "wMCnSXiV9YoIqNQyNvFVTdM8XnUtvrOFFIWsKelmP5NEszmNLl8YhtOKbFu3P_NgwgsYF8PsfwjYCD8f1XRpANQLoErDHwLlweryqXeJ6vzT2x0pS_GwSx0m6tBI0zOmHQOq_2De8A87x6zUOPwufC2T6dkidFxiuq8Sey2-5vUo_iNKCij3WTeCnKx78PUIg_U1gp4_0KTvYVtRBRvH0kz5usizBxPaiFu3TPhB9XLviScvdUVSbSYJ0Z" 143 // first letter 'p' denotes private data namespace. We can use 'h' to denote hashed data namespace as defined in 144 // privacyenabledstate/common_storage_db.go 145 coll := "pvWjtfSTXVK8WJus5s6zWoMIciXd7qHRZIusF9SkOS6m8XuHCiJDE9cCRuVerq22Na8qBL2ywDGFpVMIuzfyEXLjeJb0mMuH4cwewT6r1INOTOSYwrikwOLlT_fl0V1L7IQEwUBB8WCvRqSdj6j5-E5aGul_pv_0UeCdwWiyA_GrZmP7ocLzfj2vP8btigrajqdH-irLO2ydEjQUAvf8fiuxru9la402KmKRy457GgI98UHoUdqV3f3FCdR" 146 147 truncatedChainName := "tob2g.y-z0f.qwp-rq5g4-ogid5g6oucyryg9sc16mz0t4vuak" 148 truncatedEscapedNs := "w$m$cn$s$xi$v9$yo$iq$n$qy$nv$f$v$td$m8$xn$utvr$o$f" 149 truncatedEscapedColl := "pv$wjtf$s$t$x$v$k8$w$jus5s6z$wo$m$ici$xd7q$h$r$z$i" 150 assert.Equal(t, chainNameAllowedLength, len(truncatedChainName)) 151 assert.Equal(t, namespaceNameAllowedLength, len(truncatedEscapedNs)) 152 assert.Equal(t, collectionNameAllowedLength, len(truncatedEscapedColl)) 153 154 untruncatedDBName := chainName + "_" + ns + "$$" + coll 155 hash := hex.EncodeToString(util.ComputeSHA256([]byte(untruncatedDBName))) 156 expectedDBName := truncatedChainName + "_" + truncatedEscapedNs + "$$" + truncatedEscapedColl + "(" + hash + ")" 157 // <first 50 chars (i.e., chainNameAllowedLength) of chainName> + 1 char for '_' + <first 50 chars 158 // (i.e., namespaceNameAllowedLength) of escaped namespace> + 2 chars for '$$' + <first 50 chars 159 // (i.e., collectionNameAllowedLength) of escaped collection> + 1 char for '(' + <64 chars for SHA256 hash 160 // (hex encoding) of untruncated chainName_ns$$coll> + 1 char for ')' = 219 chars 161 expectedDBNameLength := 219 162 163 namespace := ns + "$$" + coll 164 constructedDBName := ConstructNamespaceDBName(chainName, namespace) 165 assert.Equal(t, expectedDBNameLength, len(constructedDBName)) 166 assert.Equal(t, expectedDBName, constructedDBName) 167 168 // === SCENARIO 2: chainName_ns === 169 170 untruncatedDBName = chainName + "_" + ns 171 hash = hex.EncodeToString(util.ComputeSHA256([]byte(untruncatedDBName))) 172 expectedDBName = truncatedChainName + "_" + truncatedEscapedNs + "(" + hash + ")" 173 // <first 50 chars (i.e., chainNameAllowedLength) of chainName> + 1 char for '_' + <first 50 chars 174 // (i.e., namespaceNameAllowedLength) of escaped namespace> + 1 char for '(' + <64 chars for SHA256 hash 175 // (hex encoding) of untruncated chainName_ns> + 1 char for ')' = 167 chars 176 expectedDBNameLength = 167 177 178 namespace = ns 179 constructedDBName = ConstructNamespaceDBName(chainName, namespace) 180 assert.Equal(t, expectedDBNameLength, len(constructedDBName)) 181 assert.Equal(t, expectedDBName, constructedDBName) 182 }