예제 #1
0
func domainCacheTest(database *mgo.Database, r *http.Request,
	requestContent, header string, domainCacheTestData []DomainCacheTestData) {

	mux := handy.NewHandy()

	h := new(handler.DomainHandler)
	mux.Handle("/domain/{fqdn}", func() handy.Handler {
		return h
	})

	for _, item := range domainCacheTestData {
		r.Header.Set(header, item.HeaderValue)

		if len(requestContent) > 0 {
			utils.BuildHTTPHeader(r, []byte(requestContent))
		} else {
			utils.BuildHTTPHeader(r, nil)
		}

		w := httptest.NewRecorder()
		mux.ServeHTTP(w, r)

		responseContent, err := ioutil.ReadAll(w.Body)
		if err != nil {
			utils.Fatalln("Error reading response body", err)
		}

		if w.Code != item.ExpectedHTTPStatus {
			utils.Fatalln(fmt.Sprintf("Error in %s test using %s [%s] "+
				"HTTP header. Expected HTTP status code %d and got %d",
				r.Method, header, item.HeaderValue, item.ExpectedHTTPStatus,
				w.Code), errors.New(string(responseContent)))
		}
	}
}
예제 #2
0
func retrieveScansMetadata(database *mgo.Database) {
	mux := handy.NewHandy()

	h := new(handler.ScansHandler)
	mux.Handle("/scans", func() handy.Handler {
		return h
	})

	r, err := http.NewRequest("GET", "/scans/?orderby=startedat:desc&pagesize=10&page=1", nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	response1 := *h.Response
	responseContent, err := ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body", err)
	}

	if w.Code != http.StatusOK {
		utils.Fatalln("Error retrieving scans", errors.New(string(responseContent)))
	}

	r, err = http.NewRequest("HEAD", "/scans/?orderby=startedat:desc&pagesize=10&page=1", nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	mux.ServeHTTP(w, r)

	response2 := *h.Response
	responseContent, err = ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body", err)
	}

	if w.Code != http.StatusOK {
		utils.Fatalln("Error retrieving scans", errors.New(string(responseContent)))
	}

	if !utils.CompareProtocolScans(response1, response2) {
		utils.Fatalln("At this point the GET and HEAD method should "+
			"return the same body content", nil)
	}
}
예제 #3
0
func retrieveScanMetadata(database *mgo.Database, startedAt time.Time) {
	mux := handy.NewHandy()

	h := new(handler.ScanHandler)
	mux.Handle("/scan/{started-at}", func() handy.Handler {
		return h
	})

	r, err := http.NewRequest("GET", fmt.Sprintf("/scan/%s", startedAt.Format(time.RFC3339Nano)), nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	response1 := *h.Response
	responseContent, err := ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body", err)
	}

	if w.Code != http.StatusOK {
		utils.Fatalln("Error retrieving scan", errors.New(string(responseContent)))
	}

	r, err = http.NewRequest("HEAD", fmt.Sprintf("/scan/%s", startedAt.Format(time.RFC3339Nano)), nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	mux.ServeHTTP(w, r)

	response2 := *h.Response
	responseContent, err = ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body", err)
	}

	if w.Code != http.StatusOK {
		utils.Fatalln("Error retrieving scan", errors.New(string(responseContent)))
	}

	if !utils.CompareProtocolScan(response1, response2) {
		utils.Fatalln("At this point the GET and HEAD method should "+
			"return the same body content", nil)
	}
}
예제 #4
0
func retrieveDomainMetadata(database *mgo.Database) {
	mux := handy.NewHandy()

	h := new(handler.DomainHandler)
	mux.Handle("/domain/{fqdn}", func() handy.Handler {
		return h
	})

	r, err := http.NewRequest("GET", "/domain/example.com.br.", nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	response1 := *h.Response
	responseContent, err := ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body from GET", err)
	}

	if w.Code != http.StatusOK {
		utils.Fatalln("Error retrieving domain", errors.New(string(responseContent)))
	}

	r, err = http.NewRequest("HEAD", "/domain/example.com.br.", nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	mux.ServeHTTP(w, r)

	response2 := *h.Response
	responseContent, err = ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body from HEAD", err)
	}

	if w.Code != http.StatusOK {
		utils.Fatalln("Error retrieving domain", errors.New(string(responseContent)))
	}

	if !utils.CompareProtocolDomain(response1, response2) {
		utils.Fatalln("At this point the GET and HEAD method should "+
			"return the same body content", nil)
	}
}
예제 #5
0
func retrieveScansIfNoneMatch(database *mgo.Database) {
	mux := handy.NewHandy()

	h := new(handler.ScansHandler)
	mux.Handle("/scans", func() handy.Handler {
		return h
	})

	r, err := http.NewRequest("GET", "/scans", nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	scansCacheTest(database, r, "If-None-Match", []ScansCacheTestData{
		{
			HeaderValue:        w.Header().Get("ETag"),
			ExpectedHTTPStatus: http.StatusNotModified,
		},
		{
			HeaderValue:        w.Header().Get("ETag") + "x",
			ExpectedHTTPStatus: http.StatusOK,
		},
	})
}
예제 #6
0
func retrieveUnknownScan(database *mgo.Database, startedAt time.Time) {
	mux := handy.NewHandy()

	h := new(handler.ScanHandler)
	mux.Handle("/scan/{started-at}", func() handy.Handler {
		return h
	})

	r, err := http.NewRequest("GET", fmt.Sprintf("/scan/%s", startedAt.Add(-1*time.Millisecond).Format(time.RFC3339Nano)), nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	responseContent, err := ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body", err)
	}

	if w.Code != http.StatusNotFound {
		utils.Fatalln("Error retrieving unknown scan", errors.New(string(responseContent)))
	}
}
예제 #7
0
func deleteDomains(database *mgo.Database) {
	mux := handy.NewHandy()

	h := new(handler.DomainHandler)
	mux.Handle("/domain/{fqdn}", func() handy.Handler {
		return h
	})

	for i := 0; i < 100; i++ {
		r, err := http.NewRequest("DELETE", fmt.Sprintf("/domain/example%d.com.br.", i), nil)
		if err != nil {
			utils.Fatalln("Error creating the HTTP request", err)
		}
		utils.BuildHTTPHeader(r, nil)

		w := httptest.NewRecorder()
		mux.ServeHTTP(w, r)

		responseContent, err := ioutil.ReadAll(w.Body)
		if err != nil {
			utils.Fatalln("Error reading response body", err)
		}

		if w.Code != http.StatusNoContent {
			utils.Fatalln("Error removing domain", errors.New(string(responseContent)))
		}
	}
}
예제 #8
0
func retrieveUnknownDomain(database *mgo.Database) {
	mux := handy.NewHandy()

	h := new(handler.DomainHandler)
	mux.Handle("/domain/{fqdn}", func() handy.Handler {
		return h
	})

	r, err := http.NewRequest("GET", "/domain/example.com.br.", nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	responseContent, err := ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body", err)
	}

	if w.Code != http.StatusNotFound {
		utils.Fatalln("Error retrieving unknown domain", errors.New(string(responseContent)))
	}
}
예제 #9
0
func retrieveDomainsWithCache() {
	var client http.Client

	url := ""
	if len(config.ShelterConfig.WebClient.Listeners) > 0 {
		url = fmt.Sprintf("http://%s:%d", config.ShelterConfig.WebClient.Listeners[0].IP,
			config.ShelterConfig.WebClient.Listeners[0].Port)
	}

	if len(url) == 0 {
		utils.Fatalln("There's no interface to connect to", nil)
	}

	var r *http.Request
	var err error

	r, err = http.NewRequest("GET", fmt.Sprintf("%s%s", url, "/domains"), nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}

	utils.BuildHTTPHeader(r, nil)

	response, err := client.Do(r)
	if err != nil {
		utils.Fatalln("Error sending request", err)
	}

	r, err = http.NewRequest("GET", fmt.Sprintf("%s%s", url, "/domains"), nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}

	r.Header.Add("If-None-Match", response.Header.Get("ETag"))
	utils.BuildHTTPHeader(r, nil)

	response, err = client.Do(r)
	if err != nil {
		utils.Fatalln("Error sending request", err)
	}

	if response.StatusCode != http.StatusNotModified {
		utils.Fatalln(fmt.Sprintf("Expected HTTP status %d and got %d for method GET and URI /domains",
			http.StatusNotModified, response.StatusCode), nil)
	}
}
예제 #10
0
func createDomains() {
	var client http.Client

	url := ""
	if len(config.ShelterConfig.WebClient.Listeners) > 0 {
		url = fmt.Sprintf("http://%s:%d", config.ShelterConfig.WebClient.Listeners[0].IP,
			config.ShelterConfig.WebClient.Listeners[0].Port)
	}

	if len(url) == 0 {
		utils.Fatalln("There's no interface to connect to", nil)
	}

	var r *http.Request
	var err error

	for i := 0; i < 100; i++ {
		uri := fmt.Sprintf("/domain/example%d.com.br.", i)
		content := `{
  "Nameservers": [
    { "host": "a.dns.br." },
    { "host": "b.dns.br." }
  ],
  "Owners": [
    { "email": "*****@*****.**", "language": "pt-br" }
  ]
}`

		r, err = http.NewRequest("PUT", fmt.Sprintf("%s%s", url, uri),
			bytes.NewReader([]byte(content)))

		if err != nil {
			utils.Fatalln("Error creating the HTTP request", err)
		}

		utils.BuildHTTPHeader(r, []byte(content))

		response, err := client.Do(r)
		if err != nil {
			utils.Fatalln("Error sending request", err)
		}

		if response.StatusCode != http.StatusCreated {
			responseContent, err := ioutil.ReadAll(response.Body)

			if err == nil {
				utils.Fatalln(fmt.Sprintf("Expected HTTP status %d and got %d for method PUT and URI %s",
					http.StatusCreated, response.StatusCode, uri),
					errors.New(string(responseContent)),
				)
			} else {
				utils.Fatalln(fmt.Sprintf("Expected HTTP status %d and got %d for method PUT and URI %s",
					http.StatusCreated, response.StatusCode, uri), nil)
			}
		}
	}
}
예제 #11
0
func retrieveScan(database *mgo.Database, startedAt time.Time) {
	mux := handy.NewHandy()

	h := new(handler.ScanHandler)
	mux.Handle("/scan/{started-at}", func() handy.Handler {
		return h
	})

	r, err := http.NewRequest("GET", fmt.Sprintf("/scan/%s", startedAt.Format(time.RFC3339Nano)), nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	responseContent, err := ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body", err)
	}

	if w.Code != http.StatusOK {
		utils.Fatalln("Error retrieving scan", errors.New(string(responseContent)))
	}

	if w.Header().Get("ETag") != "1" {
		utils.Fatalln("Not setting ETag in scan retrieve response", nil)
	}

	if len(w.Header().Get("Last-Modified")) == 0 {
		utils.Fatalln("Not setting Last-Modified in scan retrieve response", nil)
	}

	if h.Response.Status != "EXECUTED" {
		utils.Fatalln("Scan's status was not persisted correctly", nil)
	}

	if h.Response.DomainsScanned != 5000000 {
		utils.Fatalln("Scan's domains scanned information is wrong", nil)
	}

	if h.Response.DomainsWithDNSSECScanned != 250000 {
		utils.Fatalln("Scan's domains with DNSSEC scanned information is wrong", nil)
	}

	if h.Response.NameserverStatistics["OK"] != 4800000 ||
		h.Response.NameserverStatistics["TIMEOUT"] != 200000 {
		utils.Fatalln("Scan's nameserver statistics are wrong", nil)
	}

	if h.Response.DSStatistics["OK"] != 220000 ||
		h.Response.DSStatistics["EXPSIG"] != 30000 {
		utils.Fatalln("Scan's nameserver statistics are wrong", nil)
	}
}
예제 #12
0
func retrieveCurrentScan() {
	var client http.Client

	url := ""
	if len(config.ShelterConfig.WebClient.Listeners) > 0 {
		url = fmt.Sprintf("http://%s:%d", config.ShelterConfig.WebClient.Listeners[0].IP,
			config.ShelterConfig.WebClient.Listeners[0].Port)
	}

	if len(url) == 0 {
		utils.Fatalln("There's no interface to connect to", nil)
	}

	r, err := http.NewRequest("GET", fmt.Sprintf("%s%s", url, "/scans/?current"), nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}

	utils.BuildHTTPHeader(r, nil)

	response, err := client.Do(r)
	if err != nil {
		utils.Fatalln("Error sending request", err)
	}

	responseContent, err := ioutil.ReadAll(response.Body)
	if err != nil {
		utils.Fatalln("Error reading response content", err)
	}

	if response.StatusCode != http.StatusOK {
		utils.Fatalln(fmt.Sprintf("Expected HTTP status %d and got %d for method GET "+
			"and URI /scan/currentscan",
			http.StatusOK, response.StatusCode),
			errors.New(string(responseContent)),
		)
	}

	var scansResponse protocol.ScansResponse
	if err := json.Unmarshal(responseContent, &scansResponse); err != nil {
		utils.Fatalln("Error decoding scan response", err)
	}

	if len(scansResponse.Scans) != 1 {
		utils.Fatalln("Not returning the current scan in the result set or "+
			"more items than expected were found", nil)
	}

	if scansResponse.Scans[0].Status != model.ScanStatusToString(model.ScanStatusWaitingExecution) {
		utils.Fatalln(fmt.Sprintf("Invalid status returned by current scan. Expected %s and got %s",
			model.ScanStatusToString(model.ScanStatusWaitingExecution), scansResponse.Scans[0].Status), nil)
	}
}
예제 #13
0
func retrieveDomain(database *mgo.Database) {
	mux := handy.NewHandy()

	h := new(handler.DomainHandler)
	mux.Handle("/domain/{fqdn}", func() handy.Handler {
		return h
	})

	r, err := http.NewRequest("GET", "/domain/example.com.br.", nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	responseContent, err := ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body", err)
	}

	if w.Code != http.StatusOK {
		utils.Fatalln("Error retrieving domain", errors.New(string(responseContent)))
	}

	if w.Header().Get("ETag") != "2" {
		utils.Fatalln("Not setting ETag in domain retrieve response", nil)
	}

	if len(w.Header().Get("Last-Modified")) == 0 {
		utils.Fatalln("Not setting Last-Modified in domain retrieve response", nil)
	}

	if h.Response.FQDN != "example.com.br." {
		utils.Fatalln("Domain's FQDN was not persisted correctly", nil)
	}

	if len(h.Response.Nameservers) != 2 ||
		h.Response.Nameservers[0].Host != "ns1.example.com.br." ||
		h.Response.Nameservers[0].IPv4 != "127.0.0.1" ||
		h.Response.Nameservers[1].Host != "ns3.example.com.br." ||
		h.Response.Nameservers[1].IPv6 != "::1" {
		utils.Fatalln("Domain's nameservers were not persisted correctly", nil)
	}

	if len(h.Response.Owners) != 1 ||
		h.Response.Owners[0].Email != "*****@*****.**" {

		utils.Fatalln("Domain's owners were not persisted correctly", nil)
	}
}
예제 #14
0
func updateDomain(database *mgo.Database) {
	mux := handy.NewHandy()

	h := new(handler.DomainHandler)
	mux.Handle("/domain/{fqdn}", func() handy.Handler {
		return h
	})

	requestContent := `{
      "Nameservers": [
        { "host": "ns1.example.com.br.", "ipv4": "127.0.0.1" },
        { "host": "ns3.example.com.br.", "ipv6": "::1" }
      ],
      "Owners": [
        { "email": "*****@*****.**", "language": "pt-br" }
      ]
    }`

	r, err := http.NewRequest("PUT", "/domain/example.com.br.", strings.NewReader(requestContent))
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, []byte(requestContent))

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	responseContent, err := ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body", err)
	}

	if w.Code != http.StatusNoContent {
		utils.Fatalln(fmt.Sprintf("Error updating domain. "+
			"Expecting %d and got %d", http.StatusNoContent, w.Code),
			errors.New(string(responseContent)))
	}

	if w.Header().Get("ETag") != "2" {
		utils.Fatalln("Not setting ETag in domain update response", nil)
	}

	if len(w.Header().Get("Last-Modified")) == 0 {
		utils.Fatalln("Not setting Last-Modified in domain update response", nil)
	}

	if len(w.Header().Get("Location")) > 0 {
		utils.Fatalln("Setting Location in domain update response", nil)
	}
}
예제 #15
0
파일: rest.go 프로젝트: rafaeljusto/shelter
func restActionReport(numberOfItems int,
	content []byte,
	requestBuilder func(int) (*http.Request, error),
	expectedStatus int,
	action string) string {

	var client http.Client

	totalDuration, domainsPerSecond := calculateRESTDurations(func() {
		for i := 0; i < numberOfItems; i++ {
			r, err := requestBuilder(i)
			if err != nil {
				utils.Fatalln("Error creating the HTTP request", err)
			}

			utils.BuildHTTPHeader(r, content)

			response, err := client.Do(r)
			if err != nil {
				utils.Fatalln(fmt.Sprintf("Error detected when sending request for action %s"+
					" and URI \"%s\"", action, r.URL.RequestURI()), err)
			}

			_, err = ioutil.ReadAll(response.Body)
			if err != nil {
				utils.Fatalln(fmt.Sprintf("Error detected when reading the response body for action %s"+
					" and URI \"%s\"", action, r.URL.RequestURI()), err)
			}
			response.Body.Close()

			if response.StatusCode != expectedStatus {
				utils.Fatalln(fmt.Sprintf("Error with the domain object in the action %s. "+
					"Expected HTTP status %d and got %d",
					action, expectedStatus, response.StatusCode), nil)
			}
		}
	}, numberOfItems)

	var memStats runtime.MemStats
	runtime.ReadMemStats(&memStats)

	return fmt.Sprintf("% -8d | %-9s | %16s | %4d | %14.2f\n",
		numberOfItems,
		action,
		time.Duration(int64(totalDuration)).String(),
		domainsPerSecond,
		float64(memStats.Alloc)/float64(MB),
	)
}
예제 #16
0
func removeDomains() {
	var client http.Client

	url := ""
	if len(config.ShelterConfig.WebClient.Listeners) > 0 {
		url = fmt.Sprintf("http://%s:%d", config.ShelterConfig.WebClient.Listeners[0].IP,
			config.ShelterConfig.WebClient.Listeners[0].Port)
	}

	if len(url) == 0 {
		utils.Fatalln("There's no interface to connect to", nil)
	}

	var r *http.Request
	var err error

	for i := 0; i < 100; i++ {
		uri := fmt.Sprintf("/domain/example%d.com.br.", i)

		r, err = http.NewRequest("DELETE", fmt.Sprintf("%s%s", url, uri), nil)

		if err != nil {
			utils.Fatalln("Error creating the HTTP request", err)
		}

		utils.BuildHTTPHeader(r, nil)

		response, err := client.Do(r)
		if err != nil {
			utils.Fatalln("Error sending request", err)
		}

		if response.StatusCode != http.StatusNoContent {
			responseContent, err := ioutil.ReadAll(response.Body)

			if err == nil {
				utils.Fatalln(fmt.Sprintf("Expected HTTP status %d and got %d for method DELETE and URI %s",
					http.StatusCreated, response.StatusCode, uri),
					errors.New(string(responseContent)),
				)
			} else {
				utils.Fatalln(fmt.Sprintf("Expected HTTP status %d and got %d for method DELETE and URI %s",
					http.StatusCreated, response.StatusCode, uri), nil)
			}
		}
	}
}
예제 #17
0
func retrieveCurrentScan(database *mgo.Database) {
	mux := handy.NewHandy()

	h := new(handler.ScansHandler)
	mux.Handle("/scans", func() handy.Handler {
		return h
	})

	r, err := http.NewRequest("GET", "/scans?current", nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	responseContent, err := ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body", err)
	}

	if w.Code != http.StatusOK {
		utils.Fatalln("Error retrieving scan", errors.New(string(responseContent)))
	}

	if len(w.Header().Get("Last-Modified")) == 0 {
		utils.Fatalln("Current scan should return the Last-Modified header", nil)
	}

	if len(h.Response.Scans) != 1 {
		utils.Fatalln("Current scan not returned when it should", nil)
	}

	if h.Response.Scans[0].Status != "RUNNING" ||
		h.Response.Scans[0].DomainsToBeScanned != 5 ||
		h.Response.Scans[0].DomainsScanned != 4 ||
		h.Response.Scans[0].DomainsWithDNSSECScanned != 1 {

		utils.Fatalln("Not retrieving current scan information correctly", nil)
	}
}
예제 #18
0
func createDomains(database *mgo.Database) {
	mux := handy.NewHandy()

	h := new(handler.DomainHandler)
	mux.Handle("/domain/{fqdn}", func() handy.Handler {
		return h
	})

	for i := 0; i < 100; i++ {
		requestContent := `{
      "Nameservers": [
        { "host": "ns1.example.com.br." },
        { "host": "ns2.example.com.br." }
      ],
      "Owners": [
        { "email": "*****@*****.**", "language": "pt-br" }
      ]
    }`

		r, err := http.NewRequest("PUT", fmt.Sprintf("/domain/example%d.com.br.", i),
			strings.NewReader(requestContent))
		if err != nil {
			utils.Fatalln("Error creating the HTTP request", err)
		}
		utils.BuildHTTPHeader(r, []byte(requestContent))

		w := httptest.NewRecorder()
		mux.ServeHTTP(w, r)

		responseContent, err := ioutil.ReadAll(w.Body)
		if err != nil {
			utils.Fatalln("Error reading response body", err)
		}

		if w.Code != http.StatusCreated {
			utils.Fatalln("Error creating domain", errors.New(string(responseContent)))
		}
	}
}
예제 #19
0
func invalidFQDN(database *mgo.Database) {
	mux := handy.NewHandy()

	h := new(handler.DomainHandler)
	mux.Handle("/domain/{fqdn}", func() handy.Handler {
		return h
	})

	r, err := http.NewRequest("GET", "/domain/!!!", nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	if w.Code != http.StatusBadRequest {
		utils.Fatalln(fmt.Sprintf("Not verifying if FQDN exists in the URI. "+
			"Expected status %d and got %d", http.StatusBadRequest, w.Code), nil)
	}
}
func scanPersistedDomain(domainDAO dao.DomainDAO) {
	dns.HandleFunc("example.com.br.", func(w dns.ResponseWriter, dnsRequestMessage *dns.Msg) {
		defer w.Close()

		dnsResponseMessage := &dns.Msg{
			MsgHdr: dns.MsgHdr{
				Authoritative: true,
			},
			Question: dnsRequestMessage.Question,
			Answer: []dns.RR{
				&dns.SOA{
					Hdr: dns.RR_Header{
						Name:   "example.com.br.",
						Rrtype: dns.TypeSOA,
						Class:  dns.ClassINET,
						Ttl:    86400,
					},
					Ns:      "ns1.example.com.br.",
					Mbox:    "rafael.justo.net.br.",
					Serial:  2013112600,
					Refresh: 86400,
					Retry:   86400,
					Expire:  86400,
					Minttl:  900,
				},
			},
		}

		dnsResponseMessage.SetReply(dnsRequestMessage)
		w.WriteMsg(dnsResponseMessage)
	})

	mux := handy.NewHandy()

	h := new(handler.DomainVerificationHandler)
	mux.Handle("/domain/{fqdn}/verification", func() handy.Handler {
		return h
	})

	requestContent := `{
      "Nameservers": [
        { "Host": "ns1.example.com.br.", "ipv4": "127.0.0.1" },
        { "Host": "ns2.example.com.br.", "ipv4": "127.0.0.1" }
      ]
    }`

	domain := model.Domain{
		FQDN: "example.com.br.",
		Nameservers: []model.Nameserver{
			{
				Host: "ns1.example.com.br.",
				IPv4: net.ParseIP("127.0.0.1"),
			},
			{
				Host: "ns2.example.com.br.",
				IPv4: net.ParseIP("127.0.0.1"),
			},
		},
	}

	if err := domainDAO.Save(&domain); err != nil {
		utils.Fatalln("Error saving domain", err)
	}

	r, err := http.NewRequest("PUT", "/domain/example.com.br./verification",
		strings.NewReader(requestContent))
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, []byte(requestContent))

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	responseContent, err := ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body", err)
	}

	if w.Code != http.StatusOK {
		utils.Fatalln(fmt.Sprintf("Error scanning domain. "+
			"Expected %d and got %d", http.StatusOK, w.Code),
			errors.New(string(responseContent)))
	}

	domain, err = domainDAO.FindByFQDN(domain.FQDN)
	if err != nil {
		utils.Fatalln("Error retrieving the domain", err)
	}

	if len(domain.Nameservers) != 2 ||
		domain.Nameservers[0].LastStatus != model.NameserverStatusOK ||
		domain.Nameservers[1].LastStatus != model.NameserverStatusOK {

		utils.Fatalln("Not updating domain on a scan", nil)
	}

	// Now we are going to test when the database domain is different from the scanned domain

	if err := domainDAO.RemoveByFQDN(domain.FQDN); err != nil {
		utils.Fatalln("Error removing domain", err)
	}

	domain = model.Domain{
		FQDN: "example.com.br.",
		Nameservers: []model.Nameserver{
			{
				Host: "ns1.example.com.br.",
				IPv4: net.ParseIP("127.0.0.1"),
			},
			{
				Host: "ns3.example.com.br.",
				IPv4: net.ParseIP("127.0.0.1"),
			},
		},
	}

	if err := domainDAO.Save(&domain); err != nil {
		utils.Fatalln("Error saving domain", err)
	}

	mux.ServeHTTP(w, r)

	responseContent, err = ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body", err)
	}

	if w.Code != http.StatusOK {
		utils.Fatalln(fmt.Sprintf("Error scanning domain. "+
			"Expected %d and got %d", http.StatusOK, w.Code),
			errors.New(string(responseContent)))
	}

	domain, err = domainDAO.FindByFQDN(domain.FQDN)
	if err != nil {
		utils.Fatalln("Error retrieving the domain", err)
	}

	if len(domain.Nameservers) != 2 ||
		domain.Nameservers[0].LastStatus != model.NameserverStatusNotChecked ||
		domain.Nameservers[1].LastStatus != model.NameserverStatusNotChecked {

		utils.Fatalln("Updating domain on a scan when it shouldn't", nil)
	}
}
예제 #21
0
func retrieveDomains(database *mgo.Database) {
	data := []struct {
		URI                string
		ExpectedHTTPStatus int
		ContentCheck       func(*protocol.DomainsResponse)
	}{
		{
			URI:                "/domains/?orderby=xxx:desc&pagesize=10&page=1",
			ExpectedHTTPStatus: http.StatusBadRequest,
		},
		{
			URI:                "/domains/?orderby=fqdn:xxx&pagesize=10&page=1",
			ExpectedHTTPStatus: http.StatusBadRequest,
		},
		{
			URI:                "/domains/?orderby=fqdn:desc&pagesize=xxx&page=1",
			ExpectedHTTPStatus: http.StatusBadRequest,
		},
		{
			URI:                "/domains/?orderby=fqdn:desc&pagesize=10&page=xxx",
			ExpectedHTTPStatus: http.StatusBadRequest,
		},
		{
			URI:                "/domains/?orderby=fqdn:desc&pagesize=10&page=1",
			ExpectedHTTPStatus: http.StatusOK,
			ContentCheck: func(domainsResponse *protocol.DomainsResponse) {
				if len(domainsResponse.Domains) != 10 {
					utils.Fatalln("Error retrieving the wrong number of domains", nil)
				}
			},
		},
		{
			URI:                "/domains",
			ExpectedHTTPStatus: http.StatusOK,
			ContentCheck: func(domainsResponse *protocol.DomainsResponse) {
				if len(domainsResponse.Domains) == 0 {
					utils.Fatalln("Error retrieving domains", nil)
				}

				if len(domainsResponse.Domains[0].Nameservers) == 0 {
					utils.Fatalln("Error retrieving domains, no nameservers", nil)
				}

				if len(domainsResponse.Domains[0].Nameservers[0].Host) > 0 {
					utils.Fatalln("Not compressing domains result", nil)
				}
			},
		},
		{
			URI:                "/domains?expand",
			ExpectedHTTPStatus: http.StatusOK,
			ContentCheck: func(domainsResponse *protocol.DomainsResponse) {
				if len(domainsResponse.Domains) == 0 {
					utils.Fatalln("Error retrieving domains", nil)
				}

				if len(domainsResponse.Domains[0].Nameservers) == 0 {
					utils.Fatalln("Error retrieving domains, no nameservers", nil)
				}

				if len(domainsResponse.Domains[0].Nameservers[0].Host) == 0 {
					utils.Fatalln("Compressing domains result when it's not necessary", nil)
				}
			},
		},
	}

	mux := handy.NewHandy()

	h := new(handler.DomainsHandler)
	mux.Handle("/domains", func() handy.Handler {
		return h
	})

	for _, item := range data {
		r, err := http.NewRequest("GET", item.URI, nil)
		if err != nil {
			utils.Fatalln("Error creating the HTTP request", err)
		}
		utils.BuildHTTPHeader(r, nil)

		w := httptest.NewRecorder()
		mux.ServeHTTP(w, r)

		responseContent, err := ioutil.ReadAll(w.Body)
		if err != nil {
			utils.Fatalln("Error reading response body", err)
		}

		if w.Code != item.ExpectedHTTPStatus {
			utils.Fatalln(fmt.Sprintf("Error when requesting domains using the URI [%s]. "+
				"Expected HTTP status code %d but got %d", item.URI,
				item.ExpectedHTTPStatus, w.Code), errors.New(string(responseContent)))
		}

		if item.ContentCheck != nil {
			item.ContentCheck(h.Response)
		}
	}
}
func scanDomain() {
	dns.HandleFunc("example.com.br.", func(w dns.ResponseWriter, dnsRequestMessage *dns.Msg) {
		defer w.Close()

		dnsResponseMessage := &dns.Msg{
			MsgHdr: dns.MsgHdr{
				Authoritative: true,
			},
			Question: dnsRequestMessage.Question,
			Answer: []dns.RR{
				&dns.SOA{
					Hdr: dns.RR_Header{
						Name:   "example.com.br.",
						Rrtype: dns.TypeSOA,
						Class:  dns.ClassINET,
						Ttl:    86400,
					},
					Ns:      "ns1.example.com.br.",
					Mbox:    "rafael.justo.net.br.",
					Serial:  2013112600,
					Refresh: 86400,
					Retry:   86400,
					Expire:  86400,
					Minttl:  900,
				},
			},
		}

		dnsResponseMessage.SetReply(dnsRequestMessage)
		w.WriteMsg(dnsResponseMessage)
	})

	mux := handy.NewHandy()

	h := new(handler.DomainVerificationHandler)
	mux.Handle("/domain/{fqdn}/verification", func() handy.Handler {
		return h
	})

	requestContent := `{
      "Nameservers": [
        { "Host": "ns1.example.com.br.", "ipv4": "127.0.0.1" },
        { "Host": "ns2.example.com.br.", "ipv4": "127.0.0.1" }
      ]
    }`

	r, err := http.NewRequest("PUT", "/domain/example.com.br./verification",
		strings.NewReader(requestContent))
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, []byte(requestContent))

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	responseContent, err := ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body", err)
	}

	if w.Code != http.StatusOK {
		utils.Fatalln(fmt.Sprintf("Error scanning domain. "+
			"Expected %d and got %d", http.StatusOK, w.Code),
			errors.New(string(responseContent)))
	}

	if len(h.Response.Nameservers) != 2 {
		utils.Fatalln("Wrong number of nameservers", nil)
	}

	if h.Response.Nameservers[0].LastStatus !=
		model.NameserverStatusToString(model.NameserverStatusOK) {

		utils.Fatalln("Scan did not work for ns1", nil)
	}

	if h.Response.Nameservers[1].LastStatus !=
		model.NameserverStatusToString(model.NameserverStatusOK) {

		utils.Fatalln("Scan did not work for ns2", nil)
	}
}
예제 #23
0
func retrieveScans(database *mgo.Database) {
	data := []struct {
		URI                string
		ExpectedHTTPStatus int
		ContentCheck       func(*protocol.ScansResponse)
	}{
		{
			URI:                "/scans/?orderby=xxx:desc&pagesize=10&page=1",
			ExpectedHTTPStatus: http.StatusBadRequest,
		},
		{
			URI:                "/scans/?orderby=startedat:xxx&pagesize=10&page=1",
			ExpectedHTTPStatus: http.StatusBadRequest,
		},
		{
			URI:                "/scans/?orderby=startedat:desc&pagesize=xxx&page=1",
			ExpectedHTTPStatus: http.StatusBadRequest,
		},
		{
			URI:                "/scans/?orderby=startedat:desc&pagesize=10&page=xxx",
			ExpectedHTTPStatus: http.StatusBadRequest,
		},
		{
			URI:                "/scans/?orderby=startedat:desc&pagesize=10&page=1",
			ExpectedHTTPStatus: http.StatusOK,
			ContentCheck: func(scansResponse *protocol.ScansResponse) {
				if len(scansResponse.Scans) != 10 {
					utils.Fatalln("Error retrieving the wrong number of scans", nil)
				}
			},
		},
		{
			URI:                "/scans",
			ExpectedHTTPStatus: http.StatusOK,
			ContentCheck: func(scansResponse *protocol.ScansResponse) {
				if len(scansResponse.Scans) == 0 {
					utils.Fatalln("Error retrieving scans", nil)
				}

				if scansResponse.Scans[0].DomainsScanned > 0 {
					utils.Fatalln("Not compressing scans result", nil)
				}
			},
		},
		{
			URI:                "/scans?expand",
			ExpectedHTTPStatus: http.StatusOK,
			ContentCheck: func(scansResponse *protocol.ScansResponse) {
				if len(scansResponse.Scans) == 0 {
					utils.Fatalln("Error retrieving scans", nil)
				}

				if scansResponse.Scans[0].DomainsScanned == 0 {
					utils.Fatalln("Compressing scans result when it shouldn't", nil)
				}
			},
		},
		{
			URI:                "/scans?current",
			ExpectedHTTPStatus: http.StatusOK,
			ContentCheck: func(scansResponse *protocol.ScansResponse) {
				if len(scansResponse.Scans) == 0 {
					utils.Fatalln("Error retrieving current scan", nil)
				}

				if scansResponse.Scans[0].Status != "WAITINGEXECUTION" {
					utils.Fatalln("Current scan with wrong status", nil)
				}
			},
		},
	}

	mux := handy.NewHandy()

	h := new(handler.ScansHandler)
	mux.Handle("/scans", func() handy.Handler {
		return h
	})

	for _, item := range data {
		r, err := http.NewRequest("GET", item.URI, nil)
		if err != nil {
			utils.Fatalln("Error creating the HTTP request", err)
		}
		utils.BuildHTTPHeader(r, nil)

		w := httptest.NewRecorder()
		mux.ServeHTTP(w, r)

		responseContent, err := ioutil.ReadAll(w.Body)
		if err != nil {
			utils.Fatalln("Error reading response body", err)
		}

		if w.Code != item.ExpectedHTTPStatus {
			utils.Fatalln(fmt.Sprintf("Error when requesting scans using the URI [%s]. "+
				"Expected HTTP status code %d but got %d",
				item.URI, item.ExpectedHTTPStatus, w.Code), errors.New(string(responseContent)))
		}

		if item.ContentCheck != nil {
			item.ContentCheck(h.Response)
		}
	}
}
예제 #24
0
func domainLifeCycle() {
	data := []struct {
		method         string
		uri            string
		expectedStatus int
		content        string
		expectedBody   string
	}{
		{
			method:         "PUT",
			uri:            "/domain/example.com.br.",
			expectedStatus: http.StatusCreated,
			content: `{
  "Nameservers": [
    { "host": "ns1.example.com.br.", "ipv4": "127.0.0.1" },
    { "host": "ns2.example.com.br.", "ipv6": "::1" }
  ],
  "Owners": [
    { "email": "*****@*****.**", "language": "pt-br" }
  ]
}`,
		},
		{
			method:         "PUT",
			uri:            "/domain/example.com.br.",
			expectedStatus: http.StatusNoContent,
			content: `{
  "Nameservers": [
    { "host": "ns1.example.com.br.", "ipv4": "127.0.0.1" }
  ],
  "Owners": [
    { "email": "*****@*****.**", "language": "en-us" }
  ]
}`,
		},
		{
			method:         "GET",
			uri:            "/domain/example.com.br.",
			expectedStatus: http.StatusOK,
			expectedBody:   `{"fqdn":"example.com.br.","nameservers":[{"host":"ns1.example.com.br.","ipv4":"127.0.0.1","lastStatus":"NOTCHECKED","lastCheckAt":"0001-01-01T00:00:00Z","lastOKAt":"0001-01-01T00:00:00Z"}],"owners":[{"email":"*****@*****.**","language":"en-US"}],"links":[{"types":["self"],"href":"/domain/example.com.br."}]}`,
		},
		{
			method:         "DELETE",
			uri:            "/domain/example.com.br.",
			expectedStatus: http.StatusNoContent,
		},
	}

	var client http.Client

	url := ""
	if len(config.ShelterConfig.WebClient.Listeners) > 0 {
		url = fmt.Sprintf("http://%s:%d", config.ShelterConfig.WebClient.Listeners[0].IP,
			config.ShelterConfig.WebClient.Listeners[0].Port)
	}

	if len(url) == 0 {
		utils.Fatalln("There's no interface to connect to", nil)
	}

	for _, item := range data {
		var r *http.Request
		var err error

		if len(item.content) > 0 {
			r, err = http.NewRequest(item.method, fmt.Sprintf("%s%s", url, item.uri),
				bytes.NewReader([]byte(item.content)))

		} else {
			r, err = http.NewRequest(item.method, fmt.Sprintf("%s%s", url, item.uri), nil)
		}

		if err != nil {
			utils.Fatalln("Error creating the HTTP request", err)
		}

		utils.BuildHTTPHeader(r, []byte(item.content))

		response, err := client.Do(r)
		if err != nil {
			utils.Fatalln("Error sending request", err)
		}

		var responseContent []byte
		if response.ContentLength > 0 {
			responseContent, err = ioutil.ReadAll(response.Body)
			if err != nil {
				utils.Fatalln(fmt.Sprintf("Error reading response for method %s and URI %s",
					item.method, item.uri),
					err,
				)
			}
		}

		if response.StatusCode != item.expectedStatus {
			utils.Fatalln(fmt.Sprintf("Expected HTTP status %d and got %d for method %s and URI %s",
				item.expectedStatus, response.StatusCode, item.method, item.uri),
				errors.New(string(responseContent)),
			)
		} else if string(responseContent) != item.expectedBody {
			utils.Fatalln(fmt.Sprintf("Expected HTTP body [%s] and got [%s] for method %s and URI %s",
				item.expectedBody, string(responseContent), item.method, item.uri), nil)
		}
	}
}
예제 #25
0
func retrieveScan(startedAt time.Time) {
	var client http.Client

	url := ""
	if len(config.ShelterConfig.WebClient.Listeners) > 0 {
		url = fmt.Sprintf("http://%s:%d", config.ShelterConfig.WebClient.Listeners[0].IP,
			config.ShelterConfig.WebClient.Listeners[0].Port)
	}

	if len(url) == 0 {
		utils.Fatalln("There's no interface to connect to", nil)
	}

	uri := fmt.Sprintf("/scan/%s", startedAt.Format(time.RFC3339Nano))
	r, err := http.NewRequest("GET", fmt.Sprintf("%s%s", url, uri), nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}

	utils.BuildHTTPHeader(r, nil)

	response, err := client.Do(r)
	if err != nil {
		utils.Fatalln("Error sending request", err)
	}

	responseContent, err := ioutil.ReadAll(response.Body)
	if err != nil {
		utils.Fatalln("Error reading response content", err)
	}

	if response.StatusCode != http.StatusOK {
		utils.Fatalln(fmt.Sprintf("Expected HTTP status %d and got %d for method GET "+
			"and URI /scan/currentscan",
			http.StatusOK, response.StatusCode),
			errors.New(string(responseContent)),
		)
	}

	var scanResponse protocol.ScanResponse
	if err := json.Unmarshal(responseContent, &scanResponse); err != nil {
		utils.Fatalln("Error decoding scan response", err)
	}

	if scanResponse.Status != model.ScanStatusToString(model.ScanStatusExecuted) {
		utils.Fatalln("Invalid status returned by scan", nil)
	}

	// Tolerance of one second
	if scanResponse.StartedAt.Sub(startedAt) > (1*time.Second) ||
		scanResponse.StartedAt.Sub(startedAt) < (-1*time.Second) {
		utils.Fatalln("Invalid start date returned by scan", nil)
	}

	if scanResponse.DomainsScanned != 5000000 {
		utils.Fatalln("Invalid number of domains scanned returned by scan", nil)
	}

	if scanResponse.DomainsWithDNSSECScanned != 250000 {
		utils.Fatalln("Invalid number of domains with DNSSEC scanned returned by scan", nil)
	}

	if len(scanResponse.NameserverStatistics) != 2 {
		utils.Fatalln("Invalid nameserver statistics returned by scan", nil)
	}

	if len(scanResponse.DSStatistics) != 2 {
		utils.Fatalln("Invalid dsset statistics returned by scan", nil)
	}
}
func queryDomain() {
	dns.HandleFunc("example.com.br.", func(w dns.ResponseWriter, dnsRequestMessage *dns.Msg) {
		defer w.Close()

		dnsResponseMessage := &dns.Msg{
			MsgHdr:   dns.MsgHdr{},
			Question: dnsRequestMessage.Question,
			Answer: []dns.RR{
				&dns.NS{
					Hdr: dns.RR_Header{
						Name:   "example.com.br.",
						Rrtype: dns.TypeNS,
						Class:  dns.ClassINET,
						Ttl:    86400,
					},
					Ns: "a.dns.br.",
				},
				&dns.NS{
					Hdr: dns.RR_Header{
						Name:   "example.com.br.",
						Rrtype: dns.TypeNS,
						Class:  dns.ClassINET,
						Ttl:    86400,
					},
					Ns: "b.dns.br.",
				},
			},
		}

		dnsResponseMessage.SetReply(dnsRequestMessage)
		w.WriteMsg(dnsResponseMessage)
	})

	mux := handy.NewHandy()

	h := new(handler.DomainVerificationHandler)
	mux.Handle("/domain/{fqdn}/verification", func() handy.Handler {
		return h
	})

	r, err := http.NewRequest("GET", "/domain/example.com.br./verification", nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	responseContent, err := ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body", err)
	}

	if w.Code != http.StatusOK {
		utils.Fatalln(fmt.Sprintf("Error scanning domain. "+
			"Expected %d and got %d", http.StatusOK, w.Code),
			errors.New(string(responseContent)))
	}

	if len(h.Response.Nameservers) != 2 {
		utils.Fatalln("Wrong number of nameservers", nil)
	}
}
예제 #27
0
func retrieveScansWithPagination() {
	var client http.Client

	url := ""
	if len(config.ShelterConfig.WebClient.Listeners) > 0 {
		url = fmt.Sprintf("http://%s:%d", config.ShelterConfig.WebClient.Listeners[0].IP,
			config.ShelterConfig.WebClient.Listeners[0].Port)
	}

	if len(url) == 0 {
		utils.Fatalln("There's no interface to connect to", nil)
	}

	r, err := http.NewRequest("GET", fmt.Sprintf("%s%s", url, "/scans?page=5&pagesize=20"), nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}

	utils.BuildHTTPHeader(r, nil)

	response, err := client.Do(r)
	if err != nil {
		utils.Fatalln("Error sending request", err)
	}

	responseContent, err := ioutil.ReadAll(response.Body)
	if err != nil {
		utils.Fatalln("Error reading response content", err)
	}

	if response.StatusCode != http.StatusOK {
		utils.Fatalln(fmt.Sprintf("Expected HTTP status %d and got %d for method GET "+
			"and URI /scans",
			http.StatusOK, response.StatusCode),
			errors.New(string(responseContent)),
		)
	}

	var scansResponse protocol.ScansResponse
	if err := json.Unmarshal(responseContent, &scansResponse); err != nil {
		utils.Fatalln("Error decoding scan response", err)
	}

	if scansResponse.PageSize != 20 {
		utils.Fatalln("Default page size isn't 20", nil)
	}

	if scansResponse.Page != 5 {
		utils.Fatalln("Default page isn't the first one", nil)
	}

	if scansResponse.NumberOfItems != 100 {
		utils.Fatalln("Not calculating the correct number of scans", nil)
	}

	if scansResponse.NumberOfPages != 5 {
		utils.Fatalln("Not calculating the correct number of pages", nil)
	}

	if len(scansResponse.Scans) != scansResponse.PageSize {
		utils.Fatalln("Not returning all desired scans", nil)
	}
}
func scanDomain() {
	dns.HandleFunc("example.com.br.", func(w dns.ResponseWriter, dnsRequestMessage *dns.Msg) {
		defer w.Close()

		dnsResponseMessage := &dns.Msg{
			MsgHdr: dns.MsgHdr{
				Authoritative: true,
			},
			Question: dnsRequestMessage.Question,
			Answer: []dns.RR{
				&dns.SOA{
					Hdr: dns.RR_Header{
						Name:   "example.com.br.",
						Rrtype: dns.TypeSOA,
						Class:  dns.ClassINET,
						Ttl:    86400,
					},
					Ns:      "ns1.example.com.br.",
					Mbox:    "rafael.justo.net.br.",
					Serial:  2013112600,
					Refresh: 86400,
					Retry:   86400,
					Expire:  86400,
					Minttl:  900,
				},
			},
		}

		dnsResponseMessage.SetReply(dnsRequestMessage)
		w.WriteMsg(dnsResponseMessage)
	})

	var client http.Client

	url := ""
	if len(config.ShelterConfig.WebClient.Listeners) > 0 {
		url = fmt.Sprintf("http://%s:%d", config.ShelterConfig.WebClient.Listeners[0].IP,
			config.ShelterConfig.WebClient.Listeners[0].Port)
	}

	if len(url) == 0 {
		utils.Fatalln("There's no interface to connect to", nil)
	}

	content := `{
      "Nameservers": [
        { "Host": "ns1.example.com.br.", "ipv4": "127.0.0.1" },
        { "Host": "ns2.example.com.br.", "ipv4": "127.0.0.1" }
      ]
    }`

	r, err := http.NewRequest("PUT",
		fmt.Sprintf("%s%s", url, "/domain/example.com.br./verification"),
		strings.NewReader(content))

	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}

	utils.BuildHTTPHeader(r, []byte(content))

	response, err := client.Do(r)
	if err != nil {
		utils.Fatalln("Error sending request", err)
	}

	responseContent, err := ioutil.ReadAll(response.Body)
	if err != nil {
		utils.Fatalln("Error reading response content", err)
	}

	if response.StatusCode != http.StatusOK {
		utils.Fatalln("Error scanning domain", errors.New(string(responseContent)))
	}

	var domainResponse protocol.DomainResponse
	if err := json.Unmarshal(responseContent, &domainResponse); err != nil {
		utils.Fatalln("Error decoding domain response", err)
	}

	if len(domainResponse.Nameservers) != 2 {
		utils.Fatalln("Wrong number of nameservers", nil)
	}

	if domainResponse.Nameservers[0].LastStatus != model.NameserverStatusToString(model.NameserverStatusOK) {
		utils.Fatalln("Scan did not work for ns1", nil)
	}

	if domainResponse.Nameservers[1].LastStatus != model.NameserverStatusToString(model.NameserverStatusOK) {
		utils.Fatalln("Scan did not work for ns2", nil)
	}
}
func queryDomain() {
	dns.HandleFunc("example.com.br.", func(w dns.ResponseWriter, dnsRequestMessage *dns.Msg) {
		defer w.Close()

		if dnsRequestMessage.Question[0].Qtype == dns.TypeNS {
			dnsResponseMessage := &dns.Msg{
				MsgHdr: dns.MsgHdr{
					Authoritative: true,
				},
				Question: dnsRequestMessage.Question,
				Answer: []dns.RR{
					&dns.NS{
						Hdr: dns.RR_Header{
							Name:   "example.com.br.",
							Rrtype: dns.TypeNS,
							Class:  dns.ClassINET,
							Ttl:    86400,
						},
						Ns: "a.dns.br.",
					},
					&dns.NS{
						Hdr: dns.RR_Header{
							Name:   "example.com.br.",
							Rrtype: dns.TypeNS,
							Class:  dns.ClassINET,
							Ttl:    86400,
						},
						Ns: "b.dns.br.",
					},
				},
			}

			dnsResponseMessage.SetReply(dnsRequestMessage)
			w.WriteMsg(dnsResponseMessage)

		} else if dnsRequestMessage.Question[0].Qtype == dns.TypeDNSKEY {
			// Empty response
			dnsResponseMessage := &dns.Msg{
				MsgHdr: dns.MsgHdr{
					Authoritative: true,
				},
				Question: dnsRequestMessage.Question,
				Answer:   []dns.RR{},
			}

			dnsResponseMessage.SetReply(dnsRequestMessage)
			w.WriteMsg(dnsResponseMessage)
		}
	})

	var client http.Client

	url := ""
	if len(config.ShelterConfig.WebClient.Listeners) > 0 {
		url = fmt.Sprintf("http://%s:%d", config.ShelterConfig.WebClient.Listeners[0].IP,
			config.ShelterConfig.WebClient.Listeners[0].Port)
	}

	if len(url) == 0 {
		utils.Fatalln("There's no interface to connect to", nil)
	}

	r, err := http.NewRequest("GET",
		fmt.Sprintf("%s%s", url, "/domain/example.com.br./verification"), nil)

	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}

	utils.BuildHTTPHeader(r, nil)

	response, err := client.Do(r)
	if err != nil {
		utils.Fatalln("Error sending request", err)
	}

	responseContent, err := ioutil.ReadAll(response.Body)
	if err != nil {
		utils.Fatalln("Error reading response content", err)
	}

	if response.StatusCode != http.StatusOK {
		utils.Fatalln("Error querying domain", errors.New(string(responseContent)))
	}

	var domainResponse protocol.DomainResponse
	if err := json.Unmarshal(responseContent, &domainResponse); err != nil {
		utils.Fatalln("Error decoding domain response", err)
	}

	if len(domainResponse.Nameservers) != 2 {
		utils.Fatalln("Wrong number of nameservers", nil)
	}

	if len(domainResponse.DSSet) != 0 {
		utils.Fatalln("Wrong number of DS records", nil)
	}
}