blob: 5367c9edb4f3bed75e035785518ba79c9000db63 [file] [log] [blame]
TamasBakai9b780332019-02-15 08:38:16 +00001var http = require('http');
2var https = require('https');
3var ArgumentParser = require('argparse').ArgumentParser;
4var express = require('express');
5const stream = require('stream');
6var app = express();
maximessonb8b94ae2019-03-25 16:03:08 +00007var fs = require('fs');
BjornMagnussonXA42dcb262019-04-26 19:29:54 +00008const sleep = (milliseconds) => {
9 return new Promise(resolve => setTimeout(resolve, milliseconds))
10}
TamasBakai9b780332019-02-15 08:38:16 +000011var privateKey = fs.readFileSync('cert/private.key', 'utf8');
12var certificate = fs.readFileSync('cert/certificate.crt', 'utf8');
13var credentials = {key: privateKey, cert: certificate};
BjornMagnussonXAf4e18362019-04-10 13:04:08 +000014
15//For execution time calculation
16var startTime = Date.now();
17
18//Test case constants
19const tc_normal = "normal";
20const tc_none_published = "none_published";
21const tc_all_published = "all_published"
22const tc_10p_no_response = "10p_no_response";
23const tc_10first_no_response = "10first_no_response";
24const tc_100first_no_response = "100first_no_response";
BjornMagnussonXA42dcb262019-04-26 19:29:54 +000025const tc_all_delay_1s = "all_delay_1s";
BjornMagnussonXAf4e18362019-04-10 13:04:08 +000026const tc_all_delay_10s = "all_delay_10s";
27const tc_10p_delay_10s = "10p_delay_10s";
28const tc_10p_error_response = "10p_error_response";
29const tc_10first_error_response = "10first_error_response";
30const tc_100first_error_response = "100first_error_response";
31
BjornMagnussonXA42dcb262019-04-26 19:29:54 +000032var drr_sim_ip = '127.0.0.1'; //IP for redirect to DR redir sim. Can be changed by env DRR_SIM_IP
33
BjornMagnussonXAf4e18362019-04-10 13:04:08 +000034//Counters
35var ctr_publish_query = 0;
36var ctr_publish_query_published = 0;
37var ctr_publish_query_not_published = 0;
38var ctr_publish_req = 0;
39var ctr_publish_req_redirect = 0;
40var ctr_publish_req_published = 0;
BjornMagnussonXA42dcb262019-04-26 19:29:54 +000041var ctr_double_publish = 0
TamasBakai9b780332019-02-15 08:38:16 +000042
43var parser = new ArgumentParser({
44 version: '0.0.1',
45 addHelp:true,
46 description: 'Datarouter simulator'
47 });
48
49parser.addArgument('--tc' , { help: 'TC $NoOfTc' } );
50parser.addArgument('--printtc' ,
51 {
52 help: 'Print complete usage help',
53 action: 'storeTrue'
54 }
55 );
56
57var args = parser.parseArgs();
58
BjornMagnussonXAf4e18362019-04-10 13:04:08 +000059if (args.tc==tc_normal) {
60 console.log("TC: " + args.tc)
61
62} else if (args.tc==tc_none_published) {
63 console.log("TC: " + args.tc)
64
65} else if (args.tc==tc_all_published) {
66 console.log("TC: " + args.tc)
67
68} else if (args.tc==tc_10p_no_response) {
69 console.log("TC: " + args.tc)
70
71} else if (args.tc==tc_10first_no_response) {
72 console.log("TC: " + args.tc)
73
74} else if (args.tc==tc_100first_no_response) {
75 console.log("TC: " + args.tc)
76
BjornMagnussonXA42dcb262019-04-26 19:29:54 +000077} else if (args.tc==tc_all_delay_1s) {
78 console.log("TC: " + args.tc)
79
BjornMagnussonXAf4e18362019-04-10 13:04:08 +000080} else if (args.tc==tc_all_delay_10s) {
81 console.log("TC: " + args.tc)
82
83} else if (args.tc==tc_10p_delay_10s) {
84 console.log("TC: " + args.tc)
85
86} else if (args.tc==tc_10p_error_response) {
87 console.log("TC: " + args.tc)
88
89} else if (args.tc==tc_10first_error_response) {
90 console.log("TC: " + args.tc)
91
92} else if (args.tc==tc_100first_error_response) {
93 console.log("TC: " + args.tc)
94} else {
95 console.log("No TC specified, use: --tc <tc-id>");
96 process.exit(0);
TamasBakai9b780332019-02-15 08:38:16 +000097}
98
99if (args.printtc) {
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000100 console.log("TC " + tc_normal + ": Normal case, query respone based on published files. Publish responde with ok/redirect depending on if file is published or not.");
101 console.log("TC " + tc_none_published + ": Query responde 'ok'. Publish respond with redirect.");
102 console.log("TC " + tc_all_published + ": Query respond with filename. Publish respond with 'ok'.");
103 console.log("TC " + tc_10p_no_response + ": 10% % no response for query and publish. Otherwise normal case.");
104 console.log("TC " + tc_10first_no_response + ": 10 first queries and requests gives no response for query and publish. Otherwise normal case.");
105 console.log("TC " + tc_100first_no_response + ": 100 first queries and requests gives no response for query and publish. Otherwise normal case.");
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000106 console.log("TC " + tc_all_delay_1s + ": All responses delayed 1s (both query and publish).");
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000107 console.log("TC " + tc_all_delay_10s + ": All responses delayed 10s (both query and publish).");
108 console.log("TC " + tc_10p_delay_10s + ": 10% of responses delayed 10s, (both query and publish).");
109 console.log("TC " + tc_10p_error_response + ": 10% error response for query and publish. Otherwise normal case.");
110 console.log("TC " + tc_10first_error_response + ": 10 first queries and requests gives no response for query and publish. Otherwise normal case.");
111 console.log("TC " + tc_100first_error_response + ": 100 first queries and requests gives no response for query and publish. Otherwise normal case.");
112
TamasBakai9b780332019-02-15 08:38:16 +0000113 process.exit(0);
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000114 }
115
TamasBakai9b780332019-02-15 08:38:16 +0000116
117var bodyParser = require('body-parser')
118app.use(bodyParser.urlencoded({ extended: false }))
119
120// parse application/json
121app.use(bodyParser.json())
122
123// parse application/vnd.api+json as json
124app.use(bodyParser.json({ type: 'application/vnd.api+json' }))
125
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000126// parse some custom thing into a Buffer (to cater for 60MB files)
127app.use(bodyParser.raw({limit:1024*1024*60, type: 'application/octet-stream' }))
TamasBakai9b780332019-02-15 08:38:16 +0000128// parse an HTML body into a string
129app.use(bodyParser.text({ type: 'text/html' }))
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000130
131
132
133//Is alive function
TamasBakai9b780332019-02-15 08:38:16 +0000134app.get("/",function(req, res){
135 res.send("ok");
136})
maximessonb8b94ae2019-03-25 16:03:08 +0000137
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000138//Counter readout
139app.get("/ctr_publish_query",function(req, res){
140 res.send(""+ctr_publish_query);
141})
142app.get("/ctr_publish_query_published",function(req, res){
143 res.send(""+ctr_publish_query_published);
144})
145app.get("/ctr_publish_query_not_published",function(req, res){
146 res.send(""+ctr_publish_query_not_published);
147})
148app.get("/ctr_publish_req",function(req, res){
149 res.send(""+ctr_publish_req);
150})
151app.get("/ctr_publish_req_redirect",function(req, res){
152 res.send(""+ctr_publish_req_redirect);
153})
154app.get("/ctr_publish_req_published",function(req, res){
155 res.send(""+ctr_publish_req_published);
156})
157app.get("/ctr_published_files",function(req, res){
158 res.send(""+published.length);
159})
160app.get("/tc_info",function(req, res){
161 res.send(args.tc);
162})
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000163app.get("/ctr_double_publish",function(req, res){
164 res.send(""+ctr_double_publish);
165})
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000166function fmtMSS(s){
167 return(s-(s%=60))/60+(9<s?':':':0')+s //Format time diff in mm:ss
168}
169app.get("/execution_time",function(req, res){
170 diff = fmtMSS(Math.floor((Date.now()-startTime)/1000));
171 res.send(""+diff);
TamasBakai9b780332019-02-15 08:38:16 +0000172})
maximessonb8b94ae2019-03-25 16:03:08 +0000173
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000174//db of published files
175var published = [];
maximessonb8b94ae2019-03-25 16:03:08 +0000176
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000177app.get('/feedlog/1/',function(req, res){
178 console.log("url:"+req.url);
179 ctr_publish_query++;
maximessonb8b94ae2019-03-25 16:03:08 +0000180 var filename = req.query.filename;
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000181 console.log(filename);
182 var qtype = req.query.type;
maximessonb8b94ae2019-03-25 16:03:08 +0000183 if(typeof(filename) == 'undefined'){
184 res.status(400).send({error: 'No filename provided.'});
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000185 return;
186 } else if(typeof(qtype) == 'undefined'){
maximessonb8b94ae2019-03-25 16:03:08 +0000187 res.status(400).send({error: 'No type provided.'});
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000188 return;
189 }
190
191 //Ugly fix, plus signs replaces with spaces in query params....need to put them back
192 filename = filename.replace(/ /g,"+");
193
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000194 var sleeptime=0;
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000195 if (args.tc==tc_normal) {
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000196 sleeptime=0;
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000197 } else if (args.tc==tc_10p_no_response && (ctr_publish_query%10) == 0) {
198 return;
199 } else if (args.tc==tc_10first_no_response && ctr_publish_query<11) {
200 return;
201 } else if (args.tc==tc_100first_no_response && ctr_publish_query<101) {
202 return;
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000203 } else if (args.tc==tc_all_delay_1s) {
204 sleeptime=1000;
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000205 } else if (args.tc==tc_all_delay_10s) {
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000206 sleeptime=10000;
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000207 } else if (args.tc==tc_10p_delay_10s && (ctr_publish_query%10) == 0) {
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000208 sleeptime=10000;
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000209 } else if (args.tc==tc_10p_error_response && (ctr_publish_query%10) == 0) {
210 res.send(400);
211 return;
212 } else if (args.tc==tc_10first_error_response && ctr_publish_query<11) {
213 res.send(400);
214 return;
215 } else if (args.tc==tc_100first_error_response & ctr_publish_query<101) {
216 res.send(400);
217 return;
218 }
219
220 if (published.includes(filename)) {
221 ctr_publish_query_published++;
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000222 strToSend="[" + filename + "]";
maximessonb8b94ae2019-03-25 16:03:08 +0000223 } else {
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000224 ctr_publish_query_not_published++;
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000225 strToSend="[]";
maximessonb8b94ae2019-03-25 16:03:08 +0000226 }
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000227 if (sleeptime > 0) {
228 sleep(sleeptime).then(() => {
229 res.send(strToSend);
230 });
231 } else {
232 res.send(strToSend);
233 }
234});
235
maximessonb8b94ae2019-03-25 16:03:08 +0000236
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000237app.put('/publish/1/:filename', function (req, res) {
238 console.log("url:"+req.url);
239 console.log("body (first 25 bytes):"+req.body.slice(0,25));
240 console.log("headers:"+req.headers);
241 ctr_publish_req++;
242
243 var filename = req.params.filename;
244 console.log(filename);
245
246 if (args.tc==tc_normal) {
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000247 // Continue
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000248 } else if (args.tc==tc_none_published) {
249 ctr_publish_req_redirect++;
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000250 res.redirect(301, 'http://' + drr_sim_ip + ':3908/publish/1/'+filename);
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000251 return;
252 } else if (args.tc==tc_all_published) {
253 ctr_publish_req_published++;
254 res.send("ok");
255 return;
256 }else if (args.tc==tc_10p_no_response && (ctr_publish_req%10) == 0) {
257 return;
258 } else if (args.tc==tc_10first_no_response && ctr_publish_req<11) {
259 return;
260 } else if (args.tc==tc_100first_no_response && ctr_publish_req<101) {
261 return;
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000262 } else if (args.tc==tc_all_delay_1s) {
263 do_publish_delay(res, filename, 1000);
264 return;
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000265 } else if (args.tc==tc_all_delay_10s) {
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000266 do_publish_delay(res, filename, 10000);
267 return;
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000268 } else if (args.tc==tc_10p_delay_10s && (ctr_publish_req%10) == 0) {
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000269 do_publish_delay(res, filename, 10000);
270 return;
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000271 } else if (args.tc==tc_10p_error_response && (ctr_publish_req%10) == 0) {
272 res.send(400);
273 return;
274 } else if (args.tc==tc_10first_error_response && ctr_publish_req<11) {
275 res.send(400);
276 return;
277 } else if (args.tc==tc_100first_error_response & ctr_publish_req<101) {
278 res.send(400);
279 return;
280 }
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000281 if (!published.includes(filename)) {
282 ctr_publish_req_redirect++;
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000283 res.redirect(301, 'http://'+drr_sim_ip+':3908/publish/1/'+filename);
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000284 } else {
285 ctr_publish_req_published++;
286 res.send("ok");
287 }
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000288 return;
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000289})
290
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000291function do_publish_delay(res, filename, sleeptime) {
292 if (!published.includes(filename)) {
293 ctr_publish_req_redirect++;
294 sleep(1000).then(() => {
295 res.redirect(301, 'http://'+drr_sim_ip+':3908/publish/1/'+filename);
296 });
297 } else {
298 ctr_publish_req_published++;
299 sleep(1000).then(() => {
300 res.send("ok");
301 });
302 }
303}
304
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000305//Callback from DR REDIR server, when file is published ok this PUT request update the list of published files.
306app.put('/dr_redir_publish/:filename', function (req, res) {
307 console.log("url:"+req.url);
308 var filename = req.params.filename;
309 console.log(filename);
310
311 if (!published.includes(filename)) {
312 console.log("File marked as published by callback from DR redir SIM. url: " + req.url);
313 published.push(filename);
314 } else {
315 console.log("File already marked as published. Callback from DR redir SIM. url: " + req.url);
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000316 ctr_double_publish = ctr_double_publish+1;
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000317 }
318
319 res.send("ok");
320})
maximessonb8b94ae2019-03-25 16:03:08 +0000321
TamasBakai9b780332019-02-15 08:38:16 +0000322var httpServer = http.createServer(app);
323var httpsServer = https.createServer(credentials, app);
324
maximessonb8b94ae2019-03-25 16:03:08 +0000325var httpPort=3906;
326var httpsPort=3907;
TamasBakai9b780332019-02-15 08:38:16 +0000327httpServer.listen(httpPort);
maximessonb8b94ae2019-03-25 16:03:08 +0000328console.log("DR-simulator listening (http) at "+httpPort);
TamasBakai9b780332019-02-15 08:38:16 +0000329httpsServer.listen(httpsPort);
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000330console.log("DR-simulator listening (https) at "+httpsPort);
331
332if (process.env.DRR_SIM_IP) {
333 drr_sim_ip=process.env.DRR_SIM_IP;
334}
335console.log("Using IP " + drr_sim_ip + " for redirect to DR redir sim");