void
test_ofp_flow_handle_error_bad_match_length1(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  struct ofp_error expected_error;
  ofp_error_set(&expected_error, OFPET_BAD_MATCH, OFPBMC_BAD_LEN);

  ret = check_packet_parse_expect_error(ofp_multipart_request_handle_wrap,
                                        "04 12 00 40 00 00 00 10 00 01 00 00 00 00 00 00 "
                                        "01 00 00 00 00 00 00 02 00 00 00 03 00 00 00 00 "
                                        "00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 05 "
                                        "00 01 00 0b 80 00 00 04 00 00 00 10 00 00 00 00",
                                        /* <---------------------------------------------> ofp_match
                                         * <---> type = 1
                                         *       <---> length = 11 : bad length (12)
                                         *             <---------> OXM TLV header (oxm_class = 0x8000
                                         *                                          -> OFPXMC_OPENFLOW_BASIC
                                         *                                         oxm_field = 0
                                         *                                          -> OFPXMT_OFB_IN_PORT,
                                         *                                         oxm_hashmask = 0,
                                         *                                         oxm_length = 8)
                                         *                         <---------> OXML TLV payload ( value = 1)
                                         *                                     <---------> padding
                                         */
                                        &expected_error);
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OFP_ERROR, ret,
                            "bad match length error.");
}
void
test_group_mod_handle_add_invalid_type(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  struct ofp_error expected_error = {0, 0, {NULL}};
  ofp_error_set(&expected_error, OFPET_GROUP_MOD_FAILED, OFPGMFC_BAD_TYPE);
  ret = check_packet_parse_expect_error(
          ofp_group_mod_handle_wrap,
          "04 0f 00 80 00 00 00 10 00 00 fe 00 ff ff ff 00 "
          /* <----------------------------------------------... ofp_group_mod
           * <---------------------> ofp_header
           * <> version
           *    <> type
           *       <---> length = 8 * 16 bytes
           *             <---------> xid
           *                         <---> command ( 0 -> OFPGC_ADD
           *                               <> type ( fe -> unknown type
           *                                  <> padding
           *                                     <---------> group id = 0xffffff00
           */
          "00 30 00 02 00 00 00 03 00 00 00 04 00 00 00 00 "
          "00 00 00 10 00 00 00 0a 03 e8 00 00 00 00 00 00 "
          "00 00 00 10 00 00 00 14 07 d0 00 00 00 00 00 00 "
          "00 30 00 06 00 00 00 07 00 00 00 08 00 00 00 00 "
          "00 00 00 10 00 00 00 0a 03 e8 00 00 00 00 00 00 "
          "00 00 00 10 00 00 00 14 07 d0 00 00 00 00 00 00 "
          "00 10 00 02 00 00 00 03 00 00 00 04 00 00 00 00",
          &expected_error
        );
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OFP_ERROR, ret,
                            "unknown type shoud case error");
}
void
test_ofp_error_msg_handle_wrong_long_length(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  struct ofp_error expected_error = {0, 0, {NULL}};
  ofp_error_set(&expected_error, OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
  ret = check_packet_parse_expect_error(s_ofp_error_msg_handle_wrap,
                                        "04 01 00 0c 00 00",
                                        &expected_error);
  TEST_ASSERT_EQUAL_MESSAGE(ret, LAGOPUS_RESULT_OFP_ERROR,
                            "ofp_error_msg_handle(error) error.");
}
void
test_ofp_flow_handle_error_length2(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  struct ofp_error expected_error;
  ofp_error_set(&expected_error, OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
  /* invalid body */
  ret = check_packet_parse_expect_error(
          ofp_multipart_request_handle,
          "04 12 00 40 00 00 00 10 00 01 00 00 00 00 00 00",
          &expected_error);
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OFP_ERROR, ret, "invalid-body error");
}
void
test_ofp_features_handle_invalid_version_too_large(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  struct ofp_error expected_error = {0, 0, {NULL}};
  ofp_error_set(&expected_error, OFPET_BAD_REQUEST, OFPBRC_BAD_VERSION);
  ret = check_packet_parse_expect_error(
          ofp_features_request_handle,
          "05 05 00 08 00 00 00 10",
          &expected_error);
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OFP_ERROR, ret,
                            "ofp_features_request_handle(error) error.");
}
void
test_ofp_group_features_request_handle_error_invalid_length0(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  struct ofp_error expected_error;
  ofp_error_set(&expected_error, OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);

  ret = check_packet_parse_expect_error(ofp_multipart_request_handle_wrap,
                                        "04 12 00 10 00 00 00 10"
                                        "00 08 00 00 00 00 00",
                                        &expected_error);
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OFP_ERROR, ret,
                            "invalid-body error.");
}
void
test_ofp_get_config_handle_invalid_size_too_long(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  struct ofp_error expected_error = {0, 0, {NULL}};
  ofp_error_set(&expected_error, OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
  /* over size */
  ret = check_packet_parse_expect_error(
          s_ofp_get_config_request_handle_wrap,
          "04 07 00 10 00 00 00 10 00 80",
          &expected_error);
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OFP_ERROR, ret,
                            "over size error.");
}
void
test_ofp_echo_reply_handle_invalid_length_too_short(void) {
    lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
    struct ofp_error expected_error = {0, 0, {NULL}};
    ofp_error_set(&expected_error, OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
    ret = check_packet_parse_expect_error(s_ofp_echo_reply_handle_wrap,
                                          // packets says "My length is 8 bytes"
                                          // but the real length is 7 bytes.
                                          "04 02 00 08 00 00 00",
                                          &expected_error);
    TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OFP_ERROR, ret,
                              "ofp_echo_reply_handle(error) error.");
}
void
test_ofp_port_mod_handle_invalid_length1(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  struct ofp_error expected_error = {0, 0, {NULL}};
  /* no pad, hw_addr, pad2, config, mask, advertise, pad3 */
  ofp_error_set(&expected_error, OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
  ret = check_packet_parse_expect_error(
          ofp_port_mod_handle_wrap,
          "04 10 00 0c 00 00 00 10 00 00 00 01",
          &expected_error);
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OFP_ERROR, ret,
                            "invalid body error.");
}
void
test_ofp_queue_get_config_handle_invalid_length1(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  struct ofp_error expected_error;
  ofp_error_set(&expected_error, OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
  /* invalid body (no pad) */
  ret = check_packet_parse_expect_error(
          s_ofp_queue_get_config_request_handle_wrap,
          "04 16 00 0c 00 00 00 10 ff ff ff ff",
          &expected_error);
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OFP_ERROR, ret,
                            "invalid-body error");
}
void
test_ofp_port_mod_handle_invalid_length(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  struct ofp_error expected_error = {0, 0, {NULL}};
  /* header only */
  ofp_error_set(&expected_error, OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
  ret = check_packet_parse_expect_error(
          ofp_port_mod_handle_wrap,
          "04 10 00 08 00 00 00 10",
          &expected_error);
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OFP_ERROR, ret,
                            "no body error.");
}
void
test_ofp_table_mod_handle_wrap_invalid_length1(void) {
    lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
    struct ofp_error expected_error = {0, 0, {NULL}};
    ofp_error_set(&expected_error, OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
    /* no pad, config */
    ret = check_packet_parse_expect_error(
              ofp_table_mod_handle_wrap,
              "04 11 00 09 00 00 00 10 01",
              &expected_error);
    TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OFP_ERROR, ret,
                              "invalid body error.");
}
void
test_group_mod_handle_add_invalid_length4(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  struct ofp_error expected_error = {0, 0, {NULL}};
  ofp_error_set(&expected_error, OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
  /* no group_id, buckets */
  ret = check_packet_parse_expect_error(
          ofp_group_mod_handle_wrap,
          "04 0f 00 0c 00 00 00 10 00 00 00 00",
          &expected_error);
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OFP_ERROR, ret,
                            "invalid body error.");
}
void
test_ofp_get_async_request_handle_invalid_no_body(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  struct ofp_error expected_error = {0, 0, {NULL}};
  ofp_error_set(&expected_error, OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);

  /* Case of decode error.*/
  ret = check_packet_parse_expect_error(
          s_ofp_get_async_request_handle_wrap_error,
          "",
          &expected_error);
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OFP_ERROR, ret,
                            "ofp_get_async_request_handle(error) error.");
}
void
test_ofp_unsupported_handle(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  struct ofp_error expected_error = {0, 0, {NULL}};

  ofp_error_set(&expected_error, OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE);
  ret = check_packet_parse_expect_error(
          ofp_unsupported_handle_wrap,
          "04 06 00 20 00 00 00 65 00 00 00 00 00 00 0a bc "
          "00 00 ff ff 03 00 00 00 00 00 00 00 00 00 00 00",
          &expected_error);
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OFP_ERROR, ret,
                            "ofp_unsupported_handle(normal) error.");
}
void
test_ofp_port_mod_handle_invalid_length8(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  struct ofp_error expected_error = {0, 0, {NULL}};
  /* over size */
  ofp_error_set(&expected_error, OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
  ret = check_packet_parse_expect_error(
          ofp_port_mod_handle_wrap,
          "04 10 00 2c 00 00 00 10 00 00 00 01 00 00 00 00 "
          "ff ff ff ff ff ff 00 00 00 00 00 24 00 00 00 04 "
          "00 00 00 05 00 00 00 00 ff ff ff ff",
          &expected_error);
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OFP_ERROR, ret,
                            "invalid body error.");
}
void
test_ofp_set_config_handle_invalid_too_large_miss_send_len(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  struct ofp_error expected_error = {0, 0, {NULL}};
  ofp_error_set(&expected_error, OFPET_SWITCH_CONFIG_FAILED, OFPSCFC_BAD_LEN);
  ret = check_packet_parse_expect_error(
          s_ofp_set_config_request_handle_wrap,
          "04 09 00 0c 00 00 00 10"
          "00 01 ff e6",
          /*
           *   <---> flags 0xffff is undefined
           *         <---> miss_send_len > OFPCML_MAX
           */
          &expected_error);
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OFP_ERROR, ret,
                            "invalid miss send len.");
}
void
test_ofp_port_mod_handle_invalid_advertise(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  struct ofp_error expected_error = {0, 0, {NULL}};
  ofp_error_set(&expected_error, OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_ADVERTISE);
  ret = check_packet_parse_expect_error(
          ofp_port_mod_handle_wrap,
          "04 10 00 28 00 00 00 10 00 00 00 00 00 00 00 00 "
          "ff ff ff ff ff ff 00 00 00 00 00 24 00 00 00 24 "
          "80 00 00 00 00 00 00 00",
          /* ...-------------------> ofp_port_mod
           * <---------> advertise ( 1 << 31 is invalid ofp_port_features
           *             <--------> padding
           */
          &expected_error);
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OFP_ERROR, ret,
                            "invalid advertise should cause error");
}
void
test_ofp_set_config_handle_invalid_header_length(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  struct ofp_error expected_error = {0, 0, {NULL}};
  ofp_error_set(&expected_error, OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
  ret = check_packet_parse_expect_error(
          s_ofp_set_config_request_handle_wrap,
          "04 09 00 08 00 00 00 10"
          /* sizeof(ofp_switch_config) is 12, but length is 8 */
          "00 01 ff e4",
          /*
           *   <---> flags
           *         <---> miss_send_len
           */
          &expected_error);
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OFP_ERROR, ret,
                            "invalid length.");
}
void
test_ofp_role_request_handle_invalid_unknown_role(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  struct ofp_error expected_error = {0, 0, {NULL}};
  ofp_error_set(&expected_error, OFPET_ROLE_REQUEST_FAILED, OFPRRFC_BAD_ROLE);

  ret = check_packet_parse_expect_error(
          s_ofp_role_request_handle_wrap,
          "04 19 00 18 00 00 00 10 "
          "ff ff ff fe 00 00 00 00 "
          /*...-------------------... ofp_role_request
           * <---------> role = 0xfffffffe -> unknown role
           *             <---------> padding
           */
          "00 00 00 00 00 00 00 09 ",
          &expected_error);
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OFP_ERROR, ret,
                            "unknown rule should case error.");
}
void
test_ofp_get_async_handle_invalid_body_too_small(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  struct ofp_error expected_error = {0, 0, {NULL}};
  ofp_error_set(&expected_error, OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);

  ret = check_packet_parse_expect_error(
          s_ofp_get_async_request_handle_wrap,
          "04 1C 00 20 00 00 00 10"
          /* <--------------------... ofp_async_config
           * <--------------------->  ofp_header
           */
          "00 00 00 00 00 00 00 00"
          /* "00 00 00 00 00 00 00 00" too small ! */
          "00 00 00 00 00 00 00 00",
          &expected_error);
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OFP_ERROR, ret,
                            "get_async_handle error.");
}
void
test_ofp_queue_get_config_handle_invalid_too_large_port(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  struct ofp_error expected_error;
  ofp_error_set(&expected_error, OFPET_QUEUE_OP_FAILED, OFPQOFC_BAD_PORT);
  ret = check_packet_parse_expect_error(
          s_ofp_queue_get_config_request_handle_wrap,
          "04 16 00 10 00 00 00 10 ff ff ff 01 00 00 00 00",
          &expected_error);
  /* <---------------------> ofp_header
   * <> version
   *    <> type
   *       <---> length
   *             <---------> xid
   *                         <---------> port 0xffffff0x -> OFPP_MAX + 1
   *                                     <----------> padding
   */
  TEST_ASSERT_EQUAL_MESSAGE(
    LAGOPUS_RESULT_OFP_ERROR, ret,
    "too large port should case error.");
}
void
test_ofp_role_request_handle_invalid_stale_message(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  struct ofp_error expected_error = {0, 0, {NULL}};
  ofp_error_set(&expected_error, OFPET_ROLE_REQUEST_FAILED, OFPRRFC_STALE);

  ret = check_packet_parse_cont(
          s_ofp_role_request_handle_wrap,
          "04 19 00 18 00 00 00 10 "
          "00 00 00 03 00 00 00 00 "
          /*...-------------------... ofp_role_request
           * <---------> role = 3 -> OFPCR_ROLE_SLAVE
           *             <---------> padding
           */
          "00 00 00 00 00 00 00 09 ");
  /*...-------------------->  ofp_role_request
   * <---------------------> generation_id = 9
   */
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OK, ret,
                            "ofp_role_request_handle(normal) error.");

  ret = check_packet_parse_expect_error(
          s_ofp_role_request_handle_wrap,
          "04 19 00 18 00 00 00 10 "
          "00 00 00 03 00 00 00 00 "
          /*...-------------------... ofp_role_request
           * <---------> role = 3 -> OFPCR_ROLE_SLAVE
           *             <---------> padding
           */
          "00 00 00 00 00 00 00 01 ",
          &expected_error);
  /*...-------------------->  ofp_role_request
   * <---------------------> generation_id = 1
   */
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OFP_ERROR, ret,
                            "stale message should cause error");


}
void
test_ofp_port_mod_handle_invalid_config(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  struct ofp_error expected_error = {0, 0, {NULL}};
  ofp_error_set(&expected_error, OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_CONFIG);
  ret = check_packet_parse_expect_error(
          ofp_port_mod_handle_wrap,
          "04 10 00 28 00 00 00 10 00 00 00 00 00 00 00 00 "
          "ff ff ff ff ff ff 00 00 80 00 00 00 ff ff ff ff "
          /* ...-------------------------------------------... ofp_port_mod
           * <---------------> hw_addr
           *                   <---> padding
           *                         <---------> config (1 << 31 invalid ofp_port_config
           *                                     <---------> mask
           */
          "00 00 00 05 00 00 00 00",
          /* ...-------------------> ofp_port_mod
           * <---------> advertise ( OFPPF_10MB_HD | OFPPF_100MB_HD)
           *             <--------> padding
           */
          &expected_error);
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OFP_ERROR, ret,
                            "invalid config should cause error");
}
void
test_ofp_table_mod_handle_bad_config(void) {
    lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
    struct ofp_error expected_error;
    ofp_error_set(&expected_error, OFPET_TABLE_MOD_FAILED, OFPTMFC_BAD_CONFIG);

    ret = check_packet_parse_expect_error(
              ofp_table_mod_handle_wrap,
              "04 11 00 10 00 00 00 10 01 00 00 00 00 00 00 04",
              /*
               * <---------------------> ofp_header
               * <> version
               *    <> type
               *       <---> length = 16 bytes
               *             <---------> xid
               *                         <> table_id
               *                            <------> padding
               *                                     <----------> config
               *                                                   : 4 (bad config)
               */
              &expected_error);
    TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OFP_ERROR, ret,
                              "ofp_table_mod_handle(bad config) error.");
}
lagopus_result_t
check_packet_parse(ofp_handler_proc_t handler_proc,
                   const char packet[]) {
  return check_packet_parse_expect_error(handler_proc, packet, NULL);
}