blob: fffe57ce39801da784535ecad20a4a0a0dfe75f7 [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');
TamasBakai9b780332019-02-15 08:38:16 +00008var privateKey = fs.readFileSync('cert/private.key', 'utf8');
9var certificate = fs.readFileSync('cert/certificate.crt', 'utf8');
10var credentials = {key: privateKey, cert: certificate};
BjornMagnussonXAf4e18362019-04-10 13:04:08 +000011
12//For execution time calculation
13var startTime = Date.now();
14
15//Test case constants
16const tc_normal = "normal";
17const tc_none_published = "none_published";
18const tc_all_published = "all_published"
19const tc_10p_no_response = "10p_no_response";
20const tc_10first_no_response = "10first_no_response";
21const tc_100first_no_response = "100first_no_response";
22const tc_all_delay_10s = "all_delay_10s";
23const tc_10p_delay_10s = "10p_delay_10s";
24const tc_10p_error_response = "10p_error_response";
25const tc_10first_error_response = "10first_error_response";
26const tc_100first_error_response = "100first_error_response";
27
28//Counters
29var ctr_publish_query = 0;
30var ctr_publish_query_published = 0;
31var ctr_publish_query_not_published = 0;
32var ctr_publish_req = 0;
33var ctr_publish_req_redirect = 0;
34var ctr_publish_req_published = 0;
TamasBakai9b780332019-02-15 08:38:16 +000035
36var parser = new ArgumentParser({
37 version: '0.0.1',
38 addHelp:true,
39 description: 'Datarouter simulator'
40 });
41
42parser.addArgument('--tc' , { help: 'TC $NoOfTc' } );
43parser.addArgument('--printtc' ,
44 {
45 help: 'Print complete usage help',
46 action: 'storeTrue'
47 }
48 );
49
50var args = parser.parseArgs();
51
BjornMagnussonXAf4e18362019-04-10 13:04:08 +000052if (args.tc==tc_normal) {
53 console.log("TC: " + args.tc)
54
55} else if (args.tc==tc_none_published) {
56 console.log("TC: " + args.tc)
57
58} else if (args.tc==tc_all_published) {
59 console.log("TC: " + args.tc)
60
61} else if (args.tc==tc_10p_no_response) {
62 console.log("TC: " + args.tc)
63
64} else if (args.tc==tc_10first_no_response) {
65 console.log("TC: " + args.tc)
66
67} else if (args.tc==tc_100first_no_response) {
68 console.log("TC: " + args.tc)
69
70} else if (args.tc==tc_all_delay_10s) {
71 console.log("TC: " + args.tc)
72
73} else if (args.tc==tc_10p_delay_10s) {
74 console.log("TC: " + args.tc)
75
76} else if (args.tc==tc_10p_error_response) {
77 console.log("TC: " + args.tc)
78
79} else if (args.tc==tc_10first_error_response) {
80 console.log("TC: " + args.tc)
81
82} else if (args.tc==tc_100first_error_response) {
83 console.log("TC: " + args.tc)
84} else {
85 console.log("No TC specified, use: --tc <tc-id>");
86 process.exit(0);
TamasBakai9b780332019-02-15 08:38:16 +000087}
88
89if (args.printtc) {
BjornMagnussonXAf4e18362019-04-10 13:04:08 +000090 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.");
91 console.log("TC " + tc_none_published + ": Query responde 'ok'. Publish respond with redirect.");
92 console.log("TC " + tc_all_published + ": Query respond with filename. Publish respond with 'ok'.");
93 console.log("TC " + tc_10p_no_response + ": 10% % no response for query and publish. Otherwise normal case.");
94 console.log("TC " + tc_10first_no_response + ": 10 first queries and requests gives no response for query and publish. Otherwise normal case.");
95 console.log("TC " + tc_100first_no_response + ": 100 first queries and requests gives no response for query and publish. Otherwise normal case.");
96 console.log("TC " + tc_all_delay_10s + ": All responses delayed 10s (both query and publish).");
97 console.log("TC " + tc_10p_delay_10s + ": 10% of responses delayed 10s, (both query and publish).");
98 console.log("TC " + tc_10p_error_response + ": 10% error response for query and publish. Otherwise normal case.");
99 console.log("TC " + tc_10first_error_response + ": 10 first queries and requests gives no response for query and publish. Otherwise normal case.");
100 console.log("TC " + tc_100first_error_response + ": 100 first queries and requests gives no response for query and publish. Otherwise normal case.");
101
TamasBakai9b780332019-02-15 08:38:16 +0000102 process.exit(0);
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000103 }
104
TamasBakai9b780332019-02-15 08:38:16 +0000105
106var bodyParser = require('body-parser')
107app.use(bodyParser.urlencoded({ extended: false }))
108
109// parse application/json
110app.use(bodyParser.json())
111
112// parse application/vnd.api+json as json
113app.use(bodyParser.json({ type: 'application/vnd.api+json' }))
114
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000115// parse some custom thing into a Buffer (to cater for 60MB files)
116app.use(bodyParser.raw({limit:1024*1024*60, type: 'application/octet-stream' }))
TamasBakai9b780332019-02-15 08:38:16 +0000117// parse an HTML body into a string
118app.use(bodyParser.text({ type: 'text/html' }))
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000119
120
121
122//Is alive function
TamasBakai9b780332019-02-15 08:38:16 +0000123app.get("/",function(req, res){
124 res.send("ok");
125})
maximessonb8b94ae2019-03-25 16:03:08 +0000126
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000127//Counter readout
128app.get("/ctr_publish_query",function(req, res){
129 res.send(""+ctr_publish_query);
130})
131app.get("/ctr_publish_query_published",function(req, res){
132 res.send(""+ctr_publish_query_published);
133})
134app.get("/ctr_publish_query_not_published",function(req, res){
135 res.send(""+ctr_publish_query_not_published);
136})
137app.get("/ctr_publish_req",function(req, res){
138 res.send(""+ctr_publish_req);
139})
140app.get("/ctr_publish_req_redirect",function(req, res){
141 res.send(""+ctr_publish_req_redirect);
142})
143app.get("/ctr_publish_req_published",function(req, res){
144 res.send(""+ctr_publish_req_published);
145})
146app.get("/ctr_published_files",function(req, res){
147 res.send(""+published.length);
148})
149app.get("/tc_info",function(req, res){
150 res.send(args.tc);
151})
152function fmtMSS(s){
153 return(s-(s%=60))/60+(9<s?':':':0')+s //Format time diff in mm:ss
154}
155app.get("/execution_time",function(req, res){
156 diff = fmtMSS(Math.floor((Date.now()-startTime)/1000));
157 res.send(""+diff);
TamasBakai9b780332019-02-15 08:38:16 +0000158})
maximessonb8b94ae2019-03-25 16:03:08 +0000159
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000160//db of published files
161var published = [];
maximessonb8b94ae2019-03-25 16:03:08 +0000162
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000163app.get('/feedlog/1/',function(req, res){
164 console.log("url:"+req.url);
165 ctr_publish_query++;
maximessonb8b94ae2019-03-25 16:03:08 +0000166 var filename = req.query.filename;
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000167 console.log(filename);
168 var qtype = req.query.type;
maximessonb8b94ae2019-03-25 16:03:08 +0000169 if(typeof(filename) == 'undefined'){
170 res.status(400).send({error: 'No filename provided.'});
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000171 return;
172 } else if(typeof(qtype) == 'undefined'){
maximessonb8b94ae2019-03-25 16:03:08 +0000173 res.status(400).send({error: 'No type provided.'});
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000174 return;
175 }
176
177 //Ugly fix, plus signs replaces with spaces in query params....need to put them back
178 filename = filename.replace(/ /g,"+");
179
180 if (args.tc==tc_normal) {
181 //continue
182 } else if (args.tc==tc_none_published) {
183 ctr_publish_query_not_published++;
184 res.send("[]");
185 return;
186 } else if (args.tc==tc_all_published) {
187 ctr_publish_query_published++;
188 res.send("[" + filename + "]");
189 return;
190 } else if (args.tc==tc_10p_no_response && (ctr_publish_query%10) == 0) {
191 return;
192 } else if (args.tc==tc_10first_no_response && ctr_publish_query<11) {
193 return;
194 } else if (args.tc==tc_100first_no_response && ctr_publish_query<101) {
195 return;
196 } else if (args.tc==tc_all_delay_10s) {
197 console.log("sleep begin");
198 timer(10000).then(_=>console.log("sleeping done"));
199 } else if (args.tc==tc_10p_delay_10s && (ctr_publish_query%10) == 0) {
200 console.log("sleep begin");
201 timer(10000).then(_=>console.log("sleeping done"));
202 } else if (args.tc==tc_10p_error_response && (ctr_publish_query%10) == 0) {
203 res.send(400);
204 return;
205 } else if (args.tc==tc_10first_error_response && ctr_publish_query<11) {
206 res.send(400);
207 return;
208 } else if (args.tc==tc_100first_error_response & ctr_publish_query<101) {
209 res.send(400);
210 return;
211 }
212
213 if (published.includes(filename)) {
214 ctr_publish_query_published++;
215 res.send("[" + filename + "]");
maximessonb8b94ae2019-03-25 16:03:08 +0000216 } else {
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000217 ctr_publish_query_not_published++;
218 res.send("[]");
maximessonb8b94ae2019-03-25 16:03:08 +0000219 }
220})
221
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000222app.put('/publish/1/:filename', function (req, res) {
223 console.log("url:"+req.url);
224 console.log("body (first 25 bytes):"+req.body.slice(0,25));
225 console.log("headers:"+req.headers);
226 ctr_publish_req++;
227
228 var filename = req.params.filename;
229 console.log(filename);
230
231 if (args.tc==tc_normal) {
232 //continue
233 } else if (args.tc==tc_none_published) {
234 ctr_publish_req_redirect++;
235 res.redirect(301, 'http://127.0.0.1:3908/publish/1/'+filename);
236 return;
237 } else if (args.tc==tc_all_published) {
238 ctr_publish_req_published++;
239 res.send("ok");
240 return;
241 }else if (args.tc==tc_10p_no_response && (ctr_publish_req%10) == 0) {
242 return;
243 } else if (args.tc==tc_10first_no_response && ctr_publish_req<11) {
244 return;
245 } else if (args.tc==tc_100first_no_response && ctr_publish_req<101) {
246 return;
247 } else if (args.tc==tc_all_delay_10s) {
248 console.log("sleep begin");
249 timer(10000).then(_=>console.log("sleeping done"));
250 } else if (args.tc==tc_10p_delay_10s && (ctr_publish_req%10) == 0) {
251 console.log("sleep begin");
252 timer(10000).then(_=>console.log("sleeping done"));
253 } else if (args.tc==tc_10p_error_response && (ctr_publish_req%10) == 0) {
254 res.send(400);
255 return;
256 } else if (args.tc==tc_10first_error_response && ctr_publish_req<11) {
257 res.send(400);
258 return;
259 } else if (args.tc==tc_100first_error_response & ctr_publish_req<101) {
260 res.send(400);
261 return;
262 }
263
264 if (!published.includes(filename)) {
265 ctr_publish_req_redirect++;
266 res.redirect(301, 'http://127.0.0.1:3908/publish/1/'+filename);
267 } else {
268 ctr_publish_req_published++;
269 res.send("ok");
270 }
271})
272
273//Callback from DR REDIR server, when file is published ok this PUT request update the list of published files.
274app.put('/dr_redir_publish/:filename', function (req, res) {
275 console.log("url:"+req.url);
276 var filename = req.params.filename;
277 console.log(filename);
278
279 if (!published.includes(filename)) {
280 console.log("File marked as published by callback from DR redir SIM. url: " + req.url);
281 published.push(filename);
282 } else {
283 console.log("File already marked as published. Callback from DR redir SIM. url: " + req.url);
284 }
285
286 res.send("ok");
287})
maximessonb8b94ae2019-03-25 16:03:08 +0000288
TamasBakai9b780332019-02-15 08:38:16 +0000289var httpServer = http.createServer(app);
290var httpsServer = https.createServer(credentials, app);
291
maximessonb8b94ae2019-03-25 16:03:08 +0000292var httpPort=3906;
293var httpsPort=3907;
TamasBakai9b780332019-02-15 08:38:16 +0000294httpServer.listen(httpPort);
maximessonb8b94ae2019-03-25 16:03:08 +0000295console.log("DR-simulator listening (http) at "+httpPort);
TamasBakai9b780332019-02-15 08:38:16 +0000296httpsServer.listen(httpsPort);
maximessonb8b94ae2019-03-25 16:03:08 +0000297console.log("DR-simulator listening (https) at "+httpsPort);