File size: 4,831 Bytes
bf8c0ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Tests\Integration\Tracker;

use Piwik\Date;
use Piwik\Tracker\FingerprintSalt;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

/**
 * @group Core
 * @group ActionTest
 */
class FingerprintSaltTest extends IntegrationTestCase
{
    /**
     * @var FingerprintSalt
     */
    private $fingerprintSalt;

    public function setUp(): void
    {
        parent::setUp();

        $this->fingerprintSalt = new FingerprintSalt();
    }

    public function testGenerateSalt()
    {
        $salt = $this->fingerprintSalt->generateSalt();
        $this->assertEquals(32, strlen($salt));
        $this->assertTrue(ctype_alnum($salt));
    }

    public function testGenerateSaltIsRandom()
    {
        $this->assertNotSame($this->fingerprintSalt->generateSalt(), $this->fingerprintSalt->generateSalt());
    }

    public function testGetDateString()
    {
        $date = Date::factory('2020-05-05 14:04:05');
        $this->assertSame('2020-05-06', $this->fingerprintSalt->getDateString($date, 'Pacific/Auckland'));
        $this->assertSame('2020-05-05', $this->fingerprintSalt->getDateString($date, 'Europe/Berlin'));
    }

    public function testGetDateStringDoubleCheckingWeAreGeneratingRightString()
    {
        for ($i = 0; $i <= 23; $i++) {
            $d  = '2020-05-05 ' . $i . ':04:05';
            $date = Date::factory($d);
            // double checking using the logic used in CoreHome::VisitRequestProcesser::wasLastActionNotToday where we detect midnight
            // in timezone just to double check we return the correct date string for the given timezone. Should anything change and test not pass
            // then we might have a problem that we would generate a different fingerprint for the same visitor even though it is not midnight
            // just yet. Meaning during the day we would suddenly change the fingerprint hash causing additional visits to be created when not needed yet
            $this->assertSame($this->fingerprintSalt->getDateString($date, 'Pacific/Auckland'), Date::factory($d, 'Pacific/Auckland')->toString());
            $this->assertSame($this->fingerprintSalt->getDateString($date, 'Europe/Berlin'), Date::factory($d, 'Europe/Berlin')->toString());
            $this->assertSame($this->fingerprintSalt->getDateString($date, 'America/Los_Angeles'), Date::factory($d, 'America/Los_Angeles')->toString());
        }
    }

    public function testGetSaltRemembersSaltPerSite()
    {
        $salt05_1 = $this->fingerprintSalt->getSalt('2020-05-05', $idSite = 1);
        $salt06_1 = $this->fingerprintSalt->getSalt('2020-05-06', $idSite = 1);
        $salt05_2 = $this->fingerprintSalt->getSalt('2020-05-05', $idSite = 2);
        $salt06_2 = $this->fingerprintSalt->getSalt('2020-05-06', $idSite = 2);

        $this->assertNotSame($salt05_1, $salt06_1);
        $this->assertNotSame($salt05_2, $salt06_2);
        $this->assertNotSame($salt06_1, $salt06_2);

        $this->assertSame($salt05_1, $this->fingerprintSalt->getSalt('2020-05-05', $idSite = 1));
        $this->assertSame($salt06_1, $this->fingerprintSalt->getSalt('2020-05-06', $idSite = 1));
        $this->assertSame($salt05_2, $this->fingerprintSalt->getSalt('2020-05-05', $idSite = 2));
    }

    public function testDeleteOldSaltsWhenNothingToDelete()
    {
        $this->fingerprintSalt->getSalt('2020-05-05', $idSite = 1);
        $this->fingerprintSalt->getSalt('2020-05-06', $idSite = 1);

        Date::$now = time() - FingerprintSalt::DELETE_FINGERPRINT_OLDER_THAN_SECONDS + 30;// they would expire in 30 seconds
        $this->fingerprintSalt->getSalt('2020-05-05', $idSite = 2);
        $this->fingerprintSalt->getSalt('2020-05-06', $idSite = 2);

        Date::$now = time();
        $this->assertSame(array(), $this->fingerprintSalt->deleteOldSalts());
    }

    public function testDeleteOldSaltsSomeToBeDeleted()
    {
        $this->fingerprintSalt->getSalt('2020-05-05', $idSite = 1);

        Date::$now = time() - FingerprintSalt::DELETE_FINGERPRINT_OLDER_THAN_SECONDS - 30; // these entries should be expired
        $this->fingerprintSalt->getSalt('2020-05-06', $idSite = 1);
        $this->fingerprintSalt->getSalt('2020-05-05', $idSite = 2);
        $this->fingerprintSalt->getSalt('2020-05-06', $idSite = 2);

        Date::$now = time();
        $this->assertSame(array(
            'fingerprint_salt_1_2020-05-06',
            'fingerprint_salt_2_2020-05-05',
            'fingerprint_salt_2_2020-05-06',
        ), $this->fingerprintSalt->deleteOldSalts());

        // executing it again wont delete anything
        $this->assertSame(array(), $this->fingerprintSalt->deleteOldSalts());
    }
}