File: pwm_file_parser_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 (207 lines) | stat: -rw-r--r-- 8,502 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "mega/pwm_file_parser.h"
#include "sdk_test_utils.h"

#include <gmock/gmock.h>
#include <gtest/gtest.h>

using namespace mega::pwm::import;
using testing::HasSubstr;
using testing::Not;

TEST(PWMImportGooglePasswordCSVFile, WellFormatedFile)
{
    constexpr std::string_view fileContents{R"(name,url,username,password,note
foo.com,https://foo.com/,tx,"hola""""\""\"".,,",
hello.co,https://hello.co/,hello,hello.1234,Description with Γ±
test.com,https://test.com/,test3,"hello.12,34",
test.com,https://test.com/,txema,hel\nlo.1234,""
test2.com,https://test2.com/,test,hello.1234,
,https://nopassname.com/,test,hello.1234,
HeLLO😍🀣πŸ₯°πŸ˜‰πŸ₯°πŸ˜ŒπŸ₯°πŸ˜‹πŸ˜˜πŸ˜Œ,https://m.facebook.com/,😌,123,😍HeLLO😌
)"};
    const std::vector<std::vector<std::string_view>> expected{
        {"foo.com", "https://foo.com/", "tx", R"(hola""\"\".,,)", ""},
        {"hello.co", "https://hello.co/", "hello", "hello.1234", "Description with Γ±"},
        {"test.com", "https://test.com/", "test3", "hello.12,34", ""},
        {"test.com", "https://test.com/", "txema", "hel\\nlo.1234", ""},
        {"test2.com", "https://test2.com/", "test", "hello.1234", ""},
        {"", "https://nopassname.com/", "test", "hello.1234", ""},
        {"HeLLO😍🀣πŸ₯°πŸ˜‰πŸ₯°πŸ˜ŒπŸ₯°πŸ˜‹πŸ˜˜πŸ˜Œ",
         "https://m.facebook.com/",
         "😌",
         "123",
         "😍HeLLO😌"}};
    const std::string fname = "test.csv";
    sdk_test::LocalTempFile f{fname, fileContents};

    auto results = parseGooglePasswordCSVFile(fname);
    ASSERT_TRUE(results.mErrMsg.empty());
    ASSERT_EQ(results.mErrCode, PassFileParseResult::ErrCode::OK);

    size_t i = 0;
    ASSERT_EQ(results.mResults.size(), expected.size());
    for (const auto& result: results.mResults)
    {
        EXPECT_EQ(result.mErrCode, PassEntryParseResult::ErrCode::OK);
        const auto& e = expected[i++];
        EXPECT_EQ(result.mName, e[0]);
        EXPECT_EQ(result.mUrl, e[1]);
        EXPECT_EQ(result.mUserName, e[2]);
        EXPECT_EQ(result.mPassword, e[3]);
        EXPECT_EQ(result.mNote, e[4]);
    }
}

TEST(PWMImportGooglePasswordCSVFile, MissingHeader)
{
    constexpr std::string_view fileContents{
        R"(hello.co,https://hello.co/,hello,hello.1234,Description with Γ±
test2.com,https://test2.com/,test,hello.1234,
)"};
    const std::string fname = "test.csv";
    sdk_test::LocalTempFile f{fname, fileContents};

    auto results = parseGooglePasswordCSVFile(fname);
    ASSERT_THAT(results.mErrMsg, HasSubstr("column with name: name"));
    ASSERT_THAT(results.mErrMsg, HasSubstr("column with name: url"));
    ASSERT_THAT(results.mErrMsg, HasSubstr("column with name: username"));
    ASSERT_THAT(results.mErrMsg, HasSubstr("column with name: password"));
    ASSERT_THAT(results.mErrMsg, HasSubstr("column with name: note"));
    ASSERT_THAT(results.mErrMsg, HasSubstr("expected to be a header with the column"));

    ASSERT_EQ(results.mErrCode, PassFileParseResult::ErrCode::INVALID_HEADER);
    ASSERT_TRUE(results.mResults.empty());
}

TEST(PWMImportGooglePasswordCSVFile, MissingColumnInHeader)
{
    constexpr std::string_view fileContents{
        R"(name,url,username,password,noteWrong
hello.co,https://hello.co/,hello,hello.1234,Description with Γ±
test2.com,https://test2.com/,test,hello.1234,
)"};
    const std::string fname = "test.csv";
    sdk_test::LocalTempFile f{fname, fileContents};

    auto results = parseGooglePasswordCSVFile(fname);
    ASSERT_THAT(results.mErrMsg, HasSubstr("column with name: note"));
    ASSERT_THAT(results.mErrMsg, Not(HasSubstr("expected to be a header with the column")));

    ASSERT_EQ(results.mErrCode, PassFileParseResult::ErrCode::MISSING_COLUMN);
    ASSERT_TRUE(results.mResults.empty());
}

TEST(PWMImportGooglePasswordCSVFile, MissingColumnInEntry)
{
    constexpr std::string_view fileContents{
        R"(name,url,username,password,note
https://hello.co/,hel"lo,hello.1234,Description with Γ±
test.com,https://test.com/,test3,hello.1234,
)"};
    const std::string fname = "test.csv";
    sdk_test::LocalTempFile f{fname, fileContents};

    auto results = parseGooglePasswordCSVFile(fname);
    ASSERT_TRUE(results.mErrMsg.empty());

    ASSERT_EQ(results.mErrCode, PassFileParseResult::ErrCode::OK);
    ASSERT_EQ(results.mResults.size(), 2);

    // The first is wrong
    const PassEntryParseResult& first = results.mResults[0];
    EXPECT_EQ(first.mErrCode, PassEntryParseResult::ErrCode::INVALID_NUM_OF_COLUMN);
    ASSERT_EQ(first.mOriginalContent, "https://hello.co/,hel\"lo,hello.1234,Description with Γ±");

    const PassEntryParseResult& second = results.mResults[1];
    EXPECT_EQ(second.mErrCode, PassEntryParseResult::ErrCode::OK);
    EXPECT_EQ(second.mName, "test.com");
    EXPECT_EQ(second.mUrl, "https://test.com/");
    EXPECT_EQ(second.mUserName, "test3");
    EXPECT_EQ(second.mPassword, "hello.1234");
    EXPECT_EQ(second.mNote, "");
}

TEST(PWMImportGooglePasswordCSVFile, AllEntriesWrong)
{
    constexpr std::string_view fileContents{
        R"(name,url,username,password,note
https://hello.co/,hello,hello.1234,Description with Γ±
test.com,https://test.com/,hello.1234,
)"};
    const std::string fname = "test.csv";
    sdk_test::LocalTempFile f{fname, fileContents};

    auto results = parseGooglePasswordCSVFile(fname);

    ASSERT_EQ(results.mErrCode, PassFileParseResult::ErrCode::NO_VALID_ENTRIES);
    EXPECT_EQ(results.mErrMsg, "All the entries in the file were wrongly formatted");
    ASSERT_EQ(results.mResults.size(), 2);

    // The first is wrong
    const PassEntryParseResult& first = results.mResults[0];
    EXPECT_EQ(first.mErrCode, PassEntryParseResult::ErrCode::INVALID_NUM_OF_COLUMN);
    ASSERT_EQ(first.mOriginalContent, "https://hello.co/,hello,hello.1234,Description with Γ±");

    const PassEntryParseResult& second = results.mResults[1];
    EXPECT_EQ(second.mErrCode, PassEntryParseResult::ErrCode::INVALID_NUM_OF_COLUMN);
    ASSERT_EQ(second.mOriginalContent, "test.com,https://test.com/,hello.1234,");
}

TEST(PWMImportGooglePasswordCSVFile, CompletelyWrongFile)
{
    constexpr std::string_view fileContents{
        R"(This is the conent of a text file not a csv
so this should trigger some errors.
)"};
    const std::string fname = "test.csv";
    sdk_test::LocalTempFile f{fname, fileContents};

    auto results = parseGooglePasswordCSVFile(fname);

    ASSERT_EQ(results.mErrCode, PassFileParseResult::ErrCode::INVALID_HEADER);
    ASSERT_THAT(results.mErrMsg, HasSubstr("column with name: name"));
    ASSERT_THAT(results.mErrMsg, HasSubstr("column with name: url"));
    ASSERT_THAT(results.mErrMsg, HasSubstr("column with name: username"));
    ASSERT_THAT(results.mErrMsg, HasSubstr("column with name: password"));
    ASSERT_THAT(results.mErrMsg, HasSubstr("column with name: note"));
    ASSERT_THAT(results.mErrMsg, HasSubstr("expected to be a header with the column"));
}

TEST(PWMImportGooglePasswordCSVFile, EmptyFile)
{
    const std::string fname = "test.csv";
    sdk_test::LocalTempFile f{fname, 0};

    auto results = parseGooglePasswordCSVFile(fname);

    ASSERT_EQ(results.mErrCode, PassFileParseResult::ErrCode::INVALID_HEADER);
    ASSERT_THAT(results.mErrMsg, HasSubstr("File should have at least a header row"));
}

TEST(PWMReadImportFile, FileDoesNotExist)
{
    const std::string fname = "test.csv";
    auto results = readPasswordImportFile(fname, FileSource::GOOGLE_PASSWORD);
    // The file existence is checked at higher levels but a cantOpenFile should be triggered
    ASSERT_EQ(results.mErrCode, PassFileParseResult::ErrCode::CANT_OPEN_FILE);
    ASSERT_THAT(results.mErrMsg, HasSubstr("could not be opened"));
}

TEST(PWMReadImportFile, GooglePassword)
{
    const std::string fname = "test.csv";

    constexpr std::string_view fileContents{R"(name,url,username,password,note
foo.com,https://foo.com/,tx,"hola""""\""\"".,,",
hello.co,https://hello.co/,hello,hello.1234,Description with Γ±
test.com,https://test.com/,test3,"hello.12,34",
test.com,https://test.com/,txema,hel\nlo.1234,""
test2.com,https://test2.com/,test,hello.1234,
)"};
    sdk_test::LocalTempFile f{fname, fileContents};
    auto resultsRead = readPasswordImportFile(fname, FileSource::GOOGLE_PASSWORD);
    auto resultsDirect = parseGooglePasswordCSVFile(fname);
    ASSERT_EQ(resultsDirect.mErrMsg, resultsRead.mErrMsg);
    ASSERT_EQ(resultsDirect.mErrCode, resultsRead.mErrCode);
    ASSERT_EQ(resultsDirect.mResults.size(), resultsRead.mResults.size());
}