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