Goit

Simple and lightweight Git web server
git clone http://git.omkov.net/Goit
Log | Tree | Refs | README | Download

Goit/src/goit/auth_test.go (69 lines, 1.6 KiB) -rw-r--r-- file download

c2c5250 Jakob Wakeling 2023-12-12 09:13:37
0
// Copyright (C) 2023, Jakob Wakeling
c2c5250 Jakob Wakeling 2023-12-12 09:13:37
1
// All rights reserved.
c2c5250 Jakob Wakeling 2023-12-12 09:13:37
2
410f65b Jakob Wakeling 2023-10-23 15:14:42
3
package goit_test
410f65b Jakob Wakeling 2023-10-23 15:14:42
4
410f65b Jakob Wakeling 2023-10-23 15:14:42
5
import (
410f65b Jakob Wakeling 2023-10-23 15:14:42
6
	"fmt"
410f65b Jakob Wakeling 2023-10-23 15:14:42
7
	"slices"
410f65b Jakob Wakeling 2023-10-23 15:14:42
8
	"sync"
410f65b Jakob Wakeling 2023-10-23 15:14:42
9
	"testing"
410f65b Jakob Wakeling 2023-10-23 15:14:42
10
	"time"
410f65b Jakob Wakeling 2023-10-23 15:14:42
11
945fd71 Jakob Wakeling 2023-11-05 15:39:31
12
	"github.com/Jamozed/Goit/src/goit"
410f65b Jakob Wakeling 2023-10-23 15:14:42
13
)
410f65b Jakob Wakeling 2023-10-23 15:14:42
14
410f65b Jakob Wakeling 2023-10-23 15:14:42
15
func TestNewSession(t *testing.T) {
410f65b Jakob Wakeling 2023-10-23 15:14:42
16
	goit.Sessions = map[int64][]goit.Session{}
410f65b Jakob Wakeling 2023-10-23 15:14:42
17
	goit.SessionsMutex = sync.RWMutex{}
410f65b Jakob Wakeling 2023-10-23 15:14:42
18
410f65b Jakob Wakeling 2023-10-23 15:14:42
19
	var uid int64 = 1
410f65b Jakob Wakeling 2023-10-23 15:14:42
20
	var session = goit.Session{Ip: "127.0.0.1", Expiry: time.Unix(0, 0)}
410f65b Jakob Wakeling 2023-10-23 15:14:42
21
b6c2a7b Jakob Wakeling 2024-03-15 21:11:22
22
	goit.Conf.IpSessions = true
410f65b Jakob Wakeling 2023-10-23 15:14:42
23
	s, err := goit.NewSession(uid, session.Ip, session.Expiry)
410f65b Jakob Wakeling 2023-10-23 15:14:42
24
	if err != nil {
410f65b Jakob Wakeling 2023-10-23 15:14:42
25
		t.Fatal(err.Error())
410f65b Jakob Wakeling 2023-10-23 15:14:42
26
	}
410f65b Jakob Wakeling 2023-10-23 15:14:42
27
410f65b Jakob Wakeling 2023-10-23 15:14:42
28
	if goit.Sessions[uid] == nil {
410f65b Jakob Wakeling 2023-10-23 15:14:42
29
		t.Fatal("UID slice not added to the sessions map")
410f65b Jakob Wakeling 2023-10-23 15:14:42
30
	}
410f65b Jakob Wakeling 2023-10-23 15:14:42
31
	if len(goit.Sessions[uid]) != 1 {
410f65b Jakob Wakeling 2023-10-23 15:14:42
32
		t.Fatal("Incorrect number of sessions added to the sessions map")
410f65b Jakob Wakeling 2023-10-23 15:14:42
33
	}
410f65b Jakob Wakeling 2023-10-23 15:14:42
34
	if s != goit.Sessions[uid][0] {
410f65b Jakob Wakeling 2023-10-23 15:14:42
35
		t.Fatal("Added and returned sessions do not match")
410f65b Jakob Wakeling 2023-10-23 15:14:42
36
	}
410f65b Jakob Wakeling 2023-10-23 15:14:42
37
	if s.Ip != session.Ip {
410f65b Jakob Wakeling 2023-10-23 15:14:42
38
		t.Fatal("Added session IP is incorrect")
410f65b Jakob Wakeling 2023-10-23 15:14:42
39
	}
410f65b Jakob Wakeling 2023-10-23 15:14:42
40
	if s.Expiry != session.Expiry {
410f65b Jakob Wakeling 2023-10-23 15:14:42
41
		t.Fatal("Added session expiry is incorrect")
410f65b Jakob Wakeling 2023-10-23 15:14:42
42
	}
410f65b Jakob Wakeling 2023-10-23 15:14:42
43
	if !s.Seen.Before(time.Now()) {
410f65b Jakob Wakeling 2023-10-23 15:14:42
44
		t.Fatal("Session seen time is in the future")
410f65b Jakob Wakeling 2023-10-23 15:14:42
45
	}
410f65b Jakob Wakeling 2023-10-23 15:14:42
46
	if len(s.Token) != 32 {
410f65b Jakob Wakeling 2023-10-23 15:14:42
47
		t.Fatal("Session token length is incorrect")
410f65b Jakob Wakeling 2023-10-23 15:14:42
48
	}
410f65b Jakob Wakeling 2023-10-23 15:14:42
49
	if goit.SessionsMutex.TryLock() == false {
410f65b Jakob Wakeling 2023-10-23 15:14:42
50
		t.Fatal("Sessions mutex was not unlocked")
410f65b Jakob Wakeling 2023-10-23 15:14:42
51
	}
410f65b Jakob Wakeling 2023-10-23 15:14:42
52
}
410f65b Jakob Wakeling 2023-10-23 15:14:42
53
410f65b Jakob Wakeling 2023-10-23 15:14:42
54
func TestHash(t *testing.T) {
410f65b Jakob Wakeling 2023-10-23 15:14:42
55
	var pass = "password"
410f65b Jakob Wakeling 2023-10-23 15:14:42
56
	var salt = make([]byte, 16)
410f65b Jakob Wakeling 2023-10-23 15:14:42
57
	var hash = []byte{
410f65b Jakob Wakeling 2023-10-23 15:14:42
58
		0x00, 0xB1, 0xEE, 0xD9, 0xBE, 0xE6, 0xDC, 0x06, 0x41, 0xA5, 0x07, 0x71,
410f65b Jakob Wakeling 2023-10-23 15:14:42
59
		0x7D, 0xB7, 0x6B, 0x65, 0x20, 0xEC, 0x87, 0x6E, 0xCE, 0x6C, 0xD1, 0x09,
410f65b Jakob Wakeling 2023-10-23 15:14:42
60
		0x25, 0xE4, 0x38, 0x75, 0xB5, 0x43, 0x57, 0x5E,
410f65b Jakob Wakeling 2023-10-23 15:14:42
61
	}
410f65b Jakob Wakeling 2023-10-23 15:14:42
62
410f65b Jakob Wakeling 2023-10-23 15:14:42
63
	if !slices.Equal(goit.Hash(pass, salt), hash) {
410f65b Jakob Wakeling 2023-10-23 15:14:42
64
		fmt.Printf("%x", goit.Hash(pass, salt))
410f65b Jakob Wakeling 2023-10-23 15:14:42
65
		t.Fatal("Hash output is incorrect")
410f65b Jakob Wakeling 2023-10-23 15:14:42
66
	}
410f65b Jakob Wakeling 2023-10-23 15:14:42
67
}
68