2018-08-31 11:05:49 +00:00
|
|
|
package securejson
|
|
|
|
import (
|
2018-09-03 10:32:18 +00:00
|
|
|
"errors"
|
2018-08-31 11:05:49 +00:00
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StubStorage struct {
|
|
|
|
jsonBytes []byte
|
|
|
|
}
|
|
|
|
|
2018-09-03 10:32:18 +00:00
|
|
|
func (obj *StubStorage) Put(key string, value []byte) (error) {
|
|
|
|
obj.jsonBytes = value
|
|
|
|
//fmt.Println("Put", string(obj.jsonBytes))
|
2018-08-31 11:05:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-03 10:32:18 +00:00
|
|
|
func (obj *StubStorage) Get(key string) ([]byte, error) {
|
|
|
|
if len(obj.jsonBytes)==0 {
|
|
|
|
return []byte{}, errors.New("Empty")
|
|
|
|
}
|
|
|
|
//fmt.Println("Get", string(obj.jsonBytes))
|
|
|
|
return obj.jsonBytes, nil
|
2018-08-31 11:05:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func ExampleNew() {
|
|
|
|
storage := new(StubStorage)
|
|
|
|
obj := New(storage)
|
|
|
|
jsonBytes, err := obj.GenerateJson("MyUser", "1234", "MyData")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-09-03 10:32:18 +00:00
|
|
|
ok, err := obj.VerifyJson(jsonBytes)
|
|
|
|
if err != nil || !ok {
|
|
|
|
fmt.Println("Verify the Generated Json Fail")
|
|
|
|
}
|
|
|
|
_, err = obj.GetJson(jsonBytes)
|
|
|
|
if err == nil {
|
|
|
|
fmt.Println("Expecting error when no value stored")
|
|
|
|
}
|
|
|
|
err = obj.PutJson(jsonBytes)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Put Json fail")
|
|
|
|
}
|
|
|
|
_, err = obj.GetJson(jsonBytes)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
fmt.Println("Get Json Fail")
|
|
|
|
}
|
|
|
|
ok, err = obj.VerifyJson(jsonBytes)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Verify Json Fail")
|
|
|
|
}
|
2018-08-31 11:05:49 +00:00
|
|
|
fmt.Println(ok)
|
|
|
|
//output:
|
|
|
|
//true
|
|
|
|
}
|