github.com/zntrio/harp/v2@v2.0.9/pkg/sdk/security/password/generator_test.go (about)

     1  // Licensed to Elasticsearch B.V. under one or more contributor
     2  // license agreements. See the NOTICE file distributed with
     3  // this work for additional information regarding copyright
     4  // ownership. Elasticsearch B.V. licenses this file to you under
     5  // the Apache License, Version 2.0 (the "License"); you may
     6  // not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package password
    19  
    20  import (
    21  	"testing"
    22  
    23  	fuzz "github.com/google/gofuzz"
    24  )
    25  
    26  func TestFromProfile(t *testing.T) {
    27  	type args struct {
    28  		p *Profile
    29  	}
    30  	tests := []struct {
    31  		name    string
    32  		args    args
    33  		want    string
    34  		wantErr bool
    35  	}{
    36  		{
    37  			name:    "nil",
    38  			wantErr: true,
    39  		},
    40  		{
    41  			name: "paranoid",
    42  			args: args{
    43  				p: ProfileParanoid,
    44  			},
    45  			wantErr: false,
    46  		},
    47  		{
    48  			name: "noSymbol",
    49  			args: args{
    50  				p: ProfileNoSymbol,
    51  			},
    52  			wantErr: false,
    53  		},
    54  		{
    55  			name: "strong",
    56  			args: args{
    57  				p: ProfileStrong,
    58  			},
    59  			wantErr: false,
    60  		},
    61  	}
    62  	for _, tt := range tests {
    63  		t.Run(tt.name, func(t *testing.T) {
    64  			_, err := FromProfile(tt.args.p)
    65  			if (err != nil) != tt.wantErr {
    66  				t.Errorf("FromProfile() error = %v, wantErr %v", err, tt.wantErr)
    67  				return
    68  			}
    69  		})
    70  	}
    71  }
    72  
    73  func TestPredefined(t *testing.T) {
    74  	tests := []struct {
    75  		name       string
    76  		callable   func() (string, error)
    77  		wantLength int
    78  		wantErr    bool
    79  	}{
    80  		{
    81  			name:       "paranoid",
    82  			callable:   Paranoid,
    83  			wantLength: ProfileParanoid.Length,
    84  			wantErr:    false,
    85  		},
    86  		{
    87  			name:       "strong",
    88  			callable:   Strong,
    89  			wantLength: ProfileStrong.Length,
    90  			wantErr:    false,
    91  		},
    92  		{
    93  			name:       "noSymbol",
    94  			callable:   NoSymbol,
    95  			wantLength: ProfileNoSymbol.Length,
    96  			wantErr:    false,
    97  		},
    98  	}
    99  	for _, tt := range tests {
   100  		t.Run(tt.name, func(t *testing.T) {
   101  			got, err := tt.callable()
   102  			if (err != nil) != tt.wantErr {
   103  				t.Errorf("Predefined() error = %v, wantErr %v", err, tt.wantErr)
   104  				return
   105  			}
   106  			gotLength := len(got)
   107  			if (tt.wantLength > 0) && tt.wantLength != gotLength {
   108  				t.Errorf("Predefined() expected length = %v, got %v", tt.wantLength, gotLength)
   109  				return
   110  			}
   111  		})
   112  	}
   113  }
   114  
   115  // -----------------------------------------------------------------------------
   116  
   117  func TestGenerate_Fuzz(t *testing.T) {
   118  	// Making sure that it never panics
   119  	for i := 0; i < 50; i++ {
   120  		f := fuzz.New()
   121  
   122  		// Prepare arguments
   123  		var (
   124  			length, numDigits, numSymbol int
   125  			noUpper, allowRepeat         bool
   126  		)
   127  
   128  		// Fuzz input
   129  		f.Fuzz(&length)
   130  		f.Fuzz(&numDigits)
   131  		f.Fuzz(&numSymbol)
   132  		f.Fuzz(&noUpper)
   133  		f.Fuzz(&allowRepeat)
   134  
   135  		// Execute
   136  		Generate(length, numDigits, numSymbol, noUpper, allowRepeat)
   137  	}
   138  }
   139  
   140  func TestFromProfile_Fuzz(t *testing.T) {
   141  	// Making sure that it never panics
   142  	for i := 0; i < 50; i++ {
   143  		f := fuzz.New()
   144  
   145  		// Prepare arguments
   146  		var p Profile
   147  
   148  		// Fuzz input
   149  		f.Fuzz(&p)
   150  
   151  		// Execute
   152  		FromProfile(&p)
   153  	}
   154  }