Skip to content

Commit

Permalink
add mustB64dec (and mustB32dec) which fails on bad b64 data instead o…
Browse files Browse the repository at this point in the history
…f decoding to "illegal base64 data at input byte ..."

Closes Masterminds#417
  • Loading branch information
dastrobu committed Nov 19, 2024
1 parent 8cb06fe commit ed3d23a
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/encoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ Sprig has the following encoding and decoding functions:

- `b64enc`/`b64dec`: Encode or decode with Base64
- `b32enc`/`b32dec`: Encode or decode with Base32
- `mustB32dec`/`mustB64dec`: will return an error in case data is not valid encoded data.
10 changes: 6 additions & 4 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,12 @@ var genericMap = map[string]interface{}{
"osIsAbs": filepath.IsAbs,

// Encoding:
"b64enc": base64encode,
"b64dec": base64decode,
"b32enc": base32encode,
"b32dec": base32decode,
"b64enc": base64encode,
"b64dec": base64decode,
"mustB64dec": mustBase64decode,
"b32enc": base32encode,
"b32dec": base32decode,
"mustB32dec": mustBase32decode,

// Data Structures:
"tuple": list, // FIXME: with the addition of append/prepend these are no longer immutable.
Expand Down
24 changes: 20 additions & 4 deletions strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,39 @@ func base64encode(v string) string {
}

func base64decode(v string) string {
data, err := base64.StdEncoding.DecodeString(v)
data, err := mustBase64decode(v)
if err != nil {
return err.Error()
}
return string(data)
return data
}

func mustBase64decode(v string) (string, error) {
data, err := base64.StdEncoding.DecodeString(v)
if err != nil {
return "", err
}
return string(data), nil
}

func base32encode(v string) string {
return base32.StdEncoding.EncodeToString([]byte(v))
}

func base32decode(v string) string {
data, err := base32.StdEncoding.DecodeString(v)
data, err := mustBase32decode(v)
if err != nil {
return err.Error()
}
return string(data)
return data
}

func mustBase32decode(v string) (string, error) {
data, err := base32.StdEncoding.DecodeString(v)
if err != nil {
return "", err
}
return string(data), nil
}

func abbrev(width int, s string) string {
Expand Down
52 changes: 52 additions & 0 deletions strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func TestSortAlpha(t *testing.T) {
assert.NoError(t, runt(tpl, expect))
}
}

func TestBase64EncodeDecode(t *testing.T) {
magicWord := "coffee"
expect := base64.StdEncoding.EncodeToString([]byte(magicWord))
Expand All @@ -171,6 +172,32 @@ func TestBase64EncodeDecode(t *testing.T) {
t.Error(err)
}
}

func TestBase64DecodeErr(t *testing.T) {
expect := "illegal base64 data at input byte 4"

tpl := `{{b64dec "coffee"}}`
if err := runt(tpl, expect); err != nil {
t.Error(err)
}
}

func TestMustBase64Decode(t *testing.T) {
expect := "coffee"
b64data := base64.StdEncoding.EncodeToString([]byte(expect))

tpl := fmt.Sprintf("{{mustB64dec %q}}", b64data)
if err := runt(tpl, expect); err != nil {
t.Error(err)
}
}

func TestMustBase64DecodeErr(t *testing.T) {
tpl := `{{mustB64dec "coffee"}}`
_, err := runRaw(tpl, nil)
assert.EqualError(t, err, `template: test:1:2: executing "test" at <mustB64dec "coffee">: error calling mustB64dec: illegal base64 data at input byte 4`)
}

func TestBase32EncodeDecode(t *testing.T) {
magicWord := "coffee"
expect := base32.StdEncoding.EncodeToString([]byte(magicWord))
Expand All @@ -189,6 +216,31 @@ func TestBase32EncodeDecode(t *testing.T) {
}
}

func TestBase32DecodeErr(t *testing.T) {
expect := "illegal base32 data at input byte 0"

tpl := `{{b32dec "coffee"}}`
if err := runt(tpl, expect); err != nil {
t.Error(err)
}
}

func TestMustBase32Decode(t *testing.T) {
expect := "coffee"
b32data := base32.StdEncoding.EncodeToString([]byte(expect))

tpl := fmt.Sprintf("{{mustB32dec %q}}", b32data)
if err := runt(tpl, expect); err != nil {
t.Error(err)
}
}

func TestMustBase32DecodeErr(t *testing.T) {
tpl := `{{mustB32dec "coffee"}}`
_, err := runRaw(tpl, nil)
assert.EqualError(t, err, `template: test:1:2: executing "test" at <mustB32dec "coffee">: error calling mustB32dec: illegal base32 data at input byte 0`)
}

func TestGoutils(t *testing.T) {
tests := map[string]string{
`{{abbrev 5 "hello world"}}`: "he...",
Expand Down

0 comments on commit ed3d23a

Please sign in to comment.