summaryrefslogtreecommitdiffstats
path: root/application/test/tests/test_filebin_helper.php
blob: a46d4bc3ca73af8e03323c30cce07f4bdf9ec234 (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
<?php
/*
 * Copyright 2016 Florian "Bluewind" Pritz <bluewind@server-speed.net>
 *
 * Licensed under AGPLv3
 * (see COPYING for full license text)
 *
 */

namespace test\tests;

class test_filebin_helper extends \test\Test {

	public function __construct()
	{
		parent::__construct();
	}

	public function init()
	{
	}

	public function cleanup()
	{
	}

  public function test_expiration_duration()
  {
    $this->t->is(expiration_duration(60*60*24*2), "2 days", "2 days");
    $this->t->is(expiration_duration(60*60*24), "1 day", "1 day");
    $this->t->is(expiration_duration(60*60*2), "2 hours", "2 hours");
    $this->t->is(expiration_duration(60*60), "1 hour", "1 hour");
    $this->t->is(expiration_duration(60*2), "2 minutes", "2 minutes");
    $this->t->is(expiration_duration(60), "1 minute", "1 minute");
    $this->t->is(expiration_duration(59), "59 seconds", "59 seconds");
    $this->t->is(expiration_duration(1), "1 second", "1 second");

    $this->t->is(expiration_duration(60*60*24 + 60*60 + 60), "1 day, 1 hour, 1 minute", "1 day, 1 hour, 1 minute");
    $this->t->is(expiration_duration(60*60*24 + 60*60 + 120), "1 day, 1 hour, 2 minutes", "1 day, 1 hour, 2 minutes");
    $this->t->is(expiration_duration(60*60*24 + 60*60*2 + 60), "1 day, 2 hours, 1 minute", "1 day, 2 hours, 1 minute");
    $this->t->is(expiration_duration(60*60*24 + 60*60*2 + 120), "1 day, 2 hours, 2 minutes", "1 day, 2 hours, 2 minutes");
    $this->t->is(expiration_duration(60*60*24*2 + 60*60 + 60), "2 days, 1 hour, 1 minute", "2 days, 1 hour, 1 minute");
    $this->t->is(expiration_duration(60*60*24*2 + 60*60 + 120), "2 days, 1 hour, 2 minutes", "2 days, 1 hour, 2 minutes");
    $this->t->is(expiration_duration(60*60*24*2 + 60*60*2 + 60), "2 days, 2 hours, 1 minute", "2 days, 2 hours, 1 minute");
    $this->t->is(expiration_duration(60*60*24*2 + 60*60*2 + 120), "2 days, 2 hours, 2 minutes", "2 days, 2 hours, 2 minutes");

    $this->t->is(expiration_duration(60*60*24 + 60*60), "1 day, 1 hour", "1 day, 1 hour");
    $this->t->is(expiration_duration(60*60*24 + 60*60*2), "1 day, 2 hours", "1 day, 2 hours");
    $this->t->is(expiration_duration(60*60*24*2 + 60*60), "2 days, 1 hour", "2 days, 1 hour");
    $this->t->is(expiration_duration(60*60*24*2 + 60*60*2), "2 days, 2 hours", "2 days, 2 hours");

    $this->t->is(expiration_duration(60*60*24 + 60), "1 day, 1 minute", "1 day, 1 minute");
    $this->t->is(expiration_duration(60*60*24 + 120), "1 day, 2 minutes", "1 day, 2 minutes");
    $this->t->is(expiration_duration(60*60*24*2 + 60), "2 days, 1 minute", "2 days, 1 minute");
    $this->t->is(expiration_duration(60*60*2*24 + 120), "2 days, 2 minutes", "2 days, 2 minutes");

    $this->t->is(expiration_duration(60*60 + 60), "1 hour, 1 minute", "1 hour, 1 minute");
    $this->t->is(expiration_duration(60*60 + 120), "1 hour, 2 minutes", "1 hour, 2 minutes");
    $this->t->is(expiration_duration(60*60*2 + 60), "2 hours, 1 minute", "2 hours, 1 minute");
    $this->t->is(expiration_duration(60*60*2 + 120), "2 hours, 2 minutes", "2 hours, 2 minutes");

    $this->t->is(expiration_duration(61), "1 minute, 1 second", "1 minute, 1 second");
    $this->t->is(expiration_duration(62), "1 minute, 2 seconds", "1 minute, 2 seconds");
    $this->t->is(expiration_duration(121), "2 minutes, 1 second", "2 minutes, 1 second");
    $this->t->is(expiration_duration(122), "2 minutes, 2 seconds", "2 minutes, 2 seconds");

    $this->t->is(expiration_duration(60*60*24 + 60*60*23 + 60*59), "1 day, 23 hours, 59 minutes", "1 day, 23 hours, 59 minutes");
    $this->t->is(expiration_duration(60*60*23 + 60*59), "23 hours, 59 minutes", "23 hours, 59 minutes");
    $this->t->is(expiration_duration(60*60*2 + 60*59), "2 hours, 59 minutes", "2 hours, 59 minutes");
  }

	public function test_format_bytes()
	{
		$this->t->is(format_bytes(500), "500B", "500B");
		$this->t->is(format_bytes(1500), "1500B", "1500B");
		$this->t->is(format_bytes(1500*1024), "1500.00KiB", "1500.00KiB");
		$this->t->is(format_bytes(1500*1024*1024), "1500.00MiB", "1500.00MiB");
		$this->t->is(format_bytes(1500*1024*1024*1024), "1500.00GiB", "1500.00GiB");
		$this->t->is(format_bytes(1500*1024*1024*1024*1024), "1500.00TiB", "1500.00TiB");
		$this->t->is(format_bytes(1500*1024*1024*1024*1024*1024), "1500.00PiB", "1500.00PiB");
	}

	public function test_files_are_equal()
	{
		$a1 = FCPATH.'/data/tests/message1.bin';
		$a2 = FCPATH.'/data/tests/message2.bin';
		$b = FCPATH.'/data/tests/simple.pdf';
		$this->t->is(files_are_equal($a1, $a2), false, "Same hash, but different file");
		$this->t->is(files_are_equal($a1, $b), false, "Different filesize");
		$this->t->is(files_are_equal($a1, $a1), true, "Same file");
		$this->t->is(files_are_equal($a2, $a2), true, "Same file");
	}

	public function test_return_bytes()
	{
		$this->t->is(return_bytes("1k"), 1*1024, "1k");
		$this->t->is(return_bytes("1M"), 1*1024*1024, "1M");
		$this->t->is(return_bytes("1G"), 1*1024*1024*1024, "1G");

		try {
			return_bytes("1P");
		} catch (\exceptions\ApiException $e) {
			$this->t->is($e->get_error_id(), 'filebin-helper/invalid-input-unit', "unhandled text: 1P");
		}

		$this->t->is(return_bytes("106954752"), 106954752, "value without unit is returned as int");
	}
}