2022-04-08 05:59:40 +02:00
|
|
|
package data
|
2022-01-09 06:59:28 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2022-04-12 08:27:47 +02:00
|
|
|
"time"
|
2022-04-08 05:59:40 +02:00
|
|
|
|
|
|
|
"github.com/ddworken/hishtory/shared"
|
2022-01-09 06:59:28 +01:00
|
|
|
)
|
|
|
|
|
2022-04-03 07:27:20 +02:00
|
|
|
func TestEncryptDecrypt(t *testing.T) {
|
2022-04-08 05:59:40 +02:00
|
|
|
k1 := EncryptionKey("key")
|
|
|
|
k2 := EncryptionKey("key")
|
2022-04-03 07:27:20 +02:00
|
|
|
if string(k1) != string(k2) {
|
|
|
|
t.Fatalf("Expected EncryptionKey to be deterministic!")
|
|
|
|
}
|
|
|
|
|
|
|
|
ciphertext, nonce, err := Encrypt("key", []byte("hello world!"), []byte("extra"))
|
2022-04-08 05:59:40 +02:00
|
|
|
shared.Check(t, err)
|
2022-04-03 07:27:20 +02:00
|
|
|
plaintext, err := Decrypt("key", ciphertext, []byte("extra"), nonce)
|
2022-04-08 05:59:40 +02:00
|
|
|
shared.Check(t, err)
|
2022-04-03 07:27:20 +02:00
|
|
|
if string(plaintext) != "hello world!" {
|
|
|
|
t.Fatalf("Expected decrypt(encrypt(x)) to work, but it didn't!")
|
|
|
|
}
|
|
|
|
}
|
2022-04-09 03:23:17 +02:00
|
|
|
|
|
|
|
func TestParseTimeGenerously(t *testing.T) {
|
|
|
|
ts, err := parseTimeGenerously("2006-01-02T15:04:00-08:00")
|
|
|
|
shared.Check(t, err)
|
|
|
|
if ts.Unix() != 1136243040 {
|
|
|
|
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
|
|
|
|
}
|
2022-09-08 07:53:48 +02:00
|
|
|
ts, err = parseTimeGenerously("2006-01-02 T15:04:00 -08:00")
|
|
|
|
shared.Check(t, err)
|
|
|
|
if ts.Unix() != 1136243040 {
|
|
|
|
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
|
|
|
|
}
|
|
|
|
ts, err = parseTimeGenerously("2006-01-02_T15:04:00_-08:00")
|
|
|
|
shared.Check(t, err)
|
|
|
|
if ts.Unix() != 1136243040 {
|
|
|
|
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
|
|
|
|
}
|
2022-04-12 07:56:23 +02:00
|
|
|
ts, err = parseTimeGenerously("2006-01-02T15:04:00")
|
|
|
|
shared.Check(t, err)
|
2022-04-12 08:27:47 +02:00
|
|
|
if ts.Year() != 2006 || ts.Month() != time.January || ts.Day() != 2 || ts.Hour() != 15 || ts.Minute() != 4 || ts.Second() != 0 {
|
2022-04-12 07:56:23 +02:00
|
|
|
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
|
|
|
|
}
|
2022-09-08 07:53:48 +02:00
|
|
|
ts, err = parseTimeGenerously("2006-01-02_T15:04:00")
|
|
|
|
shared.Check(t, err)
|
|
|
|
if ts.Year() != 2006 || ts.Month() != time.January || ts.Day() != 2 || ts.Hour() != 15 || ts.Minute() != 4 || ts.Second() != 0 {
|
|
|
|
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
|
|
|
|
}
|
|
|
|
ts, err = parseTimeGenerously("2006-01-02_15:04:00")
|
|
|
|
shared.Check(t, err)
|
|
|
|
if ts.Year() != 2006 || ts.Month() != time.January || ts.Day() != 2 || ts.Hour() != 15 || ts.Minute() != 4 || ts.Second() != 0 {
|
|
|
|
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
|
|
|
|
}
|
2022-04-12 07:56:23 +02:00
|
|
|
ts, err = parseTimeGenerously("2006-01-02T15:04")
|
|
|
|
shared.Check(t, err)
|
2022-04-12 08:30:24 +02:00
|
|
|
if ts.Year() != 2006 || ts.Month() != time.January || ts.Day() != 2 || ts.Hour() != 15 || ts.Minute() != 4 || ts.Second() != 0 {
|
2022-04-12 07:56:23 +02:00
|
|
|
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
|
2022-04-09 03:23:17 +02:00
|
|
|
}
|
2022-09-08 07:53:48 +02:00
|
|
|
ts, err = parseTimeGenerously("2006-01-02_15:04")
|
|
|
|
shared.Check(t, err)
|
|
|
|
if ts.Year() != 2006 || ts.Month() != time.January || ts.Day() != 2 || ts.Hour() != 15 || ts.Minute() != 4 || ts.Second() != 0 {
|
|
|
|
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
|
|
|
|
}
|
2022-04-09 03:23:17 +02:00
|
|
|
ts, err = parseTimeGenerously("2006-01-02")
|
|
|
|
shared.Check(t, err)
|
2022-04-12 08:30:24 +02:00
|
|
|
if ts.Year() != 2006 || ts.Month() != time.January || ts.Day() != 2 || ts.Hour() != 0 || ts.Minute() != 0 || ts.Second() != 0 {
|
2022-04-09 03:23:17 +02:00
|
|
|
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
|
|
|
|
}
|
|
|
|
}
|