Skip to content

Commit c9b9185

Browse files
committed
Add tests
1 parent a9c69ad commit c9b9185

File tree

7 files changed

+49
-11
lines changed

7 files changed

+49
-11
lines changed

internal/ota/encoder_test.go

+49-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package ota
2020
import (
2121
"bytes"
2222
"encoding/hex"
23+
"io/ioutil"
2324
"log"
2425

2526
"fmt"
@@ -38,7 +39,6 @@ func TestComputeCrc32Checksum(t *testing.T) {
3839
}
3940

4041
func TestEncoderWrite(t *testing.T) {
41-
4242
// Setup test data
4343
data, _ := hex.DecodeString("DEADBEEF") // uncompressed, or 'ef 6b 77 de f0' (compressed w/ LZSS)
4444

@@ -47,7 +47,7 @@ func TestEncoderWrite(t *testing.T) {
4747
productID := "8054" // MRK Wifi 1010
4848

4949
otaWriter := NewWriter(&w, vendorID, productID)
50-
defer otaWriter.Close()
50+
otaWriter.Close()
5151

5252
n, err := otaWriter.Write(data)
5353
if err != nil {
@@ -72,3 +72,50 @@ func TestEncoderWrite(t *testing.T) {
7272

7373
assert.Assert(t, res == 0) // 0 means equal
7474
}
75+
76+
func TestEncoderWriteFiles(t *testing.T) {
77+
tests := []struct {
78+
name string
79+
infile string
80+
outfile string
81+
}{
82+
{
83+
name: "blink",
84+
infile: "testdata/blink.bin",
85+
outfile: "testdata/blink.ota",
86+
},
87+
{
88+
name: "cloud sketch",
89+
infile: "testdata/cloud.bin",
90+
outfile: "testdata/cloud.ota",
91+
},
92+
}
93+
94+
for _, tt := range tests {
95+
t.Run(tt.name, func(t *testing.T) {
96+
input, err := ioutil.ReadFile(tt.infile)
97+
if err != nil {
98+
t.Fatal("couldn't open test file")
99+
}
100+
101+
want, err := ioutil.ReadFile(tt.outfile)
102+
if err != nil {
103+
t.Fatal("couldn't open test file")
104+
}
105+
106+
var got bytes.Buffer
107+
vendorID := "2341" // Arduino
108+
productID := "8057" // Nano 33 IoT
109+
otaWriter := NewWriter(&got, vendorID, productID)
110+
_, err = otaWriter.Write(input)
111+
if err != nil {
112+
t.Error(err)
113+
}
114+
otaWriter.Close()
115+
116+
if !bytes.Equal(want, got.Bytes()) {
117+
t.Error("encoding failed")
118+
}
119+
})
120+
}
121+
}

internal/ota/testdata/blink.bin

11.2 KB
Binary file not shown.

internal/ota/testdata/blink.ota

9.38 KB
Binary file not shown.

internal/ota/testdata/cloud.bin

90 KB
Binary file not shown.

internal/ota/testdata/cloud.ota

73.2 KB
Binary file not shown.

internal/ota/testdata/lorem.lzss

-1.7 KB
Binary file not shown.

internal/ota/testdata/lorem.txt

-9
This file was deleted.

0 commit comments

Comments
 (0)