1#!/usr/bin/perl 2 3# Copyright (C) 2006 Alexey Proskuryakov (ap@nypop.com) 4# 5# Redistribution and use in source and binary forms, with or without 6# modification, are permitted provided that the following conditions 7# are met: 8# 9# 1. Redistributions of source code must retain the above copyright 10# notice, this list of conditions and the following disclaimer. 11# 2. Redistributions in binary form must reproduce the above copyright 12# notice, this list of conditions and the following disclaimer in the 13# documentation and/or other materials provided with the distribution. 14# 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 15# its contributors may be used to endorse or promote products derived 16# from this software without specific prior written permission. 17# 18# THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 19# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21# DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 22# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 25# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29# A script to semi-automatically run iExploder tests. 30 31use strict; 32use warnings; 33 34use Cwd; 35use FindBin; 36use Getopt::Long; 37use IPC::Open2; 38 39use lib $FindBin::Bin; 40use webkitdirs; 41 42sub openHTTPDIfNeeded(); 43sub closeHTTPD(); 44sub runSafariWithIExploder(); 45 46# Argument handling 47my $guardMalloc = ''; 48my $httpdPort = 8000; 49my $downloadTest; 50 51GetOptions( 52 'guard-malloc|g' => \$guardMalloc, 53 'get=s' => \$downloadTest, 54 'port=i' => \$httpdPort 55); 56 57 58setConfiguration(); 59my $productDir = productDir(); 60 61chdirWebKit(); 62 63checkFrameworks(); 64 65my $httpdOpen = 0; 66openHTTPDIfNeeded(); 67 68if ($downloadTest) { 69 system "/usr/bin/curl -o ~/Desktop/iexploder$downloadTest.html \"http://127.0.0.1:$httpdPort/iexploder.cgi?lookup=1&test=$downloadTest\""; 70 print "Saved the test as iexploder$downloadTest.html on the desktop\n"; 71} else { 72 runSafariWithIExploder(); 73 print "Last generated tests:\n"; 74 system "grep 'iexploder.cgi' /tmp/WebKit/access_log.txt | tail -n -5 | awk -F'[ =&\\?]' '{if (\$8 == \"lookup\") print \$11; else print \$9}'"; 75} 76 77closeHTTPD(); 78 79 80sub runSafariWithIExploder() 81{ 82 my $redirectTo; 83 if (@ARGV) { 84 $redirectTo = "http://127.0.0.1:$httpdPort/iexploder.cgi?lookup=1&test=$ARGV[0]"; 85 } else { 86 $redirectTo = "http://127.0.0.1:$httpdPort/index.html"; 87 } 88 89 open REDIRECT_HTML, ">", "/tmp/WebKit/redirect.html" or die; 90 print REDIRECT_HTML "<html>\n"; 91 print REDIRECT_HTML " <head>\n"; 92 print REDIRECT_HTML " <meta http-equiv=\"refresh\" content=\"1;URL=$redirectTo\" />\n"; 93 print REDIRECT_HTML " <script type=\"text/javascript\">\n"; 94 print REDIRECT_HTML " document.location = \"$redirectTo\";\n"; 95 print REDIRECT_HTML " </script>\n"; 96 print REDIRECT_HTML " </head>\n"; 97 print REDIRECT_HTML " <body>\n"; 98 print REDIRECT_HTML " </body>\n"; 99 print REDIRECT_HTML "</html>\n"; 100 close REDIRECT_HTML; 101 102 local %ENV; 103 $ENV{DYLD_INSERT_LIBRARIES} = "/usr/lib/libgmalloc.dylib" if $guardMalloc; 104 system "WebKitTools/Scripts/run-safari", "-NSOpen", "/tmp/WebKit/redirect.html"; 105} 106 107sub openHTTPDIfNeeded() 108{ 109 return if $httpdOpen; 110 111 mkdir "/tmp/WebKit"; 112 113 if (-f "/tmp/WebKit/httpd.pid") { 114 my $oldPid = `cat /tmp/WebKit/httpd.pid`; 115 chomp $oldPid; 116 if (0 != kill 0, $oldPid) { 117 print "\nhttpd is already running: pid $oldPid, killing...\n"; 118 kill 15, $oldPid; 119 120 my $retryCount = 20; 121 while ((0 != kill 0, $oldPid) && $retryCount) { 122 sleep 1; 123 --$retryCount; 124 } 125 126 die "Timed out waiting for httpd to quit" unless $retryCount; 127 } 128 } 129 130 my $testDirectory = getcwd() . "/LayoutTests"; 131 my $iExploderDirectory = getcwd() . "/WebKitTools/iExploder"; 132 my $httpdPath = "/usr/sbin/httpd"; 133 my $httpdConfig = "$testDirectory/http/conf/httpd.conf"; 134 $httpdConfig = "$testDirectory/http/conf/apache2-httpd.conf" if `$httpdPath -v` =~ m|Apache/2|; 135 my $documentRoot = "$iExploderDirectory/htdocs"; 136 my $typesConfig = "$testDirectory/http/conf/mime.types"; 137 my $sslCertificate = "$testDirectory/http/conf/webkit-httpd.pem"; 138 my $listen = "127.0.0.1:$httpdPort"; 139 140 open2(\*HTTPDIN, \*HTTPDOUT, $httpdPath, 141 "-f", "$httpdConfig", 142 "-C", "DocumentRoot \"$documentRoot\"", 143 "-C", "Listen $listen", 144 "-c", "TypesConfig \"$typesConfig\"", 145 "-c", "CustomLog \"/tmp/WebKit/access_log.txt\" common", 146 "-c", "ErrorLog \"/tmp/WebKit/error_log.txt\"", 147 "-c", "SSLCertificateFile \"$sslCertificate\"", 148 # Apache wouldn't run CGIs with permissions==700 otherwise 149 "-c", "User \"#$<\""); 150 151 my $retryCount = 20; 152 while (system("/usr/bin/curl -q --silent --stderr - --output /dev/null $listen") && $retryCount) { 153 sleep 1; 154 --$retryCount; 155 } 156 157 die "Timed out waiting for httpd to start" unless $retryCount; 158 159 $httpdOpen = 1; 160} 161 162sub closeHTTPD() 163{ 164 return if !$httpdOpen; 165 166 close HTTPDIN; 167 close HTTPDOUT; 168 169 kill 15, `cat /tmp/WebKit/httpd.pid` if -f "/tmp/WebKit/httpd.pid"; 170 171 $httpdOpen = 0; 172} 173