190 lines
5.3 KiB
Go
190 lines
5.3 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"crypto/tls"
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"io/ioutil"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
"testing"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func newHTTPClient(timeout int, keyFile, certFile string) (*http.Client, error) {
|
||
|
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
tlsConfig := &tls.Config{
|
||
|
Certificates: []tls.Certificate{cert},
|
||
|
InsecureSkipVerify: true,
|
||
|
}
|
||
|
tlsConfig.BuildNameToCertificate()
|
||
|
transport := &http.Transport{TLSClientConfig: tlsConfig}
|
||
|
return &http.Client{
|
||
|
Timeout: time.Duration(time.Duration(timeout) * time.Second),
|
||
|
Transport: transport}, nil
|
||
|
}
|
||
|
|
||
|
func jsonPost(url string, data []byte, client *http.Client) ([]byte, error) {
|
||
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
req.Header.Set("Content-Type", "application/json")
|
||
|
resp, err := client.Do(req)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
defer resp.Body.Close()
|
||
|
return ioutil.ReadAll(resp.Body)
|
||
|
}
|
||
|
|
||
|
func deleteZone(url string, client *http.Client) error {
|
||
|
req, err := http.NewRequest("DELETE", url, nil)
|
||
|
if err != nil {
|
||
|
return nil
|
||
|
}
|
||
|
if _, err := client.Do(req); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func TestRecording(t *testing.T) {
|
||
|
// JSONRPCResponse is a jsonRPC response structure
|
||
|
type JSONRPCErrorOrResponse struct {
|
||
|
ID int `json:"id"`
|
||
|
JSONRPC string `json:"jsonrpc"`
|
||
|
Result []*JSONRPCResult `json:"result"`
|
||
|
Error struct {
|
||
|
Code int `json:"code"`
|
||
|
Message string `json:"message"`
|
||
|
} `json:"error"`
|
||
|
}
|
||
|
const (
|
||
|
configFile = "pdns-proxy-unit-test.conf"
|
||
|
commandsFile = "fixtures/replay/commands.asc"
|
||
|
replayFile = "fixtures/replay/record"
|
||
|
)
|
||
|
var orig, rerun []*JSONRPCErrorOrResponse
|
||
|
|
||
|
badClient, err := newHTTPClient(30, "fixtures/test/client-key.pem", "fixtures/test/badclient-cert.pem")
|
||
|
if err != nil {
|
||
|
t.Errorf("cannot create bad client : %s", err)
|
||
|
return
|
||
|
}
|
||
|
client, err := newHTTPClient(30, "fixtures/test/client-key.pem", "fixtures/test/client-cert.pem")
|
||
|
if err != nil {
|
||
|
t.Errorf("cannot create client : %s", err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
h, err := loadConfig(configFile)
|
||
|
if err != nil {
|
||
|
log.Println(err)
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
// disable nonce security
|
||
|
h.nonceGen = ""
|
||
|
go h.Run()
|
||
|
time.Sleep(3 * time.Second)
|
||
|
|
||
|
jsonCommands, err := ioutil.ReadFile(commandsFile)
|
||
|
// no command files, no need to continue
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
for _, zone := range []string{
|
||
|
"10.in-addr.arpa",
|
||
|
"2.0.192.in-addr.arpa",
|
||
|
"toto.example.com",
|
||
|
"example.com",
|
||
|
} {
|
||
|
url := fmt.Sprintf("https://127.0.0.1:8443/api/v1/servers/localhost/zones/%s", zone)
|
||
|
deleteZone(url, client)
|
||
|
}
|
||
|
|
||
|
result, err := jsonPost("https://127.0.0.1:8443/jsonrpc", []byte("{}"), badClient)
|
||
|
if err != nil {
|
||
|
t.Errorf("Issue with the server call : %s", err)
|
||
|
return
|
||
|
}
|
||
|
badClientResponse := &JSONRPCErrorOrResponse{}
|
||
|
if err := json.Unmarshal(result, badClientResponse); err != nil {
|
||
|
t.Errorf("cannot decode response : %s", err)
|
||
|
return
|
||
|
}
|
||
|
if badClientResponse.Error.Message != "Certificate for invalidserver is revoked" {
|
||
|
t.Errorf("CRL not working")
|
||
|
return
|
||
|
}
|
||
|
|
||
|
result, err = jsonPost("https://127.0.0.1:8443/jsonrpc", jsonCommands, client)
|
||
|
if err != nil {
|
||
|
t.Errorf("Issue with the server call : %s", err)
|
||
|
return
|
||
|
}
|
||
|
// get the structuve of the query
|
||
|
jsonRPC, _, _, _, err := h.jrpcDecodeQuery(jsonCommands)
|
||
|
|
||
|
recording, err := ioutil.ReadFile(replayFile)
|
||
|
// no command files, let's try to save the result for the next time
|
||
|
if err != nil {
|
||
|
if err := ioutil.WriteFile(replayFile, result, 0644); err != nil {
|
||
|
t.Errorf("cannot write replay file")
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
// let's compare the 2 runs
|
||
|
if err := json.Unmarshal(recording, &orig); err != nil {
|
||
|
t.Errorf("cannot read replay file : %s", err)
|
||
|
return
|
||
|
}
|
||
|
if err := json.Unmarshal(result, &rerun); err != nil {
|
||
|
t.Errorf("cannot read result file : %s", err)
|
||
|
return
|
||
|
}
|
||
|
last := len(orig) - 1
|
||
|
for i := range orig {
|
||
|
if orig[i].Error != rerun[i].Error {
|
||
|
t.Errorf("the command \"pdns %s\" (line %d) has a different error message\n>>>>>>\n%s\n<<<<<<<\n%s",
|
||
|
jsonRPC[i], i, rerun[i].Error.Message, orig[i].Error.Message)
|
||
|
return
|
||
|
}
|
||
|
if rerun[i].Error.Message != "" {
|
||
|
log.Printf("\"dmdns %s\" : %s\n", jsonRPC[i], rerun[i].Error.Message)
|
||
|
}
|
||
|
|
||
|
if len(orig[i].Result) != len(rerun[i].Result) {
|
||
|
t.Errorf("the command \"pdns %s\" (line %d) has a different result length", jsonRPC[i], i)
|
||
|
return
|
||
|
}
|
||
|
for j := range orig[i].Result {
|
||
|
if orig[i].Result[j].Comment != rerun[i].Result[j].Comment {
|
||
|
t.Errorf("the command \"pdns %s\" (line %d) has a different error message\n>>>>>>\n%s\n<<<<<<<\n%s",
|
||
|
jsonRPC[i], i, rerun[i].Result[j].Comment, orig[i].Result[j].Comment)
|
||
|
return
|
||
|
}
|
||
|
if orig[i].Result[j].Result != rerun[i].Result[j].Result {
|
||
|
t.Errorf("the command \"pdns %s\" (line %d) has a different result %d:\n>>>>>>\n%s\n<<<<<<<\n%s",
|
||
|
jsonRPC[i], i, j, rerun[i].Result[j].Result, orig[i].Result[j].Result)
|
||
|
return
|
||
|
}
|
||
|
if orig[i].Result[j].Changes != rerun[i].Result[j].Changes {
|
||
|
t.Errorf("the command \"pdns %s\" (line %d) has a different result %d:\n>>>>>>\n%s\n<<<<<<<\n%s",
|
||
|
jsonRPC[i], i, j, rerun[i].Result[j].Changes, orig[i].Result[j].Changes)
|
||
|
return
|
||
|
}
|
||
|
log.Printf("%s : %s\n", rerun[i].Result[j].Comment, orig[i].Result[j].Result)
|
||
|
if i == last {
|
||
|
log.Println("\n" + rerun[i].Result[j].Changes)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|