forked from owasp-amass/amass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sources_test.go
128 lines (105 loc) · 2.62 KB
/
sources_test.go
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
// Copyright 2017 Jeff Foley. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package datasrcs
import (
"context"
"flag"
"log"
"os"
"strings"
"testing"
"time"
"github.com/OWASP/Amass/v3/config"
eb "github.com/OWASP/Amass/v3/eventbus"
"github.com/OWASP/Amass/v3/requests"
"github.com/OWASP/Amass/v3/systems"
)
var (
networkTest = flag.Bool("network", false, "Run tests that require connectivity (take more time)")
configPath = flag.String("config", "", "Path to the INI configuration file. Additional details below")
outputDir = flag.String("dir", "", "Path to the directory containing the output files")
domainTest = "owasp.org"
testConfig *config.Config
testSystem systems.System
expectedTest = 1
timeoutTest = time.Second * 30
)
// TestMain will parse the test flags and setup for integration tests.
func TestMain(m *testing.M) {
flag.Parse()
testConfig = setupConfig(domainTest)
if testConfig == nil {
return
}
var err error
testSystem, err = systems.NewLocalSystem(testConfig)
if err != nil {
return
}
os.Exit(m.Run())
}
func TestCleanName(t *testing.T) {
tests := []struct {
name string
domain string
expected string
}{
{"Test 1: Domain", " .owasp.org", "owasp.org"},
{"Test 2: Subdomain", ".sub.owasp.org", "sub.owasp.org"},
}
for _, tt := range tests {
result := cleanName(tt.domain)
if result != tt.expected {
t.Errorf("Failed %s: got %s expected %s", tt.name, result, tt.expected)
}
}
}
func setupConfig(domain string) *config.Config {
cfg := config.NewConfig()
config.AcquireConfig(*outputDir, *configPath, cfg)
cfg.AddDomain(domain)
buf := new(strings.Builder)
cfg.Log = log.New(buf, "", log.Lmicroseconds)
return cfg
}
func testDNSRequest(srvName string) int {
out := make(chan *requests.DNSRequest)
bus := eb.NewEventBus(1000)
defer bus.Stop()
fn := func(req *requests.DNSRequest) {
out <- req
}
bus.Subscribe(requests.NewNameTopic, fn)
defer bus.Unsubscribe(requests.NewNameTopic, fn)
var srv requests.Service
for _, s := range testSystem.DataSources() {
if s.String() == srvName {
srv = s
break
}
}
if srv == nil {
return 0
}
ctx := context.WithValue(context.Background(), requests.ContextConfig, testConfig)
ctx = context.WithValue(ctx, requests.ContextEventBus, bus)
srv.DNSRequest(ctx, &requests.DNSRequest{
Name: domainTest,
Domain: domainTest,
})
count := 0
doneTimer := time.After(timeoutTest)
loop:
for {
select {
case <-out:
count++
if count == expectedTest {
break loop
}
case <-doneTimer:
break loop
}
}
return count
}