-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy path_MyLib_test.cpp
More file actions
4355 lines (3672 loc) · 107 KB
/
_MyLib_test.cpp
File metadata and controls
4355 lines (3672 loc) · 107 KB
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file Tests for MyLib
* @brief
* @ref
* @author Yonhgwhan, Roh (fixbrain@gmail.com)
* @date 2014/01/25 created.
* @copyright All rights reserved by Yonghwan, Roh.
**/
#include "stdafx.h"
#include <regex>
#include <unordered_map>
#include <winioctl.h>
#include "libzippp/libzippp.h"
#include <boost/program_options.hpp>
#include "_MyLib/src/process_tree.h"
#include "_MyLib/src/base64.h"
#include "_MyLib/src/rc4.h"
#include "_MyLib/src/md5.h"
#include "_MyLib/src/sha2.h"
#include "_MyLib/src/Win32Utils.h"
#include "_MyLib/src/send_ping.h"
#include "_MyLib/src/wmi_client.h"
#include "_MyLib/src/nt_name_conv.h"
#include "_MyLib/src/crc64.h"
#include "_MyLib/src/StopWatch.h"
#include "_MyLib/src/GeneralHashFunctions.h"
#include "_MyLib/src/Singleton.h"
#include "_MyLib/src/account_info.h"
#include "_MyLib/src/CStream.h"
#include "_MyLib/src/sched_client.h"
// test_get_mbr_or_gpt_info.cpp
extern bool get_mbr_gpt_info();
extern bool ntp_client();
// test_unicode_string_wcsstr.cpp
extern bool test_uni_wcsstr();
// test_match.cpp
extern bool test_match();
// test_std_future_async.cpp
extern bool test_std_future_async();
// test_CStream.cpp
extern bool test_cstream();
extern bool test_cstream_read_only();
extern bool test_cstream_read_write_string();
// _test_log.cpp
extern bool test_log_rotate();
// _test_memory_leak.cpp
extern bool test_memory_leak();
extern bool test_aes256_crypt_buffer();
// _test_steady_timer.cpp
extern bool test_steady_timer();
extern bool test_steady_multiple_timer_in_single_thread();
// _test_dns_query.cpp
extern bool test_ip_to_dns();
extern bool test_ip_to_dns2();
extern bool test_dns_to_ip();
// _test_net_util.cpp
extern bool test_get_adapters();
extern bool test_get_addr_info();
extern bool test_is_reserved_ipv4();
extern bool test_is_valid_ipv4();
// test_iphelp_api.cpp
extern bool test_iphelp_api();
// _test_process_token.cpp
extern bool test_process_token();
// _test_monster_checksum_verifier.cpp
extern bool test_monster_checksum_verifier_main();
bool test_set_security_attributes();
bool test_GeneralHashFunctions();
bool test_GeneralHashFunctions2();
bool test_get_file_extension();
bool test_raii_xxx();
bool test_suspend_resume_process();
bool test_to_str();
bool test_convert_file_time();
// _test_ppl.cpp
extern bool test_ppl();
// _test_file_io_helper.cpp
bool test_file_io_helper();
bool test_file_io_helper2();
// _test_scm.cpp
extern bool test_scm_context();
bool test_alignment_error_test();
bool test_crc64();
bool test_NameConverter_iterate();
bool test_NameConverter_get_canon_name();
bool test_NameConverter_dosname_to_devicename();
extern bool test_NtCreateFile();
extern bool test_wmi_client();
bool test_ping();
bool test_regexp();
bool test_device_name_from_nt_name();
bool test_rstrnicmp();
bool test_get_drive_type();
bool test_os_version();
bool test_for_each();
bool test_find_and_replace();
// disk, volume stuffs.
bool read_file_offset(_In_ HANDLE file_handle, _In_ uint64_t offset, _In_ uint8_t* buf, _In_ uint32_t size);
bool write_file_offset(_In_ HANDLE file_handle, _In_ uint64_t offset, _In_ uint8_t* buf, _In_ uint32_t size);
void dump_file_offset(_In_ HANDLE file_handle, _In_ uint64_t offset, _In_ uint32_t size);
bool test_enum_physical_drive();
bool test_get_disk_volume_info();
bool test_dump_xxx();
bool test_write_mbr_vbr();
// _test_asm.cpp
bool test_asm_func();
// _test_x64.cpp
bool test_x64_calling_convension();
bool test_2_complement();
bool test_print_64int();
bool test_std_string_find_and_substr();
bool test_to_lower_uppper_string();
//bool test_const_position(); // 컴파일 불가 테스트
bool test_initialize_string();
bool test_partial_copy_string();
bool test_base64();
bool test_random();
bool test_ip_mac();
bool test_ip_to_str();
bool test_strtok();
bool test_split_stringw();
// test_process_tree.cpp
extern bool test_process_tree();
extern bool test_iterate_process_tree();
extern bool test_image_path_by_pid();
extern bool test_get_process_creation_time();
// _test_cpp_test.cpp
bool test_cpp_class();
// win32utils.cpp
bool test_find_files();
bool test_get_filepath_by_handle();
bool test_nt_name_to_dos_name();
bool test_query_dos_device();
bool test_bin_to_hex();
bool test_str_to_xxx();
bool test_set_get_file_position();
bool test_get_module_path();
bool test_dump_memory();
bool test_bin_to_str();
bool test_get_environment_value();
bool test_get_account_infos();
bool test_get_installed_programs();
bool test_get_file_company_name();
bool test_get_file_original_name();
bool test_generate_random_string();
bool test_bit_check_set_clear();
bool test_file_time_stuff();
// rc4.cpp
bool test_rc4_encrypt();
// md5.cpp / sha2.cpp
bool test_md5_sha2();
// _test_boost_asio_timer.cpp
extern bool test_boost_asio_timer();
// _test_boost.cpp
extern bool boost_lexical_cast();
extern bool boost_shared_ptr_void();
extern bool boost_shared_ptr_handle_01();
extern bool boost_shared_ptr_handle_02();
extern bool boost_shared_ptr_handle_03();
extern bool boost_tuple();
extern bool boost_format();
// _test_boost_bind.cpp
extern bool boost_bind();
extern bool boost_bind2();
extern bool boost_bind3();
extern bool boost_bind4();
extern bool boost_bind5();
// _test_std_map_set.cpp
extern bool test_set_insert();
extern bool test_map_insert();
extern bool test_std_map();
extern bool test_map_plus_algorithm_1();
extern bool test_map_plus_algorithm_2();
extern bool test_map_plus_algorithm_3();
extern bool test_map_plus_algorithm_4();
extern bool test_std_unordered_map();
extern bool test_std_unordered_map_object();
extern bool test_unorded_map_test_move();
extern bool test_map_insert_swap();
// _test_regstry_util.cpp
extern bool test_registry_util();
extern bool test_read_mouted_device();
extern bool test_set_binary_data();
extern bool test_reg_multi_value();
// _test_curl.cpp
bool test_curl_https_down_with_auth();
bool test_curl_https();
bool test_curl_http();
bool test_curl_http_upload();
bool test_curl_http_post_with_response_header();
bool test_curl_http_patch();
// thread_pool.h
extern bool test_thread_pool();
// _test_boost_thread.cpp
extern bool test_boost_thread();
// _test_std_thread.cpp
extern bool test_std_thread_with_lambda();
//_test_aes256.cpp
extern bool test_aes256_encrypt_decrypt_file();
// _test_sched_client.cpp
extern bool test_sched_client();
// test_unique_ptr.cpp
extern bool test_unique_ptr();
extern bool test_unique_ptr_assign();
extern bool test_unique_ptr_list();
extern bool test_unique_ptr_list_remove();
extern bool test_make_unique_struct_allocate();
extern bool test_return_unique_ptr();
// _test_call_by_value_container.cpp
extern bool test_callby_value_container();
// _test_rvo_move.cpp
extern bool test_rvo_and_move();
// _test_cppjson.cpp
extern bool test_cpp_joson();
// _test_template.cpp
extern bool test_template();
bool test_create_guid();
bool test_is_executable_file_w();
bool test_singleton();
bool test_trivia();
bool test_alignment();
bool test_create_string_from_buffer();
bool test_stop_watch();
bool test_bit_field();
bool test_interlock_operation();
bool test_auto_manual_reset_event();
bool test_get_module_dirEx();
bool test_read_line();
bool test_zip_unzip();
bool test_clear_stringsstream();
bool test_print_percent();
// _test_machine_id.cpp
extern bool test_generate_machine_id();
bool test_get_sid();
bool test_std_string_find();
void run_test()
{
UINT32 _pass_count = 0;
UINT32 _fail_count = 0;
bool ret = false;
assert_bool(true, test_aes256_crypt_buffer);
assert_bool(true, test_aes256_encrypt_decrypt_file)
//assert_bool(true, get_mbr_gpt_info);
//assert_bool(true, ntp_client);
//assert_bool(true, test_uni_wcsstr);
//assert_bool(true, test_match);
//assert_bool(true, test_cstream);
//assert_bool(true, test_cstream_read_only);
//assert_bool(true, test_cstream_read_write_string);
//assert_bool(true, test_log_rotate);
//assert_bool(true, test_steady_timer);
//assert_bool(true, test_steady_multiple_timer_in_single_thread);
//assert_bool(true, test_std_future_async);
//assert_bool(true, test_get_adapters);
//assert_bool(true, test_get_addr_info);
//assert_bool(true, test_is_reserved_ipv4);
//assert_bool(true, test_is_valid_ipv4);
//assert_bool(true, test_ip_to_dns);
//assert_bool(true, test_dns_to_ip);
//assert_bool(true, test_ip_to_dns2);
//assert_bool(true, test_iphelp_api);
//assert_bool(true, test_create_guid);
//assert_bool(true, test_process_token);
//assert_bool(true, test_is_executable_file_w);
//assert_bool(true, test_singleton);
//assert_bool(true, test_set_security_attributes);
//assert_bool(true, test_GeneralHashFunctions);
//assert_bool(true, test_GeneralHashFunctions2);
//assert_bool(true, test_get_file_extension);
//assert_bool(true, test_raii_xxx);
//assert_bool(true, test_suspend_resume_process);
//assert_bool(true, test_convert_file_time);
//assert_bool(true, test_ppl);
//assert_bool(true, test_find_and_replace);
//assert_bool(true, test_file_io_helper);
//assert_bool(true, test_file_io_helper2);
//assert_bool(true, test_scm_context);
//assert_bool(true, test_regexp);
//assert_bool(true, test_ping);
//assert_bool(true, test_alignment_error_test);
//assert_bool(true, test_crc64);
//assert_bool(true, test_NameConverter_iterate);
//assert_bool(true, test_NameConverter_get_canon_name);
//assert_bool(true, test_NameConverter_dosname_to_devicename);
//assert_bool(true, test_wmi_client);
//assert_bool(true, test_NtCreateFile);
//assert_bool(true, test_device_name_from_nt_name);
//assert_bool(true, test_rstrnicmp);
//assert_bool(true, test_get_drive_type);
//assert_bool(true, test_os_version);
//assert_bool(true, test_thread_pool);
//assert_bool(true, test_boost_thread);
//assert_bool(true, test_std_thread_with_lambda);
//assert_bool(true, test_boost_asio_timer);
//assert_bool(true, test_for_each);
//assert_bool(true, test_enum_physical_drive);
//assert_bool(true, test_get_disk_volume_info);
//assert_bool(true, test_dump_xxx);
//assert_bool(true, test_asm_func);
//assert_bool(true, test_x64_calling_convension);
//assert_bool(true, test_2_complement);
//assert_bool(true , test_print_64int);
//assert_bool(true, test_std_string_find_and_substr);
//assert_bool(true, test_to_lower_uppper_string);
//assert_bool(true, test_initialize_string);
//assert_bool(true, test_partial_copy_string);
//assert_bool(true, test_process_tree);
//assert_bool(true, test_iterate_process_tree);
//assert_bool(true, test_image_path_by_pid);
//assert_bool(true, test_get_process_creation_time);
//assert_bool(true, test_base64);
//assert_bool(true, test_random);
//assert_bool(true, test_ip_mac);
//assert_bool(true, test_ip_to_str);
//assert_bool(true, test_strtok);
//assert_bool(true, test_split_stringw);
//assert_bool(true, test_cpp_class);
//assert_bool(true, test_nt_name_to_dos_name);
//assert_bool(true, test_query_dos_device);
//assert_bool(true, test_get_filepath_by_handle);
//assert_bool(true, test_find_files);
//assert_bool(true, test_bin_to_hex);
//assert_bool(true, test_str_to_xxx);
//assert_bool(true, test_set_get_file_position);
//assert_bool(true, test_get_module_path);
//assert_bool(true, test_dump_memory);
//assert_bool(true, test_bin_to_str);
//assert_bool(true, test_get_environment_value);
//assert_bool(true, test_get_account_infos);
//assert_bool(true, test_get_installed_programs);
//assert_bool(true, test_get_file_company_name);
//assert_bool(true, test_generate_random_string);
//assert_bool(true, test_bit_check_set_clear);
//assert_bool(true, test_file_time_stuff);
//assert_bool(true, test_rc4_encrypt);
//assert_bool(true, test_md5_sha2);
//assert_bool(true, boost_lexical_cast);
//assert_bool(true, boost_shared_ptr_void);
//assert_bool(true, boost_shared_ptr_handle_01);
//assert_bool(true, boost_shared_ptr_handle_02);
//assert_bool(true, boost_shared_ptr_handle_03);
//assert_bool(true, boost_tuple);
//assert_bool(true, boost_format);
//assert_bool(true, boost_bind);
//assert_bool(true, boost_bind2);
//assert_bool(true, boost_bind3);
//assert_bool(true, boost_bind4);
//assert_bool(true, boost_bind5);
//assert_bool(true, test_boost_function);
//assert_bool(true, test_set_insert);
//assert_bool(true, test_map_insert);
//assert_bool(true, test_std_map);
//assert_bool(true, test_map_plus_algorithm_1);
//assert_bool(true, test_map_plus_algorithm_2);
//assert_bool(true, test_map_plus_algorithm_3);
//assert_bool(true, test_map_plus_algorithm_4);
//assert_bool(true, test_std_unordered_map);
//assert_bool(true, test_std_unordered_map_object);
//assert_bool(true, test_unorded_map_test_move);
//assert_bool(true, test_map_insert_swap);
//assert_bool(true, test_registry_util);
//assert_bool(true, test_read_mouted_device);
//assert_bool(true, test_set_binary_data);
//assert_bool(true, test_reg_multi_value);
//assert_bool(true, test_aes256_encrypt_decrypt_file);
//assert_bool(true, test_curl_https_down_with_auth);
//assert_bool(true, test_curl_https);
//assert_bool(true, test_curl_http);
//assert_bool(true, test_curl_http_upload);
//assert_bool(true, test_curl_http_post_with_response_header);
//assert_bool(true, test_curl_http_patch);
//assert_bool(true, test_alignment);
//assert_bool(true, test_create_string_from_buffer);
//assert_bool(true, test_stop_watch);
//assert_bool(true, test_bit_field);
//assert_bool(true, test_sched_client);
//assert_bool(true, test_interlock_operation);
//assert_bool(true, test_auto_manual_reset_event);
//assert_bool(true, test_get_module_dirEx);
//assert_bool(true, test_read_line);
//assert_bool(true, test_unique_ptr);
//assert_bool(true, test_unique_ptr_assign);
//assert_bool(true, test_unique_ptr_list);
//assert_bool(true, test_unique_ptr_list_remove);
//assert_bool(true, test_make_unique_struct_allocate);
//assert_bool(true, test_return_unique_ptr);
//assert_bool(true, test_zip_unzip);
//assert_bool(true, test_callby_value_container);
//assert_bool(true, test_clear_stringsstream);
//assert_bool(true, test_print_percent);
//assert_bool(true, test_get_file_original_name);
//assert_bool(true, test_rvo_and_move);
//assert_bool(true, test_cpp_joson);
//assert_bool(true, test_template);
//assert_bool(true, test_generate_machine_id);
//assert_bool(true, test_get_sid);
//assert_bool(true, test_std_string_find);
// 유닛테스트에 포함되지 않는 그냥 테스트용 코드
//
//assert_bool(true, test_write_mbr_vbr); // 혹시라도 테스트 중 mbr 날릴 수 있으므로 빼자.
//assert_bool(true, test_const_position); // 컴파일 불가 테스트
log_info
"----------------------------------------------------"
log_end;
_pause;
log_info
"total test = %u, pass = %u, fail = %u",
_pass_count + _fail_count,
_pass_count,
_fail_count
log_end
}
/// @brief
bool test_get_drive_type()
{
UINT drive_type = GetDriveTypeW(L"c:\\");
drive_type = GetDriveTypeW(L"\\Device\\HarddiskVolume1");
drive_type = GetDriveTypeW(L"c");
drive_type = GetDriveTypeW(L"\\Device\\HarddiskVolume1\\");
drive_type = GetDriveTypeW(L"d:\\");
drive_type = GetDriveTypeW(L"e:\\");
drive_type = GetDriveTypeW(L"f:\\");
return true;
}
/// @brief
bool test_os_version()
{
OSVER os = get_os_version();
log_info "%ws", osver_to_str(os) log_end;
return true;
}
/// @brief
bool test_find_and_replace()
{
std::string src = "0123456789,Version=v4,5";
std::string find = ",";
std::string replace = " ";
log_info "before find_and_replace, %s", src.c_str() log_end;
std::string str_mod = find_and_replace_string_exa(src.c_str(), ",", "\\,");
_ASSERTE(0 == str_mod.compare("0123456789\\,Version=v4\\,5"));
log_info "after find_and_replace, %s", str_mod.c_str() log_end;
std::wstring srcw = L"0123456789,Version=v4,5";
log_info "before find_and_replace, %ws", srcw.c_str() log_end;
std::wstring str_modw = find_and_replace_string_exw(srcw.c_str(), L",", L"\\,");
_ASSERTE(0 == str_modw.compare(L"0123456789\\,Version=v4\\,5"));
log_info "after find_and_replace, %ws", str_mod.c_str() log_end;
return true;
}
/**
* @brief std::for_each, lambda expression
**/
// functor that overrides () opeator.
struct Sum
{
Sum() { sum = 0; }
void operator()(int n) { sum += n; }
int sum;
};
bool test_for_each()
{
std::vector<int> nums;
for(int i = 0; i < 11; ++i)
{
nums.push_back(i);
}
std::for_each(
nums.begin(),
nums.end(),
[](int& num)
{
printf("%d\n",num);
}
);
Sum s = std::for_each(
nums.begin(),
nums.end(),
Sum()
);
printf("sum of nums = %u\n", s.sum);
return true;
}
/**
* @brief
* @param
* @see
* @remarks
* @code
* @endcode
* @return
**/
bool test_std_string_find_and_substr()
{
std::wstring nt_name(L"\\Device\\HarddiskVolume1\\Windows\\system32\\drivers");
std::wstring dos_device_name(L"c:");
std::wstring nt_device_name(L"\\Device\\HarddiskVolume1");
std::wstring nt_device_name2(L"\\DEVICE\\HarddiskVolume1"); // 대문자
//> 대소문자가 일치하는 경우 string::find 가 정상 동작 함
size_t pos = nt_name.find(nt_device_name);
if (std::wstring::npos == pos) return false;
std::wstring out = dos_device_name +
nt_name.substr(pos + nt_device_name.size(), nt_name.size());
log_dbg
"\nnt_name = %ws \ndos_device_name = %ws \nnt_device_name = %ws \nresult = %ws",
nt_name.c_str(),
dos_device_name.c_str(),
nt_device_name.c_str(),
out.c_str()
log_end
//> 대소문자 구분 없이 find 하려면 win32util::to_lower_string() 호출 후 비교해야 함
pos = nt_name.find(nt_device_name2);
if (std::wstring::npos == pos) return true;
return true;
}
/**
* @brief 2's complement
* @param
* @see
* @remarks
* @code
* @endcode
* @return
**/
bool test_2_complement()
{
int i = -1;
// 결과: %d = -1, %x = ffffffff
// 1 = 0000 0001
// 1111 1110 + 1 (음수를 표현하기 위해 2의 보수를 취하면...)
// 1111 1111 = -1 = 0xff
//
// 2 = 0000 0010
// 1111 1101 + 1
// 1111 1110 = -2 = 0xfe
//
// 3 = 0000 0011
// 1111 1100 + 1
// 1111 1101 = -3 = 0xfd
log_dbg "%%d = %d, %%x = %x", i, i log_end
return true;
}
/**
* @brief
* @param
* @see
* @remarks
* @code
* @endcode
* @return
**/
bool test_print_64int()
{
uint64_t val = 0xffffffffffffffff;
log_dbg "%%I64d = %I64d, %%I64u = %I64u, %%I64x = %I64x", val, val, val log_end
// %I64d = -1, %I64u = 18446744073709551615, %I64x = ffffffffffffffff
return true;
}
/**
* @brief
* @param
* @see
* @remarks
* @code
* @endcode
* @return
**/
bool test_to_lower_uppper_string()
{
std::wstring str = L"ABCDEFGh1234";
log_dbg "str = %s", WcsToMbsEx(str.c_str()).c_str() log_end
str = to_lower_string(str);
log_dbg "after to_lower, str = %s", WcsToMbsEx(str.c_str()).c_str() log_end
str = to_upper_string(str);
log_dbg "after to_upper, str = %s", WcsToMbsEx(str.c_str()).c_str() log_end
return true;
}
/**
* @brief const 위치 / 의미
* @param
* @see
* @remarks
* @code
* @endcode
* @return
**/
/*
class ConstPositionTest
{
public:
//> (const char*) msg : char* 가 const, 즉 msg 가 가리키는 데이터 변경 불가
char* Function1(const char* msg)
{
msg[0] = 't'; // error
return m_msg;
}
//> char* (const msg) : msg 변수가 const, 즉 msg 포인터 변수 변경 불가
char* Function2(char* const msg)
{
msg = m_msg; //error
return m_msg;
}
//> 메소드 상수화, 이 메소드는 클래스 멤버를 읽을 수는 있으나 변경 할 수는 없음
char* Function3(char* msg) const
{
m_msg = msg; //error
return m_msg;
}
//> (const char*) : 리턴 값이 const char* 이므로 리턴 받는 변수도 const char* 이어야 함
//> 따라서 리턴되는 포인터가 가리키는 데이터 변경 불가
const char* Function4(char* msg)
{
m_msg = msg;
return m_msg; //반환 받는 타입이 const가 아닐 경우 error
}
private:
char* m_msg;
};
bool test_const_position()
{
ConstPositionTest test;
char msg[] = "hello, const!";
test.Function1(msg);
test.Function2(msg);
test.Function3(msg);
const char* pMessage = test.Function4(msg);
pMessage[0] = 0; // error
}
*/
/**
* @brief
* @param
* @see
* @remarks
* @code
* @endcode
* @return
**/
bool test_initialize_string()
{
std::wstring str = L"";
log_dbg "str = %ws", str.c_str() log_end
//> invalid null point exception 발생
//> try-except 로 못 잡음...
//> 초기화시 NULL 이면 "" 로 바꿔서 초기화 해야 함
/*
try
{
std::wstring str2 = NULL;
log_dbg "str2 = %s", str2.c_str() log_end
}
catch (...)
{
log_err "oops" log_end
}
*/
return true;
}
bool test_partial_copy_string()
{
size_t cc_copied = 0;
{
std::string src = "01234567890123456789012345678901234567890123456789";
// buf 가 src 보다 작은 경우
char bufa[0xa] = { 0 };
cc_copied = copy_string_to_buf_with_null(src, bufa, sizeof(bufa));
_ASSERTE(cc_copied == sizeof(bufa)/sizeof(char) - sizeof(char));
log_info "%s", bufa log_end;
// buf 가 src 보다 큰 경우
char bufx[1024] = { 0x0 };
cc_copied = copy_string_to_buf_with_null(src, bufx, sizeof(bufx));
_ASSERTE(cc_copied == src.size());
log_info "%s", bufx log_end;
}
{
std::wstring src = L"01234567890123456789012345678901234567890123456789";
// buf 가 src 보다 작은 경우
wchar_t bufa[0xa] = { 0xcc };
cc_copied = copy_wstring_to_buf_with_null(src, bufa, sizeof(bufa));
_ASSERTE(cc_copied == sizeof(bufa)/sizeof(wchar_t) - sizeof(wchar_t));
log_info "%ws", bufa log_end;
// buf 가 src 보다 큰 경우
wchar_t bufx[1024] = { 0xcc };
cc_copied = copy_wstring_to_buf_with_null(src, bufx, sizeof(bufx));
_ASSERTE(cc_copied == src.size());
log_info "%ws", bufx log_end;
}
return true;
}
/**
* @brief
* @param
* @see
* @remarks
* @code
* @endcode
* @return
**/
bool test_base64()
{
// http://www.opinionatedgeek.com/dotnet/tools/base64encode/
#define _base64_encoded "64yA7ZWc66+86rWt"
std::wstring string_to_encodeW = L"대한민국";
std::string string_to_encodeA = "대한민국";
std::wstring wide_str;
std::string utf8_str;
std::string base64_str;
// base 64 encode
//
// #1) multibyte -> ucs16 -> utf8 -> base64 순서로...
// #2) ucs16 -> utf8 -> base64
wide_str = MbsToWcsEx(string_to_encodeA.c_str());
utf8_str = WcsToMbsUTF8Ex(wide_str.c_str());
base64_str = base64_encode((unsigned char*)utf8_str.c_str(), (int)utf8_str.size());
if (0 != base64_str.compare(_base64_encoded)) return false;
wide_str = string_to_encodeW;
utf8_str = WcsToMbsUTF8Ex(wide_str.c_str());
base64_str = base64_encode((unsigned char*)utf8_str.c_str(), (int)utf8_str.size());
if (0 != base64_str.compare(_base64_encoded)) return false;
// base64 decode
std::string f = base64_decode(_base64_encoded);
wide_str = Utf8MbsToWcsEx(f.c_str());
if (0 != wide_str.compare(string_to_encodeW.c_str())) return false;
return true;
}
/**
* @brief
**/
bool test_random()
{
int var = rand() % 1000 + 1;
log_info "var = %d", var log_end;
return true;
}
/**
* @brief
**/
bool test_ip_mac()
{
std::wstring host_name;
std::list<std::string> ip_list;
log_info "representative ip v4=%s",
get_representative_ip_v4().c_str()
log_end;
_ASSERTE(true == get_host_name(host_name));
_ASSERTE(true == get_ip_list_v4(ip_list));
log_info "host_name = %ws", host_name.c_str() log_end
std::for_each(
ip_list.begin(),
ip_list.end(),
[](std::string& ip)
{
log_info "ip=%s, mac=%s",
ip.c_str(),
get_mac_by_ip_v4(ip.c_str()).c_str()
log_end
});
return true;
}
/// @brief
bool test_ip_to_str()
{
const wchar_t* ip_str = L"1.2.3.4";
uint32_t addr = { 0 };
if (true != str_to_ipv4(ip_str, addr)) return false;
log_info "ip = %ws -> %lu", ip_str, addr log_end;
log_info "ip = %lu -> %s", addr, ipv4_to_str(addr).c_str() log_end;
in_addr inaddr;
inaddr.S_un.S_addr = 0x0100007f;
log_info "ip = %lu -> %s", inaddr.S_un.S_addr, ipv4_to_str(inaddr).c_str() log_end;
const wchar_t* str_LLMSR = L"224.0.0.252";
uint32_t ip_llmnr = 0;
_ASSERTE(true == str_to_ipv4(str_LLMSR, ip_llmnr));
uint16_t port_llmnr = 5355;
log_info "ip=%ws -> 0x%08x, port=%u(0x%04x) -> 0x%04x",
str_LLMSR,
ip_llmnr,
port_llmnr, port_llmnr,
swap_endian_16(port_llmnr)
log_end;
return true;
}
/// @brief
bool
split_string(
_In_ const char* str,
_In_ const char* seps,
_Out_ std::vector<std::string>& tokens
)
{
#define max_str_len 2048
_ASSERTE(NULL != str);
if (NULL == str) return false;
tokens.clear();
// strtok_s() modifies the `str` buffer.
// so we should make copy.
size_t len = strlen(str);
if (max_str_len <= len + sizeof(char))
{
return false;
}
char_ptr buf((char*)malloc(len + sizeof(char)), [](char* p) {
if (nullptr != p)
{
free(p);
}
});
if (NULL == buf.get())
{
return false;
}
RtlCopyMemory(buf.get(), str, len);
buf.get()[len] = 0x00;
char* next_token = NULL;
char* token = strtok_s(buf.get(), seps, &next_token);
while (NULL != token)
{
tokens.push_back(token);
token = strtok_s(NULL, seps, &next_token);
}
return true;
}