forked from green-button/green-button.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
greentest.inc
718 lines (580 loc) · 20.1 KB
/
greentest.inc
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import namespace="System.IO" %>
<%@ Import namespace="System.Web.Services" %>
<%@ Import namespace="System.Web.Configuration" %>
<%@ Import namespace="System.Xml" %>
<%@ Import namespace="System.Xml.Xsl" %>
<%@ Import namespace="System.Xml.XPath" %>
<%@ Import namespace="System.Xml.Schema" %>
<script runat="server">
String strResults;
String strTestInfo;
bool bValidationError = false;
bool bDoReadLoop = true;
String strResultsFileName;
void Page_Load()
{
string strDownloadFileName = Request.QueryString["filename"];
if(!String.IsNullOrEmpty(strDownloadFileName))
{
ServeFile(strDownloadFileName);
return;
}
if(!String.IsNullOrEmpty(Request.Headers["X-File-Name"]))
{
DropZonePage_Load();
}
if(!String.IsNullOrEmpty(Request.Form["Scope"]))
{
FormPage_Load();
}
}
void ServeFile(String strFileName)
{
String strFullFilePath = Request.PhysicalApplicationPath + "\\uploads\\" + strFileName;
FileInfo file = new FileInfo(strFullFilePath);
if (file.Exists)
{
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/html";
//Response.ContentType = "text/html; filename=" + strFileName;
Response.AddHeader("Content-Disposition","attachment; filename=" + strFileName);
//Response.AddHeader("Content-Length", file.Length.ToString());
using (StreamReader sr = file.OpenText())
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Response.Write(s);
}
}
file.Delete();
Response.End();
}
}
bool ProcessFunctionBlock(string strDir, string strInputFile, string strFuncBlkNo)
{
String MyXmlPath = strDir + "\\" + strInputFile;
String MyXsltPath = Request.PhysicalApplicationPath + "\\conformance\\FB_" + strFuncBlkNo + ".xsl";
XslCompiledTransform XSLTransform = new XslCompiledTransform();
XSLTransform.Load(MyXsltPath);
String MyXMLResultPath = strDir + "\\FB_" + strFuncBlkNo + ".xml";
try
{
XSLTransform.Transform(MyXmlPath, MyXMLResultPath);
}
catch(Exception e)
{
String message = e.Message;
strResults = strResults + "<p><br><p>";
strResults = strResults + "Failed transform 1:" + message;
return false;
}
XmlDocument readDoc = new XmlDocument();
readDoc.Load(MyXMLResultPath);
int count = readDoc.SelectNodes("/anElement/assert").Count;
MyXsltPath = Request.PhysicalApplicationPath + "\\conformance\\WebTestPresentResults.xsl";
MyXmlPath = MyXMLResultPath;
XslCompiledTransform XSLTransform2 = new XslCompiledTransform();
XSLTransform2.Load(MyXsltPath);
String MyHTMLResultPath = strDir + "\\FB_" + strFuncBlkNo + ".html";
XSLTransform2.Transform(MyXmlPath, MyHTMLResultPath);
strResults = strResults + "<table class=\"GBInnerDataTable\" width=\"100%\"><thead><tr><th>" + GetFBDescription(strFuncBlkNo) + "</th></tr></thead></table>";
strResults = strResults + File.ReadAllText(MyHTMLResultPath);
if(count>0)
{
return false;
}
else
{
return true;
}
}
void SaveFileInfoToLog(String strFileName, String strScope)
{
String strLog = "";
strLog = "Date of test: " + System.DateTime.Now.ToString() + " File Name: " + strFileName + " Scope: " + strScope;
try
{
File.AppendAllText(Server.MapPath("~/uploads/upload.log"), strLog + Environment.NewLine);
}
catch(Exception e)
{
String message = e.Message;
strResults = strResults + "<p>Failed writing to log file:" + message + "</p>";
}
}
void GetTitles(String strXMLFile)
{
// Open the XML.
XPathNavigator nav;
XPathDocument docNav;
XPathNodeIterator NodeIter;
docNav = new XPathDocument(strXMLFile);
// Create a navigator to query with XPath.
nav = docNav.CreateNavigator();
XmlNamespaceManager ns = null;
if (nav.NameTable != null)
{
ns = new XmlNamespaceManager(nav.NameTable);
ns.AddNamespace("atom", "http://www.w3.org/2005/Atom");
}
NodeIter = nav.Select("/atom:feed/atom:entry",ns);
strResults = strResults + "\r\n";
strResults = strResults + "<table class=\"GBInnerDataTable\" width=\"100%\"><thead><tr><th>Entry Titles</th></tr></thead></table>\r\n";
strResults = strResults + "<table class=\"GBInnerDataTable\" width=\"100%\">\r\n";
strResults = strResults + "<tbody>\r\n";
//Iterate through the results showing the element value.
if(NodeIter.Count !=0)
{
while (NodeIter.MoveNext())
{
XPathNavigator clone1 = NodeIter.Current.Clone();
// Check that there is a title tag there
if(clone1.MoveToChild("title","http://www.w3.org/2005/Atom"))
{
String strResult1 = clone1.Value;
XPathNavigator clone = NodeIter.Current.Clone();
clone.MoveToChild("content","http://www.w3.org/2005/Atom");
clone.MoveToFirstChild();
String strName = clone.Name;
if(strResult1.CompareTo("")!=0)
{
strResults = strResults + "<tr><td>Entry[" + NodeIter.CurrentPosition + "] Content: " + strName + " Title: " + strResult1 + "</td></tr>\r\n";
}
}
};
}
else
{
strResults = strResults + "<tr><td>No titles are present.</td></tr>\r\n";
}
strResults = strResults + "</tbody>\r\n";
strResults = strResults + "</table>\r\n";
}
void FormPage_Load()
{
// This load method is used by the "form post" upload method
bool bTestFailed = false;
String strScope = Request.Form["Scope"];
String strDownloadResults = Request.Form["hDownloadResults"];
String strKeepFile = Request.Form["hKeepFile"];
if(String.Compare(strScope,"FB=")==0)
{
strTestInfo = "Error: Please select at least one function block for testing.";
return;
}
int n = new Random().Next(1, 100);
String strDirName = Server.MapPath("uploads") + "\\" + System.DateTime.Now.ToString("yyyy_MM_dd_H_mm_ss_") + n.ToString();
try
{
DirectoryInfo di = Directory.CreateDirectory(strDirName);
}
catch(Exception e)
{
Response.Write("Error: Could not save file. Please check with the system administrator.");
Response.End();
}
String strScopeList = strScope.Replace("FB=","");
String[] strFBs = strScopeList.Split(',');
HttpFileCollection uploadFiles = Request.Files;
// Loop over the uploaded files and save to disk.
int i = 0;
//for (i = 0; i < uploadFiles.Count; i++)
//{
// its possible we may reinstate multi-file uploads but not for now.
HttpPostedFile postedFile = uploadFiles[i];
// Access the uploaded file's content in-memory:
System.IO.Stream inStream = postedFile.InputStream;
byte[] fileData = new byte[postedFile.ContentLength];
inStream.Read(fileData, 0, postedFile.ContentLength);
String strOrigFileName = postedFile.FileName;
strOrigFileName = strOrigFileName.Replace(" ","_");
// Save the posted file in our "data" virtual directory.
postedFile.SaveAs(strDirName + "\\" + strOrigFileName);
SaveFileInfoToLog(strOrigFileName,strScope);
if(!ValidateXML(strDirName + "\\" + strOrigFileName))
{
GetTitles(strDirName + "\\" + strOrigFileName);
if(String.Compare(strKeepFile,"on")==0)
{
String strMinScope = strScope;
strMinScope = strMinScope.Replace("FB=","");
strMinScope = strMinScope.Replace(",","_");
String strSaveFile = Server.MapPath("uploads") + "\\" + strOrigFileName + "_" + System.DateTime.Now.ToString("yyyyMMddHmmss_") + "FB_" + strMinScope;
strSaveFile = strSaveFile.Replace(".xml","") + ".xml";
File.Copy(strDirName + "\\" + strOrigFileName, strSaveFile);
}
foreach( String strFB in strFBs)
{
if(!ProcessFunctionBlock(strDirName,strOrigFileName,strFB))
{
bTestFailed = true;
}
}
}
strTestInfo = "<p>File name: " + postedFile.FileName;
//}
//For future reference if we reinstate AJAX based browser results rendering.
//Response.AddHeader("RunTime", System.DateTime.Now.ToString());
//Response.AddHeader("FileLocation", "http://" + Request.Url.Host + "/Uploads/");
//Response.AddHeader("Scope", strScope);
strTestInfo = strTestInfo + " evaluated at: " + System.DateTime.Now.ToString() + "</p>";
if(String.Compare(strDownloadResults,"on")==0)
{
String strOriginalFileName = strOrigFileName;
strOriginalFileName = strOriginalFileName.Replace(".xml","");
//string strFileName = System.DateTime.Now.ToString("yyyy_MM_dd_H_mm_ss_") + n.ToString() + "_results.html";
string strFileName = strOriginalFileName + "_" + System.DateTime.Now.ToString("yyyy_MM_dd_H_mm_ss_") + n.ToString() + "_results.html";
String strFileNameResults = Server.MapPath("uploads") + "\\" + strFileName;
try
{
File.AppendAllText(strFileNameResults, strTestInfo);
File.AppendAllText(strFileNameResults, strResults);
}
catch(Exception e)
{
String message = e.Message;
strResults = strResults + "<p>Failed writing to results file:" + message + "</p>";
}
strResultsFileName = strFileName;
}
DeleteDirectory(strDirName);
}
void DropZonePage_Load()
{
// This load method is used by the AJAX drop zone upload method
String strKeepFile = Request.Headers["hKeepFile"];
String strDownloadResults = Request.Headers["hDownloadResults"];
String strScope = Request.Headers["Scope"];
if(string.IsNullOrEmpty(strScope))
{
Response.Write("Error: Please select at least one function block for testing.");
Response.End();
}
if(String.Compare(strScope,"FB=")==0)
{
Response.Write("Error: Please select at least one function block for testing.");
Response.End();
}
int n = new Random().Next(1, 100);
String strDirName = Server.MapPath("uploads") + "\\" + System.DateTime.Now.ToString("yyyy_MM_dd_H_mm_ss_") + n.ToString();
try
{
DirectoryInfo di = Directory.CreateDirectory(strDirName);
}
catch(Exception e)
{
Response.Write("Error: Could not save file. Please check with the system administrator.");
Response.End();
}
String strScopeList = strScope.Replace("FB=","");
String[] strFBs = strScopeList.Split(',');
String strOrigFileName = Request.Headers["X-File-Name"];
strOrigFileName = strOrigFileName.Replace(" ","_");
Stream inputStream = Request.InputStream;
FileStream fileStream = new FileStream(strDirName + "\\" + strOrigFileName, FileMode.OpenOrCreate);
CopyTo(inputStream,fileStream);
fileStream.Close();
SaveFileInfoToLog(strOrigFileName,strScope);
if(!ValidateXML(strDirName + "\\" + strOrigFileName))
{
GetTitles(strDirName + "\\" + strOrigFileName);
if(String.Compare(strKeepFile,"on")==0)
{
String strMinScope = strScope;
strMinScope = strMinScope.Replace("FB=","");
strMinScope = strMinScope.Replace(",","_");
String strSaveFile = Server.MapPath("uploads") + "\\" + strOrigFileName + "_" + System.DateTime.Now.ToString("yyyyMMddHmmss_") + "FB_" + strMinScope;
strSaveFile = strSaveFile.Replace(".xml","") + ".xml";
File.Copy(strDirName + "\\" + strOrigFileName, strSaveFile);
}
foreach( String strFB in strFBs)
{
ProcessFunctionBlock(strDirName,strOrigFileName,strFB);
}
//For future reference if we reinstate AJAX based browser results rendering.
//Response.AddHeader("RunTime", System.DateTime.Now.ToString());
//Response.AddHeader("FileName", Request.Headers["X-File-Name"]);
//Response.AddHeader("FileLocation", "http://" + Request.Url.Host + "/uploads/");
//Response.AddHeader("Scope", Request.Headers["Scope"]);
}
strTestInfo = "<p>File name: " + Request.Headers["X-File-Name"] + " evaluated at: " + System.DateTime.Now.ToString() + "</p>";
if(String.Compare(strDownloadResults,"on")==0)
{
String strOriginalFileName = strOrigFileName;
strOriginalFileName = strOriginalFileName.Replace(".xml","");
//string strFileName = System.DateTime.Now.ToString("yyyy_MM_dd_H_mm_ss_") + n.ToString() + "_results.html";
string strFileName = strOriginalFileName + "_" + System.DateTime.Now.ToString("yyyy_MM_dd_H_mm_ss_") + n.ToString() + "_results.html";
String strFileNameResults = Server.MapPath("uploads") + "\\" + strFileName;
try
{
File.AppendAllText(strFileNameResults,GetResultsHeader());
File.AppendAllText(strFileNameResults, strTestInfo);
File.AppendAllText(strFileNameResults, strResults);
File.AppendAllText(strFileNameResults,GetResultsTail());
}
catch(Exception e)
{
String message = e.Message;
strResults = strResults + "<p>Failed writing to results file:" + message + "</p>";
}
Response.AppendHeader("filename",strFileName);
}
Response.Write(strTestInfo);
Response.Write(strResults);
DeleteDirectory(strDirName);
Response.End();
}
bool ValidateXML(String strXMLFile)
{
bValidationError = false;
try
{
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add("http://naesb.org/espi", Server.MapPath("data") + "\\espiDerived.xsd");
schemaSet.Add("http://www.w3.org/2005/Atom", Server.MapPath("data") + "\\atom.xsd");
schemaSet.Add("http://www.w3.org/XML/1998/namespace", Server.MapPath("data") + "\\xml.xsd");
// Set the validation settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.ProhibitDtd = false;
settings.ValidationType = ValidationType.Schema;
settings.Schemas = schemaSet;
settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);
// Create the XmlReader object.
XmlReader reader;
try
{
reader = XmlReader.Create(strXMLFile, settings);
}
catch(Exception e)
{
String message = e.Message;
strResults = strResults + "<p>Schema validation Error: " + message + "<p>";
return true;
}
// Parse the file.
try
{
bDoReadLoop = true;
while (bDoReadLoop)
{
if(!reader.Read())
{
bDoReadLoop = false;
}
}
}
catch(Exception e)
{
String message = e.Message;
strResults = strResults + "<p>Schema validation Error: " + message + "<p>";
reader.Close();
return true;
}
reader.Close();
return bValidationError;
}
catch(Exception e)
{
String message = e.Message;
strResults = strResults + "<p>Schema validation Error: " + message + "<p>";
return true;
}
}
int iValidationErrCnt = 50;
void ValidationCallBack(object sender, ValidationEventArgs e)
{
strResults = strResults + "<p>Schema validation error: " + e.Message + "<br>" + "at line number:" + e.Exception.LineNumber + " position:" + e.Exception.LinePosition + "</p>";
iValidationErrCnt--;
if(iValidationErrCnt<1)
{
bDoReadLoop = false;
strResults = strResults + "<br>" + "<p>Maximum of 50 schema validation errors reported</p>";
}
bValidationError = true;
}
void DeleteDirectory(string target_dir)
{
try
{
string[] files = Directory.GetFiles(target_dir);
string[] dirs = Directory.GetDirectories(target_dir);
foreach (string file in files)
{
File.SetAttributes(file, FileAttributes.Normal);
File.Delete(file);
}
foreach (string dir in dirs)
{
DeleteDirectory(dir);
}
Directory.Delete(target_dir, false);
}
catch(Exception e)
{
String message = e.Message;
strResults = strResults + "<p>Failed deleting temp directory:" + message + "</p>";
}
}
void CopyTo(Stream input, Stream output)
{
byte[] buffer = new byte[16 * 1024]; // Fairly arbitrary size
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, bytesRead);
}
}
int getMaxRequestLength()
{
// for reference. We may need to get this working on production.
HttpRuntimeSection section = ConfigurationManager.GetSection("system.web/httpRuntime") as HttpRuntimeSection;
//return 4096 * 1024; // Default Value
if (section != null)
{
return section.MaxRequestLength * 1024; // Default Value
}
else
{
return 4096 * 1024; // Default Value
}
}
void EmitFBRow(string FBName,string Description, bool bHidden)
{
if (!bHidden)
{
Response.Write("<tr><td><input type='checkbox' id='"+ FBName +"' name='"+ FBName +"' onclick='UpdateScopeInfo()'> [" + FBName + "] " + Description + "</td></tr>");
}
}
void GetResults()
{
Response.Write(strTestInfo);
Response.Write(strResults);
}
void GetFilename()
{
Response.Write(strResultsFileName);
}
String GetFBDescription(string strFuncBlkNo)
{
String strDesc = "";
switch(strFuncBlkNo)
{
case "1":
strDesc = "Common";
break;
case "2":
strDesc = "Green Button Download My Data";
break;
case "3":
strDesc = "Green Button Connect My Data";
break;
case "4":
strDesc = "Interval Metering";
break;
case "5":
strDesc = "Interval Electricity Metering";
break;
case "6":
strDesc = "Demand Electricity Metering";
break;
case "7":
strDesc = "Net Metering";
break;
case "8":
strDesc = "Forward and Reverse Metering";
break;
case "9":
strDesc = "Register Values";
break;
case "10":
strDesc = "Gas";
break;
case "11":
strDesc = "Water";
break;
case "12":
strDesc = "Cost of Interval Data";
break;
case "13":
strDesc = "Security and Privacy classes";
break;
case "14":
strDesc = "Authorization and Authentication";
break;
case "15":
strDesc = "Usage Summary";
break;
case "16":
strDesc = "Usage Summary with Cost";
break;
case "17":
strDesc = "Power Quality Summary";
break;
case "18":
strDesc = "Multiple UsagePoints";
break;
case "19":
strDesc = "Partial update data";
break;
case "20":
strDesc = "Common";
break;
case "21":
strDesc = "Green Button Connect My Data";
break;
case "22":
strDesc = "Security and Privacy classes";
break;
case "23":
strDesc = "Authorization and Authentication Third Party Role";
break;
case "24":
strDesc = "Request bulk of UsagePoints from DataCustodian";
break;
case "25":
strDesc = "Request of Partial Update Data";
break;
case "26":
strDesc = "Respond properly to various bad or missing data";
break;
case "27":
strDesc = "Usage Summary with Demands and Previous Day Attributes";
break;
case "28":
strDesc = "Usage Summary Costs for Current Billing Period";
break;
case "29":
strDesc = "Temperature Metering";
break;
}
String strRes = "Function Block [FB_" + strFuncBlkNo + "]:" + strDesc;
return strRes;
}
String GetResultsHeader()
{
String strHeader = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><HTML " +
"xmlns=\"http://www.w3.org/1999/xhtml\"><HEAD><META content=\"IE=10.000\" " +
"http-equiv=\"X-UA-Compatible\">" +
" " +
"<META name=\"GENERATOR\" content=\"MSHTML 10.00.9200.16660\">" +
"<META name=\"viewport\" content=\"user-scalable=yes, initial-scale=1.0\">" +
"<META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">" +
"<META name=\"author\" content=\"EnerNex\">" +
"<META name=\"keywords\" content=\"greenbutton.org\"><LINK href=\"img/fav.gif\" rel=\"Shortcut Icon\" " +
"type=\"image/x-icon\"><LINK href=\"http://www.greenbuttondata.org/css/layout.css\" " +
"rel=\"stylesheet\" type=\"text/css\"> " +
" " +
" <TITLE> Green Button Conformance Testing </TITLE></HEAD> " +
"<BODY class=\"how\">";
return strHeader;
}
String GetResultsTail()
{
String strTail = "</BODY></HTML>";
return strTail;
}
</script>