aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/Files/Stream/QuotaTest.php
blob: d084f0c769cacd951da862a932f586c6662f0fb6 (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
<?php
/**
 * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

namespace Test\Files\Stream;

class QuotaTest extends \Test\TestCase {
	protected function tearDown() {
		\OC\Files\Stream\Quota::clear();
		parent::tearDown();
	}

	/**
	 * @param string $mode
	 * @param integer $limit
	 */
	protected function getStream($mode, $limit) {
		$source = fopen('php://temp', $mode);
		return \OC\Files\Stream\Quota::wrap($source, $limit);
	}

	public function testWriteEnoughSpace() {
		$stream = $this->getStream('w+', 100);
		$this->assertEquals(6, fwrite($stream, 'foobar'));
		rewind($stream);
		$this->assertEquals('foobar', fread($stream, 100));
	}

	public function testWriteNotEnoughSpace() {
		$stream = $this->getStream('w+', 3);
		$this->assertEquals(3, fwrite($stream, 'foobar'));
		rewind($stream);
		$this->assertEquals('foo', fread($stream, 100));
	}

	public function testWriteNotEnoughSpaceSecondTime() {
		$stream = $this->getStream('w+', 9);
		$this->assertEquals(6, fwrite($stream, 'foobar'));
		$this->assertEquals(3, fwrite($stream, 'qwerty'));
		rewind($stream);
		$this->assertEquals('foobarqwe', fread($stream, 100));
	}

	public function testWriteEnoughSpaceRewind() {
		$stream = $this->getStream('w+', 6);
		$this->assertEquals(6, fwrite($stream, 'foobar'));
		rewind($stream);
		$this->assertEquals(3, fwrite($stream, 'qwe'));
		rewind($stream);
		$this->assertEquals('qwebar', fread($stream, 100));
	}

	public function testWriteNotEnoughSpaceRead() {
		$stream = $this->getStream('w+', 6);
		$this->assertEquals(6, fwrite($stream, 'foobar'));
		rewind($stream);
		$this->assertEquals('foobar', fread($stream, 6));
		$this->assertEquals(0, fwrite($stream, 'qwe'));
	}

	public function testWriteNotEnoughSpaceExistingStream() {
		$source = fopen('php://temp', 'w+');
		fwrite($source, 'foobar');
		$stream = \OC\Files\Stream\Quota::wrap($source, 3);
		$this->assertEquals(3, fwrite($stream, 'foobar'));
		rewind($stream);
		$this->assertEquals('foobarfoo', fread($stream, 100));
	}

	public function testWriteNotEnoughSpaceExistingStreamRewind() {
		$source = fopen('php://temp', 'w+');
		fwrite($source, 'foobar');
		$stream = \OC\Files\Stream\Quota::wrap($source, 3);
		rewind($stream);
		$this->assertEquals(6, fwrite($stream, 'qwerty'));
		rewind($stream);
		$this->assertEquals('qwerty', fread($stream, 100));
	}

	public function testFseekReturnsSuccess() {
		$stream = $this->getStream('w+', 100);
		fwrite($stream, '0123456789');
		$this->assertEquals(0, fseek($stream, 3, SEEK_SET));
		$this->assertEquals(0, fseek($stream, -1, SEEK_CUR));
		$this->assertEquals(0, fseek($stream, -4, SEEK_END));
	}

	public function testWriteAfterSeekEndWithEnoughSpace() {
		$stream = $this->getStream('w+', 100);
		fwrite($stream, '0123456789');
		fseek($stream, -3, SEEK_END);
		$this->assertEquals(11, fwrite($stream, 'abcdefghijk'));
		rewind($stream);
		$this->assertEquals('0123456abcdefghijk', fread($stream, 100));
	}

	public function testWriteAfterSeekEndWithNotEnoughSpace() {
		$stream = $this->getStream('w+', 13);
		fwrite($stream, '0123456789');
		// seek forward first to potentially week out
		// potential limit calculation errors
		fseek($stream, 4, SEEK_SET);
		// seek to the end
		fseek($stream, -3, SEEK_END);
		$this->assertEquals(6, fwrite($stream, 'abcdefghijk'));
		rewind($stream);
		$this->assertEquals('0123456abcdef', fread($stream, 100));
	}

	public function testWriteAfterSeekSetWithEnoughSpace() {
		$stream = $this->getStream('w+', 100);
		fwrite($stream, '0123456789');
		fseek($stream, 7, SEEK_SET);
		$this->assertEquals(11, fwrite($stream, 'abcdefghijk'));
		rewind($stream);
		$this->assertEquals('0123456abcdefghijk', fread($stream, 100));
	}

	public function testWriteAfterSeekSetWithNotEnoughSpace() {
		$stream = $this->getStream('w+', 13);
		fwrite($stream, '0123456789');
		fseek($stream, 7, SEEK_SET);
		$this->assertEquals(6, fwrite($stream, 'abcdefghijk'));
		rewind($stream);
		$this->assertEquals('0123456abcdef', fread($stream, 100));
	}

	public function testWriteAfterSeekCurWithEnoughSpace() {
		$stream = $this->getStream('w+', 100);
		fwrite($stream, '0123456789');
		rewind($stream);
		fseek($stream, 3, SEEK_CUR);
		fseek($stream, 5, SEEK_CUR);
		fseek($stream, -1, SEEK_CUR);
		$this->assertEquals(11, fwrite($stream, 'abcdefghijk'));
		rewind($stream);
		$this->assertEquals('0123456abcdefghijk', fread($stream, 100));
	}

	public function testWriteAfterSeekCurWithNotEnoughSpace() {
		$stream = $this->getStream('w+', 13);
		fwrite($stream, '0123456789');
		rewind($stream);
		fseek($stream, 3, SEEK_CUR);
		fseek($stream, 5, SEEK_CUR);
		fseek($stream, -1, SEEK_CUR);
		$this->assertEquals(6, fwrite($stream, 'abcdefghijk'));
		rewind($stream);
		$this->assertEquals('0123456abcdef', fread($stream, 100));
	}
}