OSDN Git Service

Remove the types float and complex.
[pf3gnuchains/gcc-fork.git] / libgo / go / crypto / openpgp / s2k / s2k_test.go
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package s2k
6
7 import (
8         "bytes"
9         "crypto/sha1"
10         "encoding/hex"
11         "testing"
12 )
13
14 var saltedTests = []struct {
15         in, out string
16 }{
17         {"hello", "10295ac1"},
18         {"world", "ac587a5e"},
19         {"foo", "4dda8077"},
20         {"bar", "bd8aac6b9ea9cae04eae6a91c6133b58b5d9a61c14f355516ed9370456"},
21         {"x", "f1d3f289"},
22         {"xxxxxxxxxxxxxxxxxxxxxxx", "e00d7b45"},
23 }
24
25 func TestSalted(t *testing.T) {
26         h := sha1.New()
27         salt := [4]byte{1, 2, 3, 4}
28
29         for i, test := range saltedTests {
30                 expected, _ := hex.DecodeString(test.out)
31                 out := make([]byte, len(expected))
32                 Salted(out, h, []byte(test.in), salt[:])
33                 if !bytes.Equal(expected, out) {
34                         t.Errorf("#%d, got: %x want: %x", i, out, expected)
35                 }
36         }
37 }
38
39
40 var iteratedTests = []struct {
41         in, out string
42 }{
43         {"hello", "83126105"},
44         {"world", "6fa317f9"},
45         {"foo", "8fbc35b9"},
46         {"bar", "2af5a99b54f093789fd657f19bd245af7604d0f6ae06f66602a46a08ae"},
47         {"x", "5a684dfe"},
48         {"xxxxxxxxxxxxxxxxxxxxxxx", "18955174"},
49 }
50
51 func TestIterated(t *testing.T) {
52         h := sha1.New()
53         salt := [4]byte{4, 3, 2, 1}
54
55         for i, test := range iteratedTests {
56                 expected, _ := hex.DecodeString(test.out)
57                 out := make([]byte, len(expected))
58                 Iterated(out, h, []byte(test.in), salt[:], 31)
59                 if !bytes.Equal(expected, out) {
60                         t.Errorf("#%d, got: %x want: %x", i, out, expected)
61                 }
62         }
63 }
64
65
66 var parseTests = []struct {
67         spec, in, out string
68 }{
69         /* Simple with SHA1 */
70         {"0102", "hello", "aaf4c61d"},
71         /* Salted with SHA1 */
72         {"02020102030405060708", "hello", "f4f7d67e"},
73         /* Iterated with SHA1 */
74         {"03020102030405060708f1", "hello", "f2a57b7c"},
75 }
76
77 func TestParse(t *testing.T) {
78         for i, test := range parseTests {
79                 spec, _ := hex.DecodeString(test.spec)
80                 buf := bytes.NewBuffer(spec)
81                 f, err := Parse(buf)
82                 if err != nil {
83                         t.Errorf("%d: Parse returned error: %s", i, err)
84                         continue
85                 }
86
87                 expected, _ := hex.DecodeString(test.out)
88                 out := make([]byte, len(expected))
89                 f(out, []byte(test.in))
90                 if !bytes.Equal(out, expected) {
91                         t.Errorf("%d: output got: %x want: %x", i, out, expected)
92                 }
93         }
94 }