File: make_test.py

package info (click to toggle)
modsecurity 3.0.14-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 88,920 kB
  • sloc: ansic: 174,512; sh: 43,569; cpp: 26,214; python: 15,734; makefile: 3,864; yacc: 2,947; lex: 1,359; perl: 1,243; php: 42; tcl: 4
file content (74 lines) | stat: -rwxr-xr-x 1,829 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env python

import glob
import sys

def readtestdata(filename):
    """
    Read a test file and split into components
    """

    state = None
    info = {
        '--TEST--': '',
        '--INPUT--': '',
        '--EXPECTED--': ''
        }

    for line in open(filename, 'r'):
        line = line.rstrip()
        if line in ('--TEST--', '--INPUT--', '--EXPECTED--'):
            state = line
        elif state:
            info[state] += line + '\n'

    # remove last newline from input
    info['--INPUT--'] = info['--INPUT--'][0:-1]

    return (info['--TEST--'], info['--INPUT--'].strip(), info['--EXPECTED--'].strip())

def luaescape(s):
    return s.strip().replace("\\", "\\\\").replace("\n", "\\n").replace("'", "\\'")

def genluatest(fname, data):
    # TBD: change to python os.path
    name = fname.split('/')[-1]
    if name.startswith('test-tokens-'):
        testname = 'test_tokens'
        extra = "\\n"
    elif name.startswith('test-tokens_mysql'):
        testname = 'test_tokens_mysql'
        extra = "\\n"
    elif name.startswith('test-folding-'):
        testname = 'test_folding'
        extra = "\\n"
    elif name.startswith('test-sqli-'):
        testname = 'test_fingerprints'
        extra = ''
    else:
        #print "IGNORING: " + name
        return

    name = name.replace('.txt', '')

    print "is({0}('{1}'),\n   '{2}{3}',\n   '{4}')\n".format(
        testname,
        luaescape(data[1]),
        extra,
        luaescape(data[2]),
        name
    )

def test2lua(fname):
    data = readtestdata(fname)
    genluatest(fname, data)

def main():
    print "require 'testdriver'\n"
    files = glob.glob('../tests/test-*.txt')
    print "plan({0})\n".format(len(files))
    for testfile in sorted(files):
        test2lua(testfile)

if __name__ == '__main__':
    main()