-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
192 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,56 @@ | ||
package libknary | ||
|
||
import ( | ||
"github.com/miekg/dns" | ||
"net" | ||
"os" | ||
"strings" | ||
"sync" | ||
"testing" | ||
"time" | ||
) | ||
|
||
// code for 3 functions below here is taken and modified from | ||
// here: https://github.com/miekg/dns/blob/67373879ce327b5fd112d9301d0a4d62bad6b904/server_test.go | ||
func GokuServer(w dns.ResponseWriter, req *dns.Msg) { | ||
m := new(dns.Msg) | ||
m.SetReply(req) | ||
|
||
//no idea why m.Extra doesnt work but m.Answer does | ||
//m.Extra = make([]dns.RR, 1) | ||
m.Answer = make([]dns.RR, 1) | ||
m.Answer[0] = &dns.A{Hdr: dns.RR_Header{Name: m.Question[0].Name, Rrtype: dns.TypeA, Class: dns.ClassANY}, A: net.IPv4(127, 0, 0, 1)} | ||
w.WriteMsg(m) | ||
} | ||
"github.com/miekg/dns" | ||
) | ||
|
||
func RunLocalUDPServer(laddr string) (*dns.Server, string, error) { | ||
server, l, _, err := RunLocalUDPServerWithFinChan(laddr) | ||
func TestAcceptDNS(t *testing.T) { | ||
// Set up test environment | ||
os.Setenv("BIND_ADDR", "127.0.0.1") | ||
|
||
return server, l, err | ||
} | ||
// Skip test if user can't bind to port 53 | ||
server := &dns.Server{Addr: os.Getenv("BIND_ADDR") + ":53", Net: "udp"} | ||
err := server.ListenAndServe() | ||
|
||
func RunLocalUDPServerWithFinChan(laddr string, opts ...func(*dns.Server)) (*dns.Server, string, chan error, error) { | ||
pc, err := net.ListenPacket("udp", laddr) | ||
if err != nil { | ||
return nil, "", nil, err | ||
// If err contains "permission denied", skip the test | ||
if err != nil && strings.Contains(err.Error(), "permission denied") { | ||
t.Skip("Test requires root privileges") | ||
} | ||
server := &dns.Server{PacketConn: pc, ReadTimeout: time.Hour, WriteTimeout: time.Hour} | ||
|
||
waitLock := sync.Mutex{} | ||
waitLock.Lock() | ||
server.NotifyStartedFunc = waitLock.Unlock | ||
// Create a wait group to synchronize goroutines | ||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
|
||
// fin must be buffered so the goroutine below won't block | ||
// forever if fin is never read from. This always happens | ||
// in RunLocalUDPServer and can happen in TestShutdownUDP. | ||
fin := make(chan error, 1) | ||
// Start the DNS server in a goroutine | ||
go AcceptDNS(&wg) | ||
|
||
for _, opt := range opts { | ||
opt(server) | ||
// Wait for the DNS server to start | ||
time.Sleep(time.Second) | ||
|
||
// Send a DNS query to the server | ||
m := new(dns.Msg) | ||
m.SetQuestion("example.com.", dns.TypeA) | ||
c := new(dns.Client) | ||
_, _, err = c.Exchange(m, "127.0.0.1") | ||
if err != nil { | ||
t.Errorf("Failed to send DNS query: %v", err) | ||
} | ||
|
||
go func() { | ||
fin <- server.ActivateAndServe() | ||
pc.Close() | ||
}() | ||
// Wait for the DNS server to finish | ||
wg.Wait() | ||
} | ||
|
||
func TestInfoLog(t *testing.T) { | ||
ipaddr := "127.0.0.1" | ||
reverse := "example.com" | ||
name := "example" | ||
infoLog(ipaddr, reverse, name) | ||
|
||
waitLock.Lock() | ||
return server, pc.LocalAddr().String(), fin, nil | ||
// There are no assertions in this test at the moment | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package libknary | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestSendMsg(t *testing.T) { | ||
// Create a test server to capture HTTP requests | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// Verify the request URL and method | ||
switch r.URL.String() { | ||
case "/": | ||
if r.Method != http.MethodPost { | ||
t.Errorf("Expected POST request for Slack webhook, got %s", r.Method) | ||
} | ||
default: | ||
t.Errorf("Unexpected request to URL: %s", r.URL.String()) | ||
} | ||
})) | ||
|
||
defer server.Close() | ||
|
||
// Override the Slack webhook URL with the test server URL | ||
os.Setenv("SLACK_WEBHOOK", server.URL) | ||
|
||
// SLACK_WEBHOOK is set | ||
sendMsg("Test message for Slack") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters