Home | History | Annotate | Download | only in bin
      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.
     23 assert_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 
     33 assert_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.
     43 dump_vpd_log() {
     44   assert_equal "$1" "--stdout"
     45   echo "${g_vpd_data}"
     46 }
     47 
     48 iw() {
     49   assert_equal "$1" "reg"
     50   assert_equal "$2" "set"
     51   g_country_code="$3"
     52 }
     53 
     54 # Simplest input.
     55 assert_regdomain_is US <<-"EOF"
     56 	"region"="US"
     57 EOF
     58 
     59 # Properly handle lower-case region.
     60 assert_regdomain_is US <<-"EOF"
     61 	"region"="us"
     62 EOF
     63 
     64 # If region exists multiple times, take the first one.
     65 assert_regdomain_is JP <<-"EOF"
     66 	"region"="JP"
     67 	"region"="US"
     68 EOF
     69 
     70 # Other fields can come before.
     71 assert_regdomain_is US <<-"EOF"
     72 	"initial_timezone"="America/Los_Angeles"
     73 	"region"="us"
     74 EOF
     75 
     76 # Other fields can come after.
     77 assert_regdomain_is US <<-"EOF"
     78 	"region"="us"
     79 	"initial_timezone"="America/Los_Angeles"
     80 EOF
     81 
     82 # Region may include additional data after country code (1/2).
     83 assert_regdomain_is CA <<-"EOF"
     84 	"region"="ca.hybrid"
     85 EOF
     86 
     87 # Region may include additional data after country code (2/2).
     88 assert_regdomain_is BR <<-"EOF"
     89 	"region"="br.abnt"
     90 EOF
     91 
     92 # Virtual regions work correctly (1/2).
     93 assert_regdomain_is SE <<-"EOF"
     94 	"region"="nordic"
     95 EOF
     96 
     97 # Virtual regions work correctly (2/2).
     98 assert_regdomain_is "MX" <<-"EOF"
     99 	"region"="latam-es-419"
    100 EOF
    101 
    102 # End quote is required.
    103 assert_regdomain_is "" <<-"EOF"
    104 	"region"="us
    105 EOF
    106 
    107 # Quotes are required.
    108 assert_regdomain_is "" <<-"EOF"
    109 	region=us
    110 EOF
    111 
    112 # No junk allowed at end.
    113 assert_regdomain_is "" <<-"EOF"
    114 	"region"="us"andmorestuff
    115 EOF
    116 
    117 # No junk allowed at beginning.
    118 assert_regdomain_is "" <<-"EOF"
    119 	junk"region"="us"
    120 EOF
    121 
    122 # Must match "region" exactly.
    123 assert_regdomain_is "" <<-"EOF"
    124 	"jregion"="us"
    125 EOF
    126 
    127 # Random shell meta-characters are not allowed.
    128 assert_regdomain_is "" <<-"EOF"
    129 	"region"="ca>>/var/log/junk"
    130 EOF
    131 
    132 echo "PASS"
    133