Added some tests

This commit is contained in:
darmiel 2021-03-26 13:26:25 +01:00
parent 2adc702b4d
commit 48c3c540d6
7 changed files with 298 additions and 15 deletions

View file

@ -64,16 +64,20 @@ Usage:
Flags:
-b, --bind string Bind-Address (default ":1332")
-t, --default-ttl duration Default TTL (default 1m0s)
-e, --enable-encryption Enable Encryption (default true)
-h, --help help for serve
-x, --max-body-length int Max Body Length (default 1024)
-s, --max-ttl duration Max TTL (default 5m0s)
-l, --min-ttl duration Min TTL (default 5s)
--proxy-header string Proxy Header
-r, --redis-addr string Redis Address
--redis-db int Redis Database
--redis-pass string Redis Password
--redis-prefix string Redis Prefix (default "yaxc::")
--redis-prefix-hash string Redis Prefix (Hash) (default "yaxc::hash::")
--redis-prefix-value string Redis Prefix (Value) (default "yaxc::val::")
Global Flags:
--config string config file (default is $HOME/.yaxc.yaml)
--server string URL of API-Server
```

View file

@ -0,0 +1,38 @@
package common
import (
"log"
"testing"
)
func TestEncrypt(t *testing.T) {
enc, err := Encrypt("hello", "world")
if err != nil {
t.Error(err)
return
}
str := string(enc)
log.Println("Encrypted:", str)
if str == "hello" {
t.Errorf("encrypted string equal to input")
return
}
dec, err := Decrypt(str, "world")
if err != nil {
t.Error(err)
return
}
str = string(dec)
log.Println("Decrypted:", str)
AssertEqual(t, str, "hello")
}
func TestDecrypt(t *testing.T) {
strin := "Z6wyotQ5w/9dMvZnjMzGciP6p8+zvcOVr6tuKhGYRKbx"
decrypt, err := Decrypt(strin, "world")
if err != nil {
t.Error(err)
return
}
AssertEqual(t, string(decrypt), "hello")
}

View file

@ -2,11 +2,11 @@ package common
import (
"crypto/md5"
"fmt"
"encoding/hex"
)
func Hash(text string) string {
func MD5Hash(text string) string {
data := []byte(text)
sum := md5.Sum(data)
return fmt.Sprintf("%x", sum)
return hex.EncodeToString(sum[:])
}

View file

@ -0,0 +1,112 @@
package common
import "testing"
var md5Map = map[string]string{
"password": "5f4dcc3b5aa765d61d8327deb882cf99",
"123456": "e10adc3949ba59abbe56e057f20f883e",
"12345678": "25d55ad283aa400af464c76d713c07ad",
"1234": "81dc9bdb52d04dc20036dbd8313ed055",
"qwerty": "d8578edf8458ce06fbc5bb76a58c5ca4",
"12345": "827ccb0eea8a706c4c34a16891f84e7b",
"dragon": "8621ffdbc5698829397d97767ac13db3",
"pussy": "acc6f2779b808637d04c71e3d8360eeb",
"baseball": "276f8db0b86edaa7fc805516c852c889",
"football": "37b4e2d82900d5e94b8da524fbeb33c0",
"letmein": "0d107d09f5bbe40cade3de5c71e9e9b7",
"monkey": "d0763edaa9d9bd2a9516280e9044d885",
"696969": "7d0710824ff191f6a0086a7e3891641e",
"abc123": "e99a18c428cb38d5f260853678922e03",
"mustang": "bee783ee2974595487357e195ef38ca2",
"michael": "0acf4539a14b3aa27deeb4cbdf6e989f",
"shadow": "3bf1114a986ba87ed28fc1b5884fc2f8",
"master": "eb0a191797624dd3a48fa681d3061212",
"jennifer": "1660fe5c81c4ce64a2611494c439e1ba",
"111111": "96e79218965eb72c92a549dd5a330112",
"2000": "08f90c1a417155361a5c4b8d297e0d78",
"jordan": "d16d377af76c99d27093abc22244b342",
"superman": "84d961568a65073a3bcf0eb216b2a576",
"harley": "ef4cdd3117793b9fd593d7488409626d",
"1234567": "fcea920f7412b5da7be0cf42b8c93759",
"fuckme": "79cfdd0e92b120faadd7eb253eb800d0",
"hunter": "6b1b36cbb04b41490bfc0ab2bfa26f86",
"fuckyou": "596a96cc7bf9108cd896f33c44aedc8a",
"trustno1": "5fcfd41e547a12215b173ff47fdd3739",
"ranger": "ad92694923612da0600d7be498cc2e08",
"buster": "d9b23ebbf9b431d009a20df52e515db5",
"thomas": "ef6e65efc188e7dffd7335b646a85a21",
"tigger": "f78f2477e949bee2d12a2c540fb6084f",
"robert": "684c851af59965b680086b7b4896ff98",
"soccer": "da443a0ad979d5530df38ca1a74e4f80",
"fuck": "99754106633f94d350db34d548d6091a",
"batman": "ec0e2603172c73a8b644bb9456c1ff6e",
"test": "098f6bcd4621d373cade4e832627b4f6",
"pass": "1a1dc91c907325c69271ddf0c944bc72",
"killer": "b36d331451a61eb2d76860e00c347396",
"hockey": "df0349ce110b69f03b4def8012ae4970",
"george": "9b306ab04ef5e25f9fb89c998a6aedab",
"charlie": "bf779e0933a882808585d19455cd7937",
"andrew": "d914e3ecf6cc481114a3f534a5faf90b",
"michelle": "2345f10bb948c5665ef91f6773b3e455",
"love": "b5c0b187fe309af0f4d35982fd961d7e",
"sunshine": "0571749e2ac330a7455809c6b0e7af90",
"jessica": "aae039d6aa239cfc121357a825210fa3",
"asshole": "0b9a54438fba2dc0d39be8f7c6c71a58",
"6969": "7813d1590d28a7dd372ad54b5d29d033",
"pepper": "b3f952d5d9adea6f63bee9d4c6fceeaa",
"daniel": "aa47f8215c6f30a0dcdb2a36a9f4168e",
"access": "9df3b01c60df20d13843841ff0d4482c",
"123456789": "25f9e794323b453885f5181f1b624d0b",
"654321": "c33367701511b4f6020ec61ded352059",
"joshua": "d1133275ee2118be63a577af759fc052",
"maggie": "1d3d37667a8d7eb02054c6afdf9e2e1c",
"starwars": "5badcaf789d3d1d09794d8f021f40f0e",
"silver": "97f014516561ef487ec368d6158eb3f4",
"william": "fd820a2b4461bddd116c1518bc4b0f77",
"dallas": "09f8316e29649a7f795f414ba3860fc0",
"yankees": "1d10ca7f8fe2615bf72a249a7d34d6b9",
"123123": "4297f44b13955235245b2497399d7a93",
"ashley": "adff44c5102fca279fce7559abf66fee",
"666666": "f379eaf3c831b04de153469d1bec345e",
"hello": "5d41402abc4b2a76b9719d911017c592",
"amanda": "6209804952225ab3d14348307b5a4a27",
"orange": "fe01d67a002dfa0f3ac084298142eccd",
"biteme": "5ef64bad8f9d7e0c85f821580e4d6629",
"freedom": "d5aa1729c8c253e5d917a5264855eab8",
"computer": "df53ca268240ca76670c8566ee54568a",
"sexy": "348a448a51d1e0f0f5eee42337d12adc",
"thunder": "5c7686c0284e0875b26de99c1008e998",
"nicole": "fc63f87c08d505264caba37514cd0cfd",
"ginger": "6f4ec514eee84cc58c8e610a0c87d7a2",
"heather": "32d635528e3a388abb68b97a6eeedf35",
"hammer": "5f9901fc60b769b523d0dd8e79b3fe08",
"summer": "6b1628b016dff46e6fa35684be6acc96",
"corvette": "d1de459d7939792b14638cf06dcf2fca",
"taylor": "7d8bc5f1a8d3787d06ef11c97d4655df",
"fucker": "aac0a9daa4185875786c9ed154f0dece",
"austin": "229979fce5174c17d4645bf8752dae1e",
"1111": "b59c67bf196a4758191e42f76670ceba",
"merlin": "12b3638553c1f4a535a047e7003d9ac4",
"matthew": "e6a5ba0842a531163425d66839569a68",
"121212": "93279e3308bdbbeed946fc965017f67a",
"golfer": "d7bb20734ce5aa5d22c619eb9ba2e020",
"cheese": "fea0f1f6fede90bd0a925b4194deac11",
"princess": "8afa847f50a716e64932d995c8e7435a",
"martin": "925d7518fc597af0e43f5606f9a51512",
"chelsea": "91cb315a6405bfcc30e2c4571ccfb8ce",
"patrick": "6c84cbd30cf9350a990bad2bcc1bec5f",
"richard": "6ae199a93c381bf6d5de27491139d3f9",
"diamond": "75c6f03161d020201000414cd1501f9f",
"yellow": "d487dd0b55dfcacdd920ccbdaeafa351",
"bigdog": "1e0bb5b62610a9e76c3053bc2aceafd2",
"secret": "5ebe2294ecd0e0f08eab7690d2a6ee69",
"asdfgh": "a152e841783914146e4bcd4f39100686",
"sparky": "a269d17349ce29a77a89c7ec4ed0c3d4",
"cowboy": "609a5fb349c92aee01d41fae16d082cf",
}
func TestMD5Hash(t *testing.T) {
for k, v := range md5Map {
AssertEqual(t, MD5Hash(k), v)
}
}

View file

@ -0,0 +1,49 @@
package common
import (
"log"
"regexp"
)
const (
APMinLength = 3
APMaxLength = 128
)
var (
APAllowedChars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_+*!$%~@")
)
func ValidateAnywherePath(anywhere string) bool {
l := len(anywhere)
if l < APMinLength || l > APMaxLength {
return false
}
for _, c := range anywhere {
b := byte(c)
f := false
for _, a := range APAllowedChars {
if a == b {
f = true
break
}
}
if !f {
return false
}
}
return true
}
var p *regexp.Regexp
func init() {
var err error
if p, err = regexp.Compile(`^[A-Za-z0-9.\-_+*!$%~@]{3,128}$`); err != nil {
log.Fatalln(err)
}
}
func ValidateAnywherePathRegex(anywhere string) bool {
return p.MatchString(anywhere)
}

View file

@ -0,0 +1,80 @@
package common
import (
"log"
"reflect"
"strings"
"testing"
)
var valid = []string{
"hello", // lower case
"Hello", // Captial case
"HELLO", // UPPER CASE
strings.Repeat("A", 3), // Min Length
strings.Repeat("A", 128), // Max Length
"Hello!", // Special Chars
".-_+*!$%~@", // More Special Chars
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_+*!$%~@", // All valid chars
"0123456789", // numbers
}
var invalid = []string{
strings.Repeat("A", 2), // < Min Length
strings.Repeat("A", 129), // > Max Length
strings.Repeat("#", 6), // Special chars
}
func BenchmarkValidateAnywherePath(b *testing.B) {
log.Println("# Iter:", b.N, "*", len(valid)+len(invalid))
for n := 0; n < b.N; n++ {
for _, v := range valid {
ValidateAnywherePath(v)
}
for _, v := range invalid {
ValidateAnywherePath(v)
}
}
}
func BenchmarkValidateAnywherePathRegex(b *testing.B) {
log.Println("# Iter:", b.N, "*", len(valid)+len(invalid))
for n := 0; n < b.N; n++ {
for _, v := range valid {
ValidateAnywherePathRegex(v)
}
for _, v := range invalid {
ValidateAnywherePathRegex(v)
}
}
}
func TestValidateAnywherePath(t *testing.T) {
// check valid paths
for _, v := range valid {
AssertTrue(t, ValidateAnywherePath(v))
AssertTrue(t, ValidateAnywherePathRegex(v))
}
// check invalid paths
for _, i := range invalid {
AssertFalse(t, ValidateAnywherePath(i))
AssertFalse(t, ValidateAnywherePathRegex(i))
}
}
// AssertEqual checks if values are equal
func AssertEqual(t *testing.T, a interface{}, b interface{}) {
if a == b {
return
}
// debug.PrintStack()
t.Errorf("Received %v (type %v), expected %v (type %v)", a, reflect.TypeOf(a), b, reflect.TypeOf(b))
}
func AssertTrue(t *testing.T, a interface{}) {
AssertEqual(t, a, true)
}
func AssertFalse(t *testing.T, a interface{}) {
AssertEqual(t, a, false)
}

View file

@ -47,14 +47,14 @@ func (s *yAxCServer) handlePostAnywhere(ctx *fiber.Ctx) (err error) {
return ctx.Status(400).SendString("ERROR: TTL out of range")
}
hash := common.Hash(content)
hash := common.MD5Hash(content)
// Set contents
errVal := s.Backend.Set(path, content, ttl)
errHsh := s.Backend.SetHash(path, hash, ttl)
if errVal != nil || errHsh != nil {
log.Warning("ERROR saving Value / Hash:", errVal, errHsh)
log.Warning("ERROR saving Value / MD5Hash:", errVal, errHsh)
return ctx.Status(400).SendString(
fmt.Sprintf("ERROR (Val): %v\nERROR (Hsh): %v", errVal, errHsh))
}