text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE187_Partial_Comparison__char_substring_ncmp_user_pw_08.c
  Label Definition File: CWE187_Partial_Comparison.label.xml
  Template File: sources-sinks-08.tmpl.c
  */
  /*
   * @description
   * CWE: 187 Partial Comparison
   * BadSource: substring Provide a password that is a shortened version of the actual password
   * GoodSource: Provide a matching password
   * Sinks: ncmp_user_pw
   *    GoodSink: Compare the 2 passwords correctly
   *    BadSink : use strncmp() to do password match, but use the length of the user password
   * Flow Variant: 08 Control flow: if(static_returns_t()) and if(static_returns_f())
   *
   * */
  
  #include "std_testcase.h"
  
  #include <wchar.h>
  
  #define PASSWORD "Password1234"
  /* PASSWORD_SZ must equal the length of PASSWORD */
  #define PASSWORD_SZ strlen(PASSWORD)
  
  /* The two function below always return the same value, so a tool
     should be able to identify that calls to the functions will always
     return a fixed value. */
  static int static_returns_t()
  {
      return 1;
  }
  
  static int static_returns_f()
  {
      return 0;
  }
  
  #ifndef OMITBAD
  
  void CWE187_Partial_Comparison__char_substring_ncmp_user_pw_08_bad()
  {
      char * data;
      char data_buf[100] = "";
      data = data_buf;
      if(static_returns_t())
      {
          /* FLAW: Provide a shortened version of the actual password
           * NOTE: This must be a substring of PASSWORD starting with the first character in PASSWORD
           * i.e. other examples could be "Pa", "Pas", "Pass", etc. as long as it is shorter than MIN_PASSWORD_SZ
           * and does not match the full PASSWORD string */
          data = "P";
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FIX: Use the matching string */
          data = PASSWORD;
      }
      if(static_returns_t())
      {
          /* By using the strlen() of the input password, you are able to create a partial comparison
           * For example if PASSWORD=PASSWORD1234, a user supplied password of PASSWORD12 will allow access */
          /* POTENTIAL FLAW: Possibly partially compare the two passwords */
          if (strncmp(PASSWORD, data, strlen(data)) == 0)
              printLine("Access granted");
          else
              printLine("Access denied!");
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* 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 (strcmp(PASSWORD, data) == 0)
              printLine("Access granted");
          else
              printLine("Access denied!");
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodB2G1() - use badsource and goodsink by changing the second static_returns_t() to static_returns_f() */
  static void goodB2G1()
  {
      char * data;
      char data_buf[100] = "";
      data = data_buf;
      if(static_returns_t())
      {
          /* FLAW: Provide a shortened version of the actual password
           * NOTE: This must be a substring of PASSWORD starting with the first character in PASSWORD
           * i.e. other examples could be "Pa", "Pas", "Pass", etc. as long as it is shorter than MIN_PASSWORD_SZ
           * and does not match the full PASSWORD string */
          data = "P";
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FIX: Use the matching string */
          data = PASSWORD;
      }
      if(static_returns_f())
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* By using the strlen() of the input password, you are able to create a partial comparison
           * For example if PASSWORD=PASSWORD1234, a user supplied password of PASSWORD12 will allow access */
          /* POTENTIAL FLAW: Possibly partially compare the two passwords */
          if (strncmp(PASSWORD, data, strlen(data)) == 0)
              printLine("Access granted");
          else
              printLine("Access denied!");
      }
      else
      {
          /* 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 (strcmp(PASSWORD, data) == 0)
              printLine("Access granted");
          else
              printLine("Access denied!");
      }
  }
  
  /* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */
  static void goodB2G2()
  {
      char * data;
      char data_buf[100] = "";
      data = data_buf;
      if(static_returns_t())
      {
          /* FLAW: Provide a shortened version of the actual password
           * NOTE: This must be a substring of PASSWORD starting with the first character in PASSWORD
           * i.e. other examples could be "Pa", "Pas", "Pass", etc. as long as it is shorter than MIN_PASSWORD_SZ
           * and does not match the full PASSWORD string */
          data = "P";
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FIX: Use the matching string */
          data = PASSWORD;
      }
      if(static_returns_t())
      {
          /* 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 (strcmp(PASSWORD, data) == 0)
              printLine("Access granted");
          else
              printLine("Access denied!");
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* By using the strlen() of the input password, you are able to create a partial comparison
           * For example if PASSWORD=PASSWORD1234, a user supplied password of PASSWORD12 will allow access */
          /* POTENTIAL FLAW: Possibly partially compare the two passwords */
          if (strncmp(PASSWORD, data, strlen(data)) == 0)
              printLine("Access granted");
          else
              printLine("Access denied!");
      }
  }
  
  /* goodG2B1() - use goodsource and badsink by changing the first static_returns_t() to static_returns_f() */
  static void goodG2B1()
  {
      char * data;
      char data_buf[100] = "";
      data = data_buf;
      if(static_returns_f())
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FLAW: Provide a shortened version of the actual password
           * NOTE: This must be a substring of PASSWORD starting with the first character in PASSWORD
           * i.e. other examples could be "Pa", "Pas", "Pass", etc. as long as it is shorter than MIN_PASSWORD_SZ
           * and does not match the full PASSWORD string */
          data = "P";
      }
      else
      {
          /* FIX: Use the matching string */
          data = PASSWORD;
      }
      if(static_returns_t())
      {
          /* By using the strlen() of the input password, you are able to create a partial comparison
           * For example if PASSWORD=PASSWORD1234, a user supplied password of PASSWORD12 will allow access */
          /* POTENTIAL FLAW: Possibly partially compare the two passwords */
          if (strncmp(PASSWORD, data, strlen(data)) == 0)
              printLine("Access granted");
          else
              printLine("Access denied!");
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* 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 (strcmp(PASSWORD, data) == 0)
              printLine("Access granted");
          else
              printLine("Access denied!");
      }
  }
  
  /* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */
  static void goodG2B2()
  {
      char * data;
      char data_buf[100] = "";
      data = data_buf;
      if(static_returns_t())
      {
          /* FIX: Use the matching string */
          data = PASSWORD;
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FLAW: Provide a shortened version of the actual password
           * NOTE: This must be a substring of PASSWORD starting with the first character in PASSWORD
           * i.e. other examples could be "Pa", "Pas", "Pass", etc. as long as it is shorter than MIN_PASSWORD_SZ
           * and does not match the full PASSWORD string */
          data = "P";
      }
      if(static_returns_t())
      {
          /* By using the strlen() of the input password, you are able to create a partial comparison
           * For example if PASSWORD=PASSWORD1234, a user supplied password of PASSWORD12 will allow access */
          /* POTENTIAL FLAW: Possibly partially compare the two passwords */
          if (strncmp(PASSWORD, data, strlen(data)) == 0)
              printLine("Access granted");
          else
              printLine("Access denied!");
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* 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 (strcmp(PASSWORD, data) == 0)
              printLine("Access granted");
          else
              printLine("Access denied!");
      }
  }
  
  void CWE187_Partial_Comparison__char_substring_ncmp_user_pw_08_good()
  {
      goodB2G1();
      goodB2G2();
      goodG2B1();
      goodG2B2();
  }
  
  #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()...");
      CWE187_Partial_Comparison__char_substring_ncmp_user_pw_08_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE187_Partial_Comparison__char_substring_ncmp_user_pw_08_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  