github.com/shohhei1126/hugo@v0.42.2-0.20180623210752-3d5928889ad7/tpl/crypto/crypto_test.go (about)

     1  // Copyright 2017 The Hugo Authors. All rights reserved.
     2  //
     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  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package crypto
    15  
    16  import (
    17  	"fmt"
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  func TestMD5(t *testing.T) {
    25  	t.Parallel()
    26  
    27  	ns := New()
    28  
    29  	for i, test := range []struct {
    30  		in     interface{}
    31  		expect interface{}
    32  	}{
    33  		{"Hello world, gophers!", "b3029f756f98f79e7f1b7f1d1f0dd53b"},
    34  		{"Lorem ipsum dolor", "06ce65ac476fc656bea3fca5d02cfd81"},
    35  		{t, false},
    36  	} {
    37  		errMsg := fmt.Sprintf("[%d] %v", i, test.in)
    38  
    39  		result, err := ns.MD5(test.in)
    40  
    41  		if b, ok := test.expect.(bool); ok && !b {
    42  			require.Error(t, err, errMsg)
    43  			continue
    44  		}
    45  
    46  		require.NoError(t, err, errMsg)
    47  		assert.Equal(t, test.expect, result, errMsg)
    48  	}
    49  }
    50  
    51  func TestSHA1(t *testing.T) {
    52  	t.Parallel()
    53  
    54  	ns := New()
    55  
    56  	for i, test := range []struct {
    57  		in     interface{}
    58  		expect interface{}
    59  	}{
    60  		{"Hello world, gophers!", "c8b5b0e33d408246e30f53e32b8f7627a7a649d4"},
    61  		{"Lorem ipsum dolor", "45f75b844be4d17b3394c6701768daf39419c99b"},
    62  		{t, false},
    63  	} {
    64  		errMsg := fmt.Sprintf("[%d] %v", i, test.in)
    65  
    66  		result, err := ns.SHA1(test.in)
    67  
    68  		if b, ok := test.expect.(bool); ok && !b {
    69  			require.Error(t, err, errMsg)
    70  			continue
    71  		}
    72  
    73  		require.NoError(t, err, errMsg)
    74  		assert.Equal(t, test.expect, result, errMsg)
    75  	}
    76  }
    77  
    78  func TestSHA256(t *testing.T) {
    79  	t.Parallel()
    80  
    81  	ns := New()
    82  
    83  	for i, test := range []struct {
    84  		in     interface{}
    85  		expect interface{}
    86  	}{
    87  		{"Hello world, gophers!", "6ec43b78da9669f50e4e422575c54bf87536954ccd58280219c393f2ce352b46"},
    88  		{"Lorem ipsum dolor", "9b3e1beb7053e0f900a674dd1c99aca3355e1275e1b03d3cb1bc977f5154e196"},
    89  		{t, false},
    90  	} {
    91  		errMsg := fmt.Sprintf("[%d] %v", i, test.in)
    92  
    93  		result, err := ns.SHA256(test.in)
    94  
    95  		if b, ok := test.expect.(bool); ok && !b {
    96  			require.Error(t, err, errMsg)
    97  			continue
    98  		}
    99  
   100  		require.NoError(t, err, errMsg)
   101  		assert.Equal(t, test.expect, result, errMsg)
   102  	}
   103  }