|
4 | 4 | package proteus_test |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "crypto/rand" |
| 8 | + "crypto/rsa" |
| 9 | + "crypto/x509" |
| 10 | + "encoding/pem" |
7 | 11 | "math" |
8 | 12 | "net/url" |
9 | 13 | "strings" |
@@ -317,3 +321,126 @@ func (t testWriter) Write(v []byte) (int, error) { |
317 | 321 | t.t.Logf("%s", v) |
318 | 322 | return len(v), nil |
319 | 323 | } |
| 324 | + |
| 325 | +func TestRSAPrivateKey(t *testing.T) { |
| 326 | + _, privateKeyStr := generateTestKey(t) |
| 327 | + defaultKey, _ := generateTestKey(t) |
| 328 | + |
| 329 | + tests := []struct { |
| 330 | + name string |
| 331 | + params types.ParamValues |
| 332 | + shouldErr bool |
| 333 | + optionalIsNil bool |
| 334 | + useDefault bool |
| 335 | + }{ |
| 336 | + { |
| 337 | + name: "valid key for optional and required", |
| 338 | + params: types.ParamValues{ |
| 339 | + "": { |
| 340 | + "optionalkey": privateKeyStr, |
| 341 | + "requiredkey": privateKeyStr, |
| 342 | + }, |
| 343 | + }, |
| 344 | + shouldErr: false, |
| 345 | + optionalIsNil: false, |
| 346 | + }, |
| 347 | + { |
| 348 | + name: "empty string for optional key", |
| 349 | + params: types.ParamValues{ |
| 350 | + "": { |
| 351 | + "optionalkey": "", |
| 352 | + "requiredkey": privateKeyStr, |
| 353 | + }, |
| 354 | + }, |
| 355 | + shouldErr: false, |
| 356 | + optionalIsNil: true, |
| 357 | + }, |
| 358 | + { |
| 359 | + name: "empty string for optional key with default", |
| 360 | + params: types.ParamValues{ |
| 361 | + "": { |
| 362 | + "optionalkey": "", |
| 363 | + "requiredkey": privateKeyStr, |
| 364 | + }, |
| 365 | + }, |
| 366 | + shouldErr: false, |
| 367 | + optionalIsNil: false, |
| 368 | + useDefault: true, |
| 369 | + }, |
| 370 | + { |
| 371 | + name: "no value for optional key", |
| 372 | + params: types.ParamValues{ |
| 373 | + "": { |
| 374 | + "requiredkey": privateKeyStr, |
| 375 | + }, |
| 376 | + }, |
| 377 | + shouldErr: false, |
| 378 | + optionalIsNil: true, |
| 379 | + }, |
| 380 | + { |
| 381 | + name: "empty string for required key", |
| 382 | + params: types.ParamValues{ |
| 383 | + "": { |
| 384 | + "requiredkey": "", |
| 385 | + }, |
| 386 | + }, |
| 387 | + shouldErr: true, |
| 388 | + }, |
| 389 | + { |
| 390 | + name: "no value for required key", |
| 391 | + params: types.ParamValues{"": {}}, |
| 392 | + shouldErr: true, |
| 393 | + }, |
| 394 | + } |
| 395 | + |
| 396 | + for _, tt := range tests { |
| 397 | + t.Run(tt.name, func(t *testing.T) { |
| 398 | + cfg := struct { |
| 399 | + OptionalKey *xtypes.RSAPrivateKey `param:",optional"` |
| 400 | + RequiredKey *xtypes.RSAPrivateKey |
| 401 | + }{} |
| 402 | + |
| 403 | + if tt.useDefault { |
| 404 | + cfg.OptionalKey = &xtypes.RSAPrivateKey{DefaultValue: defaultKey} |
| 405 | + } |
| 406 | + |
| 407 | + testProvider := cfgtest.New(tt.params) |
| 408 | + defer testProvider.Stop() |
| 409 | + |
| 410 | + _, err := proteus.MustParse(&cfg, |
| 411 | + proteus.WithProviders(testProvider)) |
| 412 | + |
| 413 | + if tt.shouldErr { |
| 414 | + assert.Error(t, err) |
| 415 | + } else { |
| 416 | + assert.NoError(t, err) |
| 417 | + if tt.useDefault { |
| 418 | + assert.Equal(t, defaultKey, cfg.OptionalKey.Value()) |
| 419 | + } else if tt.optionalIsNil { |
| 420 | + assert.Equal(t, nil, cfg.OptionalKey.Value()) |
| 421 | + } else { |
| 422 | + assert.NotNil(t, cfg.OptionalKey.Value()) |
| 423 | + } |
| 424 | + |
| 425 | + if _, ok := tt.params[""]["requiredkey"]; ok && tt.params[""]["requiredkey"] != "" { |
| 426 | + assert.NotNil(t, cfg.RequiredKey.Value()) |
| 427 | + } |
| 428 | + } |
| 429 | + }) |
| 430 | + } |
| 431 | +} |
| 432 | + |
| 433 | +func generateTestKey(t *testing.T) (*rsa.PrivateKey, string) { |
| 434 | + t.Helper() |
| 435 | + privateKey, err := rsa.GenerateKey(rand.Reader, 2048) |
| 436 | + if err != nil { |
| 437 | + t.Fatalf("failed to generate RSA private key: %v", err) |
| 438 | + } |
| 439 | + privateKeyPEM := &pem.Block{ |
| 440 | + Type: "RSA PRIVATE KEY", |
| 441 | + Bytes: x509.MarshalPKCS1PrivateKey(privateKey), |
| 442 | + } |
| 443 | + return privateKey, string(pem.EncodeToMemory(privateKeyPEM)) |
| 444 | +} |
| 445 | + |
| 446 | + |
0 commit comments