aboutsummaryrefslogtreecommitdiff
path: root/ec_test.go
blob: b0874c63d78a852fc8d56257de960ac610d2dd97 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package ecookie

import (
	"encoding/json"
	"testing"
)

type test struct {
	Foo string
	Bar string
}

const pattr string = "ecookie: --- %v --"

const (
	foo = "foo string"
	bar = "bar string"
)

var key = []byte{
	0x11, 0x12, 0x13, 0x14,
	0x11, 0x12, 0x13, 0x14,
	0x11, 0x12, 0x13, 0x14,
	0x11, 0x12, 0x13, 0x14,
}

func TestEcookie(t *testing.T) {
	enc, err := NewEncryptor(key)
	if err != nil {
		t.Fatal(err)
	}

	tx := &test{Foo: foo, Bar: bar}
	bx := new(test)
	bt, err := json.Marshal(tx)
	if err != nil {
		t.Fatal(err)
	}

	ct, err := enc.Encrypt(bt)
	if err != nil {
		t.Fatal(err)
	}

	t.Logf(pattr, string(ct))

	dec, err := NewDecryptor(key)
	if err != nil {
		t.Fatal(err)
	}

	pt, err := dec.Decrypt(ct)
	if err != nil {
		t.Fatal(err)
	}

	if err := json.Unmarshal(pt, bx); err != nil {
		t.Fatal(err)
	}

	if !(*tx == *bx) {
		t.Error("error: tx != bx.. test failed")
	}
}

Snix LLC Git Repository Holder Copyright(C) 2022 All Rights Reserved Email To Snix.IR