Compare commits

...

6 Commits
v1.1 ... main

Author SHA1 Message Date
7e057ed6c0
+ GetSharedElementsOfStringSlices 2024-06-30 22:50:11 +02:00
0b60553c59
+ IsValidEmail 2024-06-05 21:32:49 +02:00
98f8e21ad2
+GetUniqueElementsOfStringSlices 2024-06-04 21:41:28 +02:00
Andreas Schulte
47cbbb68e8
+ IsValidUUID 2023-11-03 20:43:13 +01:00
Andreas Schulte
da8c963b7a
+ RemoveLeadingStrings 2023-10-25 22:48:39 +02:00
Andreas Schulte
f5c734e417
+ filesystem; upated validators 2023-10-13 20:38:08 +02:00
8 changed files with 458 additions and 7 deletions

39
comparisons.go Normal file
View File

@ -0,0 +1,39 @@
package utils
// GetSharedElementsOfStringSlices finds the elements that are in both slices
func GetSharedElementsOfStringSlices(a, b []string) []string {
// Create a map to store the elements of "b"
mapOfElementsOfSliceB := make(map[string]bool)
for _, e := range b {
mapOfElementsOfSliceB[e] = true
}
// Iterate through "a" and find the elements that are not in "b"
var sharedElementsOfBothSlices []string
for _, e := range a {
if mapOfElementsOfSliceB[e] {
sharedElementsOfBothSlices = append(sharedElementsOfBothSlices, e)
}
}
return sharedElementsOfBothSlices
}
// GetUniqueElementsOfStringSlices finds the elements that are in the first slice but not in the second slice
func GetUniqueElementsOfStringSlices(a, b []string) []string {
// Create a map to store the elements of "b"
mapOfElementsOfSliceB := make(map[string]bool)
for _, e := range b {
mapOfElementsOfSliceB[e] = true
}
// Iterate through "a" and find the elements that are not in "b"
var uniqueElementsOfSliceA []string
for _, e := range a {
if !mapOfElementsOfSliceB[e] {
uniqueElementsOfSliceA = append(uniqueElementsOfSliceA, e)
}
}
return uniqueElementsOfSliceA
}

93
comparisons_test.go Normal file
View File

@ -0,0 +1,93 @@
package utils
import (
"reflect"
"testing"
)
func TestGetSharedElementsOfStringSlices(t *testing.T) {
data := []map[string][]string{
{
"a": []string{"apple", "banana", "cherry"},
"b": []string{"banana", "cherry", "date", "fig"},
"expectedResult": []string{"banana", "cherry"},
},
{
"a": []string{"banana", "cherry", "date", "fig"},
"b": []string{"apple", "banana", "cherry"},
"expectedResult": []string{"banana", "cherry"},
},
{
"a": []string{"apple", "banana", "cherry"},
"b": []string{"apple", "banana", "cherry"},
"expectedResult": []string{"apple", "banana", "cherry"},
},
{
"a": []string{},
"b": []string{"apple", "banana", "cherry"},
"expectedResult": []string{},
},
{
"a": []string{"apple", "banana", "cherry"},
"b": []string{},
"expectedResult": []string{},
},
{
"a": []string{},
"b": []string{},
"expectedResult": []string{},
},
}
for _, test := range data {
result := GetSharedElementsOfStringSlices(test["a"], test["b"])
if !reflect.DeepEqual(result, test["expectedResult"]) {
if len(result) == 0 && len(test["expectedResult"]) == 0 {
continue
}
t.Errorf("\ngot: %q\nwanted: %q\nfor: %q", result, test["expectedResult"], test)
}
}
}
func TestGetUniqueElementsOfStringSlices(t *testing.T) {
data := []map[string][]string{
{
"a": []string{"apple", "banana", "cherry"},
"b": []string{"banana", "cherry", "date", "fig"},
"expectedResult": []string{"apple"},
},
{
"a": []string{"banana", "cherry", "date", "fig"},
"b": []string{"apple", "banana", "cherry"},
"expectedResult": []string{"date", "fig"},
},
{
"a": []string{"apple", "banana", "cherry"},
"b": []string{"apple", "banana", "cherry"},
"expectedResult": []string{},
},
{
"a": []string{},
"b": []string{"apple", "banana", "cherry"},
"expectedResult": []string{},
},
{
"a": []string{"apple", "banana", "cherry"},
"b": []string{},
"expectedResult": []string{"apple", "banana", "cherry"},
},
}
for _, test := range data {
result := GetUniqueElementsOfStringSlices(test["a"], test["b"])
if !reflect.DeepEqual(result, test["expectedResult"]) {
if len(result) == 0 && len(test["expectedResult"]) == 0 {
continue
}
t.Errorf("\ngot: %q\nwanted: %q\nfor: %q", result, test["expectedResult"], test)
}
}
}

108
filesystem.go Normal file
View File

@ -0,0 +1,108 @@
package utils
import (
"errors"
"os"
"path/filepath"
)
// AppendLineToFile appends the line l to file
func AppendLineToFile(file string, l string) error {
f, err := os.OpenFile(file, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0660)
if err != nil {
return err
}
defer f.Close()
if _, err = f.WriteString(l + "\n"); err != nil {
return err
}
return nil
}
// CreateFolder creates a the folder f
func CreateFolder(f string) error {
err := os.Mkdir(f, 0700)
if err != nil {
return err
}
return nil
}
// GetAllFilesInFolderByExtension returns all files by extension of a given folder recursively
func GetAllFilesInFolderByExtension(pathToFolder string, extension string) []string {
files := []string{}
filepath.Walk(pathToFolder,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if filepath.Ext(path) != extension {
return nil
}
files = append(files, path)
return nil
})
return files
}
// GetPathToParrentFolderOfThisExecutable returns the path to the folder where this executable is located
func GetPathToParrentFolderOfThisExecutable() string {
return filepath.Dir(GetPathToThisExecutable())
}
// GetPathToThisExecutable returns the path where this executable is located
func GetPathToThisExecutable() string {
e, err := os.Executable()
if err != nil {
return "."
}
return e
}
// GetSizeOfFileInBytes returns the size of a file in bytes
func GetSizeOfFileInBytes(p string) (int64, error) {
if !DoesFileExist(p) {
return 0, errors.New("not a file or does not exist")
}
fileInfo, err := os.Stat(p)
if err != nil {
return 0, err
}
return fileInfo.Size(), nil
}
// LoadStringFromFile writes a string to a file
func LoadStringFromFile(pathOfFile string) (string, error) {
if !DoesFileExist(pathOfFile) {
return "", errors.New("file " + pathOfFile + "does not exist")
}
fileContent, err := os.ReadFile(pathOfFile)
if err != nil {
return "", errors.New("could not read file " + pathOfFile)
}
return string(fileContent), nil
}
// WriteStringToFile writes a string to a file
func WriteStringToFile(file string, s string) error {
err := os.WriteFile(file, []byte(s+"\n"), 0660)
if err != nil {
return err
}
return nil
}

21
filesystem_test.go Normal file
View File

@ -0,0 +1,21 @@
package utils
import (
"testing"
)
func TestGetSizeOfFileInBytes(t *testing.T) {
data := map[string]int64{
"/usr/bin/go": 11466992, // ls -lH /usr/bin/go | awk -F ' ' '{print $5}'
"/etc/fstab": 1049, // ls -lH /etc/fstab | awk -F ' ' '{print $5}'
"/etc/os-release": 371, // ls -lH /etc/os-release | awk -F ' ' '{print $5}'
"/etc": 0,
}
for d, expectedResult := range data {
result, _ := GetSizeOfFileInBytes(d)
if result != expectedResult {
t.Errorf("\ngot: %d\nwanted: %d\nfor: %q", result, expectedResult, d)
}
}
}

View File

@ -52,6 +52,22 @@ func RemoveDoubledStrings(s string, target string) string {
)
}
// RemoveLeadingStrings removes all leading occurrences of target in s
func RemoveLeadingStrings(s string, target string) string {
lenS := len(s)
lenTarget := len(target)
if lenS < lenTarget {
return s
}
if s[:lenTarget] != target {
return s
}
return RemoveLeadingStrings(s[lenTarget:], target)
}
// SanitizeStringWithWhitelist cleans s by all substrings which do not occur in whitelist
func SanitizeStringWithWhitelist(s string, whitelist map[string]string) string {
sanitizedString := ""

View File

@ -1,6 +1,8 @@
package utils
import "testing"
import (
"testing"
)
func TestFirstCharacterToUppercase(t *testing.T) {
data := map[string]string{
@ -56,6 +58,36 @@ func TestRemoveDoubledStrings(t *testing.T) {
}
}
func TestRemoveLeadingStrings(t *testing.T) {
data := map[string]string{
"": "",
".": "",
"...Jahr 2022... die überleben wollen...": "Jahr 2022... die überleben wollen...",
}
for d, expectedResult := range data {
result := RemoveLeadingStrings(d, ".")
if result != expectedResult {
t.Errorf("\ngot: %q\nwanted: %q\nfor: %q", result, expectedResult, d)
}
}
data = map[string]string{
"": "",
".": ".",
"..": "..",
"...": "",
"....": ".",
}
for d, expectedResult := range data {
result := RemoveLeadingStrings(d, "...")
if result != expectedResult {
t.Errorf("\ngot: %q\nwanted: %q\nfor: %q", result, expectedResult, d)
}
}
}
func TestSanitizeStringWithWhitelist(t *testing.T) {
whitelistedChars := []string{
".", "-",

View File

@ -1,6 +1,52 @@
package utils
import "net/url"
import (
"net/url"
"os"
"regexp"
"github.com/google/uuid"
)
// DoesFileExist returns true, if a file exists (and actually is a file)
func DoesFileExist(p string) bool {
s, err := os.Stat(p)
if os.IsNotExist(err) {
return false
}
if s.IsDir() {
return false
}
return true
}
// DoesFolderExist returns true, if a folder exists (and actually is a folder)
func DoesFolderExist(p string) bool {
s, err := os.Stat(p)
if os.IsNotExist(err) {
return false
}
if !s.IsDir() {
return false
}
return true
}
// DoesStringContainsNonWhitelistedSubstrings returns false if s contains substrings which are not in whitelist
func DoesStringContainsNonWhitelistedSubstrings(s string, whitelist map[string]string) bool {
for _, char := range s {
_, charIsWhitelisted := whitelist[string(char)]
if !charIsWhitelisted {
return true
}
}
return false
}
// IsStringInSliceOfStrings returns true, if slice contains target
func IsStringInSliceOfStrings(slice []string, target string) bool {
@ -13,8 +59,17 @@ func IsStringInSliceOfStrings(slice []string, target string) bool {
return false
}
// IsValidUrl returns true, if s is a valid URL
func IsValidUrl(s string) bool {
// IsValidEmail returns true, if s is a email address.
func IsValidEmail(s string) bool {
// Used Regex instead of mail.ParseAddress(), as the latter incorrectly recognizes samples such as “bad@domain” as a valid address
// Shoutout to emailregex.com for the almighty regular expression
return regexp.MustCompile(
`(?:[a-z0-9!#$%&'*+/=?^_{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])`,
).MatchString(s)
}
// IsValidURL returns true, if s is a valid URL
func IsValidURL(s string) bool {
u, err := url.Parse(s)
if err != nil || u.Scheme == "" || u.Host == "" {
return false
@ -22,3 +77,9 @@ func IsValidUrl(s string) bool {
return true
}
// IsValidUUID returns true, if s is a valid UUID
func IsValidUUID(s string) bool {
_, err := uuid.Parse(s)
return err == nil
}

View File

@ -1,6 +1,44 @@
package utils
import "testing"
import (
"testing"
)
func TestDoesFileExist(t *testing.T) {
data := map[string]bool{
"/usr/bin/go": true,
"/etc/fstab": true,
"/etc/os-release": true,
"/usr/bin": false,
"/home/derschmierigetypvomaldi": false,
"/tmp/plainTextPasswords.txt": false,
}
for d, expectedResult := range data {
result := DoesFileExist(d)
if result != expectedResult {
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d)
}
}
}
func TestDoesFolderExist(t *testing.T) {
data := map[string]bool{
"/home": true,
"/tmp": true,
"/usr/bin": true,
"/home/derschmierigetypvomaldi": false,
"/tmp/fdashfglkjdahjslkjfhjdsakljöhjf": false,
"/usr/bin/go": false,
}
for d, expectedResult := range data {
result := DoesFolderExist(d)
if result != expectedResult {
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d)
}
}
}
func TestIsStringInSliceOfStrings(t *testing.T) {
slice1 := []string{"apple", "banana", "cherry", "date"}
@ -36,7 +74,7 @@ func TestIsStringInSliceOfStrings(t *testing.T) {
}
}
func TestIsValidUrl(t *testing.T) {
func TestIsValidURL(t *testing.T) {
data := map[string]bool{
"": false,
"0x0001f346": false,
@ -47,7 +85,50 @@ func TestIsValidUrl(t *testing.T) {
}
for d, expectedResult := range data {
result := IsValidUrl(d)
result := IsValidURL(d)
if result != expectedResult {
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d)
}
}
}
func TestIsValidUUID(t *testing.T) {
data := map[string]bool{
"": false,
"0x0001f346": false,
"0179eb94-7a81-11ee-b962-0242ac120002": true,
"c3bcd438-7a80-11ee-b962-0242ac120002": true,
"0a245e0a-7a81-11ee-b962-0242ac120002": true,
"9d90b107-fc59-45f8-a622-f55b0a1396d6": true,
"f327c2b7-15a5-43c4-aefb-c66c6f34a32e": true,
"02cb18ec-5d6a-428d-8159-643bde3f43bb": true,
}
for d, expectedResult := range data {
result := IsValidUUID(d)
if result != expectedResult {
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d)
}
}
}
func TestIsValidEmail(t *testing.T) {
data := map[string]bool{
"test@example.com": true,
"another.test@domain.co.uk": true,
"username@domain.c": true,
"": false,
"invalid-email": false,
"bad@domain": false,
"@missingusername.com": false,
"plainaddress": false,
"@missingdomain": false,
"missingatsign.com": false,
"username@.com": false,
}
for d, expectedResult := range data {
result := IsValidEmail(d)
if result != expectedResult {
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d)
}