awesome-patterns/playground/cbor/cbor3_test.go
Bruce 2c81132a50
CBOR
Signed-off-by: Bruce <weichou1229@gmail.com>
2019-04-10 22:08:31 +08:00

49 lines
931 B
Go

package cbor
import (
"bytes"
"fmt"
"testing"
"go.mozilla.org/cose"
)
func TestCborEncode3(t *testing.T) {
// Read origin file
b := new(bytes.Buffer)
err := getImageBytes("./on.png", b)
if err != nil {
fmt.Println(err)
return
}
origin := b.Bytes()
//image := &Image{Name:"on",Content:origin}
fmt.Println("Size: ", len(origin))
// Encode
fmt.Println("--------- Encode ---------")
encoded, error := cose.Marshal(origin)
if error != nil {
fmt.Errorf("Error decoding %s", error)
} else {
fmt.Println("Size: ", len(encoded))
//fmt.Println("Content: ",string(encoded))
}
// Decode
fmt.Println("--------- Decode ---------")
//var img Image
unmarshal, err := cose.Unmarshal(encoded)
if err != nil {
fmt.Printf("Error Unmarshal %s", err)
return
} else {
fmt.Println("Size: ", len(unmarshal.([]byte)))
//fmt.Println("Content: ",unmarshal.([]byte))
}
writeToFile("3", unmarshal.([]byte))
}