decoding_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package decoding
  14. import (
  15. "reflect"
  16. "testing"
  17. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  18. )
  19. const (
  20. base64DecodedValue string = "foo%_?bar"
  21. base64EncodedValue string = "Zm9vJV8/YmFy"
  22. base64URLEncodedValue string = "Zm9vJV8_YmFy"
  23. )
  24. func TestDecode(t *testing.T) {
  25. tests := []struct {
  26. name string
  27. strategy esv1.ExternalSecretDecodingStrategy
  28. in []byte
  29. want []byte
  30. wantErr bool
  31. }{
  32. {
  33. name: "base64 decoded",
  34. strategy: esv1.ExternalSecretDecodeBase64,
  35. in: []byte("YmFy"),
  36. want: []byte("bar"),
  37. },
  38. {
  39. name: "invalid base64",
  40. strategy: esv1.ExternalSecretDecodeBase64,
  41. in: []byte("foo"),
  42. wantErr: true,
  43. },
  44. {
  45. name: "base64url decoded",
  46. strategy: esv1.ExternalSecretDecodeBase64URL,
  47. in: []byte(base64URLEncodedValue),
  48. want: []byte(base64DecodedValue),
  49. },
  50. {
  51. name: "invalid base64url",
  52. strategy: esv1.ExternalSecretDecodeBase64URL,
  53. in: []byte("foo"),
  54. wantErr: true,
  55. },
  56. {
  57. name: "none",
  58. strategy: esv1.ExternalSecretDecodeNone,
  59. in: []byte(base64URLEncodedValue),
  60. want: []byte(base64URLEncodedValue),
  61. },
  62. {
  63. name: "empty strategy defaults to none",
  64. strategy: "",
  65. in: []byte(base64URLEncodedValue),
  66. want: []byte(base64URLEncodedValue),
  67. },
  68. {
  69. name: "auto base64",
  70. strategy: esv1.ExternalSecretDecodeAuto,
  71. in: []byte(base64EncodedValue),
  72. want: []byte(base64DecodedValue),
  73. },
  74. {
  75. name: "auto base64url",
  76. strategy: esv1.ExternalSecretDecodeAuto,
  77. in: []byte(base64URLEncodedValue),
  78. want: []byte(base64DecodedValue),
  79. },
  80. {
  81. name: "auto invalid base64 returns input",
  82. strategy: esv1.ExternalSecretDecodeAuto,
  83. in: []byte("foo"),
  84. want: []byte("foo"),
  85. },
  86. {
  87. name: "unsupported strategy",
  88. strategy: esv1.ExternalSecretDecodingStrategy("unsupported"),
  89. in: []byte("foo"),
  90. wantErr: true,
  91. },
  92. }
  93. for _, tt := range tests {
  94. t.Run(tt.name, func(t *testing.T) {
  95. got, err := Decode(tt.strategy, tt.in)
  96. if (err != nil) != tt.wantErr {
  97. t.Errorf("Decode() error = %v, wantErr %v", err, tt.wantErr)
  98. return
  99. }
  100. if !reflect.DeepEqual(got, tt.want) {
  101. t.Errorf("Decode() = %v, want %v", got, tt.want)
  102. }
  103. })
  104. }
  105. }
  106. func TestDecodeMap(t *testing.T) {
  107. type args struct {
  108. strategy esv1.ExternalSecretDecodingStrategy
  109. in map[string][]byte
  110. }
  111. tests := []struct {
  112. name string
  113. args args
  114. want map[string][]byte
  115. wantErr bool
  116. }{
  117. {
  118. name: "base64 decoded",
  119. args: args{
  120. strategy: esv1.ExternalSecretDecodeBase64,
  121. in: map[string][]byte{
  122. "foo": []byte("YmFy"),
  123. },
  124. },
  125. want: map[string][]byte{
  126. "foo": []byte("bar"),
  127. },
  128. },
  129. {
  130. name: "invalid base64",
  131. args: args{
  132. strategy: esv1.ExternalSecretDecodeBase64,
  133. in: map[string][]byte{
  134. "foo": []byte("foo"),
  135. },
  136. },
  137. wantErr: true,
  138. },
  139. {
  140. name: "base64url decoded",
  141. args: args{
  142. strategy: esv1.ExternalSecretDecodeBase64URL,
  143. in: map[string][]byte{
  144. "foo": []byte(base64URLEncodedValue),
  145. },
  146. },
  147. want: map[string][]byte{
  148. "foo": []byte(base64DecodedValue),
  149. },
  150. },
  151. {
  152. name: "invalid base64url",
  153. args: args{
  154. strategy: esv1.ExternalSecretDecodeBase64URL,
  155. in: map[string][]byte{
  156. "foo": []byte("foo"),
  157. },
  158. },
  159. wantErr: true,
  160. },
  161. {
  162. name: "none",
  163. args: args{
  164. strategy: esv1.ExternalSecretDecodeNone,
  165. in: map[string][]byte{
  166. "foo": []byte(base64URLEncodedValue),
  167. },
  168. },
  169. want: map[string][]byte{
  170. "foo": []byte(base64URLEncodedValue),
  171. },
  172. },
  173. {
  174. name: "auto",
  175. args: args{
  176. strategy: esv1.ExternalSecretDecodeAuto,
  177. in: map[string][]byte{
  178. "b64": []byte(base64EncodedValue),
  179. "invalidb64": []byte("foo"),
  180. "b64url": []byte(base64URLEncodedValue),
  181. },
  182. },
  183. want: map[string][]byte{
  184. "b64": []byte(base64DecodedValue),
  185. "invalidb64": []byte("foo"),
  186. "b64url": []byte(base64DecodedValue),
  187. },
  188. },
  189. }
  190. for _, tt := range tests {
  191. t.Run(tt.name, func(t *testing.T) {
  192. got, err := DecodeMap(tt.args.strategy, tt.args.in)
  193. if (err != nil) != tt.wantErr {
  194. t.Errorf("DecodeMap() error = %v, wantErr %v", err, tt.wantErr)
  195. return
  196. }
  197. if !reflect.DeepEqual(got, tt.want) {
  198. t.Errorf("DecodeMap() = %v, want %v", got, tt.want)
  199. }
  200. })
  201. }
  202. }