text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE284_Improper_Access_Control__w32_wchar_t_CreateDesktop_15.c
  Label Definition File: CWE284_Improper_Access_Control__w32.label.xml
  Template File: point-flaw-15.tmpl.c
  */
  /*
   * @description
   * CWE: 284 Improper Access Control
   * Sinks: CreateDesktop
   *    GoodSink: Create a desktop using CreateDesktopW() without excessive privileges
   *    BadSink : Create a desktop using CreateDesktopW() with excessive privileges
   * Flow Variant: 15 Control flow: switch(6)
   *
   * */
  
  #include "std_testcase.h"
  
  #include <windows.h>
  #pragma comment( lib, "user32" )
  
  #ifndef OMITBAD
  
  void CWE284_Improper_Access_Control__w32_wchar_t_CreateDesktop_15_bad()
  {
      switch(6)
      {
      case 6:
      {
          HDESK hDesk;
          wchar_t * desktopName = L"DesktopExample";
          /* FLAW: Call CreateDesktopW() with GENERIC_ALL as the 5th parameter */
          hDesk = CreateDesktopW(
                      desktopName,
                      NULL,
                      NULL,
                      0,
                      GENERIC_ALL,
                      NULL);
          if (hDesk == NULL)
          {
              printLine("Desktop could not be created");
          }
          else
          {
              printLine("Desktop created successfully");
              CloseDesktop(hDesk);
          }
      }
      break;
      default:
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          printLine("Benign, fixed string");
          break;
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* good1() changes the switch to switch(5) */
  static void good1()
  {
      switch(5)
      {
      case 6:
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          printLine("Benign, fixed string");
          break;
      default:
      {
          HDESK hDesk;
          wchar_t * desktopName = L"DesktopExample";
          /* FIX: Call CreateDesktopW() without GENERIC_READ as the 5th parameter to limit access */
          hDesk = CreateDesktopW(
                      desktopName,
                      NULL,
                      NULL,
                      0,
                      GENERIC_READ,
                      NULL);
          if (hDesk == NULL)
          {
              printLine("Desktop could not be created");
          }
          else
          {
              printLine("Desktop created successfully");
              CloseDesktop(hDesk);
          }
      }
      break;
      }
  }
  
  /* good2() reverses the blocks in the switch */
  static void good2()
  {
      switch(6)
      {
      case 6:
      {
          HDESK hDesk;
          wchar_t * desktopName = L"DesktopExample";
          /* FIX: Call CreateDesktopW() without GENERIC_READ as the 5th parameter to limit access */
          hDesk = CreateDesktopW(
                      desktopName,
                      NULL,
                      NULL,
                      0,
                      GENERIC_READ,
                      NULL);
          if (hDesk == NULL)
          {
              printLine("Desktop could not be created");
          }
          else
          {
              printLine("Desktop created successfully");
              CloseDesktop(hDesk);
          }
      }
      break;
      default:
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          printLine("Benign, fixed string");
          break;
      }
  }
  
  void CWE284_Improper_Access_Control__w32_wchar_t_CreateDesktop_15_good()
  {
      good1();
      good2();
  }
  
  #endif /* OMITGOOD */
  
  /* Below is the main(). It is only used when building this testcase on
     its own for testing or for building a binary to use in testing binary
     analysis tools. It is not used when compiling all the testcases as one
     application, which is how source code analysis tools are tested. */
  
  #ifdef INCLUDEMAIN
  
  int main(int argc, char * argv[])
  {
      /* seed randomness */
      srand( (unsigned)time(NULL) );
  #ifndef OMITGOOD
      printLine("Calling good()...");
      CWE284_Improper_Access_Control__w32_wchar_t_CreateDesktop_15_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE284_Improper_Access_Control__w32_wchar_t_CreateDesktop_15_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  