MCPcopy
hub / github.com/ipfs/kubo / TestValidateImportConfig_HashFunction

Function TestValidateImportConfig_HashFunction

config/import_test.go:275–324  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

273}
274
275func TestValidateImportConfig_HashFunction(t *testing.T) {
276 tests := []struct {
277 name string
278 hashFunc string
279 wantErr bool
280 errMsg string
281 }{
282 {name: "valid sha2-256", hashFunc: "sha2-256", wantErr: false},
283 {name: "valid sha2-512", hashFunc: "sha2-512", wantErr: false},
284 {name: "valid sha3-256", hashFunc: "sha3-256", wantErr: false},
285 {name: "valid blake2b-256", hashFunc: "blake2b-256", wantErr: false},
286 {name: "valid blake3", hashFunc: "blake3", wantErr: false},
287 {name: "invalid unknown", hashFunc: "unknown-hash", wantErr: true, errMsg: "unrecognized"},
288 {name: "invalid empty", hashFunc: "", wantErr: true, errMsg: "unrecognized"},
289 }
290
291 // Check for hashes that exist but are not allowed
292 // MD5 should exist but not be allowed
293 if code, ok := mh.Names["md5"]; ok {
294 tests = append(tests, struct {
295 name string
296 hashFunc string
297 wantErr bool
298 errMsg string
299 }{name: "md5 not allowed", hashFunc: "md5", wantErr: true, errMsg: "not allowed"})
300 _ = code // use the variable
301 }
302
303 for _, tt := range tests {
304 t.Run(tt.name, func(t *testing.T) {
305 cfg := &Import{
306 HashFunction: *NewOptionalString(tt.hashFunc),
307 }
308
309 err := ValidateImportConfig(cfg)
310
311 if tt.wantErr {
312 if err == nil {
313 t.Errorf("ValidateImportConfig() expected error for hashFunc=%s, got nil", tt.hashFunc)
314 } else if tt.errMsg != "" && !strings.Contains(err.Error(), tt.errMsg) {
315 t.Errorf("ValidateImportConfig() error = %v, want error containing %q", err, tt.errMsg)
316 }
317 } else {
318 if err != nil {
319 t.Errorf("ValidateImportConfig() unexpected error for hashFunc=%s: %v", tt.hashFunc, err)
320 }
321 }
322 })
323 }
324}
325
326func TestValidateImportConfig_DefaultValue(t *testing.T) {
327 // Test that default (unset) value doesn't trigger validation

Callers

nothing calls this directly

Calls 4

NewOptionalStringFunction · 0.85
ValidateImportConfigFunction · 0.85
RunMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected