forked from s-rah/onionscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env_test.go
70 lines (62 loc) · 1.83 KB
/
env_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
package deanonymization
import (
"github.com/s-rah/onionscan/config"
"github.com/s-rah/onionscan/crawldb"
"github.com/s-rah/onionscan/model"
"github.com/s-rah/onionscan/report"
"io/ioutil"
"net/http"
"os"
"testing"
)
type EnvContext struct {
t *testing.T
osreport *report.OnionScanReport
report *report.AnonymityReport
osc *config.OnionScanConfig
dbdir string
onion string
}
func CreateEnvContext(t *testing.T) *EnvContext {
ctx := new(EnvContext)
ctx.t = t
ctx.onion = "ktjts6vcmrumyy5x.onion"
var err error
ctx.dbdir, err = ioutil.TempDir("", "test-crawl")
if err != nil {
ctx.t.Errorf("Error creating temporary directory: %s", err)
}
ctx.osreport = report.NewOnionScanReport(ctx.onion)
ctx.report = new(report.AnonymityReport)
ctx.osc = new(config.OnionScanConfig)
ctx.osc.Database = new(crawldb.CrawlDB)
ctx.osc.Database.NewDB(ctx.dbdir)
return ctx
}
func (ctx *EnvContext) CreatePage(filename string, status int, contentType string, content string) {
page := new(model.Page)
page.Status = status
page.Headers = make(http.Header)
page.Headers.Add("Content-Type", contentType)
page.Snapshot = content
id, err := ctx.osc.Database.InsertCrawlRecord("http://"+ctx.onion+filename, page)
if err != nil {
ctx.t.Errorf("Error inserting test document: %s", err)
}
ctx.osreport.Crawls[filename] = id
}
func (ctx *EnvContext) CreateBinaryPage(filename string, status int, contentType string, content []byte) {
page := new(model.Page)
page.Status = status
page.Headers = make(http.Header)
page.Headers.Add("Content-Type", contentType)
page.Raw = content
id, err := ctx.osc.Database.InsertCrawlRecord("http://"+ctx.onion+filename, page)
if err != nil {
ctx.t.Errorf("Error inserting test document: %s", err)
}
ctx.osreport.Crawls[filename] = id
}
func (ctx *EnvContext) Cleanup() {
os.RemoveAll(ctx.dbdir)
}