test-stream.js
5.13 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* Copyright (c) 2013, Ben Noordhuis <info@bnoordhuis.nl>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
'use strict';
const Iconv = require('../').Iconv;
const assert = require('assert');
const stream = require('stream');
const net = require('net');
const fs = require('fs');
const PORT = 12345;
assert(new Iconv('ascii', 'ascii') instanceof stream.Stream);
(function() {
const infile = __dirname + '/fixtures/lorem-ipsum.txt';
const input = fs.createReadStream(infile,
{ bufferSize: 64,
lowWaterMark: 0,
highWaterMark: 64 });
const outfile = __dirname + '/tmp/lorem-ipsum.txt';
const output = fs.createWriteStream(outfile);
input.pipe(Iconv('ascii', 'utf-16le')).pipe(output);
output.on('end', function() {
const input = fs.readFileSync(infile);
const output = fs.readFileSync(outfile);
for (let i = 0; i < input.length; ++i) {
assert.equal(output[i * 2 + 0], input[i]);
assert.equal(output[i * 2 + 1], 0);
}
});
})();
(function() {
let ok = false;
process.on('exit', function() {
assert(ok);
});
const server = net.createServer(function(conn) {
conn.pipe(Iconv('utf-8', 'utf-7')).pipe(conn);
server.close();
});
server.listen(PORT, function() {
const conn = net.createConnection(PORT);
conn.setEncoding('utf-8');
conn.write('çxç');
conn.on('data', function(s) {
assert.equal(s, '+AOc-x+AOc-');
ok = true;
conn.destroy();
});
});
})();
(function() {
let ok = false;
const stream = Iconv('utf-8', 'utf-7');
stream.on('data', function(s) {
assert.equal(s, '+AOc-x+AOc-');
ok = true;
});
stream.write('çxç'); // String should get converted to buffer.
assert(ok);
})();
(function() {
let num_data_events = 0;
let num_end_events = 0;
const stream = Iconv('utf-8', 'ascii');
stream.on('data', function(s) {
assert.equal(num_data_events, 0);
assert.equal(num_end_events, 0);
assert.equal(s, 'test');
num_data_events += 1;
});
stream.on('end', function() {
assert.equal(num_data_events, 1);
assert.equal(num_end_events, 0);
num_end_events += 1;
});
stream.end('test');
assert.equal(num_data_events, 1);
assert.equal(num_end_events, 1);
})();
(function() {
const stream = Iconv('utf-8', 'ascii//translit');
let ok = false;
stream.on('data', function() {
assert.equal(ok, false);
});
stream.on('end', function() {
assert.equal(ok, false);
ok = true;
});
fs.createReadStream(__filename).pipe(stream);
process.on('exit', function() {
assert(ok);
});
})();
(function() {
let ok = false;
const stream = Iconv('utf-8', 'iso-8859-1');
stream.on('data', function(buf) {
assert.equal(buf.length, 1);
assert.equal(buf[0], 0xA9);
ok = true;
});
stream.write(Buffer.from([0xC2]));
stream.write(Buffer.from([0xA9]));
assert(ok);
})();
(function() {
let ok = false;
const stream = Iconv('utf-8', 'iso-8859-1');
stream.once('data', step1);
function step1(buf) {
assert.equal(buf.length, 1);
assert.equal(buf[0], 0xAE);
stream.once('data', step2);
}
function step2(buf) {
assert.equal(buf.length, 1);
assert.equal(buf[0], 0xA9);
ok = true;
}
stream.write(Buffer.from([0xC2,0xAE,0xC2]));
stream.write(Buffer.from([0xA9]));
assert(ok);
})();
(function() {
let ok = false;
const stream = Iconv('utf-8', 'iso-8859-1');
stream.once('data', function(buf) {
assert.equal(buf.toString(), 'ok');
ok = true;
});
stream.write('b2s=', 'base64');
assert(ok);
})();
(function() {
let ok = false;
const stream = Iconv('utf-8', 'iso-8859-1');
stream.once('data', function(buf) {
assert.equal(buf.toString(), 'ok');
ok = true;
});
stream.end('b2s=', 'base64');
assert(ok);
})();
(function() {
let ok = false;
const stream = Iconv('gb18030', 'utf-8');
stream.once('error', function(e) {
assert.equal(e.message, 'Incomplete character sequence.');
assert.equal(e.code, 'EINVAL');
ok = true;
});
const octets = [
0x00, 0xf1, 0x52, 0x00, 0x00, 0x78, 0x51, 0xd9, 0xf7, 0x78, 0x51, 0xd9
];
stream.end(Buffer.from(octets));
assert(ok);
})();
(function() {
let ok = false;
const stream = Iconv('utf-8', 'utf-16');
stream.once('error', function(e) {
assert.equal(e.message, 'Incomplete character sequence.');
assert.equal(e.code, 'EINVAL');
ok = true;
});
stream.end(Buffer.from([0xc3]));
assert(ok);
})();