2018-08-31 11:05:49 +00:00
|
|
|
package securejson
|
2018-09-04 02:52:35 +00:00
|
|
|
|
2018-08-31 11:05:49 +00:00
|
|
|
import (
|
2018-09-04 02:52:35 +00:00
|
|
|
"encoding/json"
|
2018-09-03 10:32:18 +00:00
|
|
|
"errors"
|
2018-08-31 11:05:49 +00:00
|
|
|
"fmt"
|
2022-07-09 17:16:08 +00:00
|
|
|
"testing"
|
2018-08-31 11:05:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type StubStorage struct {
|
2018-09-04 02:52:35 +00:00
|
|
|
jsonBytes []byte
|
2018-08-31 11:05:49 +00:00
|
|
|
}
|
|
|
|
|
2018-09-04 02:52:35 +00:00
|
|
|
func (obj *StubStorage) Put(key string, value []byte) error {
|
|
|
|
obj.jsonBytes = value
|
2022-07-09 17:16:08 +00:00
|
|
|
fmt.Println("Put", string(obj.jsonBytes))
|
2018-08-31 11:05:49 +00:00
|
|
|
return nil
|
2018-09-04 02:52:35 +00:00
|
|
|
}
|
|
|
|
|
2018-09-03 10:32:18 +00:00
|
|
|
func (obj *StubStorage) Get(key string) ([]byte, error) {
|
2018-09-04 02:52:35 +00:00
|
|
|
if len(obj.jsonBytes) == 0 {
|
2018-09-03 10:32:18 +00:00
|
|
|
return []byte{}, errors.New("Empty")
|
|
|
|
}
|
2022-07-09 17:16:08 +00:00
|
|
|
fmt.Println("Get", string(obj.jsonBytes))
|
2018-09-04 02:52:35 +00:00
|
|
|
return obj.jsonBytes, nil
|
|
|
|
}
|
2018-08-31 11:05:49 +00:00
|
|
|
|
2022-07-09 17:16:08 +00:00
|
|
|
func testJson(jsonBytes []byte, logPrefix string, obj *SecureJson, t *testing.T) {
|
2018-09-06 09:38:47 +00:00
|
|
|
ok, err := obj.VerifyJson(jsonBytes)
|
|
|
|
if err != nil || !ok {
|
2022-07-09 17:16:08 +00:00
|
|
|
t.Errorf("%s: VerifyJson failed: %s", logPrefix, err)
|
2018-09-06 09:38:47 +00:00
|
|
|
}
|
|
|
|
var data Json
|
|
|
|
err = json.Unmarshal(jsonBytes, &data)
|
|
|
|
if err != nil {
|
2022-07-09 17:16:08 +00:00
|
|
|
t.Errorf("%s: Unmarshal failed: %s", logPrefix, err)
|
2018-09-06 09:38:47 +00:00
|
|
|
}
|
|
|
|
passwd, _ := obj.hash([]byte("1234"))
|
|
|
|
plain, err := obj.Decrypt(data.EncryptedData, data.UserName, passwd)
|
|
|
|
if err != nil {
|
2022-07-09 17:16:08 +00:00
|
|
|
t.Errorf("%s: Decrypt failed: %s", logPrefix, err)
|
2018-09-06 09:38:47 +00:00
|
|
|
}
|
2022-07-09 17:16:08 +00:00
|
|
|
if plain != "MyData" {
|
|
|
|
t.Errorf("%s: Decrypt failed: %s", logPrefix, err)
|
2018-09-06 09:38:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-09 17:16:08 +00:00
|
|
|
func TestNew(t *testing.T) {
|
2018-08-31 11:05:49 +00:00
|
|
|
storage := new(StubStorage)
|
2018-09-04 02:52:35 +00:00
|
|
|
obj := New(storage)
|
2018-09-06 09:38:47 +00:00
|
|
|
jsonBytes := []byte(`{"UserName":"MyUser","Signature":"MEUCIDJmafX+XGJV+Ws2jz0lF2YdJLcrEXAw1ZBPB0/+KjJyAiEA1CR3f/pbngSl0P0mqb7McKSbveSsQ1ir5L4ulpKamuw=","EncryptedData":"F4Zw1vYy","Timestamp":"W5D07g==","PublicKey":"BCNhwc+1nmUYLSDJnacQaKQB1YyT26gdwHCZZd1iwsB14rfGvwv9fuAHjyln9Alap2Voxp/rrdiU2QvE8HuMt5s="}`)
|
2022-07-09 17:16:08 +00:00
|
|
|
testJson(jsonBytes, "Hardcoded", obj, t)
|
2018-09-06 09:38:47 +00:00
|
|
|
|
2018-09-04 02:52:35 +00:00
|
|
|
jsonBytes, err := obj.GenerateJson("MyUser", "1234", "MyData")
|
2018-08-31 11:05:49 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2018-09-04 02:52:35 +00:00
|
|
|
}
|
2022-07-09 17:16:08 +00:00
|
|
|
testJson(jsonBytes, "Generated", obj, t)
|
|
|
|
|
2018-09-03 10:32:18 +00:00
|
|
|
_, err = obj.GetJson(jsonBytes)
|
|
|
|
if err == nil {
|
|
|
|
fmt.Println("Expecting error when no value stored")
|
|
|
|
}
|
2018-09-04 02:52:35 +00:00
|
|
|
err = obj.PutJson(jsonBytes)
|
2018-09-03 10:32:18 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Put Json fail")
|
|
|
|
}
|
|
|
|
_, err = obj.GetJson(jsonBytes)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Get Json Fail")
|
|
|
|
}
|
2022-07-09 17:16:08 +00:00
|
|
|
testJson(jsonBytes, "Get", obj, t)
|
2018-09-06 09:38:47 +00:00
|
|
|
fmt.Println(true)
|
2022-07-09 17:16:08 +00:00
|
|
|
// output:
|
|
|
|
// true
|
2018-09-04 02:52:35 +00:00
|
|
|
}
|