aboutsummaryrefslogtreecommitdiff
path: root/io_test.go
blob: dddcf657d40f531c5d0798cf793baab1f2630eae (plain) (blame)
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
package rabbitio_test

import (
	"bytes"
	"encoding/hex"
	"fmt"
	"io"
	"strings"
	"testing"

	"snix.ir/rabbitio"
)

func TestNewWriterCipher(t *testing.T) {
	key, ivt := []byte("12345678abcdefgh"), []byte("1234qwer")
	txt := "dummy text to test NewWriterCipher"
	twr := strings.NewReader(txt)

	iw := new(bytes.Buffer)

	t.Logf("encrypting plain text ---")
	cw, err := rabbitio.NewWriterCipher(key, ivt, iw)
	if err != nil {
		t.Fatal(err)
	}

	if _, err := io.Copy(cw, twr); err != nil {
		t.Fatal(err)
	}

	t.Logf("cipher-text: %v", hex.EncodeToString(iw.Bytes()))
	t.Logf("decrypting cipher text ---")

	ir := new(bytes.Buffer)
	cr, err := rabbitio.NewWriterCipher(key, ivt, ir)
	if err != nil {
		t.Fatal(err)
	}

	if _, err := io.Copy(cr, iw); err != nil {
		t.Fatal(err)
	}

	if ir.String() != txt {
		t.Error("error: ir.String() is not equal to txt")
	}
}

func TestNewReaderCipher(t *testing.T) {
	key, ivt := []byte("12345678abcdefgh"), []byte("1234qwer")
	txt := "test NewReadercipher text dummy tx"
	twr := strings.NewReader(txt)

	iw := new(bytes.Buffer)

	t.Logf("encrypting plain text ---")
	cw, err := rabbitio.NewReaderCipher(key, ivt, twr)
	if err != nil {
		t.Fatal(err)
	}

	if _, err := io.Copy(iw, cw); err != nil {
		t.Fatal(err)
	}

	t.Logf("cipher-text: %v", hex.EncodeToString(iw.Bytes()))
	t.Logf("decrypting cipher text ---")

	ir := new(bytes.Buffer)
	cr, err := rabbitio.NewReaderCipher(key, ivt, iw)
	if err != nil {
		t.Fatal(err)
	}

	if _, err := io.Copy(ir, cr); err != nil {
		t.Fatal(err)
	}

	if ir.String() != txt {
		t.Error("error: ir.String() is not equal to txt")
	}
}

func TestNewCipher(t *testing.T) {
	key, ivt := []byte("12345678abcdefgh"), []byte("1234qwer")
	txt := "test NewReadercipher text dummy tx"
	cph, err := rabbitio.NewCipher(key, ivt)
	if err != nil {
		t.Fatal(err)
	}
	dst := make([]byte, len(txt))
	cph.XORKeyStream(dst, []byte(txt))
	t.Logf("cipher-text: %v", hex.EncodeToString(dst))

	cph, err = rabbitio.NewCipher(key, ivt)
	if err != nil {
		t.Fatal(err)
	}

	nds := make([]byte, len(dst))
	cph.XORKeyStream(nds, dst)
	if string(nds) != txt {
		t.Error("error: nds is not equal to txt")
	}

}

func BenchmarkNewCipher(b *testing.B) {
	b.Run(fmt.Sprintf("bench %v", b.N), func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			key, ivt := []byte("12345678abcdefgh"), []byte("1234qwer")
			txt := "test NewReadercipher text dummy tx"
			cph, err := rabbitio.NewCipher(key, ivt)
			if err != nil {
				b.Fatal(err)
			}
			dst := make([]byte, len(txt))
			cph.XORKeyStream(dst, []byte(txt))

			cph, err = rabbitio.NewCipher(key, ivt)
			if err != nil {
				b.Fatal(err)
			}

			nds := make([]byte, len(dst))
			cph.XORKeyStream(nds, dst)
			if string(nds) != txt {
				b.Error("error: nds is not equal to txt")
			}
		}
	})

}

Snix LLC Git Repository Holder Copyright(C) 2022 All Rights Reserved Email To Snix.IR