forked from GerbenJavado/LinkFinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
76 lines (64 loc) · 4.65 KB
/
tests.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pytest
from linkfinder import regex_str, parser_file, to_full_urls
# Imitate cli_output function
def get_parse_cli(str):
endpoints = parser_file(str, regex_str, mode=0)
ret = []
for endpoint in endpoints:
ret.append(endpoint["link"])
return ret
def test_parser_cli():
assert get_parse_cli("\"http://example.com\"") == ["http://example.com"]
assert get_parse_cli("\"smb://example.com\"") == ["smb://example.com"]
assert get_parse_cli("\"https://www.example.co.us\"") == ["https://www.example.co.us"]
assert get_parse_cli("\"/path/to/file\"") == ["/path/to/file"]
assert get_parse_cli("\"../path/to/file\"") == ["../path/to/file"]
assert get_parse_cli("\"./path/to/file\"") == ["./path/to/file"]
assert get_parse_cli("\"/user/create.action?user=Test\"") == ["/user/create.action?user=Test"]
assert get_parse_cli("\"/api/create.php?user=test&pass=test#home\"") == ["/api/create.php?user=test&pass=test#home"]
assert get_parse_cli("\"/wrong/file/test<>b\"") == []
assert get_parse_cli("\"api/create.php\"") == ["api/create.php"]
assert get_parse_cli("\"api/create.php?user=test\"") == ["api/create.php?user=test"]
assert get_parse_cli("\"api/create.php?user=test&pass=test\"") == ["api/create.php?user=test&pass=test"]
assert get_parse_cli("\"api/create.php?user=test#home\"") == ["api/create.php?user=test#home"]
assert get_parse_cli("\"user/create.action?user=Test\"") == ["user/create.action?user=Test"]
assert get_parse_cli("\"user/create.notaext?user=Test\"") == []
assert get_parse_cli("\"/path/to/file\"") == ["/path/to/file"]
assert get_parse_cli("\"../path/to/file\"") == ["../path/to/file"]
assert get_parse_cli("\"./path/to/file\"") == ["./path/to/file"]
assert get_parse_cli("\"/wrong/file/test<>b\"") == []
# REST API (no extension)
assert get_parse_cli("\"api/user\"") == ["api/user"]
assert get_parse_cli("\"v1/create\"") == ["v1/create"]
assert get_parse_cli("\"api/v1/user/2\"") == ["api/v1/user/2"]
assert get_parse_cli("\"api/v1/search?text=Test Hello\"") == ["api/v1/search?text=Test Hello"]
assert get_parse_cli("\"test_1.json\"") == ["test_1.json"]
assert get_parse_cli("\"test2.aspx?arg1=tmp1+tmp2&arg2=tmp3\"") == ["test2.aspx?arg1=tmp1+tmp2&arg2=tmp3"]
assert get_parse_cli("\"addUser.action\"") == ["addUser.action"]
assert get_parse_cli("\"main.js\"") == ["main.js"]
assert get_parse_cli("\"index.html\"") == ["index.html"]
assert get_parse_cli("\"robots.txt\"") == ["robots.txt"]
assert get_parse_cli("\"users.xml\"") == ["users.xml"]
assert get_parse_cli("\"UserModel.name\"") == []
assert get_parse_cli("\"app/admin/admin.controller.js\"") == ["app/admin/admin.controller.js"]
assert get_parse_cli("\"services/customer.services.js\"") == ["services/customer.services.js"]
def test_parser_cli_multi():
assert set(get_parse_cli("href=\"http://example.com\";href=\"/api/create.php\"")) == set(["http://example.com", "/api/create.php"])
def test_parser_unique():
'''
Should return only unique link
'''
assert get_parse_cli("href=\"http://example.com\";document.window.location=\"http://example.com\"") == ["http://example.com"]
assert set(get_parse_cli("href=\"http://example.com\";<img src=\"http://example.com\">;href=\"/api/create.php\"")) == set(["http://example.com", "/api/create.php"])
def test_to_full_urls():
assert to_full_urls('https://google.com', [{'link':'something.php'}])[0]['link'] == 'https://google.com:443/something.php'
assert to_full_urls('https://google.com/blah', [{'link':'something.php'}])[0]['link'] == 'https://google.com:443/something.php'
assert to_full_urls('https://google.com/blah/', [{'link':'something.php'}])[0]['link'] == 'https://google.com:443/blah/something.php'
assert to_full_urls('https://google.com:839/blah/', [{'link':'/../something.php'}])[0]['link'] == 'https://google.com:839/something.php'
assert to_full_urls('https://google.com/', [{'link':'./something.php'}])[0]['link'] == 'https://google.com:443/something.php'
assert to_full_urls('https://google.com', [{'link':'\\..\\something.php'}])[0]['link'] == 'https://google.com:443/something.php'
assert to_full_urls('https://google.com?param1=23', [{'link':'\\..\\something.php?param2=11'}], True)[0]['link'] == 'https://google.com:443/something.php?param2=11'
assert to_full_urls('https://google.com', [{'link':'\\..\\something.php?a=b#test'}], True)[0]['link'] == 'https://google.com:443/something.php?a=b#test'
assert to_full_urls('https://google.com', [{'link':'\\..\\something.php#test'}], True)[0]['link'] == 'https://google.com:443/something.php#test'