PageRenderTime 14ms CodeModel.GetById 3ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/PortletReviewer/Test/JspValidatorTest.hs

http://hdbc.googlecode.com/
Haskell | 131 lines | 114 code | 16 blank | 1 comment | 1 complexity | 70f74e93e36a49c441e39e8383eb48c4 MD5 | raw file
  1
  2module Test.JspValidatorTest where
  3
  4import Test.HUnit
  5import JspValidator
  6import Definitions
  7
  8runAllTests :: IO Counts
  9runAllTests = runTestTT (
 10    TestList [TestLabel "testValidateJavascriptFunctionsEncoded" testValidateJavascriptFunctionsEncoded,
 11              TestLabel "testValidateElementIdsEncoded" testValidateElementIdsEncoded,
 12              TestLabel "testValidateNoHtmlComments" testValidateNoHtmlComments,
 13              TestLabel "testValidateImagesAreAccessible" testValidateImagesAreAccessible,
 14              TestLabel "testValidateFormNamesEncoded" testValidateFormNamesEncoded,
 15              TestLabel "testValidateNoIllegalHtmlTags" testValidateNoIllegalHtmlTags,
 16              TestLabel "testValidatUsingPortletClasses" testValidatUsingPortletClasses,
 17              TestLabel "testValidateContextPathCalls" testValidateContextPathCalls,
 18              TestLabel "testValidateNoSessionCreated" testValidateNoSessionCreated,
 19              TestLabel "testValidateTextBoxesHaveLabels" testValidateTextBoxesHaveLabels,
 20              TestLabel "testValidateTextBoxesHaveLabels2" testValidateTextBoxesHaveLabels2,
 21              TestLabel "testValidateTextBoxesHaveLabels3" testValidateTextBoxesHaveLabels3]
 22    )
 23
 24isErrorFree :: [ErrorMessage] -> Bool
 25isErrorFree errs = length errs == 0
 26
 27testValidateJavascriptFunctionsEncoded :: Test
 28testValidateJavascriptFunctionsEncoded = TestCase (do
 29    let fileContents1 = ["", "\n", ""]
 30        fileContents2 = ["", "function myFunction() {", "}"]
 31        fileContents3 = ["<portlet:namespace  />function myFnc ()"]
 32    assertEqual "contents1 has no javascript functions" True 
 33                    (isErrorFree (validateJavascriptFunctionsEncoded fileContents1))
 34    assertEqual "contents2 is not properly encoded"  False  
 35                    (isErrorFree (validateJavascriptFunctionsEncoded fileContents2))
 36    assertEqual "contents3 is properly encoded" True  
 37                    (isErrorFree (validateJavascriptFunctionsEncoded fileContents3))
 38    )                                                   
 39
 40testValidateElementIdsEncoded :: Test
 41testValidateElementIdsEncoded = TestCase ( do
 42    let contents1 = ["", "", "\n"]
 43        contents2 = ["<form id=\"primary\" action=\"post\">", "<input name=\"insert\" />"]
 44        contents3 = ["<form id=\"<portlet:namespace />primary\" >"]
 45    assertEqual "contents1: expected 'valid'" True  (isErrorFree (validateElementIdsEncoded contents1))
 46    assertEqual "contents2: expected 'invalid'" False  (isErrorFree (validateElementIdsEncoded contents2))
 47    assertEqual "contents3: expected 'valid'" True  (isErrorFree (validateElementIdsEncoded contents3))
 48    )
 49
 50testValidateFormNamesEncoded :: Test
 51testValidateFormNamesEncoded = TestCase (do
 52    let contents1 = ["<form name=bad_name"]
 53        contents2 = ["<form id=\"some_id\" name='bad_name'"]
 54        contents3 = ["<form action=post name=\"bad_name\""]
 55        contents4 = ["<FORM method=POST name=\"<portlet:namespace />form1\" >"]
 56    assertEqual "contents1 expected invalid" False (isErrorFree (validateFormNamesEncoded contents1))
 57    assertEqual "contents2 expected invalid" False (isErrorFree (validateFormNamesEncoded contents2))
 58    assertEqual "contents3 expected invalid" False (isErrorFree (validateFormNamesEncoded contents3))
 59    assertEqual "contents4 expected invalid" True (isErrorFree (validateFormNamesEncoded contents4))
 60    )
 61
 62testValidateImagesAreAccessible :: Test
 63testValidateImagesAreAccessible = TestCase (do
 64    let contents1 = ["<img src=./and/a/path/file.img />"]
 65        contents2 = ["<IMG src=\"\" alt=\"yo\""]
 66    assertEqual "contents1 expected invalid" False (isErrorFree (validateImagesAreAccessible contents1))
 67    assertEqual "contents2 expected valid" True (isErrorFree (validateImagesAreAccessible contents2))
 68    )
 69
 70testValidateNoHtmlComments :: Test
 71testValidateNoHtmlComments = TestCase (do
 72    let contents1 = ["<!-- and a comment -->"]
 73    assertEqual "contents1: expected 'invalid'" False  (isErrorFree (validateNoHtmlComments contents1))
 74    )
 75
 76testValidateNoIllegalHtmlTags :: Test
 77testValidateNoIllegalHtmlTags = TestCase (do
 78    let contents = ["thed <iframe name=\"edkd"]
 79    assertEqual "expected failure - iframe element exists" False (isErrorFree (validateNoIllegalHtmlTags contents))
 80    )
 81
 82testValidatUsingPortletClasses :: Test
 83testValidatUsingPortletClasses = TestCase (do
 84    let contents1 = ["<div class=\"bogus-class\" >"]
 85        contents2 = ["<p class=\"portlet-msg-error\">"]
 86    assertEqual "contents1: expected 'invalid'" False (isErrorFree (validateUsingPortletClasses contents1))
 87    assertEqual "contents2: expected 'valid'" True (isErrorFree (validateUsingPortletClasses contents2))
 88    )
 89
 90testValidateContextPathCalls :: Test
 91testValidateContextPathCalls = TestCase (do
 92    let contents1 = ["String pathToImages = (String) renderRequest.getContextPath();"]
 93        contents2 = ["String pathToImages = (String) renderResponse.encodeURL(renderRequest.getContextPath() + /img/stuff.img);"]
 94    assertEqual "contents1: expected 'invalid'" False  (isErrorFree (validateContextPathCalls contents1))
 95    assertEqual "contents2: expected 'valid'" True  (isErrorFree (validateContextPathCalls contents2))
 96    )
 97
 98testValidateNoSessionCreated :: Test
 99testValidateNoSessionCreated = TestCase (do
100    let contents1 = ["<%@page session=\"false\" contentType=\"text/html\" %>"]
101        contents2 = ["<%@ page  contentType=\"text/html\"  session=\"false\"%>"]
102    assertEqual "contents1:expected 'valid'" True (isErrorFree (validateNoSessionCreated contents1))
103    assertEqual "contents2:expected 'valid'" True (isErrorFree (validateNoSessionCreated contents2))
104    )
105
106testValidateTextBoxesHaveLabels :: Test
107testValidateTextBoxesHaveLabels = TestCase (do
108     let contents = ["<label for=\"<portlet:namespace/><%=Consts.SOURCE_URL%>\">",
109                     "<spring:message code=\"config.source.url\" />",
110		     "</label>",
111		     "<br/>",
112		     "<input type=\"text\" id=\"<portlet:namespace/><%=Consts.SOURCE_URL%>\" name=\"<%=Consts.SOURCE_URL%>\" size=\"40\"",
113		     "value='<c:out value=\"${attributes.sourceUrl}\"/>' />"]
114     assertEqual "should pass as control has label" True (isErrorFree (validateTextBoxesHaveLabels contents)))
115
116testValidateTextBoxesHaveLabels2 :: Test
117testValidateTextBoxesHaveLabels2 = TestCase (do
118     let contents = ["<textarea >",
119                     "<spring:message code=\"config.source.url\" />",
120		     "</textarea>",
121		     "<input type=\"text\" id=\"<portlet:namespace/><%=Consts.SOURCE_URL%>\" name=\"<%=Consts.SOURCE_URL%>\" size=\"40\"",
122		     "value='<c:out value=\"${attributes.sourceUrl}\"/>' />"]
123     assertEqual "should not pass as control has label" False (isErrorFree (validateTextBoxesHaveLabels contents)))
124
125-- sometimes we have stuff like type=text without any quotes. Not valid html, but why would that stop anyone
126testValidateTextBoxesHaveLabels3 :: Test
127testValidateTextBoxesHaveLabels3 = TestCase (do
128     let contents = ["<input type=text name=whatever / >",
129		     "<input type=text id=\"<portlet:namespace/><%=Consts.SOURCE_URL%>\" name=\"<%=Consts.SOURCE_URL%>\" size=\"40\"",
130		     "value='<c:out value=\"${attributes.sourceUrl}\"/>' />"]
131     assertEqual "should not pass as we have two text boxes without labels" False (isErrorFree (validateTextBoxesHaveLabels contents)))