@@ -525,6 +525,22 @@ func TestHideConfigSecrets(t *testing.T) {
525525 }
526526}
527527
528+ func TestShowMarshalSecretValues (t * testing.T ) {
529+ MarshalSecretValue = true
530+ defer func () { MarshalSecretValue = false }()
531+
532+ c , err := LoadFile ("testdata/conf.good.yml" )
533+ if err != nil {
534+ t .Fatalf ("Error parsing %s: %s" , "testdata/conf.good.yml" , err )
535+ }
536+
537+ // String method must reveal authentication credentials.
538+ s := c .String ()
539+ if strings .Count (s , "<secret>" ) > 0 || ! strings .Contains (s , "mysecret" ) {
540+ t .Fatal ("config's String method must reveal authentication credentials when MarshalSecretValue = true." )
541+ }
542+ }
543+
528544func TestJSONMarshal (t * testing.T ) {
529545 c , err := LoadFile ("testdata/conf.good.yml" )
530546 if err != nil {
@@ -537,7 +553,7 @@ func TestJSONMarshal(t *testing.T) {
537553 }
538554}
539555
540- func TestJSONMarshalSecret (t * testing.T ) {
556+ func TestJSONMarshalHideSecret (t * testing.T ) {
541557 test := struct {
542558 S Secret
543559 }{
@@ -554,7 +570,24 @@ func TestJSONMarshalSecret(t *testing.T) {
554570 require .Equal (t , "{\" S\" :\" \\ u003csecret\\ u003e\" }" , string (c ), "Secret not properly elided." )
555571}
556572
557- func TestMarshalSecretURL (t * testing.T ) {
573+ func TestJSONMarshalShowSecret (t * testing.T ) {
574+ MarshalSecretValue = true
575+ defer func () { MarshalSecretValue = false }()
576+
577+ test := struct {
578+ S Secret
579+ }{
580+ S : Secret ("test" ),
581+ }
582+
583+ c , err := json .Marshal (test )
584+ if err != nil {
585+ t .Fatal (err )
586+ }
587+ require .Equal (t , "{\" S\" :\" test\" }" , string (c ), "config's String method must reveal authentication credentials when MarshalSecretValue = true." )
588+ }
589+
590+ func TestJSONMarshalHideSecretURL (t * testing.T ) {
558591 urlp , err := url .Parse ("http://example.com/" )
559592 if err != nil {
560593 t .Fatal (err )
@@ -588,6 +621,23 @@ func TestMarshalSecretURL(t *testing.T) {
588621 }
589622}
590623
624+ func TestJSONMarshalShowSecretURL (t * testing.T ) {
625+ MarshalSecretValue = true
626+ defer func () { MarshalSecretValue = false }()
627+
628+ urlp , err := url .Parse ("http://example.com/" )
629+ if err != nil {
630+ t .Fatal (err )
631+ }
632+ u := & SecretURL {urlp }
633+
634+ c , err := json .Marshal (u )
635+ if err != nil {
636+ t .Fatal (err )
637+ }
638+ require .Equal (t , "\" http://example.com/\" " , string (c ), "config's String method must reveal authentication credentials when MarshalSecretValue = true." )
639+ }
640+
591641func TestUnmarshalSecretURL (t * testing.T ) {
592642 b := []byte (`"http://example.com/se cret"` )
593643 var u SecretURL
@@ -615,6 +665,18 @@ func TestHideSecretURL(t *testing.T) {
615665 require .NotContains (t , err .Error (), "wrongurl" )
616666}
617667
668+ func TestShowMarshalSecretURL (t * testing.T ) {
669+ MarshalSecretValue = true
670+ defer func () { MarshalSecretValue = false }()
671+
672+ b := []byte (`"://wrongurl/"` )
673+ var u SecretURL
674+
675+ err := json .Unmarshal (b , & u )
676+ require .Error (t , err )
677+ require .Contains (t , err .Error (), "wrongurl" )
678+ }
679+
618680func TestMarshalURL (t * testing.T ) {
619681 for name , tc := range map [string ]struct {
620682 input * URL
0 commit comments