-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.mjs
367 lines (344 loc) · 16.9 KB
/
test.mjs
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
"use strict";
import cChef from 'cyberchef/src/node/index'
import should from 'should'
import path from 'path'
import crypto from 'crypto'
import Mocha from 'mocha'
import Service from './service'
import supertest from 'supertest'
import fs from 'fs'
import {promisify} from 'util'
import commander from 'commander'
const m = new Mocha()
m.timeout(0)
m.slow(5000)
//Taken from: https://gist.github.com/liangzan/807712#gistcomment-2632465
let rmdir = async (dirPath, options = {}) => {
const
{ removeContentOnly = false, drillDownSymlinks = false } = options,
readdirAsync = promisify(fs.readdir),
unlinkAsync = promisify(fs.unlink),
rmdirAsync = promisify(fs.rmdir),
lstatAsync = promisify(fs.lstat) // fs.lstat can detect symlinks, fs.stat can't
let
files
try {
files = await readdirAsync(dirPath)
} catch (e) {
throw new Error(e)
}
if (files.length) {
for (let fileName of files) {
let
filePath = path.join(dirPath, fileName),
fileStat = await lstatAsync(filePath),
isSymlink = fileStat.isSymbolicLink(),
isDir = fileStat.isDirectory()
if (isDir || (isSymlink && drillDownSymlinks)) {
await rmdir(filePath)
} else {
await unlinkAsync(filePath)
}
}
}
if (!removeContentOnly)
await rmdirAsync(dirPath)
}
function testOptions() {
return {
Port: 4321,
Listen: "127.0.0.1",
RecipeFolder: ".\\testrecipies",
RecipeGit: null,
RecipeGitSparse: null,
DisableLogging: true,
GitInterval: -1
}
}
////////////////// Cyber Chef Tests
let cyberChefTestSuite = new Mocha.Suite("CyberChef Simple Checks", new Mocha.Context())
cyberChefTestSuite.addTest(new Mocha.Test("simple recipe check", () => {
//Test #1
let input = "test"
let recipe = [{ "op": "To Hex", "args": ["Space"] }]
let dish = cChef.bake(input, recipe)
should.exist(dish)
should(dish.value).be.exactly("74 65 73 74")
}))
cyberChefTestSuite.addTest(new Mocha.Test("simple expected failure", () => {
//Test #2
let input = "test"
let failRecipe = [{ "op": "From Hex", "args": ["Space"] }]
let dish = null
try {
dish = cChef.bake(input, failRecipe)
} catch (e) {
should.exist(e)
should(e.message).be.exactly("Data is not a valid byteArray: [null,null]")
}
}))
m.suite.addSuite(cyberChefTestSuite)
////////////////// Internal Tests
let internalTestSuite = new Mocha.Suite("Internal Test Suite", new Mocha.Context())
internalTestSuite.addTest(new Mocha.Test("Service cTor", () => {
let s = new Service()
should.exist(s)
should.exist(s.server)
should.exist(s.list)
}))
internalTestSuite.addTest(new Mocha.Test("Service cTor with Options", () => {
(async (s) => {
should.exist(s)
should.exist(s.server)
should.exist(s.list)
})(new Service(testOptions()))
}))
internalTestSuite.addTest(new Mocha.Test("Service Init", () => {
(async (s) => {
await s.InitGit()
await s.Init()
should.exist(s.list.TestRecipe)
should.exist(s.list.TestRecipe002)
should.exist(s.list['Test Recipe 003 - domain - complex'])
should.exist(s.list['Test Recipe 004 - URL - complex'])
should.exist(s.list['Email Puller'])
should.exist(s.list['IPv4 Puller'])
should.exist(s.list['URL Puller'])
})(new Service(testOptions()))
}))
internalTestSuite.addTest(new Mocha.Test("Service filterRecipes, no filters", () => {
(async (s) => {
await s.InitGit()
await s.Init()
let ans = s.filterRecipes(s.list, null, null)
should(ans.length).be.exactly(7)
})(new Service(testOptions()))
}))
internalTestSuite.addTest(new Mocha.Test("Service filterRecipes, match filter", () => {
(async (s) => {
await s.InitGit()
await s.Init()
let ans = s.filterRecipes(s.list, "URL", null)
should(ans.length).be.exactly(2)
})(new Service(testOptions()))
}))
internalTestSuite.addTest(new Mocha.Test("Service filterRecipes, file filter", () => {
(async (s) => {
await s.InitGit()
await s.Init()
let ans = s.filterRecipes(s.list, null, "Find")
should(ans.length).be.exactly(3)
})(new Service(testOptions()))
}))
internalTestSuite.addTest(new Mocha.Test("Service filterRecipes, match and file filter", () => {
(async (s) => {
await s.InitGit()
await s.Init()
let ans = s.filterRecipes(s.list, "domain", "complex")
should(ans.length).be.exactly(1)
})(new Service(testOptions()))
}))
m.suite.addSuite(internalTestSuite)
////////////////// API Tests
let apiTestSuite = new Mocha.Suite("API Test Suite", new Mocha.Context())
apiTestSuite.addTest(new Mocha.Test("pull recipes", (done) => {
(async (s) => {
await s.InitGit()
await s.Init()
supertest(s.server.listener).get('/recipes').expect(200, function (err, resp) {
should.not.exist(err)
should.exist(resp.text)
should(resp.body.length).be.exactly(7)
done()
})
})(new Service(testOptions()))
}))
apiTestSuite.addTest(new Mocha.Test("pull a specific recipe", (done) => {
(async (s) => {
await s.InitGit()
await s.Init()
supertest(s.server.listener).get('/recipes/TestRecipe').expect(200, function (err, resp) {
should.not.exist(err)
should.exist(resp.body)
resp.body.should.have.value("filename", "001.json")
resp.body.should.have.value("author", "me")
resp.body.should.have.value("createdOn", "2019-02-14T00:00:00.000Z")
resp.body.should.have.value("name", "TestRecipe")
resp.body.should.have.value("description", "a very simple to hex recipe")
resp.body.should.have.value("recipe", [{ "op": "To Hex", "args": ["Space"] }])
done()
})
})(new Service(testOptions()))
}))
apiTestSuite.addTest(new Mocha.Test("run all recipes", (done) => {
(async (s) => {
await s.InitGit()
await s.Init()
let input = "test"
supertest(s.server.listener).post('/').set("Content-Type", "text/plain").send(input).expect(200, function (err, resp) {
should.not.exist(err)
should.exist(resp.body)
should(resp.body.length).be.exactly(7)
done()
})
})(new Service(testOptions()))
}))
apiTestSuite.addTest(new Mocha.Test("run all recipes, match by name", (done) => {
(async (s) => {
await s.InitGit()
await s.Init()
let input = "test"
supertest(s.server.listener).post('/?match=TestRecipe').set("Content-Type", "text/plain").send(input).expect(200, function (err, resp) {
should.not.exist(err)
should.exist(resp.body)
should(resp.body.length).be.exactly(2)
should(resp.body[0].result).be.exactly("74 65 73 74")
done()
})
})(new Service(testOptions()))
}))
apiTestSuite.addTest(new Mocha.Test("run all recipes, match by file", (done) => {
(async (s) => {
await s.InitGit()
await s.Init()
let input = "test"
supertest(s.server.listener).post('/?file=001').set("Content-Type", "text/plain").send(input).expect(200, function (err, resp) {
should.not.exist(err)
should.exist(resp.body)
should(resp.body.length).be.exactly(1)
should(resp.body[0].result).be.exactly("74 65 73 74")
done()
})
})(new Service(testOptions()))
}))
apiTestSuite.addTest(new Mocha.Test("run all recipes, match by name and file", (done) => {
(async (s) => {
await s.InitGit()
await s.Init()
let input = "foo bar [email protected] baz blah blah blah 02340954"
supertest(s.server.listener).post('/?file=Find&match=Email').set("Content-Type", "text/plain").send(input).expect(200, function (err, resp) {
should.not.exist(err)
should.exist(resp.body)
should(resp.body.length).be.exactly(1)
should(resp.body[0]).have.property("result", "[email protected]")
should(resp.body[0]).have.property("test", "userdefined")
should(resp.body[0]).have.property("fieldname", "Email")
done()
})
})(new Service(testOptions()))
}))
apiTestSuite.addTest(new Mocha.Test("run a specific recipe, multiple results", (done) => {
(async (s) => {
await s.InitGit()
await s.Init()
let input = "Incididunt 400.500.600.800 Lorem ipsum anim 1.2.3.4 dolor reprehenderit 123.123.123.123 nisi minim eu aliquip laboris."
supertest(s.server.listener).post('/IPv4%20Puller').set("Content-Type", "text/plain").send(input).expect(200, function (err, resp) {
should.not.exist(err)
should.exist(resp.body)
should(resp.body).have.property("result", "1.2.3.4\n123.123.123.123")
should(resp.body).have.property("test", "userdefined")
should(resp.body).have.property("fieldname", "IPv4")
done()
})
})(new Service(testOptions()))
}))
apiTestSuite.addTest(new Mocha.Test("run a specific recipe", (done) => {
(async (s) => {
await s.InitGit()
await s.Init()
let input = "H4sIADzGZlwA/2WY7U8a+bvG/5r9F9ZEUcyvjcguzb4hPGzdtg7VBnYIb4yPR9ONGvfgSUlcyMBQgalFizHwBlOU1bRSGTCkRPvGjU9Rj23Z4O4ZcBNpZ+B33YN70j3nHSEwfL/3fV2f+7r57Lbvzfddppz+GJN7bjhZMV0Izu1Fe4XvOeBMyktdebZPemLwJzRzi0b5qX3H3eKf0oeiP157RiW+teC1KRHWP8OcTfe8SbQrC64/14d3/lP3eL7r9/f35eu+s+nOg/ij0qK5Sp8x4pm5ZfN2sOPM3RLgTGfLrDw79uG5oZEYKkUdHz3tO76eNyvW8DJbnbIX3V8X4hpxzVleZc7c3bvxdiVpvK7ZGz5dgdOLL53lq4Edvm17Sn+RZrfDE/kFi8jdKW3gOcNSxCgmbLUNNhu3V3y397zW0IJLvn5w5maOp7qKwr+2PN1vp3v2OCu/4TjHfX23CvP6P5Zd5zOMtNSxtdJVU5x/Phv+0DAeJTSnUefWNfPW3X0S178T/pWdHM6HWg69ptqze/5ZreS7HZq3nkZ/3Jr6aoc3F7wPTgWzvGar+DqDU/rLBWc2yOR5y3Hsm8O0uVEbO/u5Jcs9KqVYf9ye55ndWNep4PQ/tuembx/FbKJa53zdgu9m0s7tmYlKZFCc0s+t4/wT+Ubb7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xe4jr04U4c0E9mhkjP48fOQ1vVpGVvx7F4BPKSc7RHDmZ+Q92mGhkznUQcYe1027hvK3XygTIg90VPjON6QZ1HMC3CPNU3aF/pG1MFOQCZHJO29m6yT5gvYX8LNm/8S3ode1tKM8i2w8iKxO+1GUdv9gTIO8HcD8oj0Rc8qMs6GPlN+e4fy9yBv/zVkvkZE8/TvYvzjbXBJ6YCo+zE34l3ZAyWcvcJq5NDQDPVt+w70eq/8bIN+iL3JzdmCvwYzGfcm/5B3Kjdi5tIdgL7QUbvtCq98XsRcnbEXaX7T5BWTgZta1Yaar+yaYj/mrZr811B/1GRUTYE4Pdt48PEI7kcM/A2Y6iYdqTnjLQw9WYgv+l3CrMxH7i0DMAdtD2M3VMx9T9iZGoVbYo4nVnlHVy13wWoD0g/9JbNB5lpjZfQwNYBd7apciln145GYOghsmyhLI7b7b1KMF3He46OvxI3ctuMD/xt970DY0THuNLdTcu93Mb5wJngIbJXc3z2kyihHeyUfof5j//5x6pPsw1kXZppkzEzbs7+V1/G7fTU8xI+rIGFaacbP/Qf/hJNrPKIMh2+O/GrDFhfqozLS+U5DhwRA7fH2T/XB+sAv6wecXHOrniRU5d8tuc3bPglcMmIPd9nwSuzNqolGSmGu2fP12FudUSA+Yj7THbRC3b3r9P/8GNBGp9IwSAAA="
supertest(s.server.listener)
.post('/Test%20Recipe%20003%20-%20domain%20-%20complex')
.set("Content-Type", "text/plain")
.send(input)
.expect(200)
.end((err, resp) => {
should.not.exist(err)
should.exist(resp.body)
should(resp.body).have.property("result", "foobar.example.com")
should(resp.body).have.property("something", "user defined")
should(resp.body).have.property("fieldname", "Found")
done()
})
})(new Service(testOptions()))
}))
m.suite.addSuite(apiTestSuite)
////////////////// Git Tests
let gitTestSuite = new Mocha.Suite("Git Test Suite", new Mocha.Context())
gitTestSuite.addTest(new Mocha.Test("Local Folder", (done) => {
(async (s) => {
await s.InitGit()
should.exist(s.list.TestRecipe)
should.exist(s.list.TestRecipe002)
done()
})(new Service({
Port: 4321,
Listen: "127.0.0.1",
RecipeFolder: ".\\testrecipies",
RecipeGit: null,
RecipeGitSparse: null,
DisableLogging: true,
GitInterval: -1
}))
}))
gitTestSuite.addTest(new Mocha.Test("Remote Git Repo", function(done) {
let config = {
Port: 4321,
Listen: "127.0.0.1",
RecipeFolder: ".\\recipe.git_" + crypto.randomBytes(6).toString('hex'),
RecipeGit: "https://github.com/DBHeise/Test.git",
RecipeGitSparse: null,
DisableLogging: true,
GitInterval: -1
};
this.test.timeout(10000);
(async (s, cfg) => {
await s.InitGit()
should.exist(s.list.TestRecipe001)
await rmdir(path.resolve(cfg.RecipeFolder))
done()
})(new Service(config), config);
}))
gitTestSuite.addTest(new Mocha.Test("Sparse Git Repo", function(done) {
let config = {
Port: 4321,
Listen: "127.0.0.1",
RecipeFolder: ".\\recipe.git_" + crypto.randomBytes(6).toString('hex'),
RecipeGit: "https://github.com/DBHeise/Test.git",
RecipeGitSparse: "CyberSaucier",
DisableLogging: true,
GitInterval: -1
};
this.test.timeout(10000);
(async (s, cfg) => {
await s.InitGit()
should.exist(s.list.TestRecipe001)
//Verify the TestFile.bin is NOT there
let isFileThere = true
let targetFile = path.join(path.resolve(cfg.RecipeFolder), "TestFile.bin")
try {
fs.accessSync(targetFile, fs.constants.R_OK)
} catch (err) {
isFileThere = false
}
await rmdir(path.resolve(cfg.RecipeFolder))
should(isFileThere).be.false()
done()
})(new Service(config), config);
}))
//TODO: git update - new recipe
//TODO: git update - changed recipe
//TODO: git update - removed recipe
m.suite.addSuite(gitTestSuite)
const program = new commander.Command()
program
.version("0.0.2")
.arguments("[test selector]")
.action(function(testRE) {
if (testRE) {
m.grep(testRE).run()
} else {
m.run()
}
})
program.parse(process.argv);