TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 1 | var http = require('http'); |
| 2 | var https = require('https'); |
| 3 | var ArgumentParser = require('argparse').ArgumentParser; |
| 4 | var express = require('express'); |
| 5 | const stream = require('stream'); |
| 6 | var app = express(); |
maximesson | b8b94ae | 2019-03-25 16:03:08 +0000 | [diff] [blame] | 7 | var fs = require('fs'); |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 8 | var privateKey = fs.readFileSync('cert/private.key', 'utf8'); |
| 9 | var certificate = fs.readFileSync('cert/certificate.crt', 'utf8'); |
| 10 | var credentials = {key: privateKey, cert: certificate}; |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 11 | |
| 12 | //For execution time calculation |
| 13 | var startTime = Date.now(); |
| 14 | |
| 15 | //Test case constants |
| 16 | const tc_normal = "normal"; |
| 17 | const tc_none_published = "none_published"; |
| 18 | const tc_all_published = "all_published" |
| 19 | const tc_10p_no_response = "10p_no_response"; |
| 20 | const tc_10first_no_response = "10first_no_response"; |
| 21 | const tc_100first_no_response = "100first_no_response"; |
| 22 | const tc_all_delay_10s = "all_delay_10s"; |
| 23 | const tc_10p_delay_10s = "10p_delay_10s"; |
| 24 | const tc_10p_error_response = "10p_error_response"; |
| 25 | const tc_10first_error_response = "10first_error_response"; |
| 26 | const tc_100first_error_response = "100first_error_response"; |
| 27 | |
| 28 | //Counters |
| 29 | var ctr_publish_query = 0; |
| 30 | var ctr_publish_query_published = 0; |
| 31 | var ctr_publish_query_not_published = 0; |
| 32 | var ctr_publish_req = 0; |
| 33 | var ctr_publish_req_redirect = 0; |
| 34 | var ctr_publish_req_published = 0; |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 35 | |
| 36 | var parser = new ArgumentParser({ |
| 37 | version: '0.0.1', |
| 38 | addHelp:true, |
| 39 | description: 'Datarouter simulator' |
| 40 | }); |
| 41 | |
| 42 | parser.addArgument('--tc' , { help: 'TC $NoOfTc' } ); |
| 43 | parser.addArgument('--printtc' , |
| 44 | { |
| 45 | help: 'Print complete usage help', |
| 46 | action: 'storeTrue' |
| 47 | } |
| 48 | ); |
| 49 | |
| 50 | var args = parser.parseArgs(); |
| 51 | |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 52 | if (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); |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | if (args.printtc) { |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 90 | 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 | |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 102 | process.exit(0); |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 103 | } |
| 104 | |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 105 | |
| 106 | var bodyParser = require('body-parser') |
| 107 | app.use(bodyParser.urlencoded({ extended: false })) |
| 108 | |
| 109 | // parse application/json |
| 110 | app.use(bodyParser.json()) |
| 111 | |
| 112 | // parse application/vnd.api+json as json |
| 113 | app.use(bodyParser.json({ type: 'application/vnd.api+json' })) |
| 114 | |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 115 | // parse some custom thing into a Buffer (to cater for 60MB files) |
| 116 | app.use(bodyParser.raw({limit:1024*1024*60, type: 'application/octet-stream' })) |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 117 | // parse an HTML body into a string |
| 118 | app.use(bodyParser.text({ type: 'text/html' })) |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 119 | |
| 120 | |
| 121 | |
| 122 | //Is alive function |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 123 | app.get("/",function(req, res){ |
| 124 | res.send("ok"); |
| 125 | }) |
maximesson | b8b94ae | 2019-03-25 16:03:08 +0000 | [diff] [blame] | 126 | |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 127 | //Counter readout |
| 128 | app.get("/ctr_publish_query",function(req, res){ |
| 129 | res.send(""+ctr_publish_query); |
| 130 | }) |
| 131 | app.get("/ctr_publish_query_published",function(req, res){ |
| 132 | res.send(""+ctr_publish_query_published); |
| 133 | }) |
| 134 | app.get("/ctr_publish_query_not_published",function(req, res){ |
| 135 | res.send(""+ctr_publish_query_not_published); |
| 136 | }) |
| 137 | app.get("/ctr_publish_req",function(req, res){ |
| 138 | res.send(""+ctr_publish_req); |
| 139 | }) |
| 140 | app.get("/ctr_publish_req_redirect",function(req, res){ |
| 141 | res.send(""+ctr_publish_req_redirect); |
| 142 | }) |
| 143 | app.get("/ctr_publish_req_published",function(req, res){ |
| 144 | res.send(""+ctr_publish_req_published); |
| 145 | }) |
| 146 | app.get("/ctr_published_files",function(req, res){ |
| 147 | res.send(""+published.length); |
| 148 | }) |
| 149 | app.get("/tc_info",function(req, res){ |
| 150 | res.send(args.tc); |
| 151 | }) |
| 152 | function fmtMSS(s){ |
| 153 | return(s-(s%=60))/60+(9<s?':':':0')+s //Format time diff in mm:ss |
| 154 | } |
| 155 | app.get("/execution_time",function(req, res){ |
| 156 | diff = fmtMSS(Math.floor((Date.now()-startTime)/1000)); |
| 157 | res.send(""+diff); |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 158 | }) |
maximesson | b8b94ae | 2019-03-25 16:03:08 +0000 | [diff] [blame] | 159 | |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 160 | //db of published files |
| 161 | var published = []; |
maximesson | b8b94ae | 2019-03-25 16:03:08 +0000 | [diff] [blame] | 162 | |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 163 | app.get('/feedlog/1/',function(req, res){ |
| 164 | console.log("url:"+req.url); |
| 165 | ctr_publish_query++; |
maximesson | b8b94ae | 2019-03-25 16:03:08 +0000 | [diff] [blame] | 166 | var filename = req.query.filename; |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 167 | console.log(filename); |
| 168 | var qtype = req.query.type; |
maximesson | b8b94ae | 2019-03-25 16:03:08 +0000 | [diff] [blame] | 169 | if(typeof(filename) == 'undefined'){ |
| 170 | res.status(400).send({error: 'No filename provided.'}); |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 171 | return; |
| 172 | } else if(typeof(qtype) == 'undefined'){ |
maximesson | b8b94ae | 2019-03-25 16:03:08 +0000 | [diff] [blame] | 173 | res.status(400).send({error: 'No type provided.'}); |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 174 | 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 + "]"); |
maximesson | b8b94ae | 2019-03-25 16:03:08 +0000 | [diff] [blame] | 216 | } else { |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 217 | ctr_publish_query_not_published++; |
| 218 | res.send("[]"); |
maximesson | b8b94ae | 2019-03-25 16:03:08 +0000 | [diff] [blame] | 219 | } |
| 220 | }) |
| 221 | |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 222 | app.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. |
| 274 | app.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 | }) |
maximesson | b8b94ae | 2019-03-25 16:03:08 +0000 | [diff] [blame] | 288 | |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 289 | var httpServer = http.createServer(app); |
| 290 | var httpsServer = https.createServer(credentials, app); |
| 291 | |
maximesson | b8b94ae | 2019-03-25 16:03:08 +0000 | [diff] [blame] | 292 | var httpPort=3906; |
| 293 | var httpsPort=3907; |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 294 | httpServer.listen(httpPort); |
maximesson | b8b94ae | 2019-03-25 16:03:08 +0000 | [diff] [blame] | 295 | console.log("DR-simulator listening (http) at "+httpPort); |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 296 | httpsServer.listen(httpsPort); |
maximesson | b8b94ae | 2019-03-25 16:03:08 +0000 | [diff] [blame] | 297 | console.log("DR-simulator listening (https) at "+httpsPort); |