|
| 1 | +// Copyright (C) 2014 Space Monkey, Inc. |
| 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 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// +build cgo |
| 16 | + |
| 17 | +package openssl |
| 18 | + |
| 19 | +import ( |
| 20 | + "crypto/rand" |
| 21 | + "fmt" |
| 22 | + "io" |
| 23 | + "testing" |
| 24 | +) |
| 25 | + |
| 26 | +type shaTest struct { |
| 27 | + out string |
| 28 | + in string |
| 29 | +} |
| 30 | + |
| 31 | +var golden = []shaTest{ |
| 32 | + {"f96cea198ad1dd5617ac084a3d92c6107708c0ef", ""}, |
| 33 | + {"37f297772fae4cb1ba39b6cf9cf0381180bd62f2", "a"}, |
| 34 | + {"488373d362684af3d3f7a6a408b59dfe85419e09", "ab"}, |
| 35 | + {"0164b8a914cd2a5e74c4f7ff082c4d97f1edf880", "abc"}, |
| 36 | + {"082c73b06f71185d840fb4b28eb3abade67714bc", "abcd"}, |
| 37 | + {"d624e34951bb800f0acae773001df8cffe781ba8", "abcde"}, |
| 38 | + {"2a589f7750598dc0ea0a608719e04327f609279a", "abcdef"}, |
| 39 | + {"5bdf01f9298e9d19d3f8d15520fd74eed600b497", "abcdefg"}, |
| 40 | + {"734ba8b31975d0dbae4d6e249f4e8da270796c94", "abcdefgh"}, |
| 41 | + {"e85c35055b093f7b9948898d2e7fbaf13b7ed3b4", "abcdefghi"}, |
| 42 | + {"ac2f1f843ebb6805940ae2da76b62d11ce0c2dfb", "abcdefghij"}, |
| 43 | + {"43f87ce8207df8464ec94df98c6de614259f9f9b", "Discard medicine more than two years old."}, |
| 44 | + {"556a4b84a7aae18d533c490cc6166bceeadb1e78", "He who has a shady past knows that nice guys finish last."}, |
| 45 | + {"154c149aebd6c69113a831b410d677aef75d8c16", "I wouldn't marry him with a ten foot pole."}, |
| 46 | + {"030c1ac6c94babda05f15127ef25455a090de6d8", "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave"}, |
| 47 | + {"66f72cd3e3102a22d9921f92e1080816cc6829a6", "The days of the digital watch are numbered. -Tom Stoppard"}, |
| 48 | + {"519d3b4cbaba8d214d955bcc1b6af0f9f8d4a73a", "Nepal premier won't resign."}, |
| 49 | + {"a633be186221a0a6715e0cb7f170c2be6a595434", "For every action there is an equal and opposite government program."}, |
| 50 | + {"0255fc603ab48b6f9df88990f78262359e641621", "His money is twice tainted: 'taint yours and 'taint mine."}, |
| 51 | + {"693919e639922d0b8242115512ec5cc904758fbc", "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977"}, |
| 52 | + {"17e56ae76e612337ad7b634aa839271d60beda96", "It's a tiny change to the code and not completely disgusting. - Bob Manchek"}, |
| 53 | + {"9d3d0f7017181467bc453dbb83b676ea27291604", "size: a.out: bad magic"}, |
| 54 | + {"1cc69323cc1a8523a672372c8dc076d6d2f64381", "The major problem is with sendmail. -Mark Horton"}, |
| 55 | + {"acd8d33701fc3e776ca7113e83917f87185f01a0", "Give me a rock, paper and scissors and I will move the world. CCFestoon"}, |
| 56 | + {"8803803ded9426a430761e54addc38e4541f729e", "If the enemy is within range, then so are you."}, |
| 57 | + {"e8875d30c04df24335db4a989c5ac5de295a932b", "It's well we cannot hear the screams/That we create in others' dreams."}, |
| 58 | + {"6ce8c4a10827943b88f0fc00fb075129236c3100", "You remind me of a TV show, but that's all right: I watch it anyway."}, |
| 59 | + {"941a49d51f52c2e55a54de58f49787605e6572aa", "C is as portable as Stonehedge!!"}, |
| 60 | + {"34af7a6ff354b6d0bce0a09af0984ccae2a0d14c", "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley"}, |
| 61 | + {"2398df93efff72e5a041c092b13b81844b196c28", "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule"}, |
| 62 | + {"7dda7376c190859ad60e072139fc1028171aab4b", "How can you write a big system without C++? -Paul Glick"}, |
| 63 | +} |
| 64 | + |
| 65 | +func TestSHA(t *testing.T) { |
| 66 | + for _, g := range golden { |
| 67 | + got, err := SHA([]byte(g.in)) |
| 68 | + if err != nil { |
| 69 | + t.Fatal(err) |
| 70 | + } |
| 71 | + |
| 72 | + s := fmt.Sprintf("%x", got) |
| 73 | + if s != g.out { |
| 74 | + t.Fatalf("Sum function: sha(%s) = %s want %s", g.in, s, g.out) |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func benchmarkSHA(b *testing.B, length int64) { |
| 80 | + buf := make([]byte, length) |
| 81 | + if _, err := io.ReadFull(rand.Reader, buf); err != nil { |
| 82 | + b.Fatal(err) |
| 83 | + } |
| 84 | + b.SetBytes(length) |
| 85 | + b.ResetTimer() |
| 86 | + for i := 0; i < b.N; i++ { |
| 87 | + SHA(buf) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func BenchmarkSHALarge(b *testing.B) { |
| 92 | + benchmarkSHA(b, 1024*1024) |
| 93 | +} |
| 94 | + |
| 95 | +func BenchmarkSHAMedium(b *testing.B) { |
| 96 | + benchmarkSHA(b, 1024) |
| 97 | +} |
| 98 | + |
| 99 | +func BenchmarkSHASmall(b *testing.B) { |
| 100 | + benchmarkSHA(b, 1) |
| 101 | +} |
0 commit comments