crypto_test.go (3865B)
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 "testing" 18 19 qt "github.com/frankban/quicktest" 20 ) 21 22 func TestMD5(t *testing.T) { 23 t.Parallel() 24 c := qt.New(t) 25 26 ns := New() 27 28 for i, test := range []struct { 29 in any 30 expect any 31 }{ 32 {"Hello world, gophers!", "b3029f756f98f79e7f1b7f1d1f0dd53b"}, 33 {"Lorem ipsum dolor", "06ce65ac476fc656bea3fca5d02cfd81"}, 34 {t, false}, 35 } { 36 errMsg := qt.Commentf("[%d] %v", i, test.in) 37 38 result, err := ns.MD5(test.in) 39 40 if b, ok := test.expect.(bool); ok && !b { 41 c.Assert(err, qt.Not(qt.IsNil), errMsg) 42 continue 43 } 44 45 c.Assert(err, qt.IsNil, errMsg) 46 c.Assert(result, qt.Equals, test.expect, errMsg) 47 } 48 } 49 50 func TestSHA1(t *testing.T) { 51 t.Parallel() 52 c := qt.New(t) 53 ns := New() 54 55 for i, test := range []struct { 56 in any 57 expect any 58 }{ 59 {"Hello world, gophers!", "c8b5b0e33d408246e30f53e32b8f7627a7a649d4"}, 60 {"Lorem ipsum dolor", "45f75b844be4d17b3394c6701768daf39419c99b"}, 61 {t, false}, 62 } { 63 errMsg := qt.Commentf("[%d] %v", i, test.in) 64 65 result, err := ns.SHA1(test.in) 66 67 if b, ok := test.expect.(bool); ok && !b { 68 c.Assert(err, qt.Not(qt.IsNil), errMsg) 69 continue 70 } 71 72 c.Assert(err, qt.IsNil, errMsg) 73 c.Assert(result, qt.Equals, test.expect, errMsg) 74 } 75 } 76 77 func TestSHA256(t *testing.T) { 78 t.Parallel() 79 c := qt.New(t) 80 ns := New() 81 82 for i, test := range []struct { 83 in any 84 expect any 85 }{ 86 {"Hello world, gophers!", "6ec43b78da9669f50e4e422575c54bf87536954ccd58280219c393f2ce352b46"}, 87 {"Lorem ipsum dolor", "9b3e1beb7053e0f900a674dd1c99aca3355e1275e1b03d3cb1bc977f5154e196"}, 88 {t, false}, 89 } { 90 errMsg := qt.Commentf("[%d] %v", i, test.in) 91 92 result, err := ns.SHA256(test.in) 93 94 if b, ok := test.expect.(bool); ok && !b { 95 c.Assert(err, qt.Not(qt.IsNil), errMsg) 96 continue 97 } 98 99 c.Assert(err, qt.IsNil, errMsg) 100 c.Assert(result, qt.Equals, test.expect, errMsg) 101 } 102 } 103 104 func TestHMAC(t *testing.T) { 105 t.Parallel() 106 c := qt.New(t) 107 ns := New() 108 109 for i, test := range []struct { 110 hash any 111 key any 112 msg any 113 encoding any 114 expect any 115 }{ 116 {"md5", "Secret key", "Hello world, gophers!", nil, "36eb69b6bf2de96b6856fdee8bf89754"}, 117 {"sha1", "Secret key", "Hello world, gophers!", nil, "84a76647de6cd47ac6ae4258e3753f711172ce68"}, 118 {"sha256", "Secret key", "Hello world, gophers!", nil, "b6d11b6c53830b9d87036272ca9fe9d19306b8f9d8aa07b15da27d89e6e34f40"}, 119 {"sha512", "Secret key", "Hello world, gophers!", nil, "dc3e586cd936865e2abc4c12665e9cc568b2dad714df3c9037cbea159d036cfc4209da9e3fcd30887ff441056941966899f6fb7eec9646ff9ddb592595a8eb7f"}, 120 {"md5", "Secret key", "Hello world, gophers!", "hex", "36eb69b6bf2de96b6856fdee8bf89754"}, 121 {"md5", "Secret key", "Hello world, gophers!", "binary", "6\xebi\xb6\xbf-\xe9khV\xfd\xee\x8b\xf8\x97T"}, 122 {"md5", "Secret key", "Hello world, gophers!", "foo", false}, 123 {"md5", "Secret key", "Hello world, gophers!", "", false}, 124 {"", t, "", nil, false}, 125 } { 126 errMsg := qt.Commentf("[%d] %v, %v, %v, %v", i, test.hash, test.key, test.msg, test.encoding) 127 128 result, err := ns.HMAC(test.hash, test.key, test.msg, test.encoding) 129 130 if b, ok := test.expect.(bool); ok && !b { 131 c.Assert(err, qt.Not(qt.IsNil), errMsg) 132 continue 133 } 134 135 c.Assert(err, qt.IsNil, errMsg) 136 c.Assert(result, qt.Equals, test.expect, errMsg) 137 } 138 }