Home | History | Annotate | Line # | Download | only in test
test-packet.c revision 1.1.1.1
      1 /* test-packet.c
      2  *
      3  * Copyright (c) 2023 Apple Inc. All rights reserved.
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *     https://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  *
     17  * This file contains tools for generating SRP update packets that can be fed directly into the
     18  * parser (srp-parse.c) rather than involving connections, using the pathway that's used by SRP
     19  * replication. This is useful for unit tests generally, and particularly for testing the multi-packet
     20  * functionality used by SRP replication.
     21  */
     22 
     23 #include <dns_sd.h>
     24 #include "srp.h"
     25 #include "srp-test-runner.h"
     26 #include "srp-api.h"
     27 #include "dns-msg.h"
     28 #include "ioloop.h"
     29 #include "srp-proxy.h"
     30 #include "srp-dnssd.h"
     31 #include "srp-mdns-proxy.h"
     32 #include "test-api.h"
     33 #include "srp-replication.h"
     34 #include "test-packet.h"
     35 #include "test-dnssd.h"
     36 #include "dnssd-proxy.h"
     37 #include "srp-tls.h"
     38 #include <arpa/inet.h>
     39 
     40 #define MAX_PACKETS 10
     41 typedef struct test_packet_state test_packet_state_t;
     42 struct test_packet_state {
     43     test_state_t *test_state;
     44     client_state_t *current_client;
     45     srpl_connection_t *srpl_connection;
     46     message_t *packets[MAX_PACKETS];
     47     int num_packets;
     48 };
     49 
     50 test_packet_state_t *
     51 test_packet_state_create(test_state_t *state, void (*advertise_finished_callback)(test_state_t *state))
     52 {
     53     test_packet_state_t *packet_state = calloc(1, sizeof(*packet_state));
     54     TEST_FAIL_CHECK(state, state != NULL, "unable to allocate test packet state");
     55     packet_state->test_state = state;
     56     srp_host_init(state);
     57     packet_state->current_client = srp_client_get_current();
     58 
     59     // We need to add at least one address record.
     60     srp_test_set_local_example_address(state);
     61 
     62     // We need an SRPL connection to capture the advertise_finished event. It needs to look real enough that
     63     // the event gets delivered, so we have to create a domain and an instance and tie them together, and then
     64     // hang the connection off of the instance.
     65     srpl_instance_t *instance = calloc(1, sizeof (*instance));
     66     TEST_FAIL_CHECK(state, instance != NULL, "no memory for instance");
     67     instance->instance_name = strdup("single-srpl-instance");
     68     TEST_FAIL_CHECK(state, instance->instance_name != NULL, "no memory for instance name");
     69     instance->domain = srpl_domain_create_or_copy(state->primary, "openthread.thread.home.arpa");
     70     instance->domain->srpl_opstate = SRPL_OPSTATE_ROUTINE;
     71     TEST_FAIL_CHECK(state, instance->domain != NULL, "no domain created");
     72     instance->domain->instances = instance;
     73     RETAIN_HERE(instance->domain, srpl_domain);
     74     RETAIN_HERE(instance->domain->instances, srpl_instance);
     75 
     76     srpl_connection_t *srpl_connection = srpl_connection_create(instance, false);
     77     TEST_FAIL_CHECK(state, srpl_connection != NULL, "srpl_connection_create failed");
     78     srpl_connection->state = srpl_state_test_event_intercept;
     79     instance->connection = srpl_connection;
     80     RETAIN_HERE(instance->connection, srpl_connection);
     81 
     82     srpl_connection->test_state = state;
     83     srpl_connection->advertise_finished_callback = advertise_finished_callback;
     84     packet_state->srpl_connection = srpl_connection;
     85     RETAIN_HERE(packet_state->srpl_connection, srpl_connection);
     86 
     87     return packet_state;
     88 }
     89 
     90 void
     91 test_packet_generate(test_state_t *state, uint32_t host_lease, uint32_t key_lease, bool removing, bool prepend)
     92 {
     93     test_packet_state_t *packet_state = state->test_packet_state;
     94     message_t *message = ioloop_message_create(sizeof(dns_wire_t));
     95     size_t length = message->length;
     96     message->received_time = srp_time();
     97     message->lease = host_lease;
     98     message->key_lease = key_lease;
     99     dns_wire_t *ret = srp_client_generate_update(packet_state->current_client, host_lease, key_lease,
    100                                                  &length, &message->wire, 9999, removing);
    101     TEST_FAIL_CHECK(state, length <= message->length, "srp_client_generate overflowed message length");
    102     TEST_FAIL_CHECK(state, ret != NULL, "srp_client_generate returned NULL");
    103     TEST_FAIL_CHECK(state, packet_state->num_packets < MAX_PACKETS, "more than maximum number of packets");
    104     if (prepend) {
    105         for (int i = packet_state->num_packets; i > 0; i--) {
    106             packet_state->packets[i] = packet_state->packets[i - 1];
    107         }
    108         packet_state->packets[0] = message;
    109     } else {
    110         packet_state->packets[packet_state->num_packets] = message;
    111     }
    112     packet_state->num_packets++;
    113 }
    114 
    115 void
    116 test_packet_reset_key(test_state_t *state)
    117 {
    118     test_packet_state_t *packet_state = state->test_packet_state;
    119     srp_host_key_reset_for_client(packet_state->current_client);
    120 }
    121 
    122 void
    123 test_packet_start(test_state_t *state, bool expect_fail)
    124 {
    125     test_packet_state_t *packet_state = state->test_packet_state;
    126     bool success = srp_parse_host_messages_evaluate(state->primary, packet_state->srpl_connection,
    127                                                     packet_state->packets, packet_state->num_packets);
    128     TEST_FAIL_CHECK_STATUS(state, expect_fail != success, "srp_parse_host_messages_evaluate returned " PUB_S_SRP,
    129                            success ? "true" : "false");
    130     if (expect_fail) {
    131         TEST_PASSED(state);
    132     }
    133 }
    134 
    135 bool
    136 test_packet_srpl_intercept(srpl_connection_t *srpl_connection, srpl_event_t *event)
    137 {
    138     if (event->event_type == srpl_event_advertise_finished) {
    139         test_state_t *state = srpl_connection->test_state;
    140         srpl_connection->advertise_finished_callback(state);
    141     }
    142     return false;
    143 }
    144 
    145 void
    146 test_packet_message_delete(test_state_t *test_state, int index)
    147 {
    148     test_packet_state_t *state = test_state->test_packet_state;
    149     if (index < state->num_packets) {
    150         ioloop_message_release(state->packets[index]);
    151         int j = index;
    152         for (int i = index + 1; i < state->num_packets; i++) {
    153             state->packets[j++] = state->packets[i];
    154         }
    155         state->num_packets--;
    156     }
    157 }
    158 
    159 // Local Variables:
    160 // mode: C
    161 // tab-width: 4
    162 // c-file-style: "bsd"
    163 // c-basic-offset: 4
    164 // fill-column: 108
    165 // indent-tabs-mode: nil
    166 // End:
    167