summaryrefslogtreecommitdiffstats
path: root/private/oleutest/cfmex/cfmex.cxx
blob: 37e8c08ed78172ab9f9fa10fcdd0edbe1ccf7584 (plain) (blame)
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

//+===============================================================
//
//  File:       CFMEx.cxx
//
//  Purpose:    This file provides the main() and global functions
//              for the CreateFileMonikerEx (CFMEx) DRT.
//              This DRT tests the CreateFileMonikerEx API (new to
//              Cairo), as well as related changes to BIND_OPTS
//              (use of the BIND_OPTS2 structure).
//
//              All moniker activity is performed in the CMoniker
//              object (such as creating a bind context, creating
//              a link source file, moving it, binding it, etc.).
//
//              The test engine is in the CTest object.  When a link
//              source file is moved to test link-tracking, the
//              original and final location of the file may be
//              specified by the user (on the command-line).  CTest
//              is aware of the filesystem type (FAT, NTFS, OFS)
//              of these locations, and is aware of how that will
//              effect the results.
//
//              This file also provides global functions (not associated
//              with an object).
//
//  Usage:      cfmex [-o<Directory>] [-f<Directory>]
//
//              -o<Directory> specifies the original directory for link sources
//              -f<Directory> specifies the final directory for link sources
//
//  Examples:   cfmex
//              cfmex -oC:\ -fC:\
//
//+===============================================================


//  ----
//  TODO:
//  ----
//
// - Replace CRT calls with Win32 calls.
// - Validate the directory in CDirectory.
// - Add a flag to CDirectory to indicate "indexed".
// - Add a flag to CDirectory to indicate local vs remote.
// - Add a test to verify that the moniker returned from Reduce is
//   not a tracking moniker.
//


//  -------------
//  Include Files
//  -------------


#define _DCOM_			// Allow DCOM extensions (e.g., CoInitializeEx).

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <wtypes.h>
#include <oaidl.h>
#include <dsys.h>
#include <olecairo.h>
#include "CFMEx.hxx"
#include "CMoniker.hxx"
#include "CTest.hxx"
#include "CDir.hxx"


//  ------
//  Macros
//  ------

// Early-exit macro:  put the error message in a global array,
// and jump to Exit

WCHAR wszErrorMessage[ 512 ];

#undef EXIT
#define EXIT( error )    \
                         {\
                            wcscpy( wszErrorMessage, ##error );\
                            goto Exit;\
                         }



// Run a Test:  Display a new paragraph on the screen, run a test,
// and update stats.

#define RUN_TEST( testID )    \
                        {\
			    nTotalTests++;  \
                            wprintf( L"----------------------------------------------\n" ); \
                            wprintf( L"Test %d:  ", nTotalTests );  \
                            if( cTest.##testID ) \
                               wprintf( L"Passed\n" ); \
                            else \
                                nTestsFailed++; \
                        }


//+---------------------------------------------------------------------------------------
//
//  Function:   DisplayHelp
//
//  Synopsis:   Display a help screen with usage information.
//
//  Inputs:     None.
//
//  Outputs:    None.
//
//  Effects:    None
//
//+---------------------------------------------------------------------------------------


void DisplayHelp( void )
{

    wprintf( L"This DRT tests the CreateFileMonikerEx API, and related changes.\n"
             L"Most of these tests create a link source file, create a moniker\n"
             L"representing that file, then move the file.  You can specify the\n"
             L"original and/or final locations of the link source, or let those\n"
             L"locations default to the \%TEMP\% directory\n"
             L"\n"
             L"Usage:  cfmex [-o<Directory>] [-f<Directory>]\n"
             L"\n"
             L"Where:  -o<Directory> specifies the original directory for link sources\n"
             L"        -f<Directory> specifies the final directory for link sources\n"
             L"\n"
             L"Note:   If an original or final directory is specified on the command-\n"
             L"        line, that directory must already exist.  If one of these locations\n"
             L"        is not specified, the TEMP environment variable must be defined\n"
             L"        and it must specify an extant directory.\n"
             L"\n"
             L"E.g.:   cfmex\n"
             L"        cfmex -oC:\\\n"
             L"        cfmex -oE:\\ -fF:\\temp\n"
             L"\n" );
    return;
}



//+---------------------------------------------------------------------------------------
//
//  Function:   UnicodeToAnsi
//
//  Synopsis:   Convert a Unicode (wide) string to ANSI
//
//  Inputs:     The Unicode String
//              The buffer for the ANSI string
//              The size of the above buffer
//
//  Outputs:    0 if successful
//              GetLastError() otherwise
//
//  Effects:    None
//
//+---------------------------------------------------------------------------------------

DWORD UnicodeToAnsi( const WCHAR * wszUnicode,
                     CHAR * szAnsi,
                     int cbAnsiMax )
{
    int cbAnsi = 0;

    // Convert WCS to the MBCS, using the ANSI code page.

    cbAnsi = WideCharToMultiByte( CP_ACP,
                                  WC_COMPOSITECHECK | WC_DEFAULTCHAR,
                                  wszUnicode,
                                  wcslen( wszUnicode ),
                                  szAnsi,
                                  cbAnsiMax,
                                  NULL,
                                  NULL );
    if( !cbAnsi )
    {
        // No characters were converted - there was an error.
        // Note that this will be returned if a null Unicode string is
        // passed in.

        return( GetLastError() );
    }
    else
    {
        // Terminate the Ansi string and return.

        szAnsi[ cbAnsi ] = '\0';
        return( 0L );
    }

}   // UnicodeToAnsi()



//+---------------------------------------------------------------------------------------
//
//  Function:   AnsiToUnicode
//
//  Synopsis:   Convert an ANSI string to Unicode (i.e. Wide)
//
//  Inputs:     The ANSI String
//              The buffer for the Unicode string
//              The size of the above buffer.
//
//  Outputs:    0 if successful
//              GetLastError() otherwise
//
//  Effects:    None
//
//+---------------------------------------------------------------------------------------

DWORD AnsiToUnicode( const CHAR * szAnsi,
                     WCHAR * wszUnicode,
                     int cbUnicodeMax )
{
    int cbUnicode = 0;

    cbUnicode = MultiByteToWideChar( CP_ACP,
                                     MB_PRECOMPOSED,
                                     szAnsi,
                                     strlen( szAnsi ),
                                     wszUnicode,
                                     cbUnicodeMax );

    if( !cbUnicode )
    {
        // If no characters were converted, then there was an error.
        
        return( GetLastError() );
    }
    else
    {
        // Terminate the Unicode string and return.

        wszUnicode[ cbUnicode ] = L'\0';
        return( 0L );
    }

}   // AnsiToUnicode()



//+---------------------------------------------------------------------------------------
//
//  Function:   main
//
//  Synopsis:   Run the CFMEx DRT.  All tests are in the CTest object.  These
//              tests are simply called sequentially.
//
//  Inputs:     (argc) the count of arguments
//              (argv) the arguments.  See DisplayHelp() for a description.
//
//  Outputs:    0 if completely successful
//              1 if help was displayed
//              2 if a test failed
//
//  Effects:    None
//
//+---------------------------------------------------------------------------------------


int main( int argc, char** argv )
{

    //  ---------------
    //  Local Variables
    //  ---------------

    // Test statistics.

    int nTotalTests = 0;
    int nTestsFailed = 0;

    // The original and final directories (in ANSI) of the link source file.

    CHAR* szOriginalDirectory = NULL;
    CHAR* szFinalDirectory = NULL;

    // Objects representing the original and final directories.

    CDirectory cDirectoryOriginal;
    CDirectory cDirectoryFinal;

    // The test engine.

    CTest cTest;

    int index = 0;


    //  --------------
    //  Opening Banner
    //  --------------

    printf( "\n"
            "*** CreateFileMonikerEx DRT ***\n"
            "(use \"cfmex -?\" for help)\n"
            "\n" );


    //  ------------------------------
    //  Handle command-line parameters
    //  ------------------------------


    for( index = 1; index < argc; index++ )
    {
        // The first character of an argument should be an "-" or "/"
        // (they are interchangable).

        if( ( ( argv[index][0] != '-' )
              &&
              ( argv[index][0] != '/' )
            )
            ||
            ( strlen( &argv[index][0] ) < 2 )  // Must have more than '-' & an option.
          )
        {
            printf( "Invalid argument ignored:  %s\n", argv[ index ] );
            continue;
        }


        // Switch based on the first character (which defines the option).

        switch( argv[index][1] )
        {
            // Help requested

            case '?':
                DisplayHelp();
                exit( 1 );

            // An original directory is specified.

            case 'o':
            case 'O':

                // Verify the specified path length.

                if( strlen( &argv[index][2] ) > MAX_PATH )
                {
                    printf( "Path is too long, ignored:  %s\n", &argv[index][2] );
                    break; // From the switch
                }

                szOriginalDirectory = &argv[index][2];
                break;  // From the switch

            // A final directory is specified

            case 'f':
            case 'F':

                if( strlen( &argv[index][2] ) > MAX_PATH )
                {
                    printf( "Path is too long, ignored:  %s\n", &argv[index][2] );
                    break; // From the switch
                }

                szFinalDirectory = &argv[index][2];
                break;  // From the switch

            // Invalid argument.

            default:

                printf( "Invalid option ignored:  \"-%c\"\n", argv[index][1] );
                break;
        }
    }

    //  --------------
    //  Initialization
    //  --------------

    // Initialize COM

    CoInitialize( NULL );


    // Initialize the CDirectory and CTest objects.  If no original/final
    // directory was specified above, CDirectory will create a default
    // (based on %TEMP%).

    if( !cDirectoryOriginal.Initialize( szOriginalDirectory ) )
        EXIT( L"Could not initialize cDirectoryOriginal" );

    if( !cDirectoryFinal.Initialize( szFinalDirectory ) )
        EXIT( L"Could not initialize cDirectoryFinal" );

    if( !cTest.Initialize( cDirectoryOriginal, cDirectoryFinal ) )
        EXIT( L"Could not initialize CTest" );


    //  Show the end result.

    wprintf( L"Link sources will be created in \"%s\" (%s)\n",
             cDirectoryOriginal.GetDirectoryName(),
             cDirectoryOriginal.GetFileSystemName() );
    wprintf( L"and will be moved to \"%s\" (%s)\n",
             cDirectoryFinal.GetDirectoryName(),
             cDirectoryFinal.GetFileSystemName() );


    //  ---------
    //  Run Tests
    //  ---------

    RUN_TEST( GetOversizedBindOpts() );
    RUN_TEST( GetUndersizedBindOpts() );
    RUN_TEST( SetOversizedBindOpts() );
    RUN_TEST( SetUndersizedBindOpts() );
    RUN_TEST( CreateFileMonikerEx() );
    RUN_TEST( GetDisplayName() );
    RUN_TEST( GetTimeOfLastChange() );
    RUN_TEST( ComposeWith() );
    RUN_TEST( IPersist() );
    RUN_TEST( BindToStorage() );
    RUN_TEST( BindToObject() );
    RUN_TEST( DeleteLinkSource( 0 ));           // Timeout immediately.
    RUN_TEST( DeleteLinkSource( 200 ));         // Timeout in multi-threaded search
    RUN_TEST( DeleteLinkSource( INFINITE ));    // Don't timeout

    //  ----
    //  Exit
    //  ----

Exit:

    // Show test results.

    wprintf( L"\n\nTesting complete:\n"
                 L"   Total Tests = %d\n"
                 L"   Failed =      %d\n",
             nTotalTests, nTestsFailed );

    // Free COM

    CoUninitialize();

    return( nTestsFailed ? 2 : 0 );
}