xref: /aosp_15_r20/external/libchrome-gestures/tools/replay_log (revision aed3e5085e770be5b69ce25295ecf6ddf906af95)
1#!/bin/sh
2
3# Copyright 2012 The ChromiumOS Authors
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7. /usr/share/misc/shflags
8
9DEFINE_string out "testlog.txt" "Output log from replay"
10DEFINE_string only_honor "Tap Enable,Sensitivity" \
11    "Which properties from the log should be honored"
12
13FLAGS_HELP="usage: replay_log [--out testlog.txt] [--only_honor Props] \
14infile|feedback_url"
15
16FLAGS "$@" || exit 1
17eval set -- "${FLAGS_ARGV}"
18
19# Only now can we die on error.  shflags functions leak non-zero error codes,
20# so will die prematurely if 'set -e' is specified before now.
21set -e
22
23infile="${1}"
24if [ $# -ne 1 ]; then
25  echo $FLAGS_HELP
26  exit 1
27fi
28
29COOKIE_DIR="$(readlink -f "$(dirname "$0")/../cookies")"
30COOKIE_FILE="${COOKIE_DIR}/login.cookie"
31
32setup_login_cookie_dir() {
33  mkdir -p "$COOKIE_DIR"
34  chmod 0700 "$COOKIE_DIR"
35}
36
37login() {
38  setup_login_cookie_dir
39  read -p "Username (without @google.com): " USER
40  read -s -p "Password: " PASS
41  echo
42  read -p "OTP: " OTP
43  LOGIN_POST_FILE="$COOKIE_DIR/login.post"
44  LOGIN_RESULTS_FILE="$COOKIE_DIR/login.results"
45  echo "u=${USER}&pw=${PASS}&otp=${OTP}" > "$LOGIN_POST_FILE"
46  echo "Logging in..."
47  curl -s -c "$COOKIE_FILE" -L -d @"$LOGIN_POST_FILE" \
48    'https://login.corp.google.com/login?ssoformat=CORP_SSO' \
49    > $LOGIN_RESULTS_FILE
50  local should_abort="$FLAGS_FALSE"
51  if grep -i error ${LOGIN_RESULTS_FILE} >/dev/null; then
52    echo Login failure.
53    should_abort="$FLAGS_TRUE"
54  else
55    echo Login success
56  fi
57  rm -f "$LOGIN_POST_FILE"
58  rm -f "$LOGIN_RESULTS_FILE"
59  if [ $should_abort -eq $FLAGS_TRUE ]; then
60    exit 1
61  fi
62}
63
64if [[ "$infile" = "https://"* ]]; then
65  LOG_IDNUM=$(echo "$infile" | sed 's/.*[^0-9]\([0-9][0-9][0-9][0-9]*\).*/\1/')
66  LOG_FILE="report-${LOG_IDNUM}-system_logs.bz2"
67  LOG_URL="https://feedback.corp.googleusercontent.com/\
68binarydata/${LOG_FILE}?id=${LOG_IDNUM}&logIndex=0"
69  if [ -f "${LOG_FILE}" ]; then
70    echo already have file
71    infile=${LOG_FILE}
72  else
73    echo downloading log file
74    TRIES=0
75    while true; do
76      http_proxy=http://cache.corp.google.com:3128/ curl -b "$COOKIE_FILE" \
77        -L -o "$LOG_FILE" "$LOG_URL"
78      TYPE="$(file -b "$LOG_FILE" | cut -d ' ' -f 1)"
79      if [[ "$TYPE" = bzip2 || "$TYPE" = Zip ]]; then
80        # download success
81        infile="$LOG_FILE"
82        break
83      fi
84      rm -f "$LOG_FILE"
85      TRIES=$((TRIES + 1))
86      if [ $TRIES -eq 2 ]; then
87        echo Failed twice. Aborting
88        exit 1
89      fi
90      # login failure
91      echo 'Download failure. Logging in'
92      login
93    done
94  fi
95fi
96
97# Convert the infile from a relative path to the realpath since we will
98# change directory below.
99original_infile="$infile"
100infile="$(realpath $infile)"
101
102# Go to the Gestures source root
103cd $(dirname "$0")/..
104
105make -j $(fgrep processor /proc/cpuinfo | wc -l) test
106
107expand_input_file() {
108  # Get input filetype
109  intype="$(file -b "${infile}" | cut -d ' ' -f 1)"
110  # Extra check in case file gets ASCII wrong
111  if [ "$(head -n 1 "${infile}")" = "{" ]; then
112    intype="ASCII"
113  fi
114
115  if [ "$intype" = "bzip2" -o "$intype" = "Zip" ]; then
116    # Expand to the bzip2ed file within
117    local raw_text_log="raw_text_log.txt"
118    CAT=bzcat
119    if [ "$intype" = "Zip" ]; then
120      CAT=zcat
121    fi
122    $CAT "$infile" > "$raw_text_log"
123    cp "$raw_text_log" "full_feedback_log.txt"
124    if fgrep -q hack-33025-touchpad_activity "$raw_text_log"; then
125      # Expand the hack touchpad activity log
126      awk "/hack-33025-touchpad_activity/{found=1} found{print}" \
127          "$raw_text_log" > "$raw_text_log".tmp
128      mv "$raw_text_log".tmp "$raw_text_log"
129    fi
130
131    zlogs="$(cat "$raw_text_log" | uudecode -o - | tar xvf -)"
132    rm "$raw_text_log"
133    # take newest log and reduce to next case
134    cp "$(ls -t $zlogs | grep touchpad_activity | head -n 1)" log.gz
135
136    # Also expand evdev log
137    zcat "$(ls -t $zlogs | grep cmt_input_events | head -n 1)" > evlog.txt \
138        || true
139
140    rm -f $zlogs
141    infile="log.gz"
142    intype="gzip"
143  fi
144
145  if [ "$intype" = "gzip" ]; then
146    zcat "$infile" > log.txt
147    infile="log.txt"
148    intype="ASCII"
149  fi
150
151  if [ "$intype" != "ASCII" ]; then
152    echo "Unable to read input file"
153    exit 1
154  fi
155}
156
157run_test() {
158  expand_input_file
159  # We would like the shell to survive no matter whether ./test succeeds or not.
160  result=0
161  ./test --gtest_also_run_disabled_tests \
162    --gtest_filter="ActivityReplayTest.DISABLED_SimpleTest" \
163    --outfile="$FLAGS_out" --only_honor="$FLAGS_only_honor" --in="$infile" ||
164    result=$?
165}
166
167# If infile is a file, test this log as before and there is no redirection.
168# If infile is a directory, test all logs in the directory, and output
169# the test statistics at the end.
170if [ -f "$infile" ]; then
171  echo "$original_infile is a file."
172  run_test
173elif [ -d "$infile" ]; then
174  statistics_result="$(mktemp)"
175  echo -e "\nInput directory: $original_infile\n" > $statistics_result
176  indir="$infile"
177  files=$(ls "$indir")
178  count=0
179  passed=0
180  for file in $files; do
181    infile="$indir/$file"
182    if [ -f "$infile" ]; then
183      run_test
184      if [ $result -eq 0 ]; then
185        echo "[  PASSED  ] $original_infile/$file" >> $statistics_result
186        passed=$(($passed + 1))
187      else
188        echo "[  FAILED  ] $original_infile/$file" >> $statistics_result
189      fi
190      count=$(($count + 1))
191    fi
192  done
193  cat $statistics_result
194  rm -fr $statistics_result
195  echo -e "\n$passed out of $count tests passed.\n"
196fi
197