File: session_test.cpp

package info (click to toggle)
sight 25.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,252 kB
  • sloc: cpp: 310,629; xml: 17,622; ansic: 9,960; python: 1,379; sh: 144; makefile: 33
file content (513 lines) | stat: -rw-r--r-- 16,435 bytes parent folder | download
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/************************************************************************
 *
 * Copyright (C) 2021-2025 IRCAD France
 *
 * This file is part of Sight.
 *
 * Sight is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Sight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

#include "session_test.hpp"

#include <core/com/slot.hpp>
#include <core/com/slot.hxx>
#include <core/os/temp_path.hpp>

#include <data/string.hpp>

#include <io/__/service/io_types.hpp>
#include <io/__/service/reader.hpp>
#include <io/__/service/writer.hpp>

#include <service/base.hpp>
#include <service/op.hpp>

#include <ui/test/dialog/input.hpp>
#include <ui/test/dialog/location.hpp>
#include <ui/test/dialog/message.hpp>

// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION(sight::module::io::session::ut::session_test);

namespace sight::module::io::session::ut
{

//------------------------------------------------------------------------------

void session_test::setUp()
{
}

//------------------------------------------------------------------------------

void session_test::tearDown()
{
}

//------------------------------------------------------------------------------

// Returns a basic configuration for reader and writer
inline static service::config_t setup_config(const bool _read = true, const bool _raw = false)
{
    service::config_t config;

    config.put("dialog.<xmlattr>.extension", ".perlimpinpin");
    config.put("dialog.<xmlattr>.description", "Perlimpinpin powder container");
    config.put("dialog.<xmlattr>.policy", "never");

    config.put("password.<xmlattr>.policy", "never");
    config.put("password.<xmlattr>.encryption", "password");

    if(_raw)
    {
        config.put("archive.<xmlattr>.format", "filesystem");
    }
    else if(_read)
    {
        config.put("archive.<xmlattr>.format", "archive");
    }
    else
    {
        config.put("archive.<xmlattr>.format", "optimized");
    }

    return config;
}

//------------------------------------------------------------------------------

inline static void basic_test(const bool _raw = false)
{
    // Create a temporary file
    core::os::temp_dir tmp_dir;
    const auto& tmp_file = tmp_dir / "powder.perlimpinpin";

    const std::string expected("Abracadabra");
    {
        // Create a writer service
        auto writer = std::dynamic_pointer_cast<sight::io::service::writer>(
            service::add("sight::module::io::session::writer")
        );
        CPPUNIT_ASSERT(writer);

        // Set data input
        auto in_string = std::make_shared<data::string>(expected);
        writer->set_input(in_string, sight::io::service::DATA_KEY);

        // Set file output
        writer->set_file(tmp_file);

        // Configure the writer service
        writer->set_config(setup_config(false, _raw));
        writer->configure();

        // Execute the writer service
        writer->start().wait();
        writer->update().wait();
        writer->stop().wait();

        // Cleanup
        service::unregister_service(writer);
    }

    // The file should have been created
    CPPUNIT_ASSERT(
        std::filesystem::exists(tmp_file)
        && std::filesystem::is_regular_file(tmp_file)
        && std::filesystem::file_size(tmp_file) > 0
    );

    {
        // Create a reader service
        auto reader = std::dynamic_pointer_cast<sight::io::service::reader>(
            service::add("sight::module::io::session::reader")
        );
        CPPUNIT_ASSERT(reader);

        // Set data output
        auto out_string = std::make_shared<data::string>();
        reader->set_inout(out_string, sight::io::service::DATA_KEY);

        // Set file input
        reader->set_file(tmp_file);

        // Create slot connections
        bool session_loaded = false;
        std::filesystem::path session_loaded_path;
        auto session_loaded_slot = sight::core::com::new_slot(
            [&](std::filesystem::path _path)
            {
                session_loaded      = true;
                session_loaded_path = _path;
            });
        session_loaded_slot->set_worker(sight::core::thread::get_default_worker());
        auto conn1 = reader->signal("session_loaded")->connect(session_loaded_slot);

        bool session_loading_failed = false;
        std::filesystem::path session_loading_failed_path;
        auto session_loading_failed_slot = sight::core::com::new_slot(
            [&session_loading_failed, &session_loading_failed_path](std::filesystem::path _path)
            {
                session_loading_failed      = true;
                session_loading_failed_path = _path;
            });
        session_loading_failed_slot->set_worker(sight::core::thread::get_default_worker());
        auto conn2 = reader->signal("session_loading_failed")->connect(session_loading_failed_slot);

        // Configure the reader service
        reader->set_config(setup_config(true, _raw));
        reader->configure();

        // Execute the writer service
        reader->start().wait();
        reader->update().wait();
        reader->stop().wait();

        // Cleanup
        service::unregister_service(reader);

        // Final test
        CPPUNIT_ASSERT(session_loaded);
        CPPUNIT_ASSERT_EQUAL(session_loaded_path, tmp_file);
        CPPUNIT_ASSERT(!session_loading_failed);
        CPPUNIT_ASSERT_EQUAL(session_loading_failed_path, std::filesystem::path(""));

        CPPUNIT_ASSERT_EQUAL(expected, out_string->get_value());
    }
}

//------------------------------------------------------------------------------

void session_test::basic_archive_test()
{
    basic_test(false);
}

//------------------------------------------------------------------------------

void session_test::basic_raw_test()
{
    basic_test(true);
}

//------------------------------------------------------------------------------

static void bad_policy_test(bool _reader, const std::string& _key, const std::string& _value)
{
    auto config = setup_config(_reader);
    config.put(_key, _value);
    service::base::sptr service;
    if(_reader)
    {
        service = service::add("sight::module::io::session::reader");
    }
    else
    {
        service = service::add("sight::module::io::session::writer");
    }

    CPPUNIT_ASSERT(service);
    service->set_config(config);
    CPPUNIT_ASSERT_THROW(service->configure(), sight::core::exception);
    service::remove(service);
}

//------------------------------------------------------------------------------

void session_test::reader_bad_dialog_policy_test()
{
    bad_policy_test(true, "dialog.<xmlattr>.policy", "whenever_i_want");
}

//------------------------------------------------------------------------------

void session_test::reader_bad_password_policy_test()
{
    bad_policy_test(true, "password.<xmlattr>.policy", "when_i_remember_it");
}

//------------------------------------------------------------------------------

void session_test::reader_bad_password_encryption_test()
{
    bad_policy_test(true, "password.<xmlattr>.encryption", "sweet");
}

//------------------------------------------------------------------------------

void session_test::reader_bad_file()
{
    // Create a temporary file
    core::os::temp_dir tmp_dir;
    const auto& tmp_file = tmp_dir / "powder.perlimpinpin";

    {
        // Create a reader service
        auto reader = std::dynamic_pointer_cast<sight::io::service::reader>(
            service::add("sight::module::io::session::reader")
        );
        CPPUNIT_ASSERT(reader);

        // Set data output
        auto out_string = std::make_shared<data::string>();
        reader->set_inout(out_string, sight::io::service::DATA_KEY);

        // Set file input
        reader->set_file(tmp_file);

        // Create slot connections
        bool session_loaded = false;
        std::filesystem::path session_loaded_path;
        auto session_loaded_slot = sight::core::com::new_slot(
            [&](std::filesystem::path _path)
            {
                session_loaded      = true;
                session_loaded_path = _path;
            });
        session_loaded_slot->set_worker(sight::core::thread::get_default_worker());
        auto conn1 = reader->signal("session_loaded")->connect(session_loaded_slot);

        bool session_loading_failed = false;
        std::filesystem::path session_loading_failed_path;
        auto session_loading_failed_slot = sight::core::com::new_slot(
            [&session_loading_failed, &session_loading_failed_path](std::filesystem::path _path)
            {
                session_loading_failed      = true;
                session_loading_failed_path = _path;
            });
        session_loading_failed_slot->set_worker(sight::core::thread::get_default_worker());
        auto conn2 = reader->signal("session_loading_failed")->connect(session_loading_failed_slot);

        // Configure the reader service
        reader->set_config(setup_config(true, false));
        reader->configure();

        // Execute the writer service
        reader->start().wait();
        reader->update().wait();
        reader->stop().wait();

        // Cleanup
        service::unregister_service(reader);

        // Final test
        CPPUNIT_ASSERT(!session_loaded);
        CPPUNIT_ASSERT_EQUAL(session_loaded_path, std::filesystem::path(""));
        CPPUNIT_ASSERT(session_loading_failed);
        CPPUNIT_ASSERT_EQUAL(session_loading_failed_path, tmp_file);
    }
}

//------------------------------------------------------------------------------

void session_test::writer_bad_dialog_policy_test()
{
    bad_policy_test(false, "dialog.<xmlattr>.policy", "whenever_i_want");
}

//------------------------------------------------------------------------------

void session_test::writer_bad_password_policy_test()
{
    bad_policy_test(false, "password.<xmlattr>.policy", "when_i_remember_it");
}

//------------------------------------------------------------------------------

void session_test::writer_bad_password_encryption_test()
{
    bad_policy_test(false, "password.<xmlattr>.encryption", "sweet");
}

//------------------------------------------------------------------------------

void session_test::file_dialog_test()
{
    // Create a temporary file
    core::os::temp_dir tmp_dir;
    const auto& tmp_file = tmp_dir / "powder.perlimpinpin";

    const std::string expected("Abracadabra");
    {
        // Create a writer service
        auto writer = std::dynamic_pointer_cast<sight::io::service::writer>(
            service::add("sight::module::io::session::writer")
        );
        CPPUNIT_ASSERT(writer);

        // Set data input
        auto in_string = std::make_shared<data::string>(expected);
        writer->set_input(in_string, sight::io::service::DATA_KEY);

        // Configure the writer service
        auto config = setup_config(false);
        config.put("dialog.<xmlattr>.policy", "always");
        writer->set_config(config);
        writer->configure();

        // Execute the writer service
        writer->start().wait();

        sight::ui::test::dialog::location::set_paths({tmp_file});

        writer->update().wait();
        writer->stop().wait();

        // Cleanup
        service::unregister_service(writer);

        CPPUNIT_ASSERT(sight::ui::test::dialog::location::clear());
    }

    // The file should have been created
    CPPUNIT_ASSERT(
        std::filesystem::exists(tmp_file)
        && std::filesystem::is_regular_file(tmp_file)
        && std::filesystem::file_size(tmp_file) > 0
    );

    {
        // Create a reader service
        auto reader = std::dynamic_pointer_cast<sight::io::service::reader>(
            service::add("sight::module::io::session::reader")
        );
        CPPUNIT_ASSERT(reader);

        // Set data output
        auto out_string = std::make_shared<data::string>();
        reader->set_inout(out_string, sight::io::service::DATA_KEY);

        // Configure the reader service
        auto config = setup_config(true);
        config.put("dialog.<xmlattr>.policy", "always");
        reader->set_config(config);
        reader->configure();

        // Execute the writer service
        reader->start().wait();

        sight::ui::test::dialog::location::set_paths({tmp_file});

        reader->update().wait();
        reader->stop().wait();

        // Cleanup
        service::unregister_service(reader);

        // Final test
        CPPUNIT_ASSERT_EQUAL(expected, out_string->get_value());

        CPPUNIT_ASSERT(sight::ui::test::dialog::location::clear());
    }
}

//------------------------------------------------------------------------------

void session_test::password_test()
{
    // Create a temporary file
    core::os::temp_dir tmp_dir;
    const auto& tmp_file = tmp_dir / "powder.perlimpinpin";

    const std::string expected("Abracadabra");
    {
        // Create a writer service
        auto writer = std::dynamic_pointer_cast<sight::io::service::writer>(
            service::add("sight::module::io::session::writer")
        );
        CPPUNIT_ASSERT(writer);

        // Set data input
        auto in_string = std::make_shared<data::string>(expected);
        writer->set_input(in_string, sight::io::service::DATA_KEY);

        // Set file output
        writer->set_file(tmp_file);

        // Configure the writer service
        auto config = setup_config(false);
        config.put("password.<xmlattr>.policy", "always");
        writer->set_config(config);
        writer->configure();

        // Execute the writer service
        writer->start().wait();

        sight::ui::test::dialog::input::push_input("case-sensitive");

        writer->update().wait();
        writer->stop().wait();

        // Cleanup
        service::unregister_service(writer);

        CPPUNIT_ASSERT(sight::ui::test::dialog::input::clear());
    }

    // The file should have been created
    CPPUNIT_ASSERT(
        std::filesystem::exists(tmp_file)
        && std::filesystem::is_regular_file(tmp_file)
        && std::filesystem::file_size(tmp_file) > 0
    );

    {
        // Create a reader service
        auto reader = std::dynamic_pointer_cast<sight::io::service::reader>(
            service::add("sight::module::io::session::reader")
        );
        CPPUNIT_ASSERT(reader);

        // Set data output
        auto out_string = std::make_shared<data::string>();
        reader->set_inout(out_string, sight::io::service::DATA_KEY);

        // Set file input
        reader->set_file(tmp_file);

        // Configure the reader service
        auto config = setup_config(true);
        config.put("password.<xmlattr>.policy", "always");
        reader->set_config(config);
        reader->configure();

        // Execute the writer service
        reader->start().wait();

        sight::ui::test::dialog::input::push_input("Oops");
        ui::test::dialog::message::push_action(sight::ui::test::dialog::message::retry);
        sight::ui::test::dialog::input::push_input("I forgot");
        ui::test::dialog::message::push_action(sight::ui::test::dialog::message::retry);
        sight::ui::test::dialog::input::push_input("Wait I remember");
        ui::test::dialog::message::push_action(sight::ui::test::dialog::message::retry);
        sight::ui::test::dialog::input::push_input("case-sensitive");

        reader->update().wait();
        reader->stop().wait();

        // Cleanup
        service::unregister_service(reader);

        // Final test
        CPPUNIT_ASSERT_EQUAL(expected, out_string->get_value());

        CPPUNIT_ASSERT(sight::ui::test::dialog::input::clear());
        CPPUNIT_ASSERT(ui::test::dialog::message::clear());
    }
}

//------------------------------------------------------------------------------

} // namespace sight::module::io::session::ut