github.com/lzy4123/fabric@v2.1.1+incompatible/internal/ccmetadata/validators_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package ccmetadata 8 9 import ( 10 "fmt" 11 "os" 12 "path/filepath" 13 "testing" 14 15 "github.com/stretchr/testify/assert" 16 ) 17 18 var packageTestDir = filepath.Join(os.TempDir(), "ccmetadata-validator-test") 19 20 func TestGoodIndexJSON(t *testing.T) { 21 testDir := filepath.Join(packageTestDir, "GoodIndexJSON") 22 cleanupDir(testDir) 23 defer cleanupDir(testDir) 24 25 fileName := "META-INF/statedb/couchdb/indexes/myIndex.json" 26 fileBytes := []byte(`{"index":{"fields":["data.docType","data.owner"]},"name":"indexOwner","type":"json"}`) 27 28 err := ValidateMetadataFile(fileName, fileBytes) 29 assert.NoError(t, err, "Error validating a good index") 30 } 31 32 func TestBadIndexJSON(t *testing.T) { 33 testDir := filepath.Join(packageTestDir, "BadIndexJSON") 34 cleanupDir(testDir) 35 defer cleanupDir(testDir) 36 37 fileName := "META-INF/statedb/couchdb/indexes/myIndex.json" 38 fileBytes := []byte("invalid json") 39 40 err := ValidateMetadataFile(fileName, fileBytes) 41 42 assert.Error(t, err, "Should have received an InvalidIndexContentError") 43 44 // Type assertion on InvalidIndexContentError 45 _, ok := err.(*InvalidIndexContentError) 46 assert.True(t, ok, "Should have received an InvalidIndexContentError") 47 48 t.Log("SAMPLE ERROR STRING:", err.Error()) 49 } 50 51 func TestIndexWrongLocation(t *testing.T) { 52 testDir := filepath.Join(packageTestDir, "IndexWrongLocation") 53 cleanupDir(testDir) 54 defer cleanupDir(testDir) 55 56 fileName := "META-INF/statedb/couchdb/myIndex.json" 57 fileBytes := []byte(`{"index":{"fields":["data.docType","data.owner"]},"name":"indexOwner","type":"json"}`) 58 59 err := ValidateMetadataFile(fileName, fileBytes) 60 assert.Error(t, err, "Should have received an UnhandledDirectoryError") 61 62 // Type assertion on UnhandledDirectoryError 63 _, ok := err.(*UnhandledDirectoryError) 64 assert.True(t, ok, "Should have received an UnhandledDirectoryError") 65 66 t.Log("SAMPLE ERROR STRING:", err.Error()) 67 } 68 69 func TestInvalidMetadataType(t *testing.T) { 70 testDir := filepath.Join(packageTestDir, "InvalidMetadataType") 71 cleanupDir(testDir) 72 defer cleanupDir(testDir) 73 74 fileName := "myIndex.json" 75 fileBytes := []byte(`{"index":{"fields":["data.docType","data.owner"]},"name":"indexOwner","type":"json"}`) 76 77 err := ValidateMetadataFile(fileName, fileBytes) 78 assert.Error(t, err, "Should have received an UnhandledDirectoryError") 79 80 // Type assertion on UnhandledDirectoryError 81 _, ok := err.(*UnhandledDirectoryError) 82 assert.True(t, ok, "Should have received an UnhandledDirectoryError") 83 } 84 85 func TestBadMetadataExtension(t *testing.T) { 86 testDir := filepath.Join(packageTestDir, "BadMetadataExtension") 87 cleanupDir(testDir) 88 defer cleanupDir(testDir) 89 90 fileName := "myIndex.go" 91 fileBytes := []byte(`{"index":{"fields":["data.docType","data.owner"]},"name":"indexOwner","type":"json"}`) 92 93 err := ValidateMetadataFile(fileName, fileBytes) 94 assert.Error(t, err, "Should have received an error") 95 96 } 97 98 func TestBadFilePaths(t *testing.T) { 99 testDir := filepath.Join(packageTestDir, "BadMetadataExtension") 100 cleanupDir(testDir) 101 defer cleanupDir(testDir) 102 103 // Test bad META-INF 104 fileName := "META-INF1/statedb/couchdb/indexes/test1.json" 105 fileBytes := []byte(`{"index":{"fields":["data.docType","data.owner"]},"name":"indexOwner","type":"json"}`) 106 107 err := ValidateMetadataFile(fileName, fileBytes) 108 fmt.Println(err) 109 assert.Error(t, err, "Should have received an error for bad META-INF directory") 110 111 // Test bad path length 112 fileName = "META-INF/statedb/test1.json" 113 fileBytes = []byte(`{"index":{"fields":["data.docType","data.owner"]},"name":"indexOwner","type":"json"}`) 114 115 err = ValidateMetadataFile(fileName, fileBytes) 116 fmt.Println(err) 117 assert.Error(t, err, "Should have received an error for bad length") 118 119 // Test invalid database name 120 fileName = "META-INF/statedb/goleveldb/indexes/test1.json" 121 fileBytes = []byte(`{"index":{"fields":["data.docType","data.owner"]},"name":"indexOwner","type":"json"}`) 122 123 err = ValidateMetadataFile(fileName, fileBytes) 124 fmt.Println(err) 125 assert.Error(t, err, "Should have received an error for invalid database") 126 127 // Test invalid indexes directory name 128 fileName = "META-INF/statedb/couchdb/index/test1.json" 129 fileBytes = []byte(`{"index":{"fields":["data.docType","data.owner"]},"name":"indexOwner","type":"json"}`) 130 131 err = ValidateMetadataFile(fileName, fileBytes) 132 fmt.Println(err) 133 assert.Error(t, err, "Should have received an error for invalid indexes directory") 134 135 // Test invalid collections directory name 136 fileName = "META-INF/statedb/couchdb/collection/testcoll/indexes/test1.json" 137 fileBytes = []byte(`{"index":{"fields":["data.docType","data.owner"]},"name":"indexOwner","type":"json"}`) 138 139 err = ValidateMetadataFile(fileName, fileBytes) 140 fmt.Println(err) 141 assert.Error(t, err, "Should have received an error for invalid collections directory") 142 143 // Test valid collections name 144 fileName = "META-INF/statedb/couchdb/collections/testcoll/indexes/test1.json" 145 fileBytes = []byte(`{"index":{"fields":["data.docType","data.owner"]},"name":"indexOwner","type":"json"}`) 146 147 err = ValidateMetadataFile(fileName, fileBytes) 148 fmt.Println(err) 149 assert.NoError(t, err, "Error should not have been thrown for a valid collection name") 150 151 // Test invalid collections name 152 fileName = "META-INF/statedb/couchdb/collections/#testcoll/indexes/test1.json" 153 fileBytes = []byte(`{"index":{"fields":["data.docType","data.owner"]},"name":"indexOwner","type":"json"}`) 154 155 err = ValidateMetadataFile(fileName, fileBytes) 156 fmt.Println(err) 157 assert.Error(t, err, "Should have received an error for an invalid collection name") 158 159 // Test invalid collections name 160 fileName = "META-INF/statedb/couchdb/collections/testcoll/indexes/test1.txt" 161 fileBytes = []byte(`{"index":{"fields":["data.docType","data.owner"]},"name":"indexOwner","type":"json"}`) 162 163 err = ValidateMetadataFile(fileName, fileBytes) 164 fmt.Println(err) 165 assert.Error(t, err, "Should have received an error for an invalid file name") 166 167 } 168 169 func TestIndexValidation(t *testing.T) { 170 171 // Test valid index with field sorts 172 indexDef := []byte(`{"index":{"fields":[{"size":"desc"}, {"color":"desc"}]},"ddoc":"indexSizeSortName", "name":"indexSizeSortName","type":"json"}`) 173 _, indexDefinition := isJSON(indexDef) 174 err := validateIndexJSON(indexDefinition) 175 assert.NoError(t, err) 176 177 // Test valid index without field sorts 178 indexDef = []byte(`{"index":{"fields":["size","color"]},"ddoc":"indexSizeSortName", "name":"indexSizeSortName","type":"json"}`) 179 _, indexDefinition = isJSON(indexDef) 180 err = validateIndexJSON(indexDefinition) 181 assert.NoError(t, err) 182 183 // Test valid index without design doc, name and type 184 indexDef = []byte(`{"index":{"fields":["size","color"]}}`) 185 _, indexDefinition = isJSON(indexDef) 186 err = validateIndexJSON(indexDefinition) 187 assert.NoError(t, err) 188 189 // Test valid index with partial filter selector (only tests that it will not return error if included) 190 indexDef = []byte(`{ 191 "index": { 192 "partial_filter_selector": { 193 "status": { 194 "$ne": "archived" 195 } 196 }, 197 "fields": ["type"] 198 }, 199 "ddoc" : "type-not-archived", 200 "type" : "json" 201 }`) 202 _, indexDefinition = isJSON(indexDef) 203 err = validateIndexJSON(indexDefinition) 204 assert.NoError(t, err) 205 206 } 207 208 func TestIndexValidationInvalidParameters(t *testing.T) { 209 210 // Test numeric values passed in for parameters 211 indexDef := []byte(`{"index":{"fields":[{"size":"desc"}, {"color":"desc"}]},"ddoc":1, "name":"indexSizeSortName","type":"json"}`) 212 _, indexDefinition := isJSON(indexDef) 213 err := validateIndexJSON(indexDefinition) 214 assert.Error(t, err, "Error should have been thrown for numeric design doc") 215 216 // Test invalid design doc parameter 217 indexDef = []byte(`{"index":{"fields":["size","color"]},"ddoc1":"indexSizeSortName", "name":"indexSizeSortName","type":"json"}`) 218 _, indexDefinition = isJSON(indexDef) 219 err = validateIndexJSON(indexDefinition) 220 assert.Error(t, err, "Error should have been thrown for invalid design doc parameter") 221 222 // Test invalid name parameter 223 indexDef = []byte(`{"index":{"fields":["size","color"]},"ddoc":"indexSizeSortName", "name1":"indexSizeSortName","type":"json"}`) 224 _, indexDefinition = isJSON(indexDef) 225 err = validateIndexJSON(indexDefinition) 226 assert.Error(t, err, "Error should have been thrown for invalid name parameter") 227 228 // Test invalid type parameter, numeric 229 indexDef = []byte(`{"index":{"fields":["size","color"]},"ddoc":"indexSizeSortName", "name":"indexSizeSortName","type":1}`) 230 _, indexDefinition = isJSON(indexDef) 231 err = validateIndexJSON(indexDefinition) 232 assert.Error(t, err, "Error should have been thrown for numeric type parameter") 233 234 // Test invalid type parameter 235 indexDef = []byte(`{"index":{"fields":["size","color"]},"ddoc":"indexSizeSortName", "name":"indexSizeSortName","type":"text"}`) 236 _, indexDefinition = isJSON(indexDef) 237 err = validateIndexJSON(indexDefinition) 238 assert.Error(t, err, "Error should have been thrown for invalid type parameter") 239 240 // Test invalid index parameter 241 indexDef = []byte(`{"index1":{"fields":["size","color"]},"ddoc":"indexSizeSortName", "name":"indexSizeSortName","type":"json"}`) 242 _, indexDefinition = isJSON(indexDef) 243 err = validateIndexJSON(indexDefinition) 244 assert.Error(t, err, "Error should have been thrown for invalid index parameter") 245 246 // Test missing index parameter 247 indexDef = []byte(`{"ddoc":"indexSizeSortName", "name":"indexSizeSortName","type":"json"}`) 248 _, indexDefinition = isJSON(indexDef) 249 err = validateIndexJSON(indexDefinition) 250 assert.Error(t, err, "Error should have been thrown for missing index parameter") 251 252 } 253 254 func TestIndexValidationInvalidFields(t *testing.T) { 255 256 // Test invalid fields parameter 257 indexDef := []byte(`{"index":{"fields1":[{"size":"desc"}, {"color":"desc"}]},"ddoc":"indexSizeSortName", "name":"indexSizeSortName","type":"json"}`) 258 _, indexDefinition := isJSON(indexDef) 259 err := validateIndexJSON(indexDefinition) 260 assert.Error(t, err, "Error should have been thrown for invalid fields parameter") 261 262 // Test invalid field name (numeric) 263 indexDef = []byte(`{"index":{"fields":["size", 1]},"ddoc1":"indexSizeSortName", "name":"indexSizeSortName","type":"json"}`) 264 _, indexDefinition = isJSON(indexDef) 265 err = validateIndexJSON(indexDefinition) 266 assert.Error(t, err, "Error should have been thrown for field name defined as numeric") 267 268 // Test invalid field sort 269 indexDef = []byte(`{"index":{"fields":[{"size":"desc1"}, {"color":"desc"}]},"ddoc":"indexSizeSortName", "name":"indexSizeSortName","type":"json"}`) 270 _, indexDefinition = isJSON(indexDef) 271 err = validateIndexJSON(indexDefinition) 272 assert.Error(t, err, "Error should have been thrown for invalid field sort") 273 274 // Test numeric in sort 275 indexDef = []byte(`{"index":{"fields":[{"size":1}, {"color":"desc"}]},"ddoc":"indexSizeSortName", "name":"indexSizeSortName","type":"json"}`) 276 _, indexDefinition = isJSON(indexDef) 277 err = validateIndexJSON(indexDefinition) 278 assert.Error(t, err, "Error should have been thrown for a numeric in field sort") 279 280 // Test invalid json for fields 281 indexDef = []byte(`{"index":{"fields":"size"},"ddoc":"indexSizeSortName", "name":"indexSizeSortName","type":"json"}`) 282 _, indexDefinition = isJSON(indexDef) 283 err = validateIndexJSON(indexDefinition) 284 assert.Error(t, err, "Error should have been thrown for invalid field json") 285 286 // Test missing JSON for fields 287 indexDef = []byte(`{"index":"fields","ddoc":"indexSizeSortName", "name":"indexSizeSortName","type":"json"}`) 288 _, indexDefinition = isJSON(indexDef) 289 err = validateIndexJSON(indexDefinition) 290 assert.Error(t, err, "Error should have been thrown for missing JSON for fields") 291 292 } 293 294 func cleanupDir(dir string) error { 295 // clean up any previous files 296 err := os.RemoveAll(dir) 297 if err != nil { 298 return nil 299 } 300 return os.Mkdir(dir, os.ModePerm) 301 }