summaryrefslogtreecommitdiff
path: root/libgo/go/crypto/block/xor_test.go
blob: 50f6bb08df4d930f39d9df675be8534c77a3e770 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package block

import (
	"bytes"
	"fmt"
	"io"
	"testing"
	"testing/iotest"
)

// Simple "pseudo-random" stream for testing.
type incStream struct {
	buf []byte
	n   byte
}

func newIncStream(blockSize int) *incStream {
	x := new(incStream)
	x.buf = make([]byte, blockSize)
	return x
}

func (x *incStream) Next() []byte {
	x.n++
	for i := range x.buf {
		x.buf[i] = x.n
		x.n++
	}
	return x.buf
}

func testXorWriter(t *testing.T, maxio int) {
	var plain, crypt [256]byte
	for i := 0; i < len(plain); i++ {
		plain[i] = byte(i)
	}
	b := new(bytes.Buffer)
	for block := 1; block <= 64 && block <= maxio; block *= 2 {
		// compute encrypted version
		n := byte(0)
		for i := 0; i < len(crypt); i++ {
			if i%block == 0 {
				n++
			}
			crypt[i] = plain[i] ^ n
			n++
		}

		for frag := 0; frag < 2; frag++ {
			test := fmt.Sprintf("block=%d frag=%d maxio=%d", block, frag, maxio)
			b.Reset()
			r := bytes.NewBuffer(plain[0:])
			s := newIncStream(block)
			w := newXorWriter(s, b)

			// copy plain into w in increasingly large chunks: 1, 1, 2, 4, 8, ...
			// if frag != 0, move the 1 to the end to cause fragmentation.
			if frag == 0 {
				_, err := io.Copyn(w, r, 1)
				if err != nil {
					t.Errorf("%s: first Copyn: %s", test, err)
					continue
				}
			}
			for n := 1; n <= len(plain)/2; n *= 2 {
				_, err := io.Copyn(w, r, int64(n))
				if err != nil {
					t.Errorf("%s: Copyn %d: %s", test, n, err)
				}
			}

			// check output
			crypt := crypt[0 : len(crypt)-frag]
			data := b.Bytes()
			if len(data) != len(crypt) {
				t.Errorf("%s: want %d bytes, got %d", test, len(crypt), len(data))
				continue
			}

			if string(data) != string(crypt) {
				t.Errorf("%s: want %x got %x", test, data, crypt)
			}
		}
	}
}


func TestXorWriter(t *testing.T) {
	// Do shorter I/O sizes first; they're easier to debug.
	for n := 1; n <= 256 && !t.Failed(); n *= 2 {
		testXorWriter(t, n)
	}
}

func testXorReader(t *testing.T, maxio int) {
	var readers = []func(io.Reader) io.Reader{
		func(r io.Reader) io.Reader { return r },
		iotest.OneByteReader,
		iotest.HalfReader,
	}
	var plain, crypt [256]byte
	for i := 0; i < len(plain); i++ {
		plain[i] = byte(255 - i)
	}
	b := new(bytes.Buffer)
	for block := 1; block <= 64 && block <= maxio; block *= 2 {
		// compute encrypted version
		n := byte(0)
		for i := 0; i < len(crypt); i++ {
			if i%block == 0 {
				n++
			}
			crypt[i] = plain[i] ^ n
			n++
		}

		for mode := 0; mode < len(readers); mode++ {
			for frag := 0; frag < 2; frag++ {
				test := fmt.Sprintf("block=%d mode=%d frag=%d maxio=%d", block, mode, frag, maxio)
				s := newIncStream(block)
				b.Reset()
				r := newXorReader(s, readers[mode](bytes.NewBuffer(crypt[0:maxio])))

				// read from crypt in increasingly large chunks: 1, 1, 2, 4, 8, ...
				// if frag == 1, move the 1 to the end to cause fragmentation.
				if frag == 0 {
					_, err := io.Copyn(b, r, 1)
					if err != nil {
						t.Errorf("%s: first Copyn: %s", test, err)
						continue
					}
				}
				for n := 1; n <= maxio/2; n *= 2 {
					_, err := io.Copyn(b, r, int64(n))
					if err != nil {
						t.Errorf("%s: Copyn %d: %s", test, n, err)
					}
				}

				// check output
				data := b.Bytes()
				crypt := crypt[0 : maxio-frag]
				plain := plain[0 : maxio-frag]
				if len(data) != len(plain) {
					t.Errorf("%s: want %d bytes, got %d", test, len(plain), len(data))
					continue
				}

				if string(data) != string(plain) {
					t.Errorf("%s: input=%x want %x got %x", test, crypt, plain, data)
				}
			}
		}
	}
}

func TestXorReader(t *testing.T) {
	// Do shorter I/O sizes first; they're easier to debug.
	for n := 1; n <= 256 && !t.Failed(); n *= 2 {
		testXorReader(t, n)
	}
}

// TODO(rsc): Test handling of writes after write errors.