From 5fb88865d0e0b98ccac4257b6b0a1a4869a743fd Mon Sep 17 00:00:00 2001 From: Christian Ditaputratama Date: Wed, 6 Nov 2024 20:52:09 +0700 Subject: [PATCH] test: Added test for `validTorHostname` #149 --- internal/monero/monero_test.go | 46 ++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/internal/monero/monero_test.go b/internal/monero/monero_test.go index 544bd71..7974e75 100644 --- a/internal/monero/monero_test.go +++ b/internal/monero/monero_test.go @@ -131,6 +131,52 @@ func Benchmark_QueryNodes_toSQL(b *testing.B) { } } +// Single test: +// go test -race ./internal/monero -run=TestValidTorHostname -v +func TestValidTorHostname(t *testing.T) { + tests := []struct { + name string + host string + wantValid bool + }{ + { + name: "Empty host", + host: "", + wantValid: false, + }, + { + name: "Valid tor host", + host: "cakexmrl7bonq7ovjka5kuwuyd3f7qnkz6z6s6dmsy3uckwra7bvggyd.onion", + wantValid: true, + }, + { + name: "Valid tor host with subdomain", + host: "just-test.cakexmrl7bonq7ovjka5kuwuyd3f7qnkz6z6s6dmsy3uckwra7bvggyd.onion", + wantValid: true, + }, + { + name: "Invalid host", + host: "test.com", + wantValid: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if r := validTorHostname(tt.host); r != tt.wantValid { + t.Errorf("ValidTorHostname() error = %v, wantValid %v", r, tt.wantValid) + } + }) + } +} + +// Single bench test: +// go test ./internal/monero -bench validTorHostname -benchmem -run=^$ -v +func Benchmark_validTorHostname(b *testing.B) { + for i := 0; i < b.N; i++ { + _ = validTorHostname("cakexmrl7bonq7ovjka5kuwuyd3f7qnkz6z6s6dmsy3uckwra7bvggyd.onion") + } +} + // equalArgs is helper function for testing. // // This returns true if two slices of interface{} are equal.