text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE187_Partial_Comparison__wchar_t_connect_socket_str_63b.c
  Label Definition File: CWE187_Partial_Comparison.label.xml
  Template File: sources-sinks-63b.tmpl.c
  */
  /*
   * @description
   * CWE: 187 Partial Comparison
   * BadSource: connect_socket Read data using a connect socket (client side)
   * GoodSource: Provide a matching password
   * Sinks: str
   *    GoodSink: Compare the 2 passwords correctly
   *    BadSink : use wcsstr() to do password match, which is a partial comparison
   * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files
   *
   * */
  
  #include "std_testcase.h"
  
  #include <wchar.h>
  
  #define PASSWORD L"Password1234"
  /* PASSWORD_SZ must equal the length of PASSWORD */
  #define PASSWORD_SZ wcslen(PASSWORD)
  
  #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
  # define PATH_SZ 100
  #else /* NOT _WIN32 */
  # define INVALID_SOCKET -1
  # define SOCKET_ERROR -1
  # define CLOSE_SOCKET close
  # define SOCKET int
  # define PATH_SZ PATH_MAX
  #endif
  
  #define TCP_PORT 27015
  
  #ifndef OMITBAD
  
  void CWE187_Partial_Comparison__wchar_t_connect_socket_str_63b_bad_sink(wchar_t * * data_ptr)
  {
      wchar_t * data = *data_ptr;
      /* POTENTIAL FLAW: Partially compare the two passwords */
      if (wcsstr(PASSWORD, data) != NULL)
          printLine("Access granted");
      else
          printLine("Access denied!");
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void CWE187_Partial_Comparison__wchar_t_connect_socket_str_63b_goodG2B_sink(wchar_t * * data_ptr)
  {
      wchar_t * data = *data_ptr;
      /* POTENTIAL FLAW: Partially compare the two passwords */
      if (wcsstr(PASSWORD, data) != NULL)
          printLine("Access granted");
      else
          printLine("Access denied!");
  }
  
  /* goodB2G uses the BadSource with the GoodSink */
  void CWE187_Partial_Comparison__wchar_t_connect_socket_str_63b_goodB2G_sink(wchar_t * * data_ptr)
  {
      wchar_t * data = *data_ptr;
      /* Ideally, we would want to do a check to see if the passwords are of equal length */
      /* FIX: Compare the two passwords completely and correctly */
      if (wcscmp(PASSWORD, data) == 0)
          printLine("Access granted");
      else
          printLine("Access denied!");
  }
  
  #endif /* OMITGOOD */
  