+ TS(
+ FLOW_ST_ZERO_BIT_SET,
+ "Set zero bit in operator to one (0x04)",
+ ((byte []) {
+ FLOW_TYPE_FRAGMENT, 0x84, 0x03,
+ })
+ ),
+ TS(
+ FLOW_ST_INVALID_TCP_FLAGS,
+ "Set zero bit in operand to one (TCP flags)",
+ ((byte []) {
+ FLOW_TYPE_TCP_FLAGS, 0x91, 0xff, 0x3a,
+ })
+ ),
+ TS(
+ FLOW_ST_INVALID_FRAGMENT,
+ "Set zero bit in operand to one (fragment)",
+ ((byte []) {
+ FLOW_TYPE_FRAGMENT, 0x81, 0xff,
+ })
+ ),
+ };
+#undef TS
+
+ for (uint tcase = 0; tcase < ARRAY_SIZE(tset); tcase++)
+ {
+ res = flow6_validate(tset[tcase].nlri, tset[tcase].size);
+ bt_assert_msg(res == tset[tcase].expect, "Assertion (%s == %s) %s", flow_validated_state_str(res), flow_validated_state_str(tset[tcase].expect), tset[tcase].description);
+ }
+
+ return 1;
+}
+
+static int
+t_decoding4(void)
+{
+ enum flow_validated_state res;
+
+ byte nlri1[] = {
+ FLOW_TYPE_DST_PREFIX, 24, 5, 6, 7,
+ FLOW_TYPE_SRC_PREFIX, 32, 10, 11, 12, 13,
+ FLOW_TYPE_IP_PROTOCOL, 0x81, 0x06,
+ FLOW_TYPE_PORT, 0x03, 0x89, 0x45, 0x8b, 0x91, 0x1f, 0x90,
+ FLOW_TYPE_TCP_FLAGS, 0x80, 0x55,
+ };
+
+ /* Empty NLRI */
+ res = flow4_decode(nlri1, 0);
+ bt_assert(res == FLOW_ST_VALID);
+
+ /* Valid / Not Complete testing */
+ uint valid_sizes[] = {5, 11, 14, 22, 25, 0};
+ uint valid_idx = 0;
+ for (uint size = 1; size <= sizeof(nlri1); size++)
+ {
+ res = flow4_decode(nlri1, size);
+ bt_debug("size %u, result: %s\n", size, flow_validated_state_str(res));
+ if (size == valid_sizes[valid_idx])
+ {
+ valid_idx++;
+ bt_assert(res == FLOW_ST_VALID);
+ }
+ else
+ {
+ bt_assert(res == FLOW_ST_NOT_COMPLETE);
+ }
+ }
+
+ /* Misc err tests */
+
+ struct tset {
+ enum flow_validated_state expect;
+ char *description;
+ u16 size;
+ byte *nlri;
+ byte *result;
+ };
+
+#define TS(type, msg, data, result) ((struct tset) {type, msg, sizeof(data), (data), (result)})
+ struct tset tset[] = {
+ TS(
+ FLOW_ST_EXCEED_MAX_PREFIX_LENGTH,
+ "33-length IPv4 prefix",
+ ((byte []) {
+ FLOW_TYPE_DST_PREFIX, 33, 5, 6, 7, 8, 9
+ }),
+ NULL
+ ),
+ TS(
+ FLOW_ST_EXCEED_MAX_VALUE_LENGTH,
+ "DSCP of length 2",
+ ((byte []) {
+ FLOW_TYPE_DSCP, 0x91, 00, 63,
+ }),
+ NULL
+ ),
+ TS(
+ FLOW_ST_BAD_TYPE_ORDER,
+ "Bad flowspec component type order",
+ ((byte []) {
+ FLOW_TYPE_SRC_PREFIX, 32, 10, 11, 12, 13,
+ FLOW_TYPE_DST_PREFIX, 24, 5, 6, 7,
+ }),
+ NULL
+ ),
+ TS(
+ FLOW_ST_BAD_TYPE_ORDER,
+ "Doubled destination prefix component",
+ ((byte []) {
+ FLOW_TYPE_DST_PREFIX, 24, 5, 6, 7,
+ FLOW_TYPE_DST_PREFIX, 24, 5, 6, 7,
+ }),
+ NULL
+ ),
+ TS(
+ FLOW_ST_UNKNOWN_COMPONENT,
+ "Unknown component of type number 14",
+ ((byte []) {
+ FLOW_TYPE_DST_PREFIX, 24, 5, 6, 7,
+ FLOW_TYPE_TCP_FLAGS, 0x80, 0x55,
+ 14 /*something new*/, 0x80, 0x55,
+ }),
+ NULL
+ ),
+ TS(
+ FLOW_ST_UNKNOWN_COMPONENT,
+ "Label component in IPv4",
+ ((byte []) {
+ FLOW_TYPE_DST_PREFIX, 24, 5, 6, 7,
+ FLOW_TYPE_LABEL, 0xa0, 0, 0, 0, 0x55,
+ }),
+ NULL
+ ),
+ TS(
+ FLOW_ST_VALID,
+ "Non-zero padding in prefix",
+ ((byte []) {
+ FLOW_TYPE_DST_PREFIX, 21, 5, 6, 0x7f,
+ FLOW_TYPE_SRC_PREFIX, 24, 5, 6, 0x7f,
+ }),
+ ((byte []) {
+ FLOW_TYPE_DST_PREFIX, 21, 5, 6, 0x78,
+ FLOW_TYPE_SRC_PREFIX, 24, 5, 6, 0x7f,
+ })
+ ),
+ TS(
+ FLOW_ST_VALID,
+ "The first numeric operator has set the AND bit",
+ ((byte []) {
+ FLOW_TYPE_PORT, 0x43, 0x89, 0x45, 0x8b, 0x91, 0x1f, 0x90,
+ }),
+ ((byte []) {
+ FLOW_TYPE_PORT, 0x03, 0x89, 0x45, 0x8b, 0x91, 0x1f, 0x90,
+ })
+ ),