Home | History | Annotate | Download | only in messagegen
      1 #!/bin/bash
      2 #  Copyright (C) 2015 The Android Open Source Project
      3 #
      4 #  Licensed under the Apache License, Version 2.0 (the "License");
      5 #  you may not use this file except in compliance with the License.
      6 #  You may obtain a copy of the License at
      7 #
      8 #       http://www.apache.org/licenses/LICENSE-2.0
      9 #
     10 #  Unless required by applicable law or agreed to in writing, software
     11 #  distributed under the License is distributed on an "AS IS" BASIS,
     12 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 #  See the License for the specific language governing permissions and
     14 #  limitations under the License.
     15 ###################################################################
     16 ## Script that generates SMS and MMS messages and fills the Android
     17 ## telephony provider mmssms.db. This is used for testing SMS/MMS.
     18 ###################################################################
     19 
     20 AREA_CODE=605
     21 
     22 TABLE_CANONICAL_ADDRESSES_START_ID=100
     23 TABLE_THREADS_START_ID=100
     24 TABLE_SMS_START_ID=1000
     25 
     26 START_TIMESTAMP_IN_SECONDS=1357683093 # 1/8/2013 2:11:33 PM
     27 TIMESTAMP_INC_IN_SECONDS=120
     28 
     29 PART_DIR="/data/data/com.android.providers.telephony/app_parts"
     30 
     31 USAGE='fillsms [-f] [-x] <device_phone_number> <# of threads> <# of sms per thread> <# of mms per thread> <image list file> <sql file>
     32     -f -- Only generates the SQL file, do not push to the device
     33     -x -- Only execute a SQL file
     34     -g -- For GB devices
     35 Examples:
     36     # Generate 2 threads each with 10 SMSes and 10 MMSes on device with phone
     37     # number +16508619525. MMS messages use images listed in ./images, which list
     38     # *.jpg and *.gif files in local directory. The SQL commands are in sql.txt
     39     fillsms +16508619525 2 10 10 images sql.txt
     40 
     41     # Same as above but only creating the SQL command file without pushing to
     42     # device
     43     fillsms -f +16508619525 2 10 10 images sql.txt
     44 
     45     # Just push the sql.txt to device without generating new SQLs
     46     fillsms -x +16508619525 2 10 10 images sql.txt
     47 '
     48 
     49 SMIL='<smil> <head> <layout> <root-layout height="%dpx" width="%dpx"> <region fit="meet" height="%dpx" id="Image" left="0" top="0" width="%dpx"/></root-layout> </layout> </head> <body> <par dur="5000ms"> <img region="Image" src="%s"/> </par> </body> </smil>'
     50 
     51 MAX_WORDS_PER_MESSAGE=15
     52 
     53 DICT=american-english
     54 
     55 # don't actually run the sql on device
     56 opt_sql_only=0
     57 opt_exec_only=0
     58 opt_for_gb=0
     59 
     60 while test $# -gt 0
     61 do
     62   case $1 in
     63     -f)
     64       opt_sql_only=1
     65       shift
     66       ;;
     67     -x)
     68       opt_exec_only=1
     69       shift
     70       ;;
     71     -g)
     72       opt_for_gb=1
     73       shift
     74       ;;
     75     *)
     76       break;
     77   esac
     78 done
     79 
     80 
     81 if [ $opt_sql_only -eq "1" -a $opt_exec_only -eq "1" ]; then
     82   echo "-f and -x can not coexist"
     83   echo "$USAGE"
     84   exit 1
     85 fi
     86 
     87 if [ $# -lt 6 ]; then
     88   echo "$USAGE"
     89   exit 1
     90 fi
     91 device_phone=$1
     92 shift
     93 num_of_threads=$1
     94 shift
     95 sms_per_thread=$1
     96 shift
     97 mms_per_thread=$1
     98 shift
     99 image_list_file=$1
    100 shift
    101 sql_file=$1
    102 shift
    103 
    104 dict_lines=`wc -l < $DICT`
    105 image_files=`wc -l < $image_list_file`
    106 
    107 if [ $mms_per_thread -gt "0" ]; then
    108   if [ ! -f $image_list_file ]; then
    109     echo "No image files for creating MMS messages"
    110     exit 1
    111   fi
    112 fi
    113 
    114 echoerr ()
    115 {
    116   echo "$@" 1>&2;
    117 }
    118 
    119 random_value ()
    120 {
    121   echo $(( $RANDOM % $1 + 1 ))
    122 }
    123 
    124 dict_word ()
    125 {
    126   local v=$(random_value 30000)
    127   sed $v"q;d" $DICT
    128 }
    129 
    130 gen_message ()
    131 {
    132   local words=$(random_value $MAX_WORDS_PER_MESSAGE)
    133   local message=
    134   for k in `seq 1 $words`;
    135   do
    136     local word=$(dict_word)
    137     message="$message $word"
    138   done
    139   echo $message | sed -e "s/'//g"
    140 }
    141 
    142 random_image ()
    143 {
    144   local v=$(random_value $image_files)
    145   sed $v"q;d" $image_list_file
    146 }
    147 
    148 add_sql ()
    149 {
    150   echo $1 >> $sql_file
    151 }
    152 
    153 adb_sql ()
    154 {
    155   echo $1
    156   adb shell sqlite3 data/data/com.android.providers.telephony/databases/mmssms.db "$1"
    157 }
    158 
    159 ######################################################################################
    160 ######################################################################################
    161 
    162 if [ $opt_exec_only -eq "0" ]; then
    163   # clean up sql file
    164   rm -f $sql_file
    165 
    166   # add sql to clean up database
    167   add_sql "delete from pdu where _id>=$TABLE_SMS_START_ID;"
    168   add_sql "delete from part where _id>=$TABLE_SMS_START_ID;"
    169   add_sql "delete from addr where _id>=$TABLE_SMS_START_ID;"
    170   add_sql "delete from sms where _id>=$TABLE_SMS_START_ID;"
    171   add_sql "delete from threads where _id>=$TABLE_THREADS_START_ID;"
    172   add_sql "delete from canonical_addresses where _id>=$TABLE_CANONICAL_ADDRESSES_START_ID;"
    173 
    174   for i in `seq 1 $num_of_threads`;
    175   do
    176     echo
    177     echo "Creating thread $i ......"
    178     echo
    179 
    180     # Get random phone number
    181     value=$(random_value 1000)
    182     middle=$(printf '%03d' $value)
    183     value=$(random_value 10000)
    184     last=$(printf '%04d' $value)
    185     phone="+1$AREA_CODE$middle$last"
    186     echo $phone
    187     echo
    188 
    189     timestamp=$(( $START_TIMESTAMP_IN_SECONDS + 5 * $TIMESTAMP_INC_IN_SECONDS * $i ))
    190 
    191     # Generate threads
    192     addr_id=$(( $TABLE_CANONICAL_ADDRESSES_START_ID + $i ))
    193     add_sql "insert into canonical_addresses (_id,address) values ($addr_id,'$phone');"
    194 
    195     thread_id=$(( $TABLE_THREADS_START_ID + $i ))
    196     add_sql "insert into threads (_id,date,message_count,recipient_ids,snippet,snippet_cs,read,type,error,has_attachment) values ($thread_id, $timestamp, $sms_per_thread, $addr_id, 'snippet', 0, 1, 0, 0, 0);"
    197 
    198     # Generate SMS
    199     if [ $sms_per_thread -gt "0" ]; then
    200       half_timestamp_inc=$(( 500 + ((($sms_per_thread + $mms_per_thread) * $TIMESTAMP_INC_IN_SECONDS) * 500 / $sms_per_thread) ))
    201       for j in `seq 1 $sms_per_thread`;
    202       do
    203         message=$(gen_message)
    204         date=$(( ( 1000 * $timestamp ) - $half_timestamp_inc * ( 2 * ($sms_per_thread - $j) + ( $i % 2 ) ) ))
    205         message_id=$(( $TABLE_SMS_START_ID + $sms_per_thread * $i * 2 + (2 * $j) ))
    206         message_type=$(( $j % 2 + 1 ))
    207         add_sql "insert into sms (_id,thread_id,address,person,date,status,type,body,read,seen) values ($message_id, $thread_id, '$phone', '$phone', $date, -1, $message_type, '$message', 1, 1);"
    208       done
    209     fi
    210 
    211     # Generate MMS
    212     if [ $mms_per_thread -gt "0" ]; then
    213       half_timestamp_inc=$(( 1 + ((($sms_per_thread + $mms_per_thread) * $TIMESTAMP_INC_IN_SECONDS) / ( 2 * $mms_per_thread) ) ))
    214       for j in `seq 1 $mms_per_thread`;
    215       do
    216         image_line=$(random_image)
    217         image=`echo $image_line | awk '{ print $1 }'`
    218         width=`echo $image_line | awk '{ print $2 }'`
    219         height=`echo $image_line | awk '{ print $3 }'`
    220         size=`echo $image_line | awk '{ print $4 }'`
    221         date=$(( $timestamp - $half_timestamp_inc * ( 2 * ($mms_per_thread - $j) + ( ($i+1) % 2 ) ) ))
    222         message_id=$(( $TABLE_SMS_START_ID + $sms_per_thread * $i * 2 + (2 * $j + 1) ))
    223         message_type=$(( $j % 2 + 1 ))
    224         if [ $message_type -eq '1' ]; then
    225           m_type=132
    226         else
    227           m_type=128
    228         fi
    229         if [ $opt_for_gb -eq "0" ]; then
    230           add_sql "insert into pdu (_id,thread_id,date,date_sent,msg_box,read,m_id,sub,sub_cs,ct_t,m_cls,m_type,v,m_size,pri,rr,tr_id,d_rpt,locked,seen,text_only) values ($message_id, $thread_id, $date, 0, $message_type, 1, 'hmma3p5s1a3m526 (at] w.tmomail.net', 'no subject', 106, 'application/vnd.wap.multipart.related', 'personal', $m_type, 18, $size, 129, 129 , '$message_id', 129, 0, 1, 0);"
    231         else
    232           add_sql "insert into pdu (_id,thread_id,date,msg_box,read,m_id,sub,sub_cs,ct_t,m_cls,m_type,v,m_size,pri,rr,tr_id,d_rpt,locked,seen) values ($message_id, $thread_id, $date, $message_type, 1, 'hmma3p5s1a3m526 (at] w.tmomail.net', 'no subject', 106, 'application/vnd.wap.multipart.related', 'personal', $m_type, 18, $size, 129, 129 , '$message_id', 129, 0, 1);"
    233         fi
    234         id_1=$(( $message_id ))
    235         id_2=$(( $message_id + 1 ))
    236         smil=$(printf "$SMIL" $height $width $height $width $image)
    237         add_sql "insert into part (_id,mid,seq,ct,cid,cl,text) values ($id_1, $message_id, -1, 'application/smil', '<smil>', 'smil.xml', '$smil');"
    238         image_no_suffix=${image%.*}
    239         add_sql "insert into part (_id,mid,seq,ct,cid,cl,_data) values ($id_2, $message_id, 0, 'image/jpeg', '$image_no_suffix', '$image', '$PART_DIR/$image');"
    240         if [ $message_type -eq '1' ]; then
    241           add_sql "insert into addr (_id,msg_id,address,type,charset) values ($id_1, $message_id, '$phone', 137, 106);"
    242           add_sql "insert into addr (_id,msg_id,address,type,charset) values ($id_2, $message_id, '$device_phone', 151, 106);"
    243         else
    244           add_sql "insert into addr (_id,msg_id,address,type,charset) values ($id_1, $message_id, 'insert-address-token', 137, 106);"
    245           add_sql "insert into addr (_id,msg_id,address,type,charset) values ($id_2, $message_id, '$phone', 151, 106);"
    246         fi
    247       done
    248     fi
    249   done
    250 fi
    251 
    252 # Push to device
    253 if [ $opt_sql_only -eq "0" ]; then
    254   # make sure we have access
    255   adb root
    256 
    257   # Push all local jpgs or gifs to device, being lazy here.
    258   if [ $mms_per_thread -gt "0" ]; then
    259     for file in `ls *.jpg *.gif`;
    260     do
    261       echo "adb push $file $PART_DIR/$file"
    262       adb push $file $PART_DIR/$file
    263     done
    264   fi
    265 
    266   echo "adb push $sql_file /data/fillsms"
    267   adb push $sql_file /data/fillsms
    268   echo
    269   adb_sql ".read /data/fillsms"
    270   echo
    271   adb_sql "select count(*) from canonical_addresses where _id>=$TABLE_CANONICAL_ADDRESSES_START_ID;"
    272   echo
    273   adb_sql "select count(*) from threads where _id>=$TABLE_THREADS_START_ID;"
    274   echo
    275   if [ $sms_per_thread -gt "0" ]; then
    276     adb_sql "select count(*) from sms where _id>=$TABLE_SMS_START_ID;"
    277     echo
    278   fi
    279   if [ $mms_per_thread -gt "0" ]; then
    280     adb_sql "select count(*) from pdu where _id>=$TABLE_SMS_START_ID;"
    281     echo
    282     adb_sql "select count(*) from part where _id>=$TABLE_SMS_START_ID;"
    283     echo
    284     adb_sql "select count(*) from addr where _id>=$TABLE_SMS_START_ID;"
    285     echo
    286   fi
    287 fi
    288