/*++

Copyright (c) 1989-1997  Microsoft Corporation

Module Name:

    proptest.c

Abstract:

    This module contains tests for Ntfs Property support.

--*/


extern "C" {
#include <nt.h>
#include <ntioapi.h>
#include <ntrtl.h>
#include <nturtl.h>
}

#include <windows.h>

#include <stdio.h>

#include <ddeml.h>      // for CP_WINUNICODE

#include <objidl.h>

extern "C"
{
#include <propapi.h>
}

#include <stgprop.h>

#include <stgvar.hxx>
#include <propstm.hxx>
#include <align.hxx>
#include <sstream.hxx>

#include <propvar.h>


//
//  Simple wrapper for NtCreateFile
//

NTSTATUS
OpenObject (
    WCHAR const *pwszFile,
    ULONG CreateOptions,
    ULONG DesiredAccess,
    ULONG ShareAccess,
    ULONG CreateDisposition,
    HANDLE *ph)
{
    NTSTATUS Status;
    OBJECT_ATTRIBUTES oa;
    UNICODE_STRING str;
    IO_STATUS_BLOCK isb;

    RtlDosPathNameToNtPathName_U(pwszFile, &str, NULL, NULL);

    InitializeObjectAttributes(
		&oa,
		&str,
		OBJ_CASE_INSENSITIVE,
		NULL,
		NULL);

    Status = NtCreateFile(
                ph,
                DesiredAccess | SYNCHRONIZE,
                &oa,
                &isb,
                NULL,                   // pallocationsize (none!)
                FILE_ATTRIBUTE_NORMAL,
                ShareAccess,
                CreateDisposition,
                CreateOptions,
                NULL,                   // EA buffer (none!)
                0);

    RtlFreeHeap(RtlProcessHeap(), 0, str.Buffer);
    return(Status);
}


void
SzToWsz (
    OUT WCHAR *Unicode,
    IN char *Ansi
    )
{
    while (*Unicode++ = *Ansi++)
        ;
}

void
OpenTest (
    char *FileName
    )
{
    NTSTATUS Status;
    HANDLE Handle;
    WCHAR WFileName[MAX_PATH];
    char InputBuffer[10];
    char OutputBuffer[200];
    IO_STATUS_BLOCK Iosb;


    //
    //  OPENTEST file count
    //
    
    //
    //  Create the new file
    //
    
    SzToWsz( WFileName, FileName );

    Status = OpenObject( WFileName,
                         FILE_SYNCHRONOUS_IO_NONALERT,
                         FILE_READ_DATA | FILE_WRITE_DATA,
                         FALSE,
                         FILE_CREATE,
                         &Handle );

    if (!NT_SUCCESS( Status )) {
        printf( "Unable to open %s - %x\n", FileName, Status );
    }

    //
    //  Write a small amount of data
    //

    Status = NtWriteFile( Handle, NULL, NULL, NULL, &Iosb, InputBuffer, 10, NULL, NULL );
    if (!NT_SUCCESS( Status )) {
        printf( "Unable to write %s - %x\n", FileName, Status );
    }
    
    //
    //  Close the file
    //

    Status = NtClose( Handle );
    if (!NT_SUCCESS( Status )) {
        printf( "Unable to close %s - %x\n", FileName, Status );
    }

    //
    //  Overwrite the file
    //

    Status = OpenObject( WFileName,
                         FILE_SYNCHRONOUS_IO_NONALERT,
                         FILE_READ_DATA | FILE_WRITE_DATA,
                         FALSE,
                         FILE_OVERWRITE,
                         &Handle );

    if (!NT_SUCCESS( Status )) {
        printf( "Unable to overwrite %s - %x\n", FileName, Status );
    }

    //
    //  Write a small amount of data
    //

    Status = NtWriteFile( Handle, NULL, NULL, NULL, &Iosb, InputBuffer, 10, NULL, NULL );
    if (!NT_SUCCESS( Status )) {
        printf( "Unable to write %s - %x\n", FileName, Status );
    }

    //
    //  Close the file
    //

    Status = NtClose( Handle );
    if (!NT_SUCCESS( Status )) {
        printf( "Unable to close %s - %x\n", FileName, Status );
    }

    //
    //  Delete the file
    //

    {    
        OBJECT_ATTRIBUTES oa;
        UNICODE_STRING str;
    
        RtlDosPathNameToNtPathName_U( WFileName, &str, NULL, NULL );
    
        InitializeObjectAttributes(
            &oa,
            &str,
            OBJ_CASE_INSENSITIVE,
            NULL,
            NULL);
    
        Status = NtDeleteFile( &oa );
    
        RtlFreeHeap(RtlProcessHeap(), 0, str.Buffer);

        if (!NT_SUCCESS( Status )) {
            printf( "Unable to delete %s - %x\n", FileName, Status );
        }
        
    }
}



int __cdecl
main (
    int argc,
    char **argv)
{
    DbgPrint( "--------------------------------------------\n" );
    while (--argc != 0) {
        OpenTest( *++argv );
    }
    DbgPrint( "--------------------------------------------\n" );
    return 0;
}


