-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package day382 | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/hex" | ||
) | ||
|
||
// Base64DecodeDelegate just delegates | ||
func Base64DecodeDelegate(b64 string) string { | ||
data, err := base64.StdEncoding.DecodeString(b64) | ||
if err != nil { | ||
panic("bad base64 input") | ||
} | ||
|
||
return hex.EncodeToString(data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package day382 | ||
|
||
import "testing" | ||
|
||
// nolint | ||
var testcases = []struct { | ||
base64 string | ||
decoded string | ||
}{ | ||
{"3q2+7w==", "deadbeef"}, | ||
} | ||
|
||
func TestBase64DecodeDelegate(t *testing.T) { | ||
t.Parallel() | ||
|
||
for _, tc := range testcases { | ||
if res := Base64DecodeDelegate(tc.base64); res != tc.decoded { | ||
t.Errorf("Expected %v, got %v", tc.decoded, res) | ||
} | ||
} | ||
} | ||
|
||
func TestBase64DecodeDelegateBadInput(t *testing.T) { | ||
t.Parallel() | ||
|
||
defer func() { | ||
if err := recover(); err == nil { | ||
t.Errorf("expected a panic for non-base64 input") | ||
} | ||
}() | ||
|
||
Base64DecodeDelegate("base64NOT[]") | ||
} | ||
|
||
func BenchmarkBase64DecodeDelegate(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
Base64DecodeDelegate(tc.base64) | ||
} | ||
} | ||
} |