File: data_test.go

package info (click to toggle)
golang-github-proglottis-gpgme 0.1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 180 kB
  • sloc: ansic: 116; makefile: 5
file content (158 lines) | stat: -rw-r--r-- 3,209 bytes parent folder | download
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
package gpgme

import (
	"bytes"
	"errors"
	"io"
	"io/ioutil"
	"os"
	"testing"
)

func TestData_memory_empty(t *testing.T) {
	dh, err := NewData()
	checkError(t, err)

	for i := 0; i < 5; i++ {
		_, err := dh.Write([]byte(testCipherText))
		checkError(t, err)
	}

	_, err = dh.Seek(0, SeekSet)
	checkError(t, err)

	testReader(t, dh, bytes.Repeat([]byte(testCipherText), 5))

	checkError(t, dh.Close())
}

func TestData_memory(t *testing.T) {
	// Test ordinary data, and empty slices
	for _, content := range [][]byte{[]byte(testCipherText), []byte{}} {
		dh, err := NewDataBytes(content)
		checkError(t, err)

		testReader(t, dh, content)

		checkError(t, dh.Close())
	}
}

func TestData_file(t *testing.T) {
	f, err := ioutil.TempFile("", "gpgme")
	checkError(t, err)
	defer func() {
		checkError(t, f.Close())
		checkError(t, os.Remove(f.Name()))
	}()

	dh, err := NewDataFile(f)
	checkError(t, err)

	for i := 0; i < 5; i++ {
		_, err := dh.Write([]byte(testCipherText))
		checkError(t, err)
	}

	_, err = dh.Seek(0, SeekSet)
	checkError(t, err)

	testReader(t, dh, bytes.Repeat([]byte(testCipherText), 5))

	checkError(t, dh.Close())
}

func TestData_callback_reading(t *testing.T) {
	r := bytes.NewReader([]byte(testCipherText))
	dh, err := NewDataReader(r)
	checkError(t, err)

	testReader(t, dh, []byte(testCipherText))

	checkError(t, dh.Close())
}

func TestData_callback_reading_error(t *testing.T) {
	expectedErr := errors.New("a special error")
	r := errReadSeeker{err: expectedErr}
	dh, err := NewDataReader(r)
	checkError(t, err)

	_, err = dh.Read(make([]byte, 10))
	if err != expectedErr {
		t.Errorf("err = %v, want %v", err, expectedErr)
	}

	checkError(t, dh.Close())
}

func TestData_callback_seeking_error(t *testing.T) {
	expectedErr := errors.New("a special error")
	r := errReadSeeker{err: expectedErr}
	dh, err := NewDataReader(r)
	checkError(t, err)

	_, err = dh.Seek(0, 0)
	if err != expectedErr {
		t.Errorf("err = %v, want %v", err, expectedErr)
	}

	checkError(t, dh.Close())
}

func TestData_callback_writing(t *testing.T) {
	var buf bytes.Buffer
	dh, err := NewDataWriter(&buf)
	checkError(t, err)

	for i := 0; i < 5; i++ {
		_, err := dh.Write([]byte(testCipherText))
		checkError(t, err)
	}

	expected := bytes.Repeat([]byte(testCipherText), 5)
	diff(t, buf.Bytes(), expected)

	checkError(t, dh.Close())
}

func TestData_callback_writing_error(t *testing.T) {
	expectedErr := errors.New("a special error")
	dh, err := NewDataWriter(errWriter{err: expectedErr})
	checkError(t, err)

	_, err = dh.Write([]byte(testData))
	if err != expectedErr {
		t.Errorf("err = %v, want %v", err, expectedErr)
	}

	checkError(t, dh.Close())
}

func testReader(t testing.TB, r io.Reader, content []byte) {
	var buf bytes.Buffer
	n, err := io.Copy(&buf, r)
	checkError(t, err)

	if int(n) != len(content) {
		t.Errorf("n = %d, want %d", n, len(content))
	}

	diff(t, buf.Bytes(), content)
}

type errWriter struct{ err error }

func (w errWriter) Write(p []byte) (int, error) {
	return 0, w.err
}

type errReadSeeker struct{ err error }

func (rs errReadSeeker) Read([]byte) (int, error) {
	return 0, rs.err
}

func (rs errReadSeeker) Seek(int64, int) (int64, error) {
	return 0, rs.err
}