1#!/bin/sh 2# 3# 4# Copyright (C) 2015 The Android Open Source Project 5# 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18# 19# UDEV event helper script that sets the system's WiFi regulatory domain 20# from VPD data. 21 22# Assertion helpers. 23assert_equal() { 24 local actual="$1" 25 local expected="$2" 26 27 if [ "${actual}" != "${expected}" ]; then 28 echo "FAIL: expected |${expected}|, got |${actual}|" 29 exit 1 30 fi 31} 32 33assert_regdomain_is() { 34 local expected_code="$1" 35 g_vpd_data="$(cat)" 36 g_country_code="" 37 38 . $(dirname $0)/set_wifi_regulatory 39 assert_equal "${g_country_code}" "${expected_code}" 40} 41 42# Fake out the commands that are called by set_wifi_regulatory. 43dump_vpd_log() { 44 assert_equal "$1" "--stdout" 45 echo "${g_vpd_data}" 46} 47 48iw() { 49 assert_equal "$1" "reg" 50 assert_equal "$2" "set" 51 g_country_code="$3" 52} 53 54# Simplest input. 55assert_regdomain_is US <<-"EOF" 56 "region"="US" 57EOF 58 59# Properly handle lower-case region. 60assert_regdomain_is US <<-"EOF" 61 "region"="us" 62EOF 63 64# If region exists multiple times, take the first one. 65assert_regdomain_is JP <<-"EOF" 66 "region"="JP" 67 "region"="US" 68EOF 69 70# Other fields can come before. 71assert_regdomain_is US <<-"EOF" 72 "initial_timezone"="America/Los_Angeles" 73 "region"="us" 74EOF 75 76# Other fields can come after. 77assert_regdomain_is US <<-"EOF" 78 "region"="us" 79 "initial_timezone"="America/Los_Angeles" 80EOF 81 82# Region may include additional data after country code (1/2). 83assert_regdomain_is CA <<-"EOF" 84 "region"="ca.hybrid" 85EOF 86 87# Region may include additional data after country code (2/2). 88assert_regdomain_is BR <<-"EOF" 89 "region"="br.abnt" 90EOF 91 92# Virtual regions work correctly (1/2). 93assert_regdomain_is SE <<-"EOF" 94 "region"="nordic" 95EOF 96 97# Virtual regions work correctly (2/2). 98assert_regdomain_is "MX" <<-"EOF" 99 "region"="latam-es-419" 100EOF 101 102# End quote is required. 103assert_regdomain_is "" <<-"EOF" 104 "region"="us 105EOF 106 107# Quotes are required. 108assert_regdomain_is "" <<-"EOF" 109 region=us 110EOF 111 112# No junk allowed at end. 113assert_regdomain_is "" <<-"EOF" 114 "region"="us"andmorestuff 115EOF 116 117# No junk allowed at beginning. 118assert_regdomain_is "" <<-"EOF" 119 junk"region"="us" 120EOF 121 122# Must match "region" exactly. 123assert_regdomain_is "" <<-"EOF" 124 "jregion"="us" 125EOF 126 127# Random shell meta-characters are not allowed. 128assert_regdomain_is "" <<-"EOF" 129 "region"="ca>>/var/log/junk" 130EOF 131 132echo "PASS" 133