password_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  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 password
  14. import (
  15. "encoding/base64"
  16. "encoding/hex"
  17. "errors"
  18. "reflect"
  19. "testing"
  20. "github.com/stretchr/testify/assert"
  21. apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  22. )
  23. func TestGenerate(t *testing.T) {
  24. type args struct {
  25. jsonSpec *apiextensions.JSON
  26. passGen generateFunc
  27. }
  28. tests := []struct {
  29. name string
  30. g *Generator
  31. args args
  32. want map[string][]byte
  33. wantErr bool
  34. }{
  35. {
  36. name: "no json spec should result in error",
  37. args: args{
  38. jsonSpec: nil,
  39. },
  40. wantErr: true,
  41. },
  42. {
  43. name: "invalid json spec should result in error",
  44. args: args{
  45. jsonSpec: &apiextensions.JSON{
  46. Raw: []byte(`no json`),
  47. },
  48. },
  49. wantErr: true,
  50. },
  51. {
  52. name: "empty spec should return defaults",
  53. args: args{
  54. jsonSpec: &apiextensions.JSON{
  55. Raw: []byte(`{}`),
  56. },
  57. passGen: func(len int, symbols int, symbolCharacters string, digits int, noUpper bool, allowRepeat bool,
  58. ) (string, error) {
  59. assert.Equal(t, defaultLength, len)
  60. assert.Equal(t, defaultSymbolChars, symbolCharacters)
  61. assert.Equal(t, 6, symbols)
  62. assert.Equal(t, 6, digits)
  63. assert.Equal(t, false, noUpper)
  64. assert.Equal(t, false, allowRepeat)
  65. return "foobar", nil
  66. },
  67. },
  68. want: map[string][]byte{
  69. "password": []byte(`foobar`),
  70. },
  71. wantErr: false,
  72. },
  73. {
  74. name: "spec should override defaults",
  75. args: args{
  76. jsonSpec: &apiextensions.JSON{
  77. Raw: []byte(`{"spec":{"length":48,"digits":2, "symbols":2, "symbolCharacters":"-_.", "noUpper": true, "allowRepeat": true}}`),
  78. },
  79. passGen: func(len int, symbols int, symbolCharacters string, digits int, noUpper bool, allowRepeat bool,
  80. ) (string, error) {
  81. assert.Equal(t, 48, len)
  82. assert.Equal(t, "-_.", symbolCharacters)
  83. assert.Equal(t, 2, symbols)
  84. assert.Equal(t, 2, digits)
  85. assert.Equal(t, true, noUpper)
  86. assert.Equal(t, true, allowRepeat)
  87. return "foobar", nil
  88. },
  89. },
  90. want: map[string][]byte{
  91. "password": []byte(`foobar`),
  92. },
  93. wantErr: false,
  94. },
  95. {
  96. name: "generator error should be returned",
  97. args: args{
  98. jsonSpec: &apiextensions.JSON{
  99. Raw: []byte(`{}`),
  100. },
  101. passGen: func(len int, symbols int, symbolCharacters string, digits int, noUpper bool, allowRepeat bool,
  102. ) (string, error) {
  103. return "", errors.New("boom")
  104. },
  105. },
  106. wantErr: true,
  107. },
  108. {
  109. name: "spec with hex encoding should encode password as hex",
  110. args: args{
  111. jsonSpec: &apiextensions.JSON{
  112. Raw: []byte(`{"spec":{"encoding":"hex"}}`),
  113. },
  114. passGen: func(len int, symbols int, symbolCharacters string, digits int, noUpper bool, allowRepeat bool,
  115. ) (string, error) {
  116. return "test_hex", nil
  117. },
  118. },
  119. want: map[string][]byte{
  120. "password": []byte(hex.EncodeToString([]byte("test_hex"))),
  121. },
  122. wantErr: false,
  123. },
  124. {
  125. name: "spec with raw encoding should return raw password",
  126. args: args{
  127. jsonSpec: &apiextensions.JSON{
  128. Raw: []byte(`{"spec":{"encoding":"raw"}}`),
  129. },
  130. passGen: func(len int, symbols int, symbolCharacters string, digits int, noUpper bool, allowRepeat bool,
  131. ) (string, error) {
  132. return "test_raw", nil
  133. },
  134. },
  135. want: map[string][]byte{
  136. "password": []byte(`test_raw`),
  137. },
  138. wantErr: false,
  139. },
  140. {
  141. name: "spec with base64 encoding should encode password as base64",
  142. args: args{
  143. jsonSpec: &apiextensions.JSON{
  144. Raw: []byte(`{"spec":{"encoding":"base64"}}`),
  145. },
  146. passGen: func(len int, symbols int, symbolCharacters string, digits int, noUpper bool, allowRepeat bool,
  147. ) (string, error) {
  148. return "test_base64", nil
  149. },
  150. },
  151. want: map[string][]byte{
  152. "password": []byte(base64.StdEncoding.EncodeToString([]byte("test_base64"))),
  153. },
  154. wantErr: false,
  155. },
  156. }
  157. for _, tt := range tests {
  158. t.Run(tt.name, func(t *testing.T) {
  159. g := &Generator{}
  160. got, _, err := g.generate(tt.args.jsonSpec, tt.args.passGen)
  161. if (err != nil) != tt.wantErr {
  162. t.Errorf("Generator.Generate() error = %v, wantErr %v", err, tt.wantErr)
  163. return
  164. }
  165. if !reflect.DeepEqual(got, tt.want) {
  166. t.Errorf("Generator.Generate() = %v, want %v", got, tt.want)
  167. }
  168. })
  169. }
  170. }