Make Parse take raw NFC UID and 1K image.
Drop zip/JSON from the root API. dump remains an adapter for the Android app format; inventory and an AMS wand both call Parse(uid, image).
This commit is contained in:
+50
-55
@@ -1,33 +1,51 @@
|
||||
package bambunfc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"git.s1d3sw1ped.com/s1d3sw1ped/bambu-nfc/dump"
|
||||
)
|
||||
|
||||
func TestParseZipFixtures(t *testing.T) {
|
||||
func loadRaw(t *testing.T, uidHex string) (uid, image []byte) {
|
||||
t.Helper()
|
||||
var err error
|
||||
uid, err = hex.DecodeString(uidHex)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
image, err = os.ReadFile(filepath.Join("testdata", "dumps", uidHex+".bin"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return uid, image
|
||||
}
|
||||
|
||||
func parseFile(t *testing.T, uidHex string) Tag {
|
||||
t.Helper()
|
||||
uid, image := loadRaw(t, uidHex)
|
||||
tag, err := Parse(uid, image)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return tag
|
||||
}
|
||||
|
||||
func TestParseRawFixtures(t *testing.T) {
|
||||
t.Parallel()
|
||||
f, err := os.Open("testdata/dumps.zip")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer f.Close()
|
||||
tags, err := ParseZip(f)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(tags) != 6 {
|
||||
t.Fatalf("len(tags) = %d; want 6", len(tags))
|
||||
uids := []string{
|
||||
"e27be276", "52d60177", "92a80577", "b2d32177", "92b6fb31", "f28c58ed",
|
||||
}
|
||||
var translucent, basic int
|
||||
for _, tag := range tags {
|
||||
for _, id := range uids {
|
||||
tag := parseFile(t, id)
|
||||
if tag.Brand != BrandBambuLab {
|
||||
t.Errorf("chip %s brand %q", tag.ChipUID, tag.Brand)
|
||||
}
|
||||
if tag.ChipUID != id {
|
||||
t.Errorf("chip UID %q; want %q", tag.ChipUID, id)
|
||||
}
|
||||
if tag.DiameterMM != 1.75 {
|
||||
t.Errorf("chip %s diameter %v", tag.ChipUID, tag.DiameterMM)
|
||||
}
|
||||
@@ -49,53 +67,30 @@ func TestParseZipFixtures(t *testing.T) {
|
||||
if translucent != 4 || basic != 2 {
|
||||
t.Errorf("translucent=%d basic=%d", translucent, basic)
|
||||
}
|
||||
|
||||
a := parseFile(t, "e27be276")
|
||||
if a.TrayUID != "2fb8e0e972084e74bf737393b28e7b12" {
|
||||
t.Errorf("TrayUID = %s", a.TrayUID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseImageErrors(t *testing.T) {
|
||||
func TestParseErrors(t *testing.T) {
|
||||
t.Parallel()
|
||||
raw, err := os.ReadFile("testdata/dumps/e27be276.bin")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := ParseImage("e27be276", raw[:1023], nil); !errors.Is(err, ErrImageSize) {
|
||||
uid, image := loadRaw(t, "e27be276")
|
||||
if _, err := Parse(uid, image[:1023]); !errors.Is(err, ErrImageSize) {
|
||||
t.Errorf("short image err = %v; want ErrImageSize", err)
|
||||
}
|
||||
if _, err := ParseImage("e27be276", raw, []int{2}); !errors.Is(err, ErrIncomplete) {
|
||||
t.Errorf("fail sector 2 err = %v; want ErrIncomplete", err)
|
||||
}
|
||||
blank := make([]byte, 1024)
|
||||
if _, err := ParseImage("e27be276", blank, nil); !errors.Is(err, ErrNotBambu) {
|
||||
if _, err := Parse(uid, blank); !errors.Is(err, ErrNotBambu) {
|
||||
t.Errorf("blank image err = %v; want ErrNotBambu", err)
|
||||
}
|
||||
}
|
||||
if _, err := Parse(nil, image); !errors.Is(err, ErrUID) {
|
||||
t.Errorf("empty uid err = %v; want ErrUID", err)
|
||||
}
|
||||
|
||||
func TestParseDumpJSON(t *testing.T) {
|
||||
t.Parallel()
|
||||
f, err := os.Open("testdata/dumps/e27be276.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer f.Close()
|
||||
d, err := dump.ParseJSON(f)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tag, err := ParseDump(d)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if tag.TrayUID != "2fb8e0e972084e74bf737393b28e7b12" {
|
||||
t.Errorf("TrayUID = %s", tag.TrayUID)
|
||||
}
|
||||
if tag.ChipUID != "e27be276" {
|
||||
t.Errorf("ChipUID = %s", tag.ChipUID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseImageEmptyUID(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, err := ParseImage("", bytes.Repeat([]byte{1}, 1024), nil)
|
||||
if !errors.Is(err, ErrUID) {
|
||||
t.Errorf("err = %v; want ErrUID", err)
|
||||
noTray := append([]byte(nil), image...)
|
||||
copy(noTray[9*16:10*16], make([]byte, 16))
|
||||
if _, err := Parse(uid, noTray); !errors.Is(err, ErrIncomplete) {
|
||||
t.Errorf("zero tray err = %v; want ErrIncomplete", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user