Skip to content
This repository was archived by the owner on Dec 11, 2019. It is now read-only.

Commit 4282807

Browse files
Add tor.js utility subroutine tests.
1 parent 453e327 commit 4282807

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

test/unit/app/torTest.js

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/* global describe, before, after, it */
2+
3+
const assert = require('assert')
4+
const mockery = require('mockery')
5+
6+
describe('tor unit tests', () => {
7+
let tor
8+
const fakeElectron = require('../lib/fakeElectron')
9+
before(() => {
10+
mockery.enable({
11+
warnOnReplace: false,
12+
warnOnUnregistered: false,
13+
useCleanCache: true
14+
})
15+
mockery.registerMock('electron', fakeElectron)
16+
tor = require('../../../app/tor')
17+
})
18+
after(() => {
19+
mockery.disable()
20+
})
21+
22+
it('torrcEscapeString', () => {
23+
assert.strictEqual('foobar', tor.torrcEscapeString('foobar'))
24+
assert.strictEqual('" foobar"', tor.torrcEscapeString(' foobar'))
25+
assert.strictEqual('"\\x09foobar"', tor.torrcEscapeString('\tfoobar'))
26+
assert.strictEqual('"foo\\\\\\x0abar"', tor.torrcEscapeString('foo\\\nbar'))
27+
assert.strictEqual('"foo bar"', tor.torrcEscapeString('foo bar'))
28+
assert.strictEqual('"foo#bar"', tor.torrcEscapeString('foo#bar'))
29+
assert.strictEqual('"foo\\"bar"', tor.torrcEscapeString('foo"bar'))
30+
assert.strictEqual('"foo\\\\bar"', tor.torrcEscapeString('foo\\bar'))
31+
assert.strictEqual('"fno\\xcc\\x88rd"', tor.torrcEscapeString('fnörd'))
32+
assert.strictEqual('"C:\\\\Ronald\\xe2\\x80\\x99s laptop\'s disk"',
33+
tor.torrcEscapeString('C:\\Ronald’s laptop\'s disk'))
34+
})
35+
36+
it('torrcEscapeBuffer', () => {
37+
assert.strictEqual('"\\x00\\x01\\x1f \\x7f\\x80\\xfe\\xff"',
38+
tor.torrcEscapeBuffer(Buffer.from([0, 1, 31, 32, 127, 128, 254, 255])))
39+
})
40+
41+
it('torControlParseQuoted', () => {
42+
assert.deepStrictEqual([Buffer.from('127.0.0.1:41159', 'ascii'), 17],
43+
tor.torControlParseQuoted('"127.0.0.1:41159"', 0, 17))
44+
assert.deepStrictEqual([Buffer.from('unix:/a b/c', 'ascii'), 13],
45+
tor.torControlParseQuoted('"unix:/a b/c"', 0, 13))
46+
assert.deepStrictEqual([Buffer.from('unix:/a\rb/c', 'ascii'), 14],
47+
tor.torControlParseQuoted('"unix:/a\\rb/c"', 0, 14))
48+
assert.deepStrictEqual([Buffer.from('unix:/a\nb/c', 'ascii'), 14],
49+
tor.torControlParseQuoted('"unix:/a\\nb/c"', 0, 14))
50+
assert.deepStrictEqual([Buffer.from('unix:/a\tb/c', 'ascii'), 14],
51+
tor.torControlParseQuoted('"unix:/a\\tb/c"', 0, 14))
52+
assert.deepStrictEqual([Buffer.from('unix:/a\\b/c', 'ascii'), 14],
53+
tor.torControlParseQuoted('"unix:/a\\\\b/c"', 0, 14))
54+
assert.deepStrictEqual([Buffer.from('unix:/a"b/c', 'ascii'), 14],
55+
tor.torControlParseQuoted('"unix:/a\\"b/c"', 0, 14))
56+
assert.deepStrictEqual([Buffer.from('unix:/a\'b/c', 'ascii'), 14],
57+
tor.torControlParseQuoted('"unix:/a\\\'b/c"', 0, 14))
58+
assert.deepStrictEqual([Buffer.from('unix:/a b/c', 'ascii'), 13],
59+
tor.torControlParseQuoted('"unix:/a b/c" "127.0.0.1:9050"', 0, 30))
60+
assert.deepStrictEqual([null, 12],
61+
tor.torControlParseQuoted('"unix:/a b/c', 0, 12))
62+
assert.deepStrictEqual([null, 9],
63+
tor.torControlParseQuoted('"unix:/a\\fb/c"', 0, 13))
64+
})
65+
66+
it('torControlParseKV', () => {
67+
assert.deepStrictEqual(['foo', Buffer.from('bar'), 8],
68+
tor.torControlParseKV('xfoo=bary', 1, 8))
69+
assert.deepStrictEqual(['foo', Buffer.from('bar'), 10],
70+
tor.torControlParseKV('xfoo="bar"y', 1, 10))
71+
assert.deepStrictEqual(['foo', Buffer.from('bar baz'), 14],
72+
tor.torControlParseKV('xfoo="bar baz"y', 1, 14))
73+
assert.deepStrictEqual(['foo', Buffer.from('bar"baz'), 15],
74+
tor.torControlParseKV('xfoo="bar\\"baz"y', 1, 15))
75+
assert.deepStrictEqual(['foo', Buffer.from('bar"baz'), 16],
76+
tor.torControlParseKV('xfoo="bar\\"baz" quux="zot"y', 1, 26))
77+
assert.deepStrictEqual(['foo', Buffer.from('barbaz'), 12],
78+
tor.torControlParseKV('xfoo=barbaz quux=zoty', 1, 20))
79+
})
80+
})

0 commit comments

Comments
 (0)