github.com/dgraph-io/dgraph@v1.2.8/types/password_test.go (about)

     1  /*
     2   * Copyright 2017-2018 Dgraph Labs, Inc. and Contributors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package types
    18  
    19  import "testing"
    20  
    21  func TestEncrypt(t *testing.T) {
    22  	tests := []struct {
    23  		name    string
    24  		input   string
    25  		want    int
    26  		wantErr bool
    27  	}{
    28  		{
    29  			name:    "case 1",
    30  			input:   "",
    31  			want:    0,
    32  			wantErr: true,
    33  		},
    34  		{
    35  			name:    "case 2",
    36  			input:   "12345",
    37  			want:    0,
    38  			wantErr: true,
    39  		},
    40  		{
    41  			name:    "case 3",
    42  			input:   "123456",
    43  			want:    60,
    44  			wantErr: false,
    45  		},
    46  		{
    47  			name:    "case 4",
    48  			input:   "1234567890",
    49  			want:    60,
    50  			wantErr: false,
    51  		},
    52  	}
    53  	for _, tt := range tests {
    54  		t.Run(tt.name, func(t *testing.T) {
    55  			got, err := Encrypt(tt.input)
    56  			if (err != nil) != tt.wantErr {
    57  				t.Errorf("Encrypt() error = %v, wantErr %v", err, tt.wantErr)
    58  				return
    59  			}
    60  			if len(got) != tt.want {
    61  				t.Errorf("Encrypt() = %v, want %v", len(got), tt.want)
    62  			}
    63  		})
    64  	}
    65  }
    66  
    67  func TestVerifyPassword(t *testing.T) {
    68  	type args struct {
    69  		plain     string
    70  		encrypted string
    71  	}
    72  	tests := []struct {
    73  		name    string
    74  		args    args
    75  		wantErr bool
    76  	}{
    77  		{
    78  			name: "case 1",
    79  			args: args{
    80  				plain:     "",
    81  				encrypted: "",
    82  			},
    83  			wantErr: true,
    84  		},
    85  		{
    86  			name: "case 2",
    87  			args: args{
    88  				plain:     "",
    89  				encrypted: "$2a$10$LMGgvb5dOq4/YrWXjAy6W.tBfFQC4QDNFAuOCWGRk3f/Z1TMXswaC",
    90  			},
    91  			wantErr: true,
    92  		},
    93  		{
    94  			name: "case 3",
    95  			args: args{
    96  				plain:     "1234567890",
    97  				encrypted: "",
    98  			},
    99  			wantErr: true,
   100  		},
   101  		{
   102  			name: "case 4",
   103  			args: args{
   104  				plain:     "12345",
   105  				encrypted: "$2a$10$LMGgvb5dOq4/YrWXjAy6W.tBfFQC4QDNFAuOCWGRk3f/Z1TMXswaC",
   106  			},
   107  			wantErr: true,
   108  		},
   109  		{
   110  			name: "case 5",
   111  			args: args{
   112  				plain:     "12345678",
   113  				encrypted: "$2a$10$LMGgvb5dOq4/YrWXjAy6W.tBfFQC4QDNFAuOCWGRk3f/Z1TMXswaC",
   114  			},
   115  			wantErr: true,
   116  		},
   117  		{
   118  			name: "case 6",
   119  			args: args{
   120  				plain:     "123456",
   121  				encrypted: "$2a$10$LMGgvb5dOq4/YrWXjAy6W.tBfFQC4QDNFAuOCWGRk3f/Z1TMXswaC",
   122  			},
   123  			wantErr: false,
   124  		},
   125  		{
   126  			name: "case 7",
   127  			args: args{
   128  				plain:     "123456",
   129  				encrypted: "$2a$10$kXtSFCVmzsu0lwVqaWxo5OXlLGUakcY2t18QcqcVpvoTHsPqclOca",
   130  			},
   131  			wantErr: false,
   132  		},
   133  		{
   134  			name: "case 8",
   135  			args: args{
   136  				plain:     "1234567890",
   137  				encrypted: "$2a$10$WdCWNpOP6c4l7ECv3hEWKeD3oSiszlRJFmT4uRT1P/W9V9zUye8pS",
   138  			},
   139  			wantErr: false,
   140  		},
   141  	}
   142  	for _, tt := range tests {
   143  		t.Run(tt.name, func(t *testing.T) {
   144  			if err := VerifyPassword(tt.args.plain, tt.args.encrypted); (err != nil) != tt.wantErr {
   145  				t.Errorf("VerifyPassword() error = %v, wantErr %v", err, tt.wantErr)
   146  			}
   147  		})
   148  	}
   149  }