Skip to content

Commit 0a78e71

Browse files
committed
Added unit tests
1 parent 468894a commit 0a78e71

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

command/ota/encode.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package ota
2020
import (
2121
"fmt"
2222
"os"
23+
"strings"
2324

2425
"github.com/arduino/arduino-cloud-cli/internal/ota"
2526
)
@@ -42,7 +43,12 @@ func Encode(params *EncodeParams) (*string, error) {
4243
return nil, fmt.Errorf("file %s contains a valid OTA header. Skip header encoding", params.File)
4344
}
4445

45-
otaFile := fmt.Sprintf("%s.ota", params.File)
46+
var otaFile string
47+
if strings.HasSuffix(params.File, ".bin") {
48+
otaFile = strings.Replace(params.File, ".bin", ".ota", 1)
49+
} else {
50+
otaFile = fmt.Sprintf("%s.ota", params.File)
51+
}
4652
_, err = os.Stat(otaFile)
4753
if err == nil {
4854
// file already exists, we need to delete it

internal/ota/decoder_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package ota
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestDecodeHeader(t *testing.T) {
10+
11+
header, err := DecodeOtaFirmwareHeader("testdata/cloud.ota")
12+
assert.Nil(t, err)
13+
assert.Equal(t, ArduinoVendorID, header.VID)
14+
assert.Equal(t, "8057", header.PID)
15+
assert.Equal(t, "arduino:samd:nano_33_iot", *header.FQBN)
16+
assert.Equal(t, ArduinoFqbnToPID["arduino:samd:nano_33_iot"], header.PID)
17+
18+
header, err = DecodeOtaFirmwareHeader("testdata/blink.ota")
19+
assert.Nil(t, err)
20+
assert.Equal(t, ArduinoVendorID, header.VID)
21+
assert.Equal(t, "8057", header.PID)
22+
assert.Equal(t, "arduino:samd:nano_33_iot", *header.FQBN)
23+
assert.Equal(t, ArduinoFqbnToPID["arduino:samd:nano_33_iot"], header.PID)
24+
25+
}
26+
27+
func TestDecodeWithNoHeader(t *testing.T) {
28+
29+
header, err := DecodeOtaFirmwareHeader("testdata/cloud.bin")
30+
assert.Nil(t, header)
31+
assert.NotNil(t, err)
32+
33+
}
34+
35+
func TestDecodeEsp32Header(t *testing.T) {
36+
37+
header, err := DecodeOtaFirmwareHeader("testdata/esp32.ota")
38+
assert.Nil(t, err)
39+
assert.Equal(t, Esp32MagicNumberPart1, header.VID)
40+
assert.Equal(t, Esp32MagicNumberPart2, header.PID)
41+
assert.Nil(t, header.FQBN)
42+
assert.Equal(t, "ESP32", header.BoardType)
43+
44+
}

internal/ota/encoder_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ package ota
2020
import (
2121
"bytes"
2222
"encoding/hex"
23-
"io/ioutil"
23+
"os"
2424

2525
"fmt"
2626
"hash/crc32"
@@ -90,12 +90,12 @@ func TestEncodeFiles(t *testing.T) {
9090

9191
for _, tt := range tests {
9292
t.Run(tt.name, func(t *testing.T) {
93-
input, err := ioutil.ReadFile(tt.infile)
93+
input, err := os.ReadFile(tt.infile)
9494
if err != nil {
9595
t.Fatal("couldn't open test file")
9696
}
9797

98-
want, err := ioutil.ReadFile(tt.outfile)
98+
want, err := os.ReadFile(tt.outfile)
9999
if err != nil {
100100
t.Fatal("couldn't open test file")
101101
}

internal/ota/testdata/esp32.ota

789 KB
Binary file not shown.

0 commit comments

Comments
 (0)