text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE23_Relative_Path_Traversal__char_connect_socket_w32CreateFile_64b.cpp
  Label Definition File: CWE23_Relative_Path_Traversal.label.xml
  Template File: sources-sink-64b.tmpl.cpp
  */
  /*
   * @description
   * CWE: 23 Relative Path Traversal
   * BadSource: connect_socket Read data using a connect socket (client side)
   * GoodSource: Use a fixed file name
   * Sinks: w32CreateFile
   *    BadSink : Open the file named in data using CreateFile()
   * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files
   *
   * */
  
  #include "std_testcase.h"
  
  #ifdef _WIN32
  #define BASEPATH "c:\\temp\\"
  #else
  #include <wchar.h>
  #define BASEPATH "/tmp/"
  #endif
  
  #ifdef _WIN32
  #include <winsock2.h>
  #include <windows.h>
  #include <direct.h>
  #pragma comment(lib, "ws2_32") /* include ws2_32.lib when linking */
  #define CLOSE_SOCKET closesocket
  #else /* NOT _WIN32 */
  #include <sys/types.h>
  #include <sys/socket.h>
  #include <netinet/in.h>
  #include <arpa/inet.h>
  #include <unistd.h>
  #define INVALID_SOCKET -1
  #define SOCKET_ERROR -1
  #define CLOSE_SOCKET close
  #define SOCKET int
  #endif
  
  #define TCP_PORT 27015
  #define IP_ADDRESS "127.0.0.1"
  
  
  namespace CWE23_Relative_Path_Traversal__char_connect_socket_w32CreateFile_64
  {
  
  #ifndef OMITBAD
  
  void badSink(void * dataVoidPtr)
  {
      /* cast void pointer to a pointer of the appropriate type */
      char * * dataPtr = (char * *)dataVoidPtr;
      /* dereference dataPtr into data */
      char * data = (*dataPtr);
      {
          HANDLE hFile;
          /* POTENTIAL FLAW: Possibly creating and opening a file without validating the file name or path */
          hFile = CreateFileA(data,
                              (GENERIC_WRITE|GENERIC_READ),
                              0,
                              NULL,
                              OPEN_ALWAYS,
                              FILE_ATTRIBUTE_NORMAL,
                              NULL);
          if (hFile != INVALID_HANDLE_VALUE)
          {
              CloseHandle(hFile);
          }
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void goodG2BSink(void * dataVoidPtr)
  {
      /* cast void pointer to a pointer of the appropriate type */
      char * * dataPtr = (char * *)dataVoidPtr;
      /* dereference dataPtr into data */
      char * data = (*dataPtr);
      {
          HANDLE hFile;
          /* POTENTIAL FLAW: Possibly creating and opening a file without validating the file name or path */
          hFile = CreateFileA(data,
                              (GENERIC_WRITE|GENERIC_READ),
                              0,
                              NULL,
                              OPEN_ALWAYS,
                              FILE_ATTRIBUTE_NORMAL,
                              NULL);
          if (hFile != INVALID_HANDLE_VALUE)
          {
              CloseHandle(hFile);
          }
      }
  }
  
  #endif /* OMITGOOD */
  
  } /* close namespace */
  