github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/internal/ota/encoder_test.go (about) 1 // This file is part of arduino-cloud-cli. 2 // 3 // Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/) 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published 7 // by the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <https://www.gnu.org/licenses/>. 17 18 package ota 19 20 import ( 21 "bytes" 22 "encoding/hex" 23 "os" 24 25 "fmt" 26 "hash/crc32" 27 "testing" 28 29 "gotest.tools/assert" 30 ) 31 32 func TestComputeCrc32Checksum(t *testing.T) { 33 34 data, _ := hex.DecodeString("DEADBEEF") 35 crc := crc32.ChecksumIEEE(data) 36 37 assert.Equal(t, crc, uint32(2090640218)) 38 } 39 40 func TestEncode(t *testing.T) { 41 // Setup test data 42 data, _ := hex.DecodeString("DEADBEEF") // uncompressed, or 'ef 6b 77 de f0' (compressed w/ LZSS) 43 44 var w bytes.Buffer 45 vendorID := "2341" // Arduino 46 productID := "8054" // MRK Wifi 1010 47 48 enc := NewEncoder(&w, vendorID, productID) 49 50 err := enc.Encode(data) 51 if err != nil { 52 t.Error(err) 53 } 54 55 actual := w.Bytes() 56 57 // Expected result has been computed with the following tool: 58 // https://github.com/arduino-libraries/ArduinoIoTCloud/tree/master/extras/tools . 59 expected, _ := hex.DecodeString("11000000a1744bd4548041230000000000000040ef6b77def0") 60 61 res := bytes.Compare(expected, actual) 62 63 if res != 0 { 64 fmt.Println("expected:", hex.Dump(expected), len(expected), "bytes") 65 fmt.Println("actual:", hex.Dump(actual), len(actual), "bytes") 66 } 67 68 assert.Assert(t, res == 0) // 0 means equal 69 } 70 71 // Expected '.ota' files contained in testdata have been computed with the following tool: 72 // https://github.com/arduino-libraries/ArduinoIoTCloud/tree/master/extras/tools . 73 func TestEncodeFiles(t *testing.T) { 74 tests := []struct { 75 name string 76 infile string 77 outfile string 78 }{ 79 { 80 name: "blink", 81 infile: "testdata/blink.bin", 82 outfile: "testdata/blink.ota", 83 }, 84 { 85 name: "cloud sketch", 86 infile: "testdata/cloud.bin", 87 outfile: "testdata/cloud.ota", 88 }, 89 } 90 91 for _, tt := range tests { 92 t.Run(tt.name, func(t *testing.T) { 93 input, err := os.ReadFile(tt.infile) 94 if err != nil { 95 t.Fatal("couldn't open test file") 96 } 97 98 want, err := os.ReadFile(tt.outfile) 99 if err != nil { 100 t.Fatal("couldn't open test file") 101 } 102 103 var got bytes.Buffer 104 vendorID := "2341" // Arduino 105 productID := "8057" // Nano 33 IoT 106 otaenc := NewEncoder(&got, vendorID, productID) 107 err = otaenc.Encode(input) 108 if err != nil { 109 t.Error(err) 110 } 111 112 if !bytes.Equal(want, got.Bytes()) { 113 t.Error("encoding failed") 114 } 115 }) 116 } 117 }