~ubuntu-branches/ubuntu/hardy/gallery2/hardy-security

« back to all changes in this revision

Viewing changes to modules/core/test/phpunit/PhpVmTest.class

  • Committer: Bazaar Package Importer
  • Author(s): Michael C. Schultheiss
  • Date: 2006-04-16 16:42:35 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060416164235-8uy0u4bfjdxpge2o
Tags: 2.1.1-1
* New upstream release (Closes: #362936)
  + Bugfixes for Postgres7 (Closes: #359000, #362152)

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 * $RCSfile: PhpVmTest.class,v $
4
4
 *
5
5
 * Gallery - a web based photo album viewer and editor
6
 
 * Copyright (C) 2000-2005 Bharat Mediratta
 
6
 * Copyright (C) 2000-2006 Bharat Mediratta
7
7
 *
8
8
 * This program is free software; you can redistribute it and/or modify
9
9
 * it under the terms of the GNU General Public License as published by
20
20
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
21
21
 */
22
22
/**
23
 
 * @version $Revision: 1.6 $ $Date: 2005/08/23 03:49:33 $
 
23
 * @version $Revision: 1.8 $ $Date: 2006/01/30 23:00:21 $
24
24
 * @package GalleryCore
25
25
 * @subpackage PHPUnit
26
26
 * @author Jay Rossiter <cryptographite@users.sf.net>
27
27
 */
28
28
 
29
 
GalleryCoreApi::relativeRequireOnce('modules/core/classes/GalleryPhpVm.class');
 
29
GalleryCoreApi::requireOnce('modules/core/classes/GalleryPhpVm.class');
30
30
 
31
31
/**
32
32
 * Test the GalleryPhpVm.  These are passthrough functions so all we do is perform a smoke test to
87
87
     */
88
88
    function testHeader() {
89
89
        global $gallery;
90
 
 
 
90
        
91
91
        /*
92
92
         * This test also depends on the G2 Session code as we do an actual HTTP request and
93
93
         * verify that we received a cookie header. GallerySession uses the $phpVm->header()
94
94
         * function, thus this covers what we'd like to test (and a little more)
95
95
         */
96
 
        $session =& $gallery->getSession();
97
96
        $urlGenerator =& $gallery->getUrlGenerator();
98
 
        $url = $urlGenerator->getCurrentUrl();
99
 
        /* Strip of the /lib/tools/test/ part to get a valid G2 request uri */
100
 
        $url = preg_replace('|^(.*/)lib/tools/.*$|', '$1', $url);
101
 
        list ($body, $response, $headers) = GalleryCoreApi::fetchWebPage($url);
 
97
        $url = $urlGenerator->generateUrl(array('href' => 'main.php'),
 
98
                                          array('forceFullUrl' => true));
 
99
 
 
100
        /* For guests, we don't send cookies, thus prepare an existing session */
 
101
        GalleryCoreApi::requireOnce('modules/core/classes/GallerySession.class');
 
102
        $session = new GallerySession();
 
103
        $ret = $session->initEmpty(true);
 
104
        if ($ret) {
 
105
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
106
        }
 
107
        $this->assert(!empty($session->_sessionId), 'persistent session preparation failed');
 
108
        $url = GalleryUrlGenerator::appendParamsToUrl(
 
109
            $url, array($session->getKey() => $session->getId()));
 
110
        
 
111
        list ($success, $body, $response, $headers) = GalleryCoreApi::fetchWebPage($url);
 
112
        $this->assert($success, 'Could not retrieve G2 page');
102
113
        $this->assertEquals($response, 'HTTP/1.1 200 OK', 'Could not retrieve G2 page');
103
114
        $this->assertEquals(true, isset($headers['Set-Cookie']), 'Set-Cookie header not set');
104
115
        if (isset($headers['Set-Cookie'])) {
105
 
            $this->assert(strpos($headers['Set-Cookie'], SESSION_ID_PARAMETER) !== false,
106
 
                          'no G2 SessionId found in the cookie header');
 
116
            $this->assert(strpos($headers['Set-Cookie'],
 
117
                                 SESSION_ID_PARAMETER . '=' . $session->getId()) !== false,
 
118
                          'no G2 SessionId with a correct SID found in the cookie header');
107
119
        } else {
108
 
            $this->assert(false, 'no cookie header found in http response');
 
120
            $this->assert(false, 'no cookie header found in http response for url: ' . $url);
109
121
        }
110
122
    }
111
123
 
114
126
        global $gallery;
115
127
        /* When this unit test is executed, we already sent some data to the user agent */
116
128
        $phpVm = $gallery->getPhpVm();
117
 
        $this->assert($phpVm->headers_sent(), 'headers are sent at this point, but the vm function ' .
118
 
                      'does not think so');
 
129
        $this->assert($phpVm->headers_sent(), 'headers are sent at this point, but the vm ' .
 
130
                      'function does not think so');
119
131
 
120
132
        /* The other case, $phpVm->headers_sent() == false, is covered by testHeader() */
121
133
    }
125
137
        $this->assertEquals(get_magic_quotes_gpc(), $this->_phpVm->get_magic_quotes_gpc(),
126
138
                            'The Vm function does not return the correct value');
127
139
    }
 
140
 
 
141
    function testTime() {
 
142
        /*
 
143
         * There's an incredibly slim chance that this will fail.  If it does, we should improve
 
144
         * this test.
 
145
         */
 
146
        $this->assertEquals(time(), $this->_phpVm->time());
 
147
    }
 
148
 
 
149
    function testRand() {
 
150
        $this->assert(is_int(rand()), 'is int');
 
151
        $this->assert(rand() > 0, 'is positive');
 
152
        for ($i = 0; $i < 50; $i++) {
 
153
            $rand = rand(1000, 1010);
 
154
            $this->assert($rand >= 1000 && $rand <= 1010, 'with minimum and maximum');
 
155
        }
 
156
    }
128
157
}
129
158
?>