~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/golang.org/x/crypto/nacl/secretbox/secretbox_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2012 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 secretbox
 
6
 
 
7
import (
 
8
        "bytes"
 
9
        "crypto/rand"
 
10
        "encoding/hex"
 
11
        "testing"
 
12
)
 
13
 
 
14
func TestSealOpen(t *testing.T) {
 
15
        var key [32]byte
 
16
        var nonce [24]byte
 
17
 
 
18
        rand.Reader.Read(key[:])
 
19
        rand.Reader.Read(nonce[:])
 
20
 
 
21
        var box, opened []byte
 
22
 
 
23
        for msgLen := 0; msgLen < 128; msgLen += 17 {
 
24
                message := make([]byte, msgLen)
 
25
                rand.Reader.Read(message)
 
26
 
 
27
                box = Seal(box[:0], message, &nonce, &key)
 
28
                var ok bool
 
29
                opened, ok = Open(opened[:0], box, &nonce, &key)
 
30
                if !ok {
 
31
                        t.Errorf("%d: failed to open box", msgLen)
 
32
                        continue
 
33
                }
 
34
 
 
35
                if !bytes.Equal(opened, message) {
 
36
                        t.Errorf("%d: got %x, expected %x", msgLen, opened, message)
 
37
                        continue
 
38
                }
 
39
        }
 
40
 
 
41
        for i := range box {
 
42
                box[i] ^= 0x20
 
43
                _, ok := Open(opened[:0], box, &nonce, &key)
 
44
                if ok {
 
45
                        t.Errorf("box was opened after corrupting byte %d", i)
 
46
                }
 
47
                box[i] ^= 0x20
 
48
        }
 
49
}
 
50
 
 
51
func TestSecretBox(t *testing.T) {
 
52
        var key [32]byte
 
53
        var nonce [24]byte
 
54
        var message [64]byte
 
55
 
 
56
        for i := range key[:] {
 
57
                key[i] = 1
 
58
        }
 
59
        for i := range nonce[:] {
 
60
                nonce[i] = 2
 
61
        }
 
62
        for i := range message[:] {
 
63
                message[i] = 3
 
64
        }
 
65
 
 
66
        box := Seal(nil, message[:], &nonce, &key)
 
67
        // expected was generated using the C implementation of NaCl.
 
68
        expected, _ := hex.DecodeString("8442bc313f4626f1359e3b50122b6ce6fe66ddfe7d39d14e637eb4fd5b45beadab55198df6ab5368439792a23c87db70acb6156dc5ef957ac04f6276cf6093b84be77ff0849cc33e34b7254d5a8f65ad")
 
69
 
 
70
        if !bytes.Equal(box, expected) {
 
71
                t.Fatalf("box didn't match, got\n%x\n, expected\n%x", box, expected)
 
72
        }
 
73
}
 
74
 
 
75
func TestAppend(t *testing.T) {
 
76
        var key [32]byte
 
77
        var nonce [24]byte
 
78
        var message [8]byte
 
79
 
 
80
        out := make([]byte, 4)
 
81
        box := Seal(out, message[:], &nonce, &key)
 
82
        if !bytes.Equal(box[:4], out[:4]) {
 
83
                t.Fatalf("Seal didn't correctly append")
 
84
        }
 
85
 
 
86
        out = make([]byte, 4, 100)
 
87
        box = Seal(out, message[:], &nonce, &key)
 
88
        if !bytes.Equal(box[:4], out[:4]) {
 
89
                t.Fatalf("Seal didn't correctly append with sufficient capacity.")
 
90
        }
 
91
}