aboutsummaryrefslogtreecommitdiff
path: root/io_test.go
blob: 582dd1efe8ca843a20dae73418a1171a17385b60 (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
package rabbitio_test

import (
	"bytes"
	"encoding/hex"
	"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")
	}

}

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