generate: Import the wrong type test from the dynamic encoder/decoder

This commit is contained in:
Sasha Koshka 2025-08-22 06:10:17 -04:00
parent 4fd15c79a4
commit 2db7ff88c2

View File

@ -72,7 +72,7 @@ func init() {
}
func TestGenerateRunEncodeDecode(test *testing.T) {
testGenerateRun(test, &exampleProtocol, "decode", `
testGenerateRun(test, &exampleProtocol, "encode-decode", `
// imports
`, `
log.Println("MessageConnect")
@ -198,3 +198,172 @@ func TestGenerateRunEncodeDecode(test *testing.T) {
))
`)
}
func TestGenerateRunDecodeWrongType(test *testing.T) {
protocol := defaultProtocol()
protocol.Messages[0x0000] = Message {
Name: "Uint5",
Type: TypeInt { Bits: 5 },
}
protocol.Messages[0x0001] = Message {
Name: "Uint8",
Type: TypeInt { Bits: 8 },
}
protocol.Messages[0x0002] = Message {
Name: "Uint16",
Type: TypeInt { Bits: 16 },
}
protocol.Messages[0x0003] = Message {
Name: "Uint32",
Type: TypeInt { Bits: 32 },
}
protocol.Messages[0x0004] = Message {
Name: "Uint64",
Type: TypeInt { Bits: 64 },
}
protocol.Messages[0x0005] = Message {
Name: "Int8",
Type: TypeInt { Bits: 8 },
}
protocol.Messages[0x0006] = Message {
Name: "Int16",
Type: TypeInt { Bits: 16 },
}
protocol.Messages[0x0007] = Message {
Name: "Int32",
Type: TypeInt { Bits: 32 },
}
protocol.Messages[0x0008] = Message {
Name: "Int64",
Type: TypeInt { Bits: 64 },
}
protocol.Messages[0x0009] = Message {
Name: "String",
Type: TypeString { },
}
protocol.Messages[0x000A] = Message {
Name: "Buffer",
Type: TypeBuffer { },
}
protocol.Messages[0x000B] = Message {
Name: "StringArray",
Type: TypeArray { Element: TypeString { } },
}
protocol.Messages[0x000C] = Message {
Name: "Table",
Type: TypeTable { },
}
protocol.Messages[0x000C] = Message {
Name: "TableDefined",
Type: TypeTableDefined {
Fields: map[uint16] Field {
0x0000: Field { Name: "Name", Type: TypeString { } },
0x0001: Field { Name: "Password", Type: TypeString { } },
},
},
}
testGenerateRun(test, &protocol, "decode-wrong-type", `
// imports
`, `
datas := [][]byte {
/* int8 */ []byte { byte(tape.LSI.WithCN(0)), 0x45 },
/* int16 */ []byte { byte(tape.LSI.WithCN(1)), 0x45, 0x67 },
/* int32 */ []byte { byte(tape.LSI.WithCN(3)), 0x45, 0x67, 0x89, 0xAB },
/* int64 */ []byte { byte(tape.LSI.WithCN(7)), 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23 },
/* uint5 */ []byte { byte(tape.SI.WithCN(12)) },
/* uint8 */ []byte { byte(tape.LI.WithCN(0)), 0x45 },
/* uint16 */ []byte { byte(tape.LI.WithCN(1)), 0x45, 0x67 },
/* uint32 */ []byte { byte(tape.LI.WithCN(3)), 0x45, 0x67, 0x89, 0xAB },
/* uint64 */ []byte { byte(tape.LI.WithCN(7)), 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23 },
/* string */ []byte { byte(tape.SBA.WithCN(7)), 'p', 'u', 'p', 'e', 'v', 'e', 'r' },
/* []byte */ []byte { byte(tape.SBA.WithCN(5)), 'b', 'l', 'a', 'r', 'g' },
/* []string */ []byte {
byte(tape.OTA.WithCN(0)), 2, byte(tape.LBA.WithCN(0)),
0x08, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23,
0x05, 0x11, 0x11, 0x11, 0x11, 0x11,
},
/* map[uint16] any */ []byte {
byte(tape.KTV.WithCN(0)), 2,
0x02, 0x23, byte(tape.LSI.WithCN(1)), 0x45, 0x67,
0x02, 0x23, byte(tape.LI.WithCN(3)), 0x45, 0x67, 0x89, 0xAB,
},
}
for index, data := range datas {
log.Printf("data %2d %v [%s]", index, tape.Tag(data[0]), tu.HexBytes(data[1:]))
// integers should only assign to other integers
if index > 8 {
cas := func(destination Message) {
n, err := destination.Decode(tape.NewDecoder(bytes.NewBuffer(data)))
if err != nil { log.Fatalf("error: %v | n: %d", err, n) }
reflectValue := reflect.ValueOf(destination).Elem()
if reflectValue.CanInt() {
if reflectValue.Int() != 0 {
log.Fatalf(
"destination not zero: %v",
reflectValue.Elem().Interface())
}
} else {
if reflectValue.Uint() != 0 {
log.Fatalf(
"destination not zero: %v",
reflectValue.Elem().Interface())
}
}
if n != len(data) - 1 {
log.Fatalf("n not equal: %d != %d", n, len(data) - 1)
}
}
log.Println("- MessageInt8")
{ var dest MessageInt8; cas(&dest) }
log.Println("- MessageInt16")
{ var dest MessageInt16; cas(&dest) }
log.Println("- MessageInt32")
{ var dest MessageInt32; cas(&dest) }
log.Println("- MessageInt64")
{ var dest MessageInt64; cas(&dest) }
log.Println("- MessageUint8")
{ var dest MessageUint8; cas(&dest) }
log.Println("- MessageUint16")
{ var dest MessageUint16; cas(&dest) }
log.Println("- MessageUint32")
{ var dest MessageUint32; cas(&dest) }
log.Println("- MessageUint64")
{ var dest MessageUint64; cas(&dest) }
}
arrayCase := func(destination Message) {
n, err := destination.Decode(tape.NewDecoder(bytes.NewBuffer(data)),)
if err != nil { log.Fatalf("error: %v | n: %d", err, n) }
reflectDestination := reflect.ValueOf(destination)
reflectValue := reflectDestination.Elem()
if reflectValue.Len() != 0 {
log.Fatalf("len(destination) not zero: %v", reflectValue.Interface())
}
if n != len(data) - 1 {
log.Fatalf("n not equal: %d != %d", n, len(data) - 1)
}
}
// SBA/LBA types should only assign to other SBA/LBA types
if index != 9 && index != 10 {
log.Println("- MessageString")
{ var dest MessageString; arrayCase(&dest) }
log.Println("- MessageBuffer")
{ var dest MessageBuffer; arrayCase(&dest) }
}
// arrays should only assign to other arrays
if index != 11 {
log.Println("- MessageStringArray")
{ var dest MessageStringArray; arrayCase(&dest) }
}
// tables should only assign to other tables
if index != 12 {
log.Println("- MessageTable")
{ var dest = new(MessageTable); arrayCase(&dest) }
log.Println("- MessageTableDefined")
{ var dest MessageTableDefined; arrayCase(&dest) }
}
}
`)
}