Add bambu-nfc dump parse, Key-A HKDF, and spool merge.
Parse filanfc-dump/v1 zip/JSON, Bambu 1K images, and merge stickers by tray UID. dump and mifare packages stay importable for a later common library.
This commit is contained in:
+101
@@ -0,0 +1,101 @@
|
||||
package bambunfc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"git.s1d3sw1ped.com/s1d3sw1ped/bambu-nfc/dump"
|
||||
)
|
||||
|
||||
func TestParseZipFixtures(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))
|
||||
}
|
||||
var translucent, basic int
|
||||
for _, tag := range tags {
|
||||
if tag.Brand != BrandBambuLab {
|
||||
t.Errorf("chip %s brand %q", tag.ChipUID, tag.Brand)
|
||||
}
|
||||
if tag.DiameterMM != 1.75 {
|
||||
t.Errorf("chip %s diameter %v", tag.ChipUID, tag.DiameterMM)
|
||||
}
|
||||
switch tag.Type {
|
||||
case "PETG Translucent":
|
||||
translucent++
|
||||
if tag.Color.Hex() != "#61B0FF80" {
|
||||
t.Errorf("translucent color %s", tag.Color.Hex())
|
||||
}
|
||||
case "PETG Basic":
|
||||
basic++
|
||||
if tag.Color.Hex() != "#000000FF" {
|
||||
t.Errorf("basic color %s", tag.Color.Hex())
|
||||
}
|
||||
default:
|
||||
t.Errorf("unexpected type %q", tag.Type)
|
||||
}
|
||||
}
|
||||
if translucent != 4 || basic != 2 {
|
||||
t.Errorf("translucent=%d basic=%d", translucent, basic)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseImageErrors(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) {
|
||||
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) {
|
||||
t.Errorf("blank image err = %v; want ErrNotBambu", 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user