Changeset 804

Show
Ignore:
Timestamp:
08/13/07 15:57:30 (1 year ago)
Author:
mikey
Message:

use DIRECTORY_SEPERATOR instead of hardcoded slash
escape key to prevent that DIRECTORY_SEPERATOR is contained in key and therefore in filename

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/util/cache/stubFileCacheContainer.php

    r741 r804  
    4444    public function setCacheDirectory($directory) 
    4545    { 
    46         $this->cacheDirectory = $directory . '/' . $this->id; 
    47         if (file_exists($this->cacheDirectory) == false) { 
     46        $this->cacheDirectory = $directory . DIRECTORY_SEPARATOR . $this->id; 
     47        if (file_exists($this->cacheDirectory) === false) { 
    4848            mkdir($this->cacheDirectory, 0700, true); 
    4949        } 
     
    6161    protected function doPut($key, $data) 
    6262    { 
    63         $bytes = file_put_contents($this->cacheDirectory . '/' . $key . '.cache', $data); 
     63        $bytes = file_put_contents($this->cacheDirectory . DIRECTORY_SEPARATOR . $this->escapeKey($key) . '.cache', $data); 
    6464        if (false === $bytes) { 
    6565            return false; 
     
    8585    protected function doHas($key) 
    8686    { 
    87         return file_exists($this->cacheDirectory . '/' . $key . '.cache'); 
     87        return file_exists($this->cacheDirectory . DIRECTORY_SEPARATOR . $this->escapeKey($key) . '.cache'); 
    8888    } 
    8989 
     
    9999    { 
    100100        if ($this->doHas($key) == true) { 
    101             return file_get_contents($this->cacheDirectory . '/' . $key . '.cache'); 
     101            return file_get_contents($this->cacheDirectory . DIRECTORY_SEPARATOR . $this->escapeKey($key) . '.cache'); 
    102102        } 
    103103         
     
    114114    { 
    115115        if ($this->doHas($key) == true) { 
    116             return (time() - filemtime($this->cacheDirectory . '/' . $key . '.cache')); 
     116            return (time() - filemtime($this->cacheDirectory . DIRECTORY_SEPARATOR . $this->escapeKey($key) . '.cache')); 
    117117        } 
    118118         
     
    132132        } 
    133133         
    134         return filesize($this->cacheDirectory . '/' . $key . '.cache'); 
     134        return filesize($this->cacheDirectory . DIRECTORY_SEPARATOR . $this->escapeKey($key) . '.cache'); 
    135135    } 
    136136 
     
    151151                 
    152152                $key              = str_replace('.cache', '', $file->getFilename()); 
    153                 $this->size[$key] = filesize($this->cacheDirectory . '/' . $key . '.cache'); 
     153                $this->size[$key] = filesize($this->cacheDirectory . DIRECTORY_SEPARATOR . $key . '.cache'); 
    154154            } 
    155155        } 
     
    207207            $key = str_replace('.cache', '', $file->getFilename()); 
    208208            if ($this->strategy->isExpired($this, $key) == true) { 
    209                 unlink($this->cacheDirectory . '/' . $key . '.cache'); 
     209                unlink($this->cacheDirectory . DIRECTORY_SEPARATOR . $key . '.cache'); 
    210210                if (null !== $this->size) { 
    211211                    unset($this->size[$key]); 
     
    214214        } 
    215215    } 
     216 
     217    /** 
     218     * escapes the cache key 
     219     * 
     220     * @param   string  $key 
     221     * @return  string 
     222     */ 
     223    protected function escapeKey($key) 
     224    { 
     225        return str_replace(DIRECTORY_SEPARATOR, '', $key); 
     226    } 
    216227} 
    217228?> 
  • trunk/src/test/php/net/stubbles/util/cache/stubFileCacheContainerTestCase.php

    r742 r804  
    4141    public function setUp() 
    4242    { 
    43         $this->cacheDirectory    = TEST_SRC_PATH . '/tmp/util_cache'; 
     43        $this->cacheDirectory    = TEST_SRC_PATH . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . 'util_cache'; 
    4444        $this->mockCacheStrategy = new MockstubCacheStrategy(); 
    4545        $this->cacheContainer    = new stubFileCacheContainer('foo'); 
    4646        $this->cacheContainer->setStrategy($this->mockCacheStrategy); 
    4747        $this->cacheContainer->setCacheDirectory($this->cacheDirectory); 
    48         $this->cacheDirectory   .= '/foo'; 
     48        $this->cacheDirectory   .= DIRECTORY_SEPARATOR . 'foo'; 
    4949    } 
    5050 
     
    7979        $this->assertEqual($this->cacheContainer->put('foo', 'bar'), 3); 
    8080        $this->assertEqual($this->cacheContainer->put('baz', 'bar'), false); 
    81         $this->assertEqual(file_get_contents($this->cacheDirectory . '/foo.cache'), 'bar'); 
    82         $this->assertFalse(file_exists($this->cacheDirectory . '/baz.cache')); 
     81        $this->assertEqual(file_get_contents($this->cacheDirectory . DIRECTORY_SEPARATOR . 'foo.cache'), 'bar'); 
     82        $this->assertFalse(file_exists($this->cacheDirectory . DIRECTORY_SEPARATOR . 'baz.cache')); 
    8383         
    8484        $this->assertEqual($this->cacheContainer->put('foo', 'baz'), 3); 
    85         $this->assertEqual(file_get_contents($this->cacheDirectory . '/foo.cache'), 'baz'); 
     85        $this->assertEqual(file_get_contents($this->cacheDirectory . DIRECTORY_SEPARATOR . 'foo.cache'), 'baz'); 
    8686    } 
    8787 
     
    9191    public function testHas() 
    9292    { 
    93         file_put_contents($this->cacheDirectory . '/foo.cache', 'bar'); 
     93        file_put_contents($this->cacheDirectory . DIRECTORY_SEPARATOR . 'foo.cache', 'bar'); 
    9494        $this->mockCacheStrategy->setReturnValueAt(0, 'isExpired', false); 
    9595        $this->mockCacheStrategy->setReturnValueAt(1, 'isExpired', true); 
     
    105105    public function testGet() 
    106106    { 
    107         file_put_contents($this->cacheDirectory . '/foo.cache', 'bar'); 
     107        file_put_contents($this->cacheDirectory . DIRECTORY_SEPARATOR . 'foo.cache', 'bar'); 
    108108        $this->mockCacheStrategy->setReturnValueAt(0, 'isExpired', false); 
    109109        $this->mockCacheStrategy->setReturnValueAt(1, 'isExpired', true); 
     
    119119    public function testGetSize() 
    120120    { 
    121         file_put_contents($this->cacheDirectory . '/foo.cache', 'bar'); 
     121        file_put_contents($this->cacheDirectory . DIRECTORY_SEPARATOR . 'foo.cache', 'bar'); 
    122122        $this->mockCacheStrategy->setReturnValueAt(0, 'isExpired', false); 
    123123        $this->mockCacheStrategy->setReturnValueAt(1, 'isExpired', true); 
     
    133133    public function testGetUsedSpace() 
    134134    { 
    135         file_put_contents($this->cacheDirectory . '/foo.cache', 'bar'); 
     135        file_put_contents($this->cacheDirectory . DIRECTORY_SEPARATOR . 'foo.cache', 'bar'); 
    136136        $this->assertEqual($this->cacheContainer->getUsedSpace(), 3); 
    137137        $this->mockCacheStrategy->setReturnValueAt(0, 'isCachable', true); 
     
    145145    public function testGetKeys() 
    146146    { 
    147         file_put_contents($this->cacheDirectory . '/foo.cache', 'bar'); 
     147        file_put_contents($this->cacheDirectory . DIRECTORY_SEPARATOR . 'foo.cache', 'bar'); 
    148148        $this->mockCacheStrategy->setReturnValueAt(0, 'isExpired', false); 
    149149        $this->assertEqual($this->cacheContainer->getKeys(), array('foo' => 'foo')); 
     
    163163    public function testGc() 
    164164    { 
    165         file_put_contents($this->cacheDirectory . '/foo.cache', 'bar'); 
     165        file_put_contents($this->cacheDirectory . DIRECTORY_SEPARATOR . 'foo.cache', 'bar'); 
    166166        $this->mockCacheStrategy->setReturnValue('shouldRunGc', true); 
    167167        $this->mockCacheStrategy->setReturnValueAt(0, 'isExpired', false); 
    168168        $this->cacheContainer->gc(); 
    169         $this->assertTrue(file_exists($this->cacheDirectory . '/foo.cache')); 
     169        $this->assertTrue(file_exists($this->cacheDirectory . DIRECTORY_SEPARATOR . 'foo.cache')); 
    170170         
    171171        $this->mockCacheStrategy->setReturnValueAt(1, 'isExpired', true); 
    172172        $this->cacheContainer->gc(); 
    173         $this->assertFalse(file_exists($this->cacheDirectory . '/foo.cache')); 
     173        $this->assertFalse(file_exists($this->cacheDirectory . DIRECTORY_SEPARATOR . 'foo.cache')); 
    174174        $this->mockCacheStrategy->setReturnValueAt(2, 'isExpired', false); 
    175175        $this->mockCacheStrategy->setReturnValueAt(3, 'isExpired', false); 
     
    179179        $this->assertEqual($this->cacheContainer->getSize('foo'), 0); 
    180180    } 
     181 
     182    /** 
     183     * test a key that contains a directory seperator 
     184     */ 
     185    public function testKeyContainingDirectorySeperator() 
     186    { 
     187        $this->mockCacheStrategy->setReturnValue('isCachable', true); 
     188        $this->assertEqual($this->cacheContainer->put('bar' . DIRECTORY_SEPARATOR . 'foo', 'bar'), 3); 
     189        $this->assertEqual(file_get_contents($this->cacheDirectory . DIRECTORY_SEPARATOR . 'barfoo.cache'), 'bar'); 
     190        $this->mockCacheStrategy->setReturnValueAt(0, 'isExpired', false); 
     191        $this->mockCacheStrategy->setReturnValueAt(1, 'isExpired', false); 
     192        $this->mockCacheStrategy->setReturnValueAt(2, 'isExpired', false); 
     193        $this->mockCacheStrategy->setReturnValueAt(3, 'isExpired', false); 
     194        $this->mockCacheStrategy->setReturnValueAt(4, 'isExpired', true); 
     195        $this->assertTrue($this->cacheContainer->has('bar' . DIRECTORY_SEPARATOR . 'foo')); 
     196        $this->assertEqual($this->cacheContainer->get('bar' . DIRECTORY_SEPARATOR . 'foo'), 'bar'); 
     197        $this->assertEqual($this->cacheContainer->getKeys(), array('barfoo' => 'barfoo')); 
     198        $this->assertEqual($this->cacheContainer->getSize('bar' . DIRECTORY_SEPARATOR . 'foo'), 3); 
     199        $this->assertEqual($this->cacheContainer->getUsedSpace(), 3); 
     200        $this->mockCacheStrategy->setReturnValue('shouldRunGc', true); 
     201        $this->cacheContainer->gc(); 
     202        $this->assertFalse(file_exists($this->cacheDirectory . DIRECTORY_SEPARATOR . 'barfoo.cache')); 
     203    } 
     204 
     205    /** 
     206     * test a key that contains a directory seperator 
     207     */ 
     208    public function testtestKeyContainingDirectorySeperatorAndExistingCacheFile() 
     209    { 
     210        file_put_contents($this->cacheDirectory . DIRECTORY_SEPARATOR . 'barfoo.cache', 'bar'); 
     211        $this->assertTrue($this->cacheContainer->has('bar' . DIRECTORY_SEPARATOR . 'foo')); 
     212        $this->assertEqual($this->cacheContainer->get('bar' . DIRECTORY_SEPARATOR . 'foo'), 'bar'); 
     213        $this->assertEqual($this->cacheContainer->getKeys(), array('barfoo' => 'barfoo')); 
     214        $this->assertEqual($this->cacheContainer->getSize('bar' . DIRECTORY_SEPARATOR . 'foo'), 3); 
     215        $this->assertEqual($this->cacheContainer->getUsedSpace(), 3); 
     216    } 
    181217} 
    182218?>