“Headed” Chrome テスト
最近、Chrome と WebUSB を使って Apple iPhone/iPad の usbmuxd プロトコルと通信する作業をしています。この作業を進めるうちに、開発者の使い勝手とテストが非常に重要になり、通常のウェブサイトの「ヘッドレス」テストは WebUSB がフルブラウザ機能に依存しているため実現できませんでした。さらに、理想的なテストケースとして Chromebook から WebUSB を対象にしたいと考えていました。そこで、軽量なローカルテスト用に NPM の jsdom と webusb を使用し、Chrome DevTools プロトコル経由で Pixelbook Slate にリモートでテストを駆動する方法を採用しました。
Big Scary - Much Danger
このガイドは ChromeOS のセキュリティ保護を無効化し、ブラウザをリモートで(一般的に安全な SSH 設定で)アクセス可能にする手順を示すため、実際の Google アカウントでこの Chrome デバイスにログインしたり、テストデバイス以外の用途で使用したりしないことを強く推奨します。他の目的で再利用する前に、ChromeOS の設定を非開発者バージョンに戻してください。警告は受け取りました。
開発者モードとデバッグ機能の有効化
Chrome デバイスは「Developer Mode」に設定でき、これは OS が最も安全な構成から外れた状態で動作することを許可する設定です。デバイスごとの手順は異なるため、Google / Chromium の公式手順(https://chromium.googlesource.com/chromiumos/docs/+/master/developer_mode.md#dev-mode)を参照する方が簡単です。開発者モードに入ったら、セットアッププロセスの最初の画面で「Enable debugging features」を選択し、パスワードを設定してください(https://www.chromium.org/chromium-os/how-tos-and-troubleshooting/debugging-features)。また、アップデートにより R/W ファイルシステムが壊れ root / chronos のパスワードがリセットされることがあります。その場合はデフォルトの test0000 を試すか、Powerwash してからデバッグ機能を再度有効化してください。
安全な SSH 接続の設定
デバイスが実質的に Android デバイスのように “rooted” された(root アカウントが利用可能でファイルシステムが可変)ので、リモート SSH サーバーの設定を行います。Developer Mode と Debugging Features が有効なデバイスはデフォルトで sshd が起動していますが、パスワード認証が使用されています。ネットワークが比較的プライベートでパスワード認証で問題なければこの手順はスキップできます。
以下の手順はデバイスへの SSH 接続、ローカルの VT-2 ターミナル([ Ctrl ] [ Alt ] [ → ])または crosh シェルプロンプト([ Ctrl ] [ Alt ] [ T ])から実行できます。
例では GitHub アカウントから自分の SSH 公開鍵をダウンロードしていますが、ブラウザで取得したりフラッシュドライブでデバイスに転送したりしても構いません。
# Enable Pubkey authentication
$ echo "\nPubkeyAuthentication yes" >> /etc/ssh/sshd_config
# Disable Challenge-Response authentication
$ echo "\nChallengeResponseAuthentication no" >> /etc/ssh/sshd_config
# The following is an example of downloading all SSH keys for a GitHub user, replace `rickmark` with your username.
$ curl https://github.com/rickmark.keys > /root/.ssh/authorized_keys
# The folowing is an example of copying a SSH key downloaded in the browser with the file name `id_rsa.pub`
$ cp /home/chronos/user/Downloads/id_rsa.pub /root/.ssh/authorized_keys
# Next we set the ownership and mode of authorized_keys
$ chown root:root /root/.ssh/authorized_keys
$ chmod 600 /root/.ssh/authorized_keys
# Restart the SSH server to apply
$ initctl stop openssh-server & initctl start openssh-server
Chrome DevTools リモートの有効化
デバイスへの SSH が設定されたので、残りの手順はすべて SSH 経由で実行できます。このステップでは Chrome のプロセス起動コマンドに追加のフラグを付加します。ChromeOS デバイスではブラウザが シェルそのもの になるため、実質的に OS の KDM / GDM に相当する部分の設定を変更していることになります。
# Append the parameter to the chrome process arguments
$ echo "--remote-debugging-port=1337" >> /etc/chrome_dev.conf
# Restart the device for the settings to take effect
$ shutdown -r now
Chrome へのトンネルの作成
ChromeOS デバイスの設定が完了したら、以下のコマンドで Puppeteer マシンから ChromeOS デバイスへ暗号化トンネルを SSH で作成します。このコマンドは Puppeteer を使用している間は常に実行し続ける必要があり、ChromeOS デバイスを Puppeteer で使用するたびに起動します。
$ ssh -L 1337:localhost:1337 root@<chromedevice>
Puppeteer Core からの接続
以下はリモート Chrome マシン上で puppeteer-core を使ったシンプルなテストです。実行前に yarn または npm で puppeteer-core をインストールしておいてください。puppeteer-core を使用するのは、モジュールが Chrome のコピーをダウンロードしないためですが、どちらでも同様に動作します。
import * as puppeteer from 'puppeteer-core'
const browser = await puppeteer.connect({browserURL: 'http://localhost:1337'})
const page = await browser.newPage()
await page.goto('https://google.com')
// If you do the following, the entire shell restarts (remember the browser is the OS)
await browser.close()
ボーナス: ChromeOS で Apple モバイルデバイスと WebUSB を使う方法
最近の ChromeOS バージョンには mtpd(メディア転送プロトコルエージェント)のコピーが含まれており、ChromeOS デバイスが写真をコピーできるようになっています。iPhone がこのプロトコルを実装しているため、バックグラウンドエージェントがデバイスの占有を解除せず、ブラウザで WebUSB を介してデバイスにアクセスしようとすると “Access Denied” エラーが発生することがあります。また、Linux カーネル自体が iPhone をテザーデバイスとして認識しようとすることもあります。ファイルブラウザから MTP デバイスを “eject” できる UI を提供するバグを報告していますが、現時点ではデバッグ機能を有効にした ChromeOS デバイスで以下の手順を実行し、これらの自動取得を無効化して ChromeOS の干渉を回避してください。
# Blacklist the "iPhone Ethernet" linux kernel module
$ echo "blacklist ipheth" >> /etc/modprobe.d/blacklist.conf
# Move the 'mtpd' service to the home folder so it wont be started with the operating system. Move it back to restore
$ mv /etc/init/mtpd.conf ~/
# Restart to apply changes
$ shutdown -r now