github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/remotesrv/validate_test.go (about) 1 // Copyright 2023 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package remotesrv 16 17 import ( 18 "fmt" 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22 23 remotesapi "github.com/dolthub/dolt/go/gen/proto/dolt/services/remotesapi/v1alpha1" 24 "github.com/dolthub/dolt/go/store/hash" 25 ) 26 27 var GoodRepoPath = "dolthub/database" 28 var GoodRepoId = &remotesapi.RepoId{ 29 Org: "dolthub", 30 RepoName: "database", 31 } 32 33 var GoodHash = make([]byte, hash.ByteLen) 34 var ShortHash = make([]byte, hash.ByteLen-1) 35 var LongHash = make([]byte, hash.ByteLen+1) 36 37 func TestValidateGetRepoMetadataRequest(t *testing.T) { 38 for i, errMsg := range []*remotesapi.GetRepoMetadataRequest{ 39 {}, 40 { 41 RepoPath: GoodRepoPath, 42 ClientRepoFormat: &remotesapi.ClientRepoFormat{ 43 NbfVersion: "__DOLT__", 44 }, 45 }, 46 { 47 RepoPath: GoodRepoPath, 48 ClientRepoFormat: &remotesapi.ClientRepoFormat{ 49 NbfVersion: "__DOLT__", 50 NbsVersion: "UNKNOWN", 51 }, 52 }, 53 { 54 RepoPath: GoodRepoPath, 55 ClientRepoFormat: &remotesapi.ClientRepoFormat{ 56 NbsVersion: "5", 57 }, 58 }, 59 { 60 RepoPath: GoodRepoPath, 61 ClientRepoFormat: &remotesapi.ClientRepoFormat{ 62 NbfVersion: "__UNKNOWN__", 63 NbsVersion: "5", 64 }, 65 }, 66 { 67 RepoId: &remotesapi.RepoId{}, 68 ClientRepoFormat: &remotesapi.ClientRepoFormat{ 69 NbfVersion: "__DOLT__", 70 NbsVersion: "5", 71 }, 72 }, 73 { 74 RepoId: &remotesapi.RepoId{ 75 Org: "dolthub", 76 }, 77 ClientRepoFormat: &remotesapi.ClientRepoFormat{ 78 NbfVersion: "__DOLT__", 79 NbsVersion: "5", 80 }, 81 }, 82 { 83 RepoId: &remotesapi.RepoId{ 84 RepoName: "database", 85 }, 86 ClientRepoFormat: &remotesapi.ClientRepoFormat{ 87 NbfVersion: "__DOLT__", 88 NbsVersion: "5", 89 }, 90 }, 91 } { 92 t.Run(fmt.Sprintf("Error #%02d", i), func(t *testing.T) { 93 assert.Error(t, ValidateGetRepoMetadataRequest(errMsg), "%v should not validate", errMsg) 94 }) 95 } 96 for i, msg := range []*remotesapi.GetRepoMetadataRequest{ 97 { 98 RepoPath: GoodRepoPath, 99 ClientRepoFormat: &remotesapi.ClientRepoFormat{ 100 NbfVersion: "__DOLT__", 101 NbsVersion: "5", 102 }, 103 }, 104 { 105 RepoId: GoodRepoId, 106 ClientRepoFormat: &remotesapi.ClientRepoFormat{ 107 NbfVersion: "__DOLT__", 108 NbsVersion: "5", 109 }, 110 }, 111 { 112 RepoPath: GoodRepoPath, 113 ClientRepoFormat: &remotesapi.ClientRepoFormat{ 114 NbfVersion: "__DOLT__", 115 NbsVersion: "4", 116 }, 117 }, 118 { 119 RepoPath: GoodRepoPath, 120 ClientRepoFormat: &remotesapi.ClientRepoFormat{ 121 NbfVersion: "__LD_1__", 122 NbsVersion: "5", 123 }, 124 }, 125 } { 126 t.Run(fmt.Sprintf("NoError #%02d", i), func(t *testing.T) { 127 assert.NoError(t, ValidateGetRepoMetadataRequest(msg), "%v should validate", msg) 128 }) 129 } 130 } 131 132 func TestValidateHasChunksRequest(t *testing.T) { 133 for i, errMsg := range []*remotesapi.HasChunksRequest{ 134 {}, 135 { 136 RepoId: &remotesapi.RepoId{ 137 Org: "dolthub", 138 }, 139 Hashes: [][]byte{GoodHash}, 140 }, 141 { 142 RepoId: &remotesapi.RepoId{ 143 RepoName: "database", 144 }, 145 Hashes: [][]byte{GoodHash}, 146 }, 147 { 148 RepoPath: GoodRepoPath, 149 Hashes: [][]byte{ShortHash}, 150 }, 151 { 152 RepoPath: GoodRepoPath, 153 Hashes: [][]byte{LongHash}, 154 }, 155 { 156 RepoPath: GoodRepoPath, 157 Hashes: [][]byte{GoodHash, GoodHash, LongHash, GoodHash}, 158 }, 159 } { 160 t.Run(fmt.Sprintf("Error #%02d", i), func(t *testing.T) { 161 assert.Error(t, ValidateHasChunksRequest(errMsg), "%v should not validate", errMsg) 162 }) 163 } 164 for i, msg := range []*remotesapi.HasChunksRequest{ 165 { 166 RepoPath: GoodRepoPath, 167 }, 168 { 169 RepoId: GoodRepoId, 170 }, 171 { 172 RepoPath: GoodRepoPath, 173 Hashes: [][]byte{GoodHash}, 174 }, 175 } { 176 t.Run(fmt.Sprintf("NoError #%02d", i), func(t *testing.T) { 177 assert.NoError(t, ValidateHasChunksRequest(msg), "%v should validate", msg) 178 }) 179 } 180 } 181 182 func TestValidateGetDownloadLocsRequest(t *testing.T) { 183 for i, errMsg := range []*remotesapi.GetDownloadLocsRequest{ 184 {}, 185 { 186 RepoId: &remotesapi.RepoId{ 187 Org: "dolthub", 188 }, 189 ChunkHashes: [][]byte{GoodHash}, 190 }, 191 { 192 RepoId: &remotesapi.RepoId{ 193 RepoName: "database", 194 }, 195 ChunkHashes: [][]byte{GoodHash}, 196 }, 197 { 198 RepoPath: GoodRepoPath, 199 ChunkHashes: [][]byte{ShortHash}, 200 }, 201 { 202 RepoPath: GoodRepoPath, 203 ChunkHashes: [][]byte{LongHash}, 204 }, 205 { 206 RepoPath: GoodRepoPath, 207 ChunkHashes: [][]byte{GoodHash, GoodHash, LongHash, GoodHash}, 208 }, 209 } { 210 t.Run(fmt.Sprintf("Error #%02d", i), func(t *testing.T) { 211 assert.Error(t, ValidateGetDownloadLocsRequest(errMsg), "%v should not validate", errMsg) 212 }) 213 } 214 for i, msg := range []*remotesapi.GetDownloadLocsRequest{ 215 { 216 RepoPath: GoodRepoPath, 217 }, 218 { 219 RepoId: GoodRepoId, 220 }, 221 { 222 RepoPath: GoodRepoPath, 223 ChunkHashes: [][]byte{GoodHash}, 224 }, 225 } { 226 t.Run(fmt.Sprintf("NoError #%02d", i), func(t *testing.T) { 227 assert.NoError(t, ValidateGetDownloadLocsRequest(msg), "%v should validate", msg) 228 }) 229 } 230 } 231 232 func TestValidateGetUploadLocsRequest(t *testing.T) { 233 for i, errMsg := range []*remotesapi.GetUploadLocsRequest{ 234 {}, 235 { 236 RepoId: &remotesapi.RepoId{ 237 Org: "dolthub", 238 }, 239 TableFileHashes: [][]byte{GoodHash}, 240 }, 241 { 242 RepoId: &remotesapi.RepoId{ 243 RepoName: "database", 244 }, 245 TableFileHashes: [][]byte{GoodHash}, 246 }, 247 { 248 RepoPath: GoodRepoPath, 249 TableFileHashes: [][]byte{ShortHash}, 250 }, 251 { 252 RepoPath: GoodRepoPath, 253 TableFileHashes: [][]byte{LongHash}, 254 }, 255 { 256 RepoPath: GoodRepoPath, 257 TableFileHashes: [][]byte{GoodHash, GoodHash, LongHash, GoodHash}, 258 }, 259 } { 260 t.Run(fmt.Sprintf("Error #%02d", i), func(t *testing.T) { 261 assert.Error(t, ValidateGetUploadLocsRequest(errMsg), "%v should not validate", errMsg) 262 }) 263 } 264 for i, msg := range []*remotesapi.GetUploadLocsRequest{ 265 { 266 RepoPath: GoodRepoPath, 267 }, 268 { 269 RepoId: GoodRepoId, 270 }, 271 { 272 RepoPath: GoodRepoPath, 273 TableFileHashes: [][]byte{GoodHash}, 274 }, 275 } { 276 t.Run(fmt.Sprintf("NoError #%02d", i), func(t *testing.T) { 277 assert.NoError(t, ValidateGetUploadLocsRequest(msg), "%v should validate", msg) 278 }) 279 } 280 } 281 282 func TestValidateRebaseRequest(t *testing.T) { 283 for i, errMsg := range []*remotesapi.RebaseRequest{ 284 {}, 285 { 286 RepoId: &remotesapi.RepoId{ 287 Org: "dolthub", 288 }, 289 }, 290 { 291 RepoId: &remotesapi.RepoId{ 292 RepoName: "database", 293 }, 294 }, 295 } { 296 t.Run(fmt.Sprintf("Error #%02d", i), func(t *testing.T) { 297 assert.Error(t, ValidateRebaseRequest(errMsg), "%v should not validate", errMsg) 298 }) 299 } 300 for i, msg := range []*remotesapi.RebaseRequest{ 301 { 302 RepoPath: GoodRepoPath, 303 }, 304 } { 305 t.Run(fmt.Sprintf("NoError #%02d", i), func(t *testing.T) { 306 assert.NoError(t, ValidateRebaseRequest(msg), "%v should validate", msg) 307 }) 308 } 309 } 310 311 func TestValidateRootRequest(t *testing.T) { 312 for i, errMsg := range []*remotesapi.RootRequest{ 313 {}, 314 { 315 RepoId: &remotesapi.RepoId{ 316 Org: "dolthub", 317 }, 318 }, 319 { 320 RepoId: &remotesapi.RepoId{ 321 RepoName: "database", 322 }, 323 }, 324 } { 325 t.Run(fmt.Sprintf("Error #%02d", i), func(t *testing.T) { 326 assert.Error(t, ValidateRootRequest(errMsg), "%v should not validate", errMsg) 327 }) 328 } 329 for i, msg := range []*remotesapi.RootRequest{ 330 { 331 RepoPath: GoodRepoPath, 332 }, 333 { 334 RepoId: GoodRepoId, 335 }, 336 } { 337 t.Run(fmt.Sprintf("NoError #%02d", i), func(t *testing.T) { 338 assert.NoError(t, ValidateRootRequest(msg), "%v should validate", msg) 339 }) 340 } 341 } 342 343 func TestValidateCommitRequest(t *testing.T) { 344 for i, errMsg := range []*remotesapi.CommitRequest{ 345 {}, 346 { 347 RepoPath: GoodRepoPath, 348 Last: GoodHash, 349 }, 350 { 351 RepoPath: GoodRepoPath, 352 Current: GoodHash, 353 }, 354 { 355 RepoId: &remotesapi.RepoId{ 356 Org: "dolthub", 357 }, 358 Current: GoodHash, 359 Last: GoodHash, 360 }, 361 { 362 RepoId: &remotesapi.RepoId{ 363 RepoName: "database", 364 }, 365 Current: GoodHash, 366 Last: GoodHash, 367 }, 368 { 369 RepoId: GoodRepoId, 370 Current: GoodHash, 371 Last: GoodHash, 372 ChunkTableInfo: []*remotesapi.ChunkTableInfo{ 373 { 374 Hash: GoodHash, 375 }, 376 }, 377 }, 378 { 379 RepoId: GoodRepoId, 380 Current: GoodHash, 381 Last: GoodHash, 382 ChunkTableInfo: []*remotesapi.ChunkTableInfo{ 383 { 384 Hash: LongHash, 385 ChunkCount: 32, 386 }, 387 }, 388 }, 389 } { 390 t.Run(fmt.Sprintf("Error #%02d", i), func(t *testing.T) { 391 assert.Error(t, ValidateCommitRequest(errMsg), "%v should not validate", errMsg) 392 }) 393 } 394 for i, msg := range []*remotesapi.CommitRequest{ 395 { 396 RepoPath: GoodRepoPath, 397 Current: GoodHash, 398 Last: GoodHash, 399 }, 400 { 401 RepoId: GoodRepoId, 402 Current: GoodHash, 403 Last: GoodHash, 404 }, 405 { 406 RepoId: GoodRepoId, 407 Current: GoodHash, 408 Last: GoodHash, 409 ChunkTableInfo: []*remotesapi.ChunkTableInfo{ 410 { 411 Hash: GoodHash, 412 ChunkCount: 32, 413 }, 414 }, 415 }, 416 } { 417 t.Run(fmt.Sprintf("NoError #%02d", i), func(t *testing.T) { 418 assert.NoError(t, ValidateCommitRequest(msg), "%v should validate", msg) 419 }) 420 } 421 } 422 423 func TestValidateListTableFilesRequest(t *testing.T) { 424 for i, errMsg := range []*remotesapi.ListTableFilesRequest{ 425 {}, 426 { 427 RepoId: &remotesapi.RepoId{ 428 Org: "dolthub", 429 }, 430 }, 431 { 432 RepoId: &remotesapi.RepoId{ 433 RepoName: "database", 434 }, 435 }, 436 } { 437 t.Run(fmt.Sprintf("Error #%02d", i), func(t *testing.T) { 438 assert.Error(t, ValidateListTableFilesRequest(errMsg), "%v should not validate", errMsg) 439 }) 440 } 441 for i, msg := range []*remotesapi.ListTableFilesRequest{ 442 { 443 RepoPath: GoodRepoPath, 444 }, 445 { 446 RepoId: GoodRepoId, 447 }, 448 } { 449 t.Run(fmt.Sprintf("NoError #%02d", i), func(t *testing.T) { 450 assert.NoError(t, ValidateListTableFilesRequest(msg), "%v should validate", msg) 451 }) 452 } 453 } 454 455 func TestValidateRefreshTableFileUrlRequest(t *testing.T) { 456 for i, errMsg := range []*remotesapi.RefreshTableFileUrlRequest{ 457 {}, 458 { 459 RepoId: &remotesapi.RepoId{ 460 Org: "dolthub", 461 }, 462 }, 463 { 464 RepoId: &remotesapi.RepoId{ 465 RepoName: "database", 466 }, 467 }, 468 } { 469 t.Run(fmt.Sprintf("Error #%02d", i), func(t *testing.T) { 470 assert.Error(t, ValidateRefreshTableFileUrlRequest(errMsg), "%v should not validate", errMsg) 471 }) 472 } 473 for i, msg := range []*remotesapi.RefreshTableFileUrlRequest{} { 474 t.Run(fmt.Sprintf("NoError #%02d", i), func(t *testing.T) { 475 assert.NoError(t, ValidateRefreshTableFileUrlRequest(msg), "%v should validate", msg) 476 }) 477 } 478 } 479 480 func TestValidateAddTableFilesRequest(t *testing.T) { 481 for i, errMsg := range []*remotesapi.AddTableFilesRequest{ 482 {}, 483 { 484 RepoId: &remotesapi.RepoId{ 485 Org: "dolthub", 486 }, 487 }, 488 { 489 RepoId: &remotesapi.RepoId{ 490 RepoName: "database", 491 }, 492 }, 493 { 494 RepoId: GoodRepoId, 495 ChunkTableInfo: []*remotesapi.ChunkTableInfo{ 496 { 497 Hash: GoodHash, 498 }, 499 }, 500 }, 501 { 502 RepoId: GoodRepoId, 503 ChunkTableInfo: []*remotesapi.ChunkTableInfo{ 504 { 505 Hash: LongHash, 506 ChunkCount: 32, 507 }, 508 }, 509 }, 510 } { 511 t.Run(fmt.Sprintf("Error #%02d", i), func(t *testing.T) { 512 assert.Error(t, ValidateAddTableFilesRequest(errMsg), "%v should not validate", errMsg) 513 }) 514 } 515 for i, msg := range []*remotesapi.AddTableFilesRequest{ 516 { 517 RepoPath: GoodRepoPath, 518 }, 519 { 520 RepoId: GoodRepoId, 521 }, 522 { 523 RepoId: GoodRepoId, 524 ChunkTableInfo: []*remotesapi.ChunkTableInfo{ 525 { 526 Hash: GoodHash, 527 ChunkCount: 32, 528 }, 529 }, 530 }, 531 } { 532 t.Run(fmt.Sprintf("NoError #%02d", i), func(t *testing.T) { 533 assert.NoError(t, ValidateAddTableFilesRequest(msg), "%v should validate", msg) 534 }) 535 } 536 }