File: totp_test.cpp

package info (click to toggle)
megacmd 2.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 32,592 kB
  • sloc: cpp: 326,437; ansic: 34,524; python: 4,630; java: 3,965; sh: 2,869; objc: 2,459; makefile: 197; xml: 113
file content (150 lines) | stat: -rw-r--r-- 5,535 bytes parent folder | download | duplicates (2)
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
/**
 * @file
 * @brief Tests for the contents of totp.h file
 */

#include "mega/logging.h"
#include "mega/totp.h"
#include "megaapi.h"

#include <gtest/gtest.h>

#include <chrono>
#include <string>

using namespace mega;
using namespace mega::totp;
using namespace std::chrono;

struct TotpTestRow
{
    seconds timeSinceEpoch;
    std::string expectedResult;
    HashAlgorithm algorithm;

    /**
     * @brief Return the secret used in Appendix B in https://www.rfc-editor.org/rfc/rfc6238
     * There the secrets are in hex. Here they are translated to base32
     */
    std::string getSecretInRFC() const
    {
        switch (algorithm)
        {
            case HashAlgorithm::SHA1:
                return "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ";
            case HashAlgorithm::SHA256:
                return "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZA====";
            case HashAlgorithm::SHA512:
                return "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3"
                       "TQOJQGEZDGNBVGY3TQOJQGEZDGNA=";
        }
        return "";
    }
};

/**
 * @brief Test the cases presented in Appendix B in https://www.rfc-editor.org/rfc/rfc6238
 */
TEST(GenerateTOTP, RFC6238TestVector)
{
    static const std::vector<TotpTestRow> testsVectors{
        {59s, "94287082", HashAlgorithm::SHA1},
        {59s, "46119246", HashAlgorithm::SHA256},
        {59s, "90693936", HashAlgorithm::SHA512},
        {1111111109s, "07081804", HashAlgorithm::SHA1},
        {1111111109s, "68084774", HashAlgorithm::SHA256},
        {1111111109s, "25091201", HashAlgorithm::SHA512},
        {1111111111s, "14050471", HashAlgorithm::SHA1},
        {1111111111s, "67062674", HashAlgorithm::SHA256},
        {1111111111s, "99943326", HashAlgorithm::SHA512},
        {1234567890s, "89005924", HashAlgorithm::SHA1},
        {1234567890s, "91819424", HashAlgorithm::SHA256},
        {1234567890s, "93441116", HashAlgorithm::SHA512},
        {2000000000s, "69279037", HashAlgorithm::SHA1},
        {2000000000s, "90698825", HashAlgorithm::SHA256},
        {2000000000s, "38618901", HashAlgorithm::SHA512},
        {20000000000s, "65353130", HashAlgorithm::SHA1},
        {20000000000s, "77737706", HashAlgorithm::SHA256},
        {20000000000s, "47863826", HashAlgorithm::SHA512}};

    for (const auto& tv: testsVectors)
    {
        const auto [totp, expirationTime] =
            generateTOTP(tv.getSecretInRFC(), tv.timeSinceEpoch, 8, 30s, tv.algorithm);
        EXPECT_EQ(totp, tv.expectedResult);
        EXPECT_EQ(expirationTime.count(), 30 - tv.timeSinceEpoch.count() % 30);
    }
}

TEST(GenerateTOTP, PreconditionsFailure)
{
    EXPECT_EQ(generateTOTP("").first, "") << "Empty shared secret";
    EXPECT_EQ(generateTOTP("GEZDGN==BVGY3TQOJQGEZDGNBVGY3TQOJQ").first, "")
        << "Padding in between the secret";
    EXPECT_EQ(generateTOTP("AAAAA0").first, "") << "Invalid character (0)";
    EXPECT_EQ(generateTOTP("GEZDGN", 5).first, "") << "Less digits than allowed";
    EXPECT_EQ(generateTOTP("GEZDGN", 11).first, "") << "More digits than allowed";
    EXPECT_EQ(generateTOTP("GEZDGN", 6, -5s).first, "") << "Negative time step";
    EXPECT_EQ(generateTOTP("GEZDGN", 6, 0s).first, "") << "Zero time step";
    EXPECT_EQ(generateTOTP("GEZDGN",
                           6,
                           30s,
                           HashAlgorithm::SHA1,
                           system_clock::now(),
                           system_clock::now() - 5s)
                  .first,
              "")
        << "tEval lower than t0";
    EXPECT_EQ(generateTOTP("GEZDGN", -5s).first, "") << "Negative time delta";
}

using TotpData = MegaNode::PasswordNodeData::TotpData;
using Validation = MegaNode::PasswordNodeData::TotpData::Validation;

static std::pair<std::unique_ptr<TotpData>, std::unique_ptr<Validation>>
    generateData(const char* shse, const int expT, const int alg, const int nd)
{
    std::unique_ptr<TotpData> data{TotpData::createInstance(shse, expT, alg, nd)};
    std::unique_ptr<Validation> valid{data->getValidation()};
    return {std::move(data), std::move(valid)};
};

TEST(GenerateTOTPFromTotpData, PreconditionsFailure)
{
    static constexpr std::string_view logPre{"GenerateTOTPFromTotpData.PreconditionsFailure: "};
    {
        LOG_debug << logPre << "Empty shared secret";
        const auto [data, valid] = generateData("", -1, -1, -1);
        EXPECT_TRUE(valid->sharedSecretExist());
        EXPECT_FALSE(valid->sharedSecretValid());
        // Validate defaults in first case
        EXPECT_TRUE(valid->nDigitsValid());
        EXPECT_TRUE(valid->expirationTimeValid());
        EXPECT_TRUE(valid->algorithmValid());
    }

    {
        LOG_debug << logPre << "Invalid shared secret";
        const auto [data, valid] = generateData("GEZDGN==BVGY3TQOJQGEZDGNBVGY3TQOJQ", -1, -1, -1);
        EXPECT_TRUE(valid->sharedSecretExist());
        EXPECT_FALSE(valid->sharedSecretValid());
    }

    {
        LOG_debug << logPre << "Invalid expiration time";
        const auto [data, valid] = generateData("GEZDGN", 0, -1, -1);
        EXPECT_FALSE(valid->expirationTimeValid());
    }

    {
        LOG_debug << logPre << "Invalid hash algorithm";
        const auto [data, valid] = generateData("GEZDGN", -1, 50, -1);
        EXPECT_FALSE(valid->algorithmValid());
    }

    {
        LOG_debug << logPre << "Invalid digits";
        const auto [data, valid] = generateData("GEZDGN", -1, -1, 5);
        EXPECT_FALSE(valid->nDigitsValid());
    }
}