Skip to content
Snippets Groups Projects
Commit d15828e8 authored by Jono Wenger's avatar Jono Wenger
Browse files

Add tests for array.go

parent 4ec93938
No related branches found
No related tags found
1 merge request!6XX-4050 / Send E2E test
......@@ -33,12 +33,23 @@ func Uint8ArrayToBase64(_ js.Value, args []js.Value) interface{} {
// - Decoded uint8 array (Uint8Array).
// - Throws TypeError if decoding the string fails.
func Base64ToUint8Array(_ js.Value, args []js.Value) interface{} {
b, err := base64.StdEncoding.DecodeString(args[0].String())
b, err := base64ToUint8Array(args[0])
if err != nil {
Throw(TypeError, err)
}
return CopyBytesToJS(b)
return b
}
// base64ToUint8Array is a helper function that returns an error instead of
// throwing it.
func base64ToUint8Array(base64String js.Value) (js.Value, error) {
b, err := base64.StdEncoding.DecodeString(base64String.String())
if err != nil {
return js.Value{}, err
}
return CopyBytesToJS(b), nil
}
// Uint8ArrayEquals returns true if the two Uint8Array are equal and false
......
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2022 xx foundation //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file. //
////////////////////////////////////////////////////////////////////////////////
package utils
import (
"encoding/base64"
"fmt"
"strings"
"syscall/js"
"testing"
)
var testBytes = [][]byte{
nil,
{},
{0},
{0, 1, 2, 3},
{214, 108, 207, 78, 229, 11, 42, 219, 42, 87, 205, 104, 252, 73, 223,
229, 145, 209, 79, 111, 34, 96, 238, 127, 11, 105, 114, 62, 239,
130, 145, 82, 3},
}
// Tests that a series of Uint8Array Javascript objects are correctly converted
// to base 64 strings with Uint8ArrayToBase64.
func TestUint8ArrayToBase64(t *testing.T) {
for i, val := range testBytes {
// Create Uint8Array and set each element individually
jsBytes := Uint8Array.New(len(val))
for j, v := range val {
jsBytes.SetIndex(j, v)
}
jsB64 := Uint8ArrayToBase64(js.Value{}, []js.Value{jsBytes})
expected := base64.StdEncoding.EncodeToString(val)
if expected != jsB64 {
t.Errorf("Did not receive expected base64 encoded string (%d)."+
"\nexpected: %s\nreceived: %s", i, expected, jsB64)
}
}
}
// Tests that Base64ToUint8Array correctly decodes a series of base 64 encoded
// strings into Uint8Array.
func TestBase64ToUint8Array(t *testing.T) {
for i, val := range testBytes {
b64 := base64.StdEncoding.EncodeToString(val)
jsArr, err := base64ToUint8Array(js.ValueOf(b64))
if err != nil {
t.Errorf("Failed to convert js.Value to base 64: %+v", err)
}
// Generate the expected string to match the output of toString() on a
// Uint8Array
expected := strings.ReplaceAll(fmt.Sprintf("%d", val), " ", ",")[1:]
expected = expected[:len(expected)-1]
// Get the string value of the Uint8Array
jsString := jsArr.Call("toString").String()
if expected != jsString {
t.Errorf("Failed to recevie expected string representation of "+
"the Uint8Array (%d).\nexpected: %s\nreceived: %s",
i, expected, jsString)
}
}
}
// Tests that a base 64 encoded string decoded to Uint8Array via
// Base64ToUint8Array and back to a base 64 encoded string via
// Uint8ArrayToBase64 matches the original.
func TestBase64ToUint8ArrayUint8ArrayToBase64(t *testing.T) {
for i, val := range testBytes {
b64 := base64.StdEncoding.EncodeToString(val)
jsArr, err := base64ToUint8Array(js.ValueOf(b64))
if err != nil {
t.Errorf("Failed to convert js.Value to base 64: %+v", err)
}
jsB64 := Uint8ArrayToBase64(js.Value{}, []js.Value{jsArr})
if b64 != jsB64 {
t.Errorf("JSON from Uint8Array does not match original (%d)."+
"\nexpected: %s\nreceived: %s", i, b64, jsB64)
}
}
}
func TestUint8ArrayEquals(t *testing.T) {
for i, val := range testBytes {
// Create Uint8Array and set each element individually
jsBytesA := Uint8Array.New(len(val))
for j, v := range val {
jsBytesA.SetIndex(j, v)
}
jsBytesB := CopyBytesToJS(val)
if !Uint8ArrayEquals(js.Value{}, []js.Value{jsBytesA, jsBytesB}).(bool) {
t.Errorf("Two equal byte slices were found to be different (%d)."+
"\nexpected: %s\nreceived: %s", i,
jsBytesA.Call("toString").String(),
jsBytesB.Call("toString").String())
}
}
}
......@@ -23,10 +23,7 @@ import (
// Tests that CopyBytesToGo returns a byte slice that matches the Uint8Array.
func TestCopyBytesToGo(t *testing.T) {
testValues := [][]byte{{0, 1, 2, 3}, {0}, {}, nil}
for i, val := range testValues {
for i, val := range testBytes {
// Create Uint8Array and set each element individually
jsBytes := Uint8Array.New(len(val))
for j, v := range val {
......@@ -46,9 +43,7 @@ func TestCopyBytesToGo(t *testing.T) {
// Tests that CopyBytesToJS returns a Javascript Uint8Array with values matching
// the original byte slice.
func TestCopyBytesToJS(t *testing.T) {
testValues := [][]byte{{0, 1, 2, 3}, {0}, {}, nil}
for i, val := range testValues {
for i, val := range testBytes {
jsBytes := CopyBytesToJS(val)
// Generate the expected string to match the output of toString() on a
......@@ -70,9 +65,7 @@ func TestCopyBytesToJS(t *testing.T) {
// Tests that a byte slice converted to Javascript via CopyBytesToJS and
// converted back to Go via CopyBytesToGo matches the original.
func TestCopyBytesToJSCopyBytesToGo(t *testing.T) {
testValues := [][]byte{{0, 1, 2, 3}, {0}, {}, nil}
for i, val := range testValues {
for i, val := range testBytes {
jsBytes := CopyBytesToJS(val)
goBytes := CopyBytesToGo(jsBytes)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment