简体   繁体   English

用于SQL注入的Grails Spock编写单元/集成测试用例

[英]Grails Spock writing unit / integration test case for SQL injections

To support future changes in REST API that that allows query the database using /query endpoint and uses JSON as data format for I/O starting to write down test cases. 为了支持REST API的将来更改,该更改允许使用/ query端点查询数据库并使用JSON作为I / O的数据格式,从而开始记录测试用例。

My configs are:- 我的配置是:-

  1. Grails 2.3.8 Grails 2.3.8
  2. Spock testing framework Spock测试框架

I'm concerned if we have some good support from Spock for writing test cases for SQL injections verification and up to what level generic it can be made. 我担心Spock是否能为编写用于SQL注入验证的测试用例提供良好的支持,以及它可以达到何种通用级别。

When we say generic, it means that it should be hitting a different endpoint each time it is run. 当我们说泛型时,它意味着它每次运行都应该到达不同的端点。 For example, 例如,

1st run : /api/users/query 
2nd run : /api/group/query
3rd run : /api/users/query
.
.
nth time : /api/specs/query

So, the domain chosen must be different each time. 因此,每次选择的域都必须不同。 We can have some random number generated which can be used to identify Domain endpoint from a map or list of query endpoint urls for all domains. 我们可以生成一些随机数,该随机数可用于从所有域的地图或查询端点URL列表中识别域端点。

But next thought comes to my mind is whether there could another test case in place that can call these test cases(that check for SQL injections for different endpoints on each run) a specified number of times to test it further and more accurately. 但是我想到的下一个想法是,是否有另一个测试用例可以调用这些测试用例(在每次运行中检查不同端点的SQL注入)指定次数,以进一步,更准确地进行测试。

For hitting a different endpoint each time what you can do is create a list of URLMappings and can fetch a random controller each time from list. 为了每次都能到达不同的端点,您可以创建一个URLMappings列表,并且每次可以从列表中获取一个随机控制器。

If you are not using custom urlmappings then you can iterate over all controller classes and can fetch their actions. 如果您不使用自定义urlmappings,则可以遍历所有控制器类并获取其操作。 But as you have a rest api, I'm assuming you have custom mappings defined in URLMappings.groovy file. 但是,当您拥有一个URLMappings.groovy API时,我假设您在URLMappings.groovy文件中定义了自定义映射。 In this case with the help of UrlMappingsArtefactHandler you can get the url mappings artifacts. 在这种情况下,借助UrlMappingsArtefactHandler可以获取url映射工件。 Code for that would be: 该代码为:

import org.codehaus.groovy.grails.commons.UrlMappingsArtefactHandler
import org.codehaus.groovy.grails.web.mapping.DefaultUrlMappingEvaluator
import org.codehaus.groovy.grails.web.mapping.UrlMapping
import org.springframework.mock.web.MockServletContext

private List<UrlMapping> getAllURLMappings(){
    ClassLoader classLoader = this.class.classLoader
    def mappings = grailsApplication.getArtefacts(UrlMappingsArtefactHandler.TYPE)

    MockServletContext mctx = classLoader.loadClass('org.springframework.mock.web.MockServletContext').newInstance()
    DefaultUrlMappingEvaluator evaluator = classLoader.loadClass("org.codehaus.groovy.grails.web.mapping.DefaultUrlMappingEvaluator").newInstance(mctx)
    List<UrlMapping> allMappings = []

    List<UrlMapping> grailsClassMappings
    for (mapping in mappings) {
        if (Script.isAssignableFrom(mapping.getClazz())) {
            grailsClassMappings = evaluator.evaluateMappings(mapping.getClazz())
        } else {
            grailsClassMappings = evaluator.evaluateMappings(mapping.getMappingsClosure())
        }
        allMappings.addAll(grailsClassMappings)
    }
    return allMappings
}

Then to fetch url patterns for a specific action from all mappings you can iterate over the result returned from above method using below method: 然后,要从所有映射中获取特定操作的网址格式,您可以使用以下方法遍历从上述方法返回的结果:

private List<String> getMappingForAction(List<UrlMapping> mappings, String action){
    return mappings.findAll {
        UrlMapping mapping ->
            return mapping.actionName.equals(action)
    }*.urlData.urlPattern
}

And using java.util.Random class you can fetch a random endpoint each time: 使用java.util.Random类,您可以每次获取一个随机端点:

List<UrlMapping> allMappings = getAllURLMappings()
List<String> mappings = getMappingForAction(allMappings, "query")

int size = mappings.size()

Random r = new Random()
int index = r.nextInt(size - 0)

println mappings[index]

If you're wanting to run the same test with a series of parameters that change from one test execution to the next using Spock, then you should consider using parameterization using Spock where blocks . 如果您想使用一系列参数运行相同的测试,这些参数会使用Spock从一个测试执行更改为另一个测试执行,那么您应该考虑使用使用Spock where块的 参数化

The example shown below is from the Spock documentation, but you could easily change the parameter to be an array of endpoints you wish to test. 下面显示的示例来自Spock文档,但是您可以轻松地将参数更改为要测试的端点数组。

def "computing the maximum of two numbers"() {
  expect:
  Math.max(a, b) == c

  where:
  a << [5, 3]
  b << [1, 9]
  c << [5, 9]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM