1#!/bin/bash 2# 3# Copyright (c) 2018, The OpenThread Authors. 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions are met: 8# 1. Redistributions of source code must retain the above copyright 9# notice, this list of conditions and the following disclaimer. 10# 2. Redistributions in binary form must reproduce the above copyright 11# notice, this list of conditions and the following disclaimer in the 12# documentation and/or other materials provided with the distribution. 13# 3. Neither the name of the copyright holder nor the 14# names of its contributors may be used to endorse or promote products 15# derived from this software without specific prior written permission. 16# 17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27# POSSIBILITY OF SUCH DAMAGE. 28# 29 30# 31# This script includes common utils for testing mdns 32# 33 34set -euxo pipefail 35 36DNS_SD_RESULT=result 37readonly DNS_SD_RESULT 38 39case "${OTBR_MDNS}" in 40 mDNSResponder) 41 sudo service avahi-daemon stop || true 42 sudo killall mdnsd || true 43 sudo mdnsd 44 sleep 1 45 ;; 46 47 avahi) 48 sudo killall mdnsd || true 49 sudo service avahi-daemon restart 50 sleep 1 51 ;; 52 53 *) 54 echo >&2 "Not supported" 55 exit 128 56 ;; 57esac 58 59on_exit() 60{ 61 EXIT_CODE=$? 62 readonly EXIT_CODE 63 64 kill "$PID" 65 [[ ! -e ${DNS_SD_RESULT} ]] || rm "${DNS_SD_RESULT}" || true 66 67 exit $EXIT_CODE 68} 69 70start_publisher() 71{ 72 "${OTBR_TEST_MDNS}" "$1" & 73 PID=$! 74 trap on_exit EXIT 75 sleep 2 76} 77 78####################################### 79# Check if a service is regisered 80# 81# Arguments: 82# $1 Name 83# $2 Type 84# $3 Text record 85# 86# Returns: 87# 0 Registered 88# otherwise Not registered 89####################################### 90dns_sd_check() 91{ 92 # dns-sd will not exit 93 dns-sd -L "$1" "$2" local >"${DNS_SD_RESULT}" 2>&1 & 94 DNS_SD_PID=$! 95 sleep 1 96 kill "${DNS_SD_PID}" 97 98 cat "${DNS_SD_RESULT}" 99 grep "$3" "${DNS_SD_RESULT}" 100} 101 102####################################### 103# Check if a service is registered with 104# a given type. 105# 106# Arguments: 107# $1 Name 108# $2 Type 109# 110# Returns: 111# 0 Registered 112# otherwise Not registered 113####################################### 114dns_sd_check_type() 115{ 116 # dns-sd will not exit 117 dns-sd -B "$2" local >"${DNS_SD_RESULT}" 2>&1 & 118 DNS_SD_PID=$! 119 sleep 1 120 kill "${DNS_SD_PID}" 121 122 cat "${DNS_SD_RESULT}" 123 grep "$1" "${DNS_SD_RESULT}" 124} 125 126####################################### 127# Check if a host is regisered 128# 129# Arguments: 130# $1 hostname 131# $2 address 132# 133# Returns: 134# 0 Registered 135# otherwise Not registered 136####################################### 137dns_sd_check_host() 138{ 139 # dns-sd will not exit 140 dns-sd -G v6 "$1" >"${DNS_SD_RESULT}" 2>&1 & 141 DNS_SD_PID=$! 142 sleep 1 143 kill "${DNS_SD_PID}" 144 145 cat "${DNS_SD_RESULT}" 146 grep "$2" "${DNS_SD_RESULT}" 147} 148 149####################################### 150# Check if a service is registered 151# 152# Arguments: 153# $1 Expected avahi query result string 154# $2 Service type. If omitted, all 155# services will be examined. 156# 157# Returns: 158# 0 Registered 159# otherwise Not registered 160####################################### 161avahi_check() 162{ 163 local service_type 164 (($# == 2)) && service_type="$2" || service_type="-a" 165 166 avahi-browse -prt "$service_type" | tee | grep "$1" 167} 168