summaryrefslogtreecommitdiff
path: root/tools/replay_log
blob: 76ddbbd5c7832275b0ccd4c4d01565bec107dc02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/bin/sh

# Copyright 2012 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

. /usr/share/misc/shflags

DEFINE_string out "testlog.txt" "Output log from replay"
DEFINE_string only_honor "Tap Enable,Sensitivity" \
    "Which properties from the log should be honored"

FLAGS_HELP="usage: replay_log [--out testlog.txt] [--only_honor Props] \
infile|feedback_url"

FLAGS "$@" || exit 1
eval set -- "${FLAGS_ARGV}"

# Only now can we die on error.  shflags functions leak non-zero error codes,
# so will die prematurely if 'set -e' is specified before now.
set -e

infile="${1}"
if [ $# -ne 1 ]; then
  echo $FLAGS_HELP
  exit 1
fi

COOKIE_DIR="$(readlink -f "$(dirname "$0")/../cookies")"
COOKIE_FILE="${COOKIE_DIR}/login.cookie"

setup_login_cookie_dir() {
  mkdir -p "$COOKIE_DIR"
  chmod 0700 "$COOKIE_DIR"
}

login() {
  setup_login_cookie_dir
  read -p "Username (without @google.com): " USER
  read -s -p "Password: " PASS
  echo
  read -p "OTP: " OTP
  LOGIN_POST_FILE="$COOKIE_DIR/login.post"
  LOGIN_RESULTS_FILE="$COOKIE_DIR/login.results"
  echo "u=${USER}&pw=${PASS}&otp=${OTP}" > "$LOGIN_POST_FILE"
  echo "Logging in..."
  curl -s -c "$COOKIE_FILE" -L -d @"$LOGIN_POST_FILE" \
    'https://login.corp.google.com/login?ssoformat=CORP_SSO' \
    > $LOGIN_RESULTS_FILE
  local should_abort="$FLAGS_FALSE"
  if grep -i error ${LOGIN_RESULTS_FILE} >/dev/null; then
    echo Login failure.
    should_abort="$FLAGS_TRUE"
  else
    echo Login success
  fi
  rm -f "$LOGIN_POST_FILE"
  rm -f "$LOGIN_RESULTS_FILE"
  if [ $should_abort -eq $FLAGS_TRUE ]; then
    exit 1
  fi
}

if [[ "$infile" = "https://"* ]]; then
  LOG_IDNUM=$(echo "$infile" | sed 's/.*[^0-9]\([0-9][0-9][0-9][0-9]*\).*/\1/')
  LOG_FILE="report-${LOG_IDNUM}-system_logs.bz2"
  LOG_URL="https://feedback.corp.googleusercontent.com/\
binarydata/${LOG_FILE}?id=${LOG_IDNUM}&logIndex=0"
  if [ -f "${LOG_FILE}" ]; then
    echo already have file
    infile=${LOG_FILE}
  else
    echo downloading log file
    TRIES=0
    while true; do
      http_proxy=http://cache.corp.google.com:3128/ curl -b "$COOKIE_FILE" \
        -L -o "$LOG_FILE" "$LOG_URL"
      TYPE="$(file -b "$LOG_FILE" | cut -d ' ' -f 1)"
      if [[ "$TYPE" = bzip2 || "$TYPE" = Zip ]]; then
        # download success
        infile="$LOG_FILE"
        break
      fi
      rm -f "$LOG_FILE"
      TRIES=$((TRIES + 1))
      if [ $TRIES -eq 2 ]; then
        echo Failed twice. Aborting
        exit 1
      fi
      # login failure
      echo 'Download failure. Logging in'
      login
    done
  fi
fi

# Convert the infile from a relative path to the realpath since we will
# change directory below.
original_infile="$infile"
infile="$(realpath $infile)"

# Go to the Gestures source root
cd $(dirname "$0")/..

make -j $(fgrep processor /proc/cpuinfo | wc -l) test

expand_input_file() {
  # Get input filetype
  intype="$(file -b "${infile}" | cut -d ' ' -f 1)"
  # Extra check in case file gets ASCII wrong
  if [ "$(head -n 1 "${infile}")" = "{" ]; then
    intype="ASCII"
  fi

  if [ "$intype" = "bzip2" -o "$intype" = "Zip" ]; then
    # Expand to the bzip2ed file within
    local raw_text_log="raw_text_log.txt"
    CAT=bzcat
    if [ "$intype" = "Zip" ]; then
      CAT=zcat
    fi
    $CAT "$infile" > "$raw_text_log"
    cp "$raw_text_log" "full_feedback_log.txt"
    if fgrep -q hack-33025-touchpad_activity "$raw_text_log"; then
      # Expand the hack touchpad activity log
      awk "/hack-33025-touchpad_activity/{found=1} found{print}" \
          "$raw_text_log" > "$raw_text_log".tmp
      mv "$raw_text_log".tmp "$raw_text_log"
    fi

    zlogs="$(cat "$raw_text_log" | uudecode -o - | tar xvf -)"
    rm "$raw_text_log"
    # take newest log and reduce to next case
    cp "$(ls -t $zlogs | grep touchpad_activity | head -n 1)" log.gz

    # Also expand evdev log
    zcat "$(ls -t $zlogs | grep cmt_input_events | head -n 1)" > evlog.txt \
        || true

    rm -f $zlogs
    infile="log.gz"
    intype="gzip"
  fi

  if [ "$intype" = "gzip" ]; then
    zcat "$infile" > log.txt
    infile="log.txt"
    intype="ASCII"
  fi

  if [ "$intype" != "ASCII" ]; then
    echo "Unable to read input file"
    exit 1
  fi
}

run_test() {
  expand_input_file
  # We would like the shell to survive no matter whether ./test succeeds or not.
  result=0
  ./test --gtest_also_run_disabled_tests \
    --gtest_filter="ActivityReplayTest.DISABLED_SimpleTest" \
    --outfile="$FLAGS_out" --only_honor="$FLAGS_only_honor" --in="$infile" ||
    result=$?
}

# If infile is a file, test this log as before and there is no redirection.
# If infile is a directory, test all logs in the directory, and output
# the test statistics at the end.
if [ -f "$infile" ]; then
  echo "$original_infile is a file."
  run_test
elif [ -d "$infile" ]; then
  statistics_result="$(mktemp)"
  echo -e "\nInput directory: $original_infile\n" > $statistics_result
  indir="$infile"
  files=$(ls "$indir")
  count=0
  passed=0
  for file in $files; do
    infile="$indir/$file"
    if [ -f "$infile" ]; then
      run_test
      if [ $result -eq 0 ]; then
        echo "[  PASSED  ] $original_infile/$file" >> $statistics_result
        passed=$(($passed + 1))
      else
        echo "[  FAILED  ] $original_infile/$file" >> $statistics_result
      fi
      count=$(($count + 1))
    fi
  done
  cat $statistics_result
  rm -fr $statistics_result
  echo -e "\n$passed out of $count tests passed.\n"
fi