Skip to content

Refactor ota package #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add tests
  • Loading branch information
polldo committed Aug 9, 2022
commit 60474066427f18f6d8d20383488c9afd8a1a2ba7
51 changes: 49 additions & 2 deletions internal/ota/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package ota
import (
"bytes"
"encoding/hex"
"io/ioutil"
"log"

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

func TestEncoderWrite(t *testing.T) {

// Setup test data
data, _ := hex.DecodeString("DEADBEEF") // uncompressed, or 'ef 6b 77 de f0' (compressed w/ LZSS)

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

otaWriter := NewWriter(&w, vendorID, productID)
defer otaWriter.Close()
otaWriter.Close()

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

assert.Assert(t, res == 0) // 0 means equal
}

func TestEncoderWriteFiles(t *testing.T) {
tests := []struct {
name string
infile string
outfile string
}{
{
name: "blink",
infile: "testdata/blink.bin",
outfile: "testdata/blink.ota",
},
{
name: "cloud sketch",
infile: "testdata/cloud.bin",
outfile: "testdata/cloud.ota",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
input, err := ioutil.ReadFile(tt.infile)
if err != nil {
t.Fatal("couldn't open test file")
}

want, err := ioutil.ReadFile(tt.outfile)
if err != nil {
t.Fatal("couldn't open test file")
}

var got bytes.Buffer
vendorID := "2341" // Arduino
productID := "8057" // Nano 33 IoT
otaWriter := NewWriter(&got, vendorID, productID)
_, err = otaWriter.Write(input)
if err != nil {
t.Error(err)
}
otaWriter.Close()

if !bytes.Equal(want, got.Bytes()) {
t.Error("encoding failed")
}
})
}
}
Binary file added internal/ota/testdata/blink.bin
Binary file not shown.
Binary file added internal/ota/testdata/blink.ota
Binary file not shown.
Binary file added internal/ota/testdata/cloud.bin
Binary file not shown.
Binary file added internal/ota/testdata/cloud.ota
Binary file not shown.
Binary file removed internal/ota/testdata/lorem.lzss
Binary file not shown.
9 changes: 0 additions & 9 deletions internal/ota/testdata/lorem.txt

This file was deleted.