<commit_before>/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#include <stddef.h>
#include <errno.h>
#include <string.h>
#include "testutil/testutil.h"
#include "nimble/hci_common.h"
#include "host/ble_hs_test.h"
#include "host/host_hci.h"
#include "ble_hs_test_util.h"

#define BLE_ADV_TEST_DATA_OFF   4

static void
ble_hs_adv_test_misc_verify_tx_adv_data_hdr(int data_len)
{
    uint16_t opcode;
    uint8_t *sptr;

    TEST_ASSERT(ble_hs_test_util_prev_hci_tx != NULL);

    sptr = ble_hs_test_util_prev_hci_tx;

    opcode = le16toh(sptr + 0);
    TEST_ASSERT(BLE_HCI_OGF(opcode) == BLE_HCI_OGF_LE);
    TEST_ASSERT(BLE_HCI_OCF(opcode) == BLE_HCI_OCF_LE_SET_ADV_DATA);

    TEST_ASSERT(sptr[2] == data_len + 1);
    TEST_ASSERT(sptr[3] == data_len);
}

static void
ble_hs_adv_test_misc_verify_tx_rsp_data_hdr(int data_len)
{
    uint16_t opcode;
    uint8_t *sptr;

    TEST_ASSERT(ble_hs_test_util_prev_hci_tx != NULL);

    sptr = ble_hs_test_util_prev_hci_tx;

    opcode = le16toh(sptr + 0);
    TEST_ASSERT(BLE_HCI_OGF(opcode) == BLE_HCI_OGF_LE);
    TEST_ASSERT(BLE_HCI_OCF(opcode) == BLE_HCI_OCF_LE_SET_SCAN_RSP_DATA);

    TEST_ASSERT(sptr[2] == data_len + 1);
    TEST_ASSERT(sptr[3] == data_len);
}

static void
ble_hs_adv_test_misc_verify_tx_field(int off, uint8_t type, uint8_t val_len,
                                     void *val)
{
    uint8_t *sptr;

    TEST_ASSERT_FATAL(ble_hs_test_util_prev_hci_tx != NULL);

    sptr = ble_hs_test_util_prev_hci_tx + off;

    TEST_ASSERT(sptr[0] == val_len + 1);
    TEST_ASSERT(sptr[1] == type);
    TEST_ASSERT(memcmp(sptr + 2, val, val_len) == 0);
}

struct ble_hs_adv_test_field {
    uint8_t type;       /* 0 indicates end of array. */
    uint8_t *val;
    uint8_t val_len;
};

static int
ble_hs_adv_test_misc_calc_data_len(struct ble_hs_adv_test_field *fields)
{
    struct ble_hs_adv_test_field *field;
    int len;

    len = 0;
    if (fields != NULL) {
        for (field = fields; field->type != 0; field++) {
            len += 2 + field->val_len;
        }
    }

    return len;
}

static void
ble_hs_adv_test_misc_verify_tx_fields(int off,
                                      struct ble_hs_adv_test_field *fields)
{
    struct ble_hs_adv_test_field *field;

    for (field = fields; field->type != 0; field++) {
        ble_hs_adv_test_misc_verify_tx_field(off, field->type, field->val_len,
                                          field->val);
        off += 2 + field->val_len;
    }
}

static void
ble_hs_adv_test_misc_verify_tx_adv_data(struct ble_hs_adv_test_field *fields)
{
    int data_len;

    data_len = ble_hs_adv_test_misc_calc_data_len(fields);
    ble_hs_adv_test_misc_verify_tx_adv_data_hdr(data_len);
    if (fields != NULL) {
        ble_hs_adv_test_misc_verify_tx_fields(BLE_ADV_TEST_DATA_OFF, fields);
    }
}

static void
ble_hs_adv_test_misc_verify_tx_rsp_data(struct ble_hs_adv_test_field *fields)
{
    int data_len;

    data_len = ble_hs_adv_test_misc_calc_data_len(fields);
    ble_hs_adv_test_misc_verify_tx_rsp_data_hdr(data_len);
    if (fields != NULL) {
        ble_hs_adv_test_misc_verify_tx_fields(BLE_ADV_TEST_DATA_OFF, fields);
    }
}<commit_msg>nimble host: Fix failing adv unit tests.<commit_after>/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#include <stddef.h>
#include <errno.h>
#include <string.h>
#include "testutil/testutil.h"
#include "nimble/hci_common.h"
#include "host/ble_hs_test.h"
#include "host/host_hci.h"
#include "ble_hs_test_util.h"

#define BLE_ADV_TEST_DATA_OFF   4

static void
ble_hs_adv_test_misc_verify_tx_adv_data_hdr(int data_len)
{
    uint16_t opcode;
    uint8_t *sptr;

    TEST_ASSERT(ble_hs_test_util_prev_hci_tx != NULL);

    sptr = ble_hs_test_util_prev_hci_tx;

    opcode = le16toh(sptr + 0);
    TEST_ASSERT(BLE_HCI_OGF(opcode) == BLE_HCI_OGF_LE);
    TEST_ASSERT(BLE_HCI_OCF(opcode) == BLE_HCI_OCF_LE_SET_ADV_DATA);

    TEST_ASSERT(sptr[2] == BLE_HCI_SET_ADV_DATA_LEN);
    TEST_ASSERT(sptr[3] == data_len);
}

static void
ble_hs_adv_test_misc_verify_tx_rsp_data_hdr(int data_len)
{
    uint16_t opcode;
    uint8_t *sptr;

    TEST_ASSERT(ble_hs_test_util_prev_hci_tx != NULL);

    sptr = ble_hs_test_util_prev_hci_tx;

    opcode = le16toh(sptr + 0);
    TEST_ASSERT(BLE_HCI_OGF(opcode) == BLE_HCI_OGF_LE);
    TEST_ASSERT(BLE_HCI_OCF(opcode) == BLE_HCI_OCF_LE_SET_SCAN_RSP_DATA);

    TEST_ASSERT(sptr[2] == BLE_HCI_SET_SCAN_RSP_DATA_LEN);
    TEST_ASSERT(sptr[3] == data_len);
}

static void
ble_hs_adv_test_misc_verify_tx_field(int off, uint8_t type, uint8_t val_len,
                                     void *val)
{
    uint8_t *sptr;

    TEST_ASSERT_FATAL(ble_hs_test_util_prev_hci_tx != NULL);

    sptr = ble_hs_test_util_prev_hci_tx + off;

    TEST_ASSERT(sptr[0] == val_len + 1);
    TEST_ASSERT(sptr[1] == type);
    TEST_ASSERT(memcmp(sptr + 2, val, val_len) == 0);
}

struct ble_hs_adv_test_field {
    uint8_t type;       /* 0 indicates end of array. */
    uint8_t *val;
    uint8_t val_len;
};

static int
ble_hs_adv_test_misc_calc_data_len(struct ble_hs_adv_test_field *fields)
{
    struct ble_hs_adv_test_field *field;
    int len;

    len = 0;
    if (fields != NULL) {
        for (field = fields; field->type != 0; field++) {
            len += 2 + field->val_len;
        }
    }

    return len;
}

static void
ble_hs_adv_test_misc_verify_tx_fields(int off,
                                      struct ble_hs_adv_test_field *fields)
{
    struct ble_hs_adv_test_field *field;

    for (field = fields; field->type != 0; field++) {
        ble_hs_adv_test_misc_verify_tx_field(off, field->type, field->val_len,
                                             field->val);
        off += 2 + field->val_len;
    }
}

static void
ble_hs_adv_test_misc_verify_tx_adv_data(struct ble_hs_adv_test_field *fields)
{
    int data_len;

    data_len = ble_hs_adv_test_misc_calc_data_len(fields);
    ble_hs_adv_test_misc_verify_tx_adv_data_hdr(data_len);
    if (fields != NULL) {
        ble_hs_adv_test_misc_verify_tx_fields(BLE_ADV_TEST_DATA_OFF, fields);
    }
}

static void
ble_hs_adv_test_misc_verify_tx_rsp_data(struct ble_hs_adv_test_field *fields)
{
    int data_len;

    data_len = ble_hs_adv_test_misc_calc_data_len(fields);
    ble_hs_adv_test_misc_verify_tx_rsp_data_hdr(data_len);
    if (fields != NULL) {
        ble_hs_adv_test_misc_verify_tx_fields(BLE_ADV_TEST_DATA_OFF, fields);
    }
}
{'fraction_non_alphanumeric': 0.0912422595728548, 'fraction_numerical': 0.00985719701756603, 'mean_word_length': 3.6967359050445103, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7234201', 'n_tokens_mistral': 3035, 'n_tokens_neox': 2954, 'n_words': 602}