mobiletto 1.2.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/index.d.ts +2 -0
- package/lib/cjs/index.js +30 -0
- package/lib/cjs/package.json +16 -0
- package/lib/esm/index.d.ts +2 -0
- package/lib/esm/index.js +12 -0
- package/package.json +27 -13
- package/.babelrc +0 -5
- package/.eslintrc.js +0 -14
- package/docs/README.md +0 -113
- package/docs/af/README.md +0 -481
- package/docs/am/README.md +0 -481
- package/docs/ar/README.md +0 -481
- package/docs/az/README.md +0 -481
- package/docs/be/README.md +0 -481
- package/docs/bg/README.md +0 -481
- package/docs/bn/README.md +0 -481
- package/docs/bs/README.md +0 -481
- package/docs/ca/README.md +0 -481
- package/docs/ceb/README.md +0 -481
- package/docs/co/README.md +0 -481
- package/docs/cs/README.md +0 -481
- package/docs/cy/README.md +0 -481
- package/docs/da/README.md +0 -481
- package/docs/de/README.md +0 -481
- package/docs/el/README.md +0 -481
- package/docs/eo/README.md +0 -481
- package/docs/es/README.md +0 -481
- package/docs/et/README.md +0 -481
- package/docs/eu/README.md +0 -481
- package/docs/fa/README.md +0 -481
- package/docs/fi/README.md +0 -481
- package/docs/fr/README.md +0 -481
- package/docs/fy/README.md +0 -481
- package/docs/ga/README.md +0 -481
- package/docs/gd/README.md +0 -481
- package/docs/gl/README.md +0 -481
- package/docs/gu/README.md +0 -481
- package/docs/ha/README.md +0 -481
- package/docs/haw/README.md +0 -481
- package/docs/he/README.md +0 -481
- package/docs/hi/README.md +0 -481
- package/docs/hmn/README.md +0 -481
- package/docs/hr/README.md +0 -481
- package/docs/ht/README.md +0 -481
- package/docs/hu/README.md +0 -481
- package/docs/hy/README.md +0 -481
- package/docs/id/README.md +0 -481
- package/docs/ig/README.md +0 -481
- package/docs/is/README.md +0 -481
- package/docs/it/README.md +0 -481
- package/docs/iw/README.md +0 -481
- package/docs/ja/README.md +0 -481
- package/docs/jv/README.md +0 -481
- package/docs/ka/README.md +0 -481
- package/docs/kk/README.md +0 -481
- package/docs/km/README.md +0 -481
- package/docs/kn/README.md +0 -481
- package/docs/ko/README.md +0 -481
- package/docs/ku/README.md +0 -481
- package/docs/ky/README.md +0 -481
- package/docs/la/README.md +0 -481
- package/docs/lb/README.md +0 -481
- package/docs/lo/README.md +0 -481
- package/docs/lt/README.md +0 -481
- package/docs/lv/README.md +0 -481
- package/docs/mg/README.md +0 -481
- package/docs/mi/README.md +0 -481
- package/docs/mk/README.md +0 -481
- package/docs/ml/README.md +0 -481
- package/docs/mn/README.md +0 -481
- package/docs/mr/README.md +0 -481
- package/docs/ms/README.md +0 -481
- package/docs/mt/README.md +0 -481
- package/docs/my/README.md +0 -481
- package/docs/ne/README.md +0 -481
- package/docs/nl/README.md +0 -481
- package/docs/no/README.md +0 -481
- package/docs/ny/README.md +0 -481
- package/docs/or/README.md +0 -481
- package/docs/pa/README.md +0 -481
- package/docs/pl/README.md +0 -481
- package/docs/ps/README.md +0 -481
- package/docs/pt/README.md +0 -481
- package/docs/ro/README.md +0 -481
- package/docs/ru/README.md +0 -481
- package/docs/rw/README.md +0 -481
- package/docs/sd/README.md +0 -481
- package/docs/si/README.md +0 -481
- package/docs/sk/README.md +0 -481
- package/docs/sl/README.md +0 -481
- package/docs/sm/README.md +0 -481
- package/docs/sn/README.md +0 -481
- package/docs/so/README.md +0 -481
- package/docs/sq/README.md +0 -481
- package/docs/sr/README.md +0 -481
- package/docs/st/README.md +0 -481
- package/docs/su/README.md +0 -481
- package/docs/sv/README.md +0 -481
- package/docs/sw/README.md +0 -481
- package/docs/ta/README.md +0 -481
- package/docs/te/README.md +0 -481
- package/docs/tg/README.md +0 -481
- package/docs/th/README.md +0 -481
- package/docs/tk/README.md +0 -481
- package/docs/tl/README.md +0 -481
- package/docs/tr/README.md +0 -481
- package/docs/tt/README.md +0 -481
- package/docs/ug/README.md +0 -481
- package/docs/uk/README.md +0 -481
- package/docs/ur/README.md +0 -481
- package/docs/uz/README.md +0 -481
- package/docs/vi/README.md +0 -481
- package/docs/xh/README.md +0 -481
- package/docs/yi/README.md +0 -481
- package/docs/yo/README.md +0 -481
- package/docs/zh/README.md +0 -481
- package/docs/zu/README.md +0 -481
- package/drivers/b2/index.js +0 -451
- package/drivers/local/index.js +0 -315
- package/drivers/s3/index.js +0 -316
- package/hokey.json +0 -16
- package/index.js +0 -29
- package/test/api.spec.js +0 -512
- package/test/crypt.spec.js +0 -67
package/test/api.spec.js
DELETED
|
@@ -1,512 +0,0 @@
|
|
|
1
|
-
// To run the tests, you need a .env file one level above this directory
|
|
2
|
-
require('dotenv').config()
|
|
3
|
-
|
|
4
|
-
const fs = require('fs')
|
|
5
|
-
const { basename, dirname } = require('path')
|
|
6
|
-
const randomstring = require('randomstring')
|
|
7
|
-
const crypto = require('crypto')
|
|
8
|
-
|
|
9
|
-
const {
|
|
10
|
-
M_DIR, M_FILE, MobilettoNotFoundError, logger
|
|
11
|
-
} = require('mobiletto-common')
|
|
12
|
-
|
|
13
|
-
const { connect, mobiletto, redis, closeRedis } = require('mobiletto-base')
|
|
14
|
-
|
|
15
|
-
const { registerDrivers } = require("../index")
|
|
16
|
-
registerDrivers()
|
|
17
|
-
|
|
18
|
-
const { expect, should, assert } = require('chai')
|
|
19
|
-
|
|
20
|
-
// chunk size used by generator function, used by driver's 'write' function
|
|
21
|
-
// the temp file is also TEMP_SZ_MULTIPLE of this number
|
|
22
|
-
const READ_SZ = 8 * 1024 // xfer data in 8k chunks
|
|
23
|
-
const TEMP_SZ_MULTIPLE = 3 // temp file will be ~24k (READ_SZ * 3)
|
|
24
|
-
|
|
25
|
-
const SINGLE_DRIVER = process.env.SINGLE_DRIVER || null
|
|
26
|
-
|
|
27
|
-
DRIVER_CONFIG = {
|
|
28
|
-
local: {
|
|
29
|
-
key: process.env.LOCAL_STORAGE || '/tmp'
|
|
30
|
-
},
|
|
31
|
-
s3: {
|
|
32
|
-
key: process.env.S3_ACCESS,
|
|
33
|
-
secret: process.env.S3_SECRET,
|
|
34
|
-
opts: {
|
|
35
|
-
bucket: process.env.S3_BUCKET,
|
|
36
|
-
prefix: process.env.S3_PREFIX || 'mobiletto_test',
|
|
37
|
-
region: process.env.S3_REGION
|
|
38
|
-
}
|
|
39
|
-
},
|
|
40
|
-
b2: {
|
|
41
|
-
key: process.env.B2_KEY_ID,
|
|
42
|
-
secret: process.env.B2_KEY_SECRET,
|
|
43
|
-
opts: {
|
|
44
|
-
bucket: process.env.B2_BUCKET,
|
|
45
|
-
prefix: process.env.B2_PREFIX || 'mobiletto_test'
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
DRIVER_NAMES = Object.keys(DRIVER_CONFIG)
|
|
50
|
-
|
|
51
|
-
function pickAnotherDriver (notThisOne) {
|
|
52
|
-
let choice = null
|
|
53
|
-
while (!choice) {
|
|
54
|
-
choice = DRIVER_NAMES[Math.floor(Math.random() * DRIVER_NAMES.length)]
|
|
55
|
-
if (choice === notThisOne) {
|
|
56
|
-
choice = null
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
return choice
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
async function assertMeta (api, name, expectedSize) {
|
|
63
|
-
const meta = await api.metadata(name)
|
|
64
|
-
should().exist(meta, 'expected return value from metadata call')
|
|
65
|
-
expect(meta.name).equals(name, 'expected name of written file to be correct')
|
|
66
|
-
expect(meta.size).equals(expectedSize, 'expected size of written file to equal size of randomData')
|
|
67
|
-
expect(meta.mtime).is.greaterThan(0, 'expected mtime of written file to be > 0')
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
async function assertMetaFail (api, name) {
|
|
71
|
-
try {
|
|
72
|
-
const meta = await api.metadata(name)
|
|
73
|
-
assert.fail(`loading metadata after deletion, did not expect to get meta: ${meta ? JSON.stringify(meta) : 'null or undefined'}`)
|
|
74
|
-
} catch (err) {
|
|
75
|
-
if (!(err instanceof MobilettoNotFoundError)) {
|
|
76
|
-
// MobilettoNotFoundError is expected
|
|
77
|
-
assert.fail(`loading metadata after deletion, got err: ${err}`)
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
async function assertSafeMetaNull (api, name) {
|
|
83
|
-
try {
|
|
84
|
-
const meta = await api.safeMetadata(name)
|
|
85
|
-
expect(meta).to.be.null
|
|
86
|
-
} catch (err) {
|
|
87
|
-
assert.fail(`loading metadata with safeMetadata should not throw error: ${err}`)
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
const rand = count => randomstring.generate(count)
|
|
92
|
-
|
|
93
|
-
async function writeRandomFile(fixture, size) {
|
|
94
|
-
const data = fixture.randomData
|
|
95
|
-
function* dataGenerator() {
|
|
96
|
-
// read in 8k chunks
|
|
97
|
-
for (let i = 0; i < data.length; i += size) {
|
|
98
|
-
const end = (i + size) > data.length ? data.length : (i + size)
|
|
99
|
-
yield data.slice(i, end)
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
return await fixture.api.write(fixture.name, dataGenerator())
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
const encryptionTests = () => [null, { key: rand(32) }]
|
|
106
|
-
// const encryptionTests = () => [null]
|
|
107
|
-
// const encryptionTests = () => [{ key: rand(32) }]
|
|
108
|
-
|
|
109
|
-
const REDIS_ENABLED = {
|
|
110
|
-
name: 'redis-enabled',
|
|
111
|
-
enabled: true,
|
|
112
|
-
config: cfg => Object.assign({}, cfg)
|
|
113
|
-
}
|
|
114
|
-
const REDIS_DISABLED = {
|
|
115
|
-
name: 'redis-disabled',
|
|
116
|
-
enabled: true,
|
|
117
|
-
config: (cfg) => {
|
|
118
|
-
const newConfig = Object.assign({}, cfg)
|
|
119
|
-
if (!newConfig.opts) { newConfig.opts = {} }
|
|
120
|
-
if (!newConfig.opts.redisConfig) { newConfig.opts.redisConfig = {} }
|
|
121
|
-
newConfig.opts.redisConfig.enabled = false
|
|
122
|
-
return newConfig
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
const redisTests = () => [REDIS_ENABLED, REDIS_DISABLED]
|
|
127
|
-
// const redisTests = () => [REDIS_ENABLED]
|
|
128
|
-
// const redisTests = () => [REDIS_DISABLED]
|
|
129
|
-
|
|
130
|
-
for (const redisSetup of redisTests()) {
|
|
131
|
-
if (!redisSetup.enabled) continue
|
|
132
|
-
|
|
133
|
-
for (const driverName of DRIVER_NAMES) {
|
|
134
|
-
// For testing single drivers
|
|
135
|
-
if (SINGLE_DRIVER != null && SINGLE_DRIVER !== driverName) continue
|
|
136
|
-
|
|
137
|
-
const driverTest = `${driverName} [${redisSetup.name}]`
|
|
138
|
-
const config = redisSetup.config(DRIVER_CONFIG[driverName])
|
|
139
|
-
const nonexistentFile = 'random_file_that_does_not_exist_' + rand(100) + '_' + Date.now()
|
|
140
|
-
const tempFilename = (name, i) => name + (i > 0 ? '_' + i : '')
|
|
141
|
-
describe(`${driverTest} test`, () => {
|
|
142
|
-
beforeEach ((done) => {
|
|
143
|
-
redis.flushAll().then(
|
|
144
|
-
() => { logger.info(`${driverTest} - flushed redis`) },
|
|
145
|
-
(e) => {
|
|
146
|
-
logger.error(`${driverTest} - error flushing redis: ${e}`)
|
|
147
|
-
throw e
|
|
148
|
-
}
|
|
149
|
-
).finally(() => done())
|
|
150
|
-
})
|
|
151
|
-
|
|
152
|
-
describe(`${driverTest} - create api client`, () => {
|
|
153
|
-
it("should validate the config and return an API object", async () => {
|
|
154
|
-
try {
|
|
155
|
-
const api = await mobiletto(driverName, config.key, config.secret, config.opts)
|
|
156
|
-
should().exist(api, 'expected API object to exist')
|
|
157
|
-
} catch (e) {
|
|
158
|
-
logger.error(`error creating API: ${e} ${JSON.stringify(e)}`)
|
|
159
|
-
throw e
|
|
160
|
-
}
|
|
161
|
-
})
|
|
162
|
-
})
|
|
163
|
-
|
|
164
|
-
describe(`${driverTest} - listing with no arguments returns some results`, () => {
|
|
165
|
-
it("should return some results from a default listing", async () => {
|
|
166
|
-
const api = await mobiletto(driverName, config.key, config.secret, config.opts)
|
|
167
|
-
const results = await api.list()
|
|
168
|
-
expect(results).to.have.lengthOf.greaterThan(0, 'expected some results in the default listing')
|
|
169
|
-
})
|
|
170
|
-
})
|
|
171
|
-
|
|
172
|
-
describe(`${driverTest} - write a file, read file, read metadata, delete file`, () => {
|
|
173
|
-
// some random data, plus a bit extra
|
|
174
|
-
const size = (READ_SZ * TEMP_SZ_MULTIPLE) + Math.floor(Math.random() * (READ_SZ/2))
|
|
175
|
-
const randomData = rand(size)
|
|
176
|
-
const fileSuffix = '' + Date.now()
|
|
177
|
-
let fixture
|
|
178
|
-
before((done) => {
|
|
179
|
-
const name = `test_file_${fileSuffix}`
|
|
180
|
-
connect(driverName, config.key, config.secret, config.opts)
|
|
181
|
-
.then(api => { fixture = {api, name, randomData} })
|
|
182
|
-
.finally(done)
|
|
183
|
-
})
|
|
184
|
-
it("should write a file", async () => {
|
|
185
|
-
const response = await writeRandomFile(fixture, size)
|
|
186
|
-
expect(response).to.equal(size, 'expected write API to return correct number of bytes written')
|
|
187
|
-
})
|
|
188
|
-
it("should read the file we just wrote", async () => {
|
|
189
|
-
const data = await fixture.api.readFile(fixture.name)
|
|
190
|
-
expect(data.length).is.equal(size, 'expected read API to return correct number of bytes read')
|
|
191
|
-
expect(data.toString()).to.equal(fixture.randomData, 'expected to read back the same data we wrote')
|
|
192
|
-
})
|
|
193
|
-
it("should load metadata on the file we just wrote", async () => {
|
|
194
|
-
await assertMeta(fixture.api, fixture.name, fixture.randomData.length)
|
|
195
|
-
})
|
|
196
|
-
it("should delete the file we just wrote", async () => {
|
|
197
|
-
const removed = await fixture.api.remove(fixture.name)
|
|
198
|
-
expect(!!removed).to.be.true
|
|
199
|
-
})
|
|
200
|
-
it("loading metadata on the file we wrote now fails", async () => {
|
|
201
|
-
await assertMetaFail(fixture.api, fixture.name)
|
|
202
|
-
})
|
|
203
|
-
it("loading metadata on the file we wrote using safeMeta does not fail but returns null", async () => {
|
|
204
|
-
await assertSafeMetaNull(fixture.api, fixture.name)
|
|
205
|
-
})
|
|
206
|
-
})
|
|
207
|
-
|
|
208
|
-
for (const encryption of encryptionTests()) {
|
|
209
|
-
const encDesc = encryption ? '(with encryption)' : '(without encryption)'
|
|
210
|
-
if (encryption && redisSetup.name !== 'redis-enabled' && driverName !== 'local') {
|
|
211
|
-
// encryption without redis is simply too slow, except for local filesystems
|
|
212
|
-
continue
|
|
213
|
-
}
|
|
214
|
-
describe(`${driverTest} - ${encDesc} fail to write and delete files in readOnly mode`, () => {
|
|
215
|
-
// describe(`${driverTest} - ENC fail to write and delete files in readOnly mode`, () => {
|
|
216
|
-
// const encryption = {key: rand(32)}
|
|
217
|
-
const size = 16
|
|
218
|
-
const randomData = rand(size)
|
|
219
|
-
const fileSuffix = '' + Date.now()
|
|
220
|
-
let fixture
|
|
221
|
-
before((done) => {
|
|
222
|
-
const name = `test_file_${fileSuffix}`
|
|
223
|
-
const opts = Object.assign({}, config.opts, {readOnly: true})
|
|
224
|
-
connect(driverName, config.key, config.secret, opts, encryption)
|
|
225
|
-
.then(api => { fixture = {api, name, randomData} })
|
|
226
|
-
.catch(err => { throw err })
|
|
227
|
-
.finally(done)
|
|
228
|
-
})
|
|
229
|
-
it("should try to write a file and fail", async () => {
|
|
230
|
-
const response = await writeRandomFile(fixture, size)
|
|
231
|
-
expect(response).to.be.false
|
|
232
|
-
})
|
|
233
|
-
it("should fail to read the file we did not write", async () => {
|
|
234
|
-
try {
|
|
235
|
-
await fixture.api.readFile(fixture.name)
|
|
236
|
-
assert.fail('should not have been able to read non-existent file that we did not write')
|
|
237
|
-
} catch (e) {
|
|
238
|
-
if (!(e instanceof MobilettoNotFoundError)) {
|
|
239
|
-
assert.fail(`unexpected error: ${e} trying to read non-existent file`)
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
})
|
|
243
|
-
it("should fail to load metadata on the file we did not write", async () => {
|
|
244
|
-
try {
|
|
245
|
-
await assertMeta(fixture.api, fixture.name, fixture.randomData.length)
|
|
246
|
-
assert.fail('should not have been able to read metadata for non-existent file that we did not write')
|
|
247
|
-
} catch (e) {
|
|
248
|
-
if (!(e instanceof MobilettoNotFoundError)) {
|
|
249
|
-
assert.fail(`unexpected error: ${e} trying to read metadata on non-existent file`)
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
})
|
|
253
|
-
it("should fail to delete the file we did not write", async () => {
|
|
254
|
-
const removed = await fixture.api.remove(fixture.name)
|
|
255
|
-
expect(!!removed).to.be.false
|
|
256
|
-
})
|
|
257
|
-
})
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
describe(`${driverTest} - write an encrypted file, read file, read metadata, delete file`, () => {
|
|
261
|
-
// some random data, plus a bit extra
|
|
262
|
-
const size = (READ_SZ * TEMP_SZ_MULTIPLE) + Math.floor(Math.random() * (READ_SZ/2))
|
|
263
|
-
const randomData = rand(size)
|
|
264
|
-
const fileSuffix = '' + Date.now()
|
|
265
|
-
const encryptionKey = rand(32)
|
|
266
|
-
let fixture
|
|
267
|
-
before((done) => {
|
|
268
|
-
const name = `test_file_${fileSuffix}`
|
|
269
|
-
mobiletto(driverName, config.key, config.secret, config.opts, {key: encryptionKey})
|
|
270
|
-
.then(api => { fixture = {api, name, randomData} })
|
|
271
|
-
.catch((err) => { throw err })
|
|
272
|
-
.finally(done)
|
|
273
|
-
})
|
|
274
|
-
it("should write an encrypted file", async () => {
|
|
275
|
-
const bytesWritten = await writeRandomFile(fixture, size)
|
|
276
|
-
expect(bytesWritten).is.equal(size, 'expected write API to return correct number of bytes written')
|
|
277
|
-
})
|
|
278
|
-
it("should read the encrypted file we just wrote", async () => {
|
|
279
|
-
const data = await fixture.api.readFile(fixture.name)
|
|
280
|
-
expect(data.length).is.equal(size, 'expected read API to return correct number of bytes read')
|
|
281
|
-
expect(data.toString()).to.equal(fixture.randomData, 'expected to read back the same data we wrote')
|
|
282
|
-
})
|
|
283
|
-
it("should load metadata on the encrypted file we just wrote", async () => {
|
|
284
|
-
await assertMeta(fixture.api, fixture.name, fixture.randomData.length)
|
|
285
|
-
})
|
|
286
|
-
it("should delete the encrypted file we just wrote", async () => {
|
|
287
|
-
const removed = await fixture.api.remove(fixture.name)
|
|
288
|
-
expect(!!removed).to.be.true
|
|
289
|
-
})
|
|
290
|
-
it("loading metadata on the encrypted file we wrote now fails", async () => {
|
|
291
|
-
await assertMetaFail(fixture.api, fixture.name)
|
|
292
|
-
})
|
|
293
|
-
})
|
|
294
|
-
|
|
295
|
-
for (const encryption of encryptionTests()) {
|
|
296
|
-
const encDesc = encryption ? '(with encryption)' : '(without encryption)'
|
|
297
|
-
if (encryption && redisSetup.name !== 'redis-enabled') {
|
|
298
|
-
// encryption without redis is simply too slow
|
|
299
|
-
continue
|
|
300
|
-
}
|
|
301
|
-
const mirrorDriver = pickAnotherDriver(driverName)
|
|
302
|
-
// const mirrorDriver = 'local' // for hard-coding the mirror driver
|
|
303
|
-
const mirrorConfig = DRIVER_CONFIG[mirrorDriver]
|
|
304
|
-
const mirrorDest = `mirrorDest_${rand(5)}/`
|
|
305
|
-
describe(`${driverTest} - ${encDesc} write files in a new dir, read metadata, mirror to another place (${mirrorDriver}), verify mirror, recursively delete from both`, () => {
|
|
306
|
-
// to force encryption or no encryption, uncomment one of these:
|
|
307
|
-
// const encryption = {key: rand(32)}
|
|
308
|
-
// const encryption = null
|
|
309
|
-
|
|
310
|
-
// a random directory, a subdirectory, and files within it
|
|
311
|
-
const randomParent = `testRPD_${rand(2)}/rand_${rand(4)}`
|
|
312
|
-
const subdirName = `subdir_` + Date.now()
|
|
313
|
-
const fullSubdirPath = `${randomParent}/${subdirName}`
|
|
314
|
-
const randomPath = `${fullSubdirPath}/random_file_${Date.now()}`
|
|
315
|
-
const randomStreamFileSize = READ_SZ * 10
|
|
316
|
-
const randomStreamFileData = crypto.randomBytes(randomStreamFileSize)
|
|
317
|
-
//const fileCount = 3 + Math.floor(Math.random() * 10)
|
|
318
|
-
const fileCount = 2
|
|
319
|
-
let fixture
|
|
320
|
-
before((done) => {
|
|
321
|
-
mobiletto(driverName, config.key, config.secret, config.opts, encryption)
|
|
322
|
-
.then(api => {
|
|
323
|
-
// const mirrorEnc = { key: rand(32) }
|
|
324
|
-
const mirrorEnc = null
|
|
325
|
-
mobiletto(mirrorDriver, mirrorConfig.key, mirrorConfig.secret, mirrorConfig.opts, mirrorEnc)
|
|
326
|
-
.then(mirrorApi => {
|
|
327
|
-
fixture = {api, mirrorApi, name: randomPath}
|
|
328
|
-
})
|
|
329
|
-
.catch((err) => {
|
|
330
|
-
throw err
|
|
331
|
-
})
|
|
332
|
-
.finally(done)
|
|
333
|
-
})
|
|
334
|
-
.catch((err) => { throw err })
|
|
335
|
-
})
|
|
336
|
-
it(`should write ${fileCount} files in a new directory`, async () => {
|
|
337
|
-
// return one chunk of random data
|
|
338
|
-
function* dataGenerator() { yield rand(READ_SZ) }
|
|
339
|
-
for (let i = 0; i < fileCount; i++) {
|
|
340
|
-
const bytesWritten = await fixture.api.write(tempFilename(fixture.name, i), dataGenerator())
|
|
341
|
-
expect(bytesWritten).to.equal(READ_SZ, 'expected write API to return correct number of bytes written')
|
|
342
|
-
}
|
|
343
|
-
})
|
|
344
|
-
it('should write a file in a new directory using a stream', async () => {
|
|
345
|
-
// create a random temp file, write it
|
|
346
|
-
const tempFile = `/tmp/${randomstring.generate(10)}_cool_${Date.now()}`
|
|
347
|
-
fs.writeFileSync(tempFile, randomStreamFileData)
|
|
348
|
-
const reader = fs.createReadStream(tempFile)
|
|
349
|
-
const streamFile = fixture.name + '_stream'
|
|
350
|
-
const bytesWritten = await fixture.api.write(streamFile, reader)
|
|
351
|
-
expect(bytesWritten).to.equal(randomStreamFileSize, 'expected write API to return correct number of bytes written for STREAM')
|
|
352
|
-
})
|
|
353
|
-
it("should successfully read and then remove the file that was stream-written", async () => {
|
|
354
|
-
const streamFile = fixture.name + '_stream'
|
|
355
|
-
const singleFile = (await fixture.api.readFile(streamFile))
|
|
356
|
-
expect(singleFile).to.have.lengthOf(randomStreamFileSize)
|
|
357
|
-
for (let i = 0; i < singleFile.length; i++) {
|
|
358
|
-
expect(singleFile[i]).to.equal(randomStreamFileData[i])
|
|
359
|
-
}
|
|
360
|
-
const removal = await fixture.api.remove(streamFile)
|
|
361
|
-
expect(removal).to.equal(streamFile, `expected stream file to be deleted, but was: ${removal}`)
|
|
362
|
-
})
|
|
363
|
-
it("should successfully list a file individually", async () => {
|
|
364
|
-
const singleFile = await fixture.api.list(fixture.name)
|
|
365
|
-
expect(singleFile).to.have.lengthOf(1)
|
|
366
|
-
expect(singleFile[0]).to.have.property('name', fixture.name)
|
|
367
|
-
})
|
|
368
|
-
it("should successfully list files in the base directory", async () => {
|
|
369
|
-
const baseListing = await fixture.api.list()
|
|
370
|
-
expect(baseListing).to.have.lengthOf.greaterThanOrEqual(1)
|
|
371
|
-
expect(baseListing.find(o => o.name === dirname(randomParent) || o.name === dirname(randomParent)+'/')).to.not.be.null
|
|
372
|
-
})
|
|
373
|
-
it("should successfully list files in a subdirectory, with or without a trailing slash", async () => {
|
|
374
|
-
const noSlash = await fixture.api.list(randomParent)
|
|
375
|
-
expect(noSlash).to.have.lengthOf.greaterThanOrEqual(1)
|
|
376
|
-
const withSlash = await fixture.api.list(randomParent+'/')
|
|
377
|
-
expect(withSlash).to.have.lengthOf.greaterThanOrEqual(1)
|
|
378
|
-
for (const f of noSlash) {
|
|
379
|
-
expect(withSlash.find(o => o.name === f.name)).to.not.be.null
|
|
380
|
-
}
|
|
381
|
-
for (const f of withSlash) {
|
|
382
|
-
expect(noSlash.find(o => o.name === f.name)).to.not.be.null
|
|
383
|
-
}
|
|
384
|
-
})
|
|
385
|
-
it("should successfully recursively list files and see all correct subdirectories and files", async () => {
|
|
386
|
-
const allFiles = await fixture.api.list(randomParent, { recursive: true })
|
|
387
|
-
expect(allFiles).to.have.lengthOf.greaterThanOrEqual(fileCount)
|
|
388
|
-
// Not all drivers return "dir" entries when recursively listing. We should see all the files though
|
|
389
|
-
// expect(allFiles.find(f => f.name === fullSubdirPath && f.type === M_DIR)).to.not.be.null
|
|
390
|
-
expect(allFiles.find(f => f.name === fixture.name && f.type === M_FILE)).to.not.be.null
|
|
391
|
-
for (let i = 0; i < fileCount; i++) {
|
|
392
|
-
expect(allFiles.find(f => f.name === tempFilename(fixture.name, i) && f.type === M_FILE)).to.not.be.null
|
|
393
|
-
}
|
|
394
|
-
})
|
|
395
|
-
it("should load metadata for one of the new files", async () => {
|
|
396
|
-
await assertMeta(fixture.api, fixture.name, READ_SZ)
|
|
397
|
-
})
|
|
398
|
-
it("should see correct types on objects returned from a listing of the new directory", async () => {
|
|
399
|
-
const objects = await fixture.api.list(randomParent)
|
|
400
|
-
expect(objects).to.have.lengthOf(1)
|
|
401
|
-
expect(objects[0]).to.have.property('type', M_DIR, `subdir should have type ${M_DIR}`)
|
|
402
|
-
})
|
|
403
|
-
it("should see correct types on objects returned from a listing of the subdirectory", async () => {
|
|
404
|
-
const objects = await fixture.api.list(`${randomParent}/${subdirName}/`)
|
|
405
|
-
expect(objects).to.have.lengthOf(fileCount)
|
|
406
|
-
for (let i = 0; i < fileCount; i++) {
|
|
407
|
-
// we should find all the files, and they should all have the correct type
|
|
408
|
-
expect(objects
|
|
409
|
-
.find(o => (o.type === M_FILE && o.name === tempFilename(fixture.name, i)))
|
|
410
|
-
).to.not.be.null
|
|
411
|
-
}
|
|
412
|
-
})
|
|
413
|
-
it(`should mirror its subdirectory (${driverName}/${randomParent}) to another place (${mirrorDriver}/${mirrorDest})`, async () => {
|
|
414
|
-
const results = await fixture.mirrorApi.mirror(fixture.api, mirrorDest, randomParent)
|
|
415
|
-
expect(results).to.have.property('errors', 0, 'expected no errors in mirroring')
|
|
416
|
-
expect(results).to.have.property('success').greaterThan(0, 'expected some successes in mirroring')
|
|
417
|
-
})
|
|
418
|
-
it("should see the same files mirrored both places", async () => {
|
|
419
|
-
const originalThings = await fixture.api.list(fullSubdirPath, { recursive: true })
|
|
420
|
-
const mirrorPath = `${mirrorDest}${subdirName}`
|
|
421
|
-
const mirrorThings = await fixture.mirrorApi.list(mirrorPath, { recursive: true })
|
|
422
|
-
expect(originalThings.length === mirrorThings.length).to.be.true
|
|
423
|
-
|
|
424
|
-
const originalFiles = originalThings.filter(o => o.type === M_FILE)
|
|
425
|
-
const mirrorFiles = mirrorThings.filter(o => o.type === M_FILE)
|
|
426
|
-
const intersection = originalFiles
|
|
427
|
-
.filter(orig => mirrorFiles
|
|
428
|
-
.find(m => basename(m.name) === basename(orig.name)))
|
|
429
|
-
expect(intersection.length === originalFiles.length).to.be.true
|
|
430
|
-
})
|
|
431
|
-
it("loading metadata on a mirrored file succeeds", async () => {
|
|
432
|
-
const destPath = mirrorDest + subdirName + '/' + basename(fixture.name)
|
|
433
|
-
await assertMeta(fixture.mirrorApi, destPath, READ_SZ)
|
|
434
|
-
})
|
|
435
|
-
it("should recursively delete the mirrored data", async () => {
|
|
436
|
-
const recursive = true
|
|
437
|
-
const removed = await fixture.mirrorApi.remove(mirrorDest, {recursive})
|
|
438
|
-
expect(!!removed).to.be.true
|
|
439
|
-
})
|
|
440
|
-
it("should recursively delete the directory and file we just created", async () => {
|
|
441
|
-
const recursive = true
|
|
442
|
-
const removed = await fixture.api.remove(randomParent, {recursive})
|
|
443
|
-
expect(!!removed).to.be.true
|
|
444
|
-
})
|
|
445
|
-
it("loading metadata on the file we wrote now fails", async () => {
|
|
446
|
-
await assertMetaFail(fixture.api, fixture.name)
|
|
447
|
-
})
|
|
448
|
-
it("loading metadata on a mirrored file now fails", async () => {
|
|
449
|
-
const destPath = mirrorDest + subdirName + '/' + basename(fixture.name)
|
|
450
|
-
await assertMetaFail(fixture.mirrorApi, destPath)
|
|
451
|
-
})
|
|
452
|
-
it("loading metadata on the parent dir we wrote now fails", async () => {
|
|
453
|
-
await assertMetaFail(fixture.api, randomParent)
|
|
454
|
-
})
|
|
455
|
-
})
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
describe(`${driverTest} - expect MobilettoNotFoundError when reading nonexistent file `, () => {
|
|
459
|
-
it("should throw MobilettoNotFoundError when trying to read a file that does not exist", async () => {
|
|
460
|
-
const api = await mobiletto(driverName, config.key, config.secret, config.opts)
|
|
461
|
-
try {
|
|
462
|
-
await api.read(nonexistentFile, () => {
|
|
463
|
-
assert.fail(`ERROR: reading non-existent file should NOT succeed`)
|
|
464
|
-
})
|
|
465
|
-
} catch (err) {
|
|
466
|
-
expect(err).to.be.instanceof(MobilettoNotFoundError, `expected MobilettoNotFoundError, got: ${err}`)
|
|
467
|
-
}
|
|
468
|
-
})
|
|
469
|
-
})
|
|
470
|
-
|
|
471
|
-
describe(`${driverTest} - expect MobilettoNotFoundError when reading metadata for nonexistent file`, () => {
|
|
472
|
-
it("should throw MobilettoNotFoundError when trying to read metadata for a file that does not exist", async () => {
|
|
473
|
-
const api = await mobiletto(driverName, config.key, config.secret, config.opts)
|
|
474
|
-
try {
|
|
475
|
-
const meta = await api.metadata(nonexistentFile)
|
|
476
|
-
assert.fail(`ERROR: reading metadata for non-existent file should NOT succeed, but it returned ${meta ? JSON.stringify(meta) : 'null or undefined'}`)
|
|
477
|
-
} catch (err) {
|
|
478
|
-
expect(err).to.be.instanceof(MobilettoNotFoundError, `expected MobilettoNotFoundError, got: ${err}`)
|
|
479
|
-
}
|
|
480
|
-
})
|
|
481
|
-
})
|
|
482
|
-
|
|
483
|
-
describe(`${driverTest} - expect MobilettoNotFoundError when removing non-existent file`, () => {
|
|
484
|
-
it("should throw MobilettoNotFoundError when trying to remove a file that does not exist", async () => {
|
|
485
|
-
const api = await mobiletto(driverName, config.key, config.secret, config.opts)
|
|
486
|
-
try {
|
|
487
|
-
if (await api.remove(nonexistentFile)) {
|
|
488
|
-
assert.fail('ERROR: removing non-existent file should NOT succeed')
|
|
489
|
-
}
|
|
490
|
-
} catch (err) {
|
|
491
|
-
expect(err).to.be.instanceof(MobilettoNotFoundError, `expected MobilettoNotFoundError, got: ${err}`)
|
|
492
|
-
}
|
|
493
|
-
})
|
|
494
|
-
})
|
|
495
|
-
|
|
496
|
-
describe(`${driverTest} - quietly removing non-existent file does not throw MobilettoNotFoundError`, () => {
|
|
497
|
-
it("should NOT throw MobilettoNotFoundError when quietly removing a file that does not exist", async () => {
|
|
498
|
-
const api = await mobiletto(driverName, config.key, config.secret, config.opts)
|
|
499
|
-
if (!(await api.remove(nonexistentFile, {quiet: true}))) {
|
|
500
|
-
assert.fail('ERROR: quietly removing non-existent file should still return true')
|
|
501
|
-
}
|
|
502
|
-
})
|
|
503
|
-
})
|
|
504
|
-
|
|
505
|
-
})
|
|
506
|
-
}
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
after ( (done) => {
|
|
510
|
-
logger.info('all tests finished, tearing down redis...')
|
|
511
|
-
closeRedis().finally(done)
|
|
512
|
-
})
|
package/test/crypt.spec.js
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
const fs = require('fs')
|
|
2
|
-
const { expect } = require('chai')
|
|
3
|
-
const randomstring = require('randomstring')
|
|
4
|
-
const crypto = require('crypto')
|
|
5
|
-
|
|
6
|
-
const {
|
|
7
|
-
DEFAULT_CRYPT_ALGO,
|
|
8
|
-
normalizeKey, normalizeIV,
|
|
9
|
-
encrypt, decrypt,
|
|
10
|
-
getCipher, getDecipher
|
|
11
|
-
} = require("mobiletto-base/util/crypt")
|
|
12
|
-
|
|
13
|
-
const { readStream } = require('mobiletto-common')
|
|
14
|
-
|
|
15
|
-
describe('crypto test', () => {
|
|
16
|
-
const enc = {
|
|
17
|
-
key: normalizeKey(randomstring.generate(32)),
|
|
18
|
-
iv: normalizeIV(randomstring.generate(16)),
|
|
19
|
-
algo: DEFAULT_CRYPT_ALGO
|
|
20
|
-
}
|
|
21
|
-
it("should encrypt and decrypt a data buffer successfully", async () => {
|
|
22
|
-
const plaintext = randomstring.generate((64 * 1024) + Math.floor(1024*Math.random()))
|
|
23
|
-
const ciphertext = encrypt(plaintext, enc)
|
|
24
|
-
const decrypted = decrypt(ciphertext, enc)
|
|
25
|
-
expect(decrypted).to.equal(plaintext, 'decrypted data did not match plaintext')
|
|
26
|
-
})
|
|
27
|
-
it("should encrypt and decrypt a stream successfully", async () => {
|
|
28
|
-
const plaintext = crypto.randomBytes(81920)
|
|
29
|
-
const cipher = getCipher(enc)
|
|
30
|
-
const updateData = cipher.update(plaintext)
|
|
31
|
-
const finalData = cipher.final()
|
|
32
|
-
const encrypted = Buffer.concat([updateData, finalData])
|
|
33
|
-
|
|
34
|
-
const decipher = getDecipher(enc)
|
|
35
|
-
const decryptUpdate = decipher.update(encrypted)
|
|
36
|
-
const decipherFinal = decipher.final()
|
|
37
|
-
const decrypted = Buffer.concat([decryptUpdate, decipherFinal])
|
|
38
|
-
expect(decrypted.length).to.equal(plaintext.length)
|
|
39
|
-
for (let i = 0; i < decrypted.length; i++ ) {
|
|
40
|
-
expect(decrypted[i]).to.equal(plaintext[i], `decrypted data did not match plaintext at position ${i}`)
|
|
41
|
-
}
|
|
42
|
-
})
|
|
43
|
-
it("should encrypt and decrypt a stream successfully using readStream", async () => {
|
|
44
|
-
const plaintext = crypto.randomBytes(81920)
|
|
45
|
-
const cipher = getCipher(enc)
|
|
46
|
-
const updateData = cipher.update(plaintext)
|
|
47
|
-
const finalData = cipher.final()
|
|
48
|
-
const encrypted = Buffer.concat([updateData, finalData])
|
|
49
|
-
|
|
50
|
-
const tempFile = `/tmp/stream_foo_${Date.now}.tmp`
|
|
51
|
-
fs.writeFileSync(tempFile, encrypted)
|
|
52
|
-
|
|
53
|
-
const rs = fs.createReadStream(tempFile)
|
|
54
|
-
const decipher = getDecipher(enc)
|
|
55
|
-
const buffers = []
|
|
56
|
-
await readStream(rs, (chunk) => {
|
|
57
|
-
buffers.push(decipher.update(chunk))
|
|
58
|
-
}, () => {
|
|
59
|
-
buffers.push(decipher.final())
|
|
60
|
-
})
|
|
61
|
-
const decrypted = Buffer.concat(buffers)
|
|
62
|
-
expect(decrypted.length).to.equal(plaintext.length)
|
|
63
|
-
for (let i = 0; i < decrypted.length; i++ ) {
|
|
64
|
-
expect(decrypted[i]).to.equal(plaintext[i], `decrypted data did not match plaintext at position ${i}`)
|
|
65
|
-
}
|
|
66
|
-
})
|
|
67
|
-
})
|