mirror of
https://github.com/ericyan/shield.git
synced 2025-09-27 03:25:54 +08:00
Add test for stream package
This commit is contained in:
27
stream/stream_test.go
Normal file
27
stream/stream_test.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package stream
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
type fakePRNG struct{}
|
||||||
|
|
||||||
|
func (*fakePRNG) Uint64() uint64 {
|
||||||
|
return uint64(0XDEADBEEF)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStreamCipher(t *testing.T) {
|
||||||
|
plaintext := []byte("hello, world")
|
||||||
|
|
||||||
|
ciphertext := make([]byte, len(plaintext))
|
||||||
|
c1 := NewCipher(new(fakePRNG))
|
||||||
|
c1.XORKeyStream(ciphertext, plaintext)
|
||||||
|
|
||||||
|
plaintext2 := make([]byte, len(plaintext))
|
||||||
|
c2 := NewCipher(new(fakePRNG))
|
||||||
|
c2.XORKeyStream(plaintext2, ciphertext)
|
||||||
|
|
||||||
|
for i, v := range plaintext2 {
|
||||||
|
if v != plaintext[i] {
|
||||||
|
t.Fatalf("mismatch at byte %d:\nhave: % x\nwant: % x", i, plaintext2, plaintext)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user