<commit_before>package srslog

import (
	"crypto/tls"
	"crypto/x509"
	"fmt"
	"io/ioutil"
	"reflect"
	"regexp"
	"runtime"
	"testing"
)

// match checks if the given function (f) is the one inside the function pointer
// (name) which is read via runtime reflection. This is necessary because different
// platforms have a different reflection path. (IE on OS X it starts with "github.com..."
// but on Linux it has an absolute path, "/home/<username>/go/src/github.com...")
func match(f, name string) bool {
	re := fmt.Sprintf(".*github.com/RackSec/srslog\\.\\(Writer\\)\\.\\(.*github.com/RackSec/srslog.%v\\)-fm", f)
	matched, _ := regexp.MatchString(re, name)
	return matched
}

func TestGetDialer(t *testing.T) {
	w := Writer{
		priority: LOG_ERR,
		tag:      "tag",
		hostname: "",
		network:  "",
		raddr:    "",
	}

	dialer := w.getDialer()
	name := runtime.FuncForPC(reflect.ValueOf(dialer).Pointer()).Name()
	if !match("unixDialer", name) {
		t.Errorf("should get unixDialer, got: %v", name)
	}

	w.network = "tcp+tls"
	dialer = w.getDialer()
	name = runtime.FuncForPC(reflect.ValueOf(dialer).Pointer()).Name()
	if !match("tlsDialer", name) {
		t.Errorf("should get tlsDialer, got: %v", name)
	}

	w.network = "tcp"
	dialer = w.getDialer()
	name = runtime.FuncForPC(reflect.ValueOf(dialer).Pointer()).Name()
	if !match("basicDialer", name) {
		t.Errorf("should get basicDialer, got: %v", name)
	}

	w.network = "udp"
	dialer = w.getDialer()
	name = runtime.FuncForPC(reflect.ValueOf(dialer).Pointer()).Name()
	if !match("basicDialer", name) {
		t.Errorf("should get basicDialer, got: %v", name)
	}

	w.network = "something else entirely"
	dialer = w.getDialer()
	name = runtime.FuncForPC(reflect.ValueOf(dialer).Pointer()).Name()
	if !match("basicDialer", name) {
		t.Errorf("should get basicDialer, got: %v", name)
	}
}

func TestUnixDialer(t *testing.T) {
	w := Writer{
		priority: LOG_ERR,
		tag:      "tag",
		hostname: "",
		network:  "",
		raddr:    "",
	}

	_, hostname, err := w.unixDialer()

	if err != nil {
		t.Errorf("failed to dial: %v", err)
	}

	if hostname != "localhost" {
		t.Errorf("should set blank hostname")
	}

	w.hostname = "my other hostname"

	_, hostname, err = w.unixDialer()

	if err != nil {
		t.Errorf("failed to dial: %v", err)<commit_msg>Simplified function comparison, now without regexp!<commit_after>package srslog

import (
	"crypto/tls"
	"crypto/x509"
	"io/ioutil"
	"reflect"
	"testing"
)

// funcEquals checks if the two given functions are the same. This grabs the
// function pointer of each of them and compares them. This seems to be a viable
// way to work around the fact that Go does not have actual function equality.
func funcEquals(f1, f2 interface{}) bool {
	av := reflect.ValueOf(f1).Pointer()
	bv := reflect.ValueOf(f2).Pointer()

	return av == bv
}

func TestGetDialer(t *testing.T) {
	w := Writer{
		priority: LOG_ERR,
		tag:      "tag",
		hostname: "",
		network:  "",
		raddr:    "",
	}

	dialer := w.getDialer()
	if !funcEquals(w.unixDialer, dialer) {
		t.Errorf("should get unixDialer, got: %v", dialer)
	}

	w.network = "tcp+tls"
	dialer = w.getDialer()
	if !funcEquals(w.tlsDialer, dialer) {
		t.Errorf("should get tlsDialer, got: %v", dialer)
	}

	w.network = "tcp"
	dialer = w.getDialer()
	if !funcEquals(w.basicDialer, dialer) {
		t.Errorf("should get basicDialer, got: %v", dialer)
	}

	w.network = "udp"
	dialer = w.getDialer()
	if !funcEquals(w.basicDialer, dialer) {
		t.Errorf("should get basicDialer, got: %v", dialer)
	}

	w.network = "something else entirely"
	dialer = w.getDialer()
	if !funcEquals(w.basicDialer, dialer) {
		t.Errorf("should get basicDialer, got: %v", dialer)
	}
}

func TestUnixDialer(t *testing.T) {
	w := Writer{
		priority: LOG_ERR,
		tag:      "tag",
		hostname: "",
		network:  "",
		raddr:    "",
	}

	_, hostname, err := w.unixDialer()

	if err != nil {
		t.Errorf("failed to dial: %v", err)
	}

	if hostname != "localhost" {
		t.Errorf("should set blank hostname")
	}

	w.hostname = "my other hostname"

	_, hostname, err = w.unixDialer()

	if err != nil {
		t.Errorf("failed to dial: %v", err)
{'fraction_non_alphanumeric': 0.16686996828494755, 'fraction_numerical': 0.0024396194193705783, 'mean_word_length': 3.7072330654420207, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4842293', 'n_tokens_mistral': 1671, 'n_tokens_neox': 1601, 'n_words': 393}