-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy path_MyLib_test.cpp
3070 lines (2556 loc) · 79.9 KB
/
_MyLib_test.cpp
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
/**----------------------------------------------------------------------------
* _MyLib_test.cpp
*-----------------------------------------------------------------------------
*
*-----------------------------------------------------------------------------
* All rights reserved by Noh,Yonghwan ([email protected], [email protected])
*-----------------------------------------------------------------------------
* 2014:1:25 13:34 created
**---------------------------------------------------------------------------*/
#include "stdafx.h"
#include "process_tree.h"
#include "base64.h"
#include "rc4.h"
#include "thread_pool.h"
#include "md5.h"
#include "sha2.h"
#include "Win32Utils.h"
#include "send_ping.h"
#include <regex>
#include "wmi_client.h"
#include "nt_name_conv.h"
#include "crc64.h"
#include "StopWatch.h"
#include "GeneralHashFunctions.h"
#include <unordered_map>
bool test_log_xxx();
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();
// _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_base64();
bool test_random();
bool test_get_local_ip_list();
bool test_get_mac_address();
bool test_ip_to_str();
bool test_strtok();
// test_process_tree.cpp
extern bool test_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_get_environment_value();
// 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.cpp
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();
// _test_regstry_util.cpp
extern bool test_registry_util();
extern bool test_read_mouted_device();
extern bool test_set_binary_data();
// thread_pool.h
bool test_thread_pool();
// _test_boost_thread.cpp
extern bool test_boost_thread();
//_test_aes256.cpp
bool test_aes256();
class aaa
{
public:
aaa(bool value) : _value(value) { }
virtual ~aaa() { log_info "..." log_end }
void run() { log_info "%s", _value == true ? "Ture" : "False" log_end;}
protected:
bool _value;
};
class bbb : public aaa
{
public:
bbb(bool value): aaa(value) { }
virtual ~bbb() { }
void run() { log_info "%s", _value == true ? "Ture" : "False" log_end;}
};
class ccc : public bbb
{
public:
ccc(bool value) : bbb(value) { }
virtual ~ccc() { }
void run() { log_info "%s", _value == true ? "Ture" : "False" log_end;}
};
void run_test()
{
UINT32 _pass_count = 0;
UINT32 _fail_count = 0;
{
boost::wformat f = boost::wformat(L"%s\\%s") % get_current_module_dirEx().c_str() % L"_MyLib_tst.log";
if (true != initialize_log(log_mask_all,
log_level_debug,
log_to_all,
f.str().c_str())) return;
set_log_format(false, false, false);
}
//
// std::string 관련
//
std::wstring wstr = L"12345";
log_info "wstr.size() = %u, wcslen(wstr.c_str()) = %u (same size)",
wstr.size(), wcslen(wstr.c_str())
log_end;
_ASSERTE(wstr.size() == wcslen(wstr.c_str()));
std::wstring s = L"";
_ASSERTE(true == s.empty());
std::wstring ss = L" ";
_ASSERTE(true != ss.empty());
//
// 64비트 정수 출력
//
uint64_t t = 0xffffffff00112233;
log_info "0x%llx, 0x%016llx", t, t log_end;
uint64_t y = 0x00112233;
log_info "0x%llx, 0x%016llx", y, y log_end;
// list 순서
std::list<int> li;
li.push_back(1);
li.push_back(2);
li.push_back(3);
for (auto v : li)
{
log_info "%d", v log_end;
}
//
// 생성자/소멸자 호출
//
ccc c(true);
c.run();
bool ret = false;
//assert_bool(true, test_log_xxx);
//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_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_boost_thread);
//assert_bool(true, test_thread_pool);
//
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_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_get_local_ip_list);
//assert_bool(true, test_get_mac_address);
//assert_bool(true, test_ip_to_str);
//assert_bool(true, test_strtok);
//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_get_environment_value);
//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_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_registry_util);
//assert_bool(true, test_read_mouted_device);
//assert_bool(true, test_set_binary_data);
//assert_bool(true, test_aes256);
//
// 유닛테스트에 포함되지 않는 그냥 테스트용 코드
//
//assert_bool(true, test_write_mbr_vbr); // 혹시라도 테스트 중 mbr 날릴 수 있으므로 빼자.
//assert_bool(true, test_const_position); // 컴파일 불가 테스트
log_info
"----------------------------------------------------"
log_end
log_info
"total test = %u, pass = %u, fail = %u",
_pass_count + _fail_count,
_pass_count,
_fail_count
log_end
finalize_log();
}
/// @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
to_lower_string(str);
log_dbg "after to_lower, str = %s", WcsToMbsEx(str.c_str()).c_str() log_end
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;
}
/**
* @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_get_local_ip_list()
{
std::wstring host_name;
std::vector<std::wstring> ip_list;
if (true != get_local_ip_list(host_name, ip_list)) return false;
log_info "host_name = %ws", host_name.c_str() log_end
/*std::vector<std::wstring>::iterator its = ip_list.begin();
std::vector<std::wstring>::iterator ite = ip_list.end();
for(; its != ite; ++its)
{
log_info "ip = %ws", its->c_str() log_end
}*/
std::for_each(
ip_list.begin(),
ip_list.end(),
[](std::wstring& ip)
{
log_info "ip = %ws", ip.c_str() log_end
}
);
return true;
}
/// @brief
bool test_get_mac_address()
{
std::wstring host_name;
std::vector<std::wstring> ip_list;
if (true != get_local_ip_list(host_name, ip_list)) return false;
log_info "hot name = %ws", host_name.c_str() log_end;
for (auto ip : ip_list)
{
std::wstring mac_str;
if (true != get_local_mac_by_ipv4(ip.c_str(), mac_str)) return false;
log_info "ip = %ws, mac = %ws", ip.c_str(), mac_str.c_str() log_end;
}
return true;
}
/// @brief
bool test_ip_to_str()
{
const wchar_t* ip_str = L"211.221.93.88";
in_addr addr = { 0 };
if (true != str_to_ipv4(ip_str, addr)) return false;
log_info "ip = %ws -> %lu", ip_str, addr.S_un.S_addr log_end;
log_info "ip = %lu -> %ws", addr.S_un.S_addr, ipv4_to_str(addr).c_str() log_end;
addr.S_un.S_addr = 0x0100007f;
log_info "ip = %lu -> %ws", addr.S_un.S_addr, ipv4_to_str(addr).c_str() 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;
}
bool test_strtok()
{
char string1[] = "A string\tof ,,tokens\nand some more tokens";
char string2[] = "Another string\n\tparsed at the same time.";
char seps[] = " ,\t\n";
std::vector<std::string> tokens1;
std::vector<std::string> tokens2;
//
// Establish string and get the first token:
//
if (false == split_string(string1, seps, tokens1) ||
false == split_string(string2, seps, tokens2))
{
return false;
}
log_info "tokens1 : %s\n", string1 log_end;
for (auto token: tokens1)
{
log_info "\t%s", token.c_str() log_end;
}
log_info "\n" log_end;
log_info "tokens2 : %s\n", string2 log_end;
for (auto token : tokens2)
{
log_info "\t%s", token.c_str() log_end;
}
log_info "\n" log_end;
return true;
}
/// @brief c:\temp\dbg X
/// c:\temp\dbg\ O
/// c:\temp\dbg\* O
/// c:\temp\dbg\*.* O
/// c:\temp\dbg\*.exe O
/// c:\temp\dbg\*.txt O
bool WINAPI ffcb(_In_ DWORD_PTR tag, _In_ const wchar_t* path)
{
std::list<std::wstring>* files = (std::list<std::wstring>*)tag;
files->push_back(path);
return true;
}
bool test_find_files()
{
//
// 테스트용 디렉토리/파일 생성
//
{
if (true == is_file_existsW(L"c:\\temp\\dbg"))
{
if (!WUDeleteDirectoryW(L"c:\\temp\\dbg"))
{
return false;
}
}
if (true != WUCreateDirectory(L"c:\\temp\\dbg"))
{
return false;
}
HANDLE h = open_file_to_write(L"c:\\temp\\dbg\\1111.txt");
if (INVALID_HANDLE_VALUE == h)
return false;
else
CloseHandle(h);
h = open_file_to_write(L"c:\\temp\\dbg\\1112.txt");
if (INVALID_HANDLE_VALUE == h)
return false;
else
CloseHandle(h);
h = open_file_to_write(L"c:\\temp\\dbg\\1111.exe");
if (INVALID_HANDLE_VALUE == h)
return false;
else
CloseHandle(h);
h = open_file_to_write(L"c:\\temp\\dbg\\1111.dll");
if (INVALID_HANDLE_VALUE == h)
return false;
else
CloseHandle(h);
h = open_file_to_write(L"c:\\temp\\dbg\\1111");
if (INVALID_HANDLE_VALUE == h)
return false;
else
CloseHandle(h);
}
typedef struct root_and_count
{
wchar_t* root_path;
size_t count;
} *proot_and_count;
root_and_count roots[] = {
{ L"c:\\temp\\dbg\\1112.txt", 1 },
{ L"c:\\temp\\dbg\\1111.*", 4 },
{ L"c:\\temp\\dbg\\1111", 1 },
{L"c:\\temp\\dbg", 5},
{ L"c:\\temp\\dbg\\", 5},
{ L"c:\\temp\\dbg\\*", 5 },
{ L"c:\\temp\\dbg\\*.*", 5},
{ L"c:\\temp\\dbg\\*.exe", 1},
{ L"c:\\temp\\dbg\\*.txt", 2}
};
for (int i = 0; i < sizeof(roots) / sizeof(root_and_count); ++i)
{
std::list<std::wstring> files;
_ASSERTE(true == find_files(roots[i].root_path, ffcb, (DWORD_PTR)&files, false));
_ASSERTE(roots[i].count == files.size());
std::wstringstream strm;
for (auto file: files)
{
strm << file << std::endl;
}
log_info "root=%ws\n%ws", roots[i].root_path, strm.str().c_str() log_end;
}
return true;
}
/**
* @brief
* @param
* @see
* @remarks
* @code
* @endcode
* @return
**/
bool test_get_filepath_by_handle()
{
typedef boost::shared_ptr< boost::remove_pointer<HANDLE>::type > shared_file_handle;
shared_file_handle file_handle(
open_file_to_read(L"c:\\windows\\system32\\drivers\\etc\\hosts"),
CloseHandle
);
if(INVALID_HANDLE_VALUE == file_handle.get()) return false;
std::wstring file_path;
bool ret = get_filepath_by_handle(file_handle.get(), file_path);
if (true == ret)