<commit_before>/**
 * @license
 * Copyright 2018 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

foam.CLASS({
  package: 'foam.util',
  name: 'PasswordTest',
  extends: 'foam.nanos.test.Test',

  methods: [
    {
      name: 'runTest',
      javaCode: `
        // hashing tests
        Password_HashWithInvalidInput_IllegalArgumentException(null, "Password hashing with null throws an IllegalArgumentException");;
        Password_HashWithInvalidInput_IllegalArgumentException("", "Password hashing with empty string input throws an IllegalArgumentException");
        Password_HashWithValidInput_Succeeds("Testing123");

        // verify tests - null/empty input
        Password_Verify(null, null, false, "Password verification returns false with password and hash both null");
        Password_Verify("", null, false, "Password verification returns false with password empty and hash null");
        Password_Verify(null, "", false, "Password verification returns false with password null and hash empty");
        Password_Verify("", "", false, "Password verification returns false with password and hash both empty");

        // verify tests - correct password invalid hashed password
        Password_Verify("<PASSWORD>", null, false, "Password verification returns false with correct password and null hashed password");
        Password_Verify("<PASSWORD>", "", false, "Password verification returns false with correct password and empty hashed password");
        Password_Verify("<PASSWORD>", "<PASSWORD>", false, "Password verification returns false with correct password and garbage data for hashed password");
        Password_Verify("<PASSWORD>", "ZgZ8/RVYJjc=:67iHRqR2TmD7JQNNDMRIek/vcNmJlwBOXBaENDJsa+8fOsyW6A6k6/jZLvalXI9suIngb0J/ruyrHAkCCUkQ6w==", false, "Password verification returns false with correct password, incorrect salt but correct hashed password");
        Password_Verify("<PASSWORD>", "ZgZ8/RVYJjg=:ZoRxd0SUr9TJposPLwrucP3aZNy1R2Fj2wgBk1uY5ShhNasUJMJV8ayj21NtSOJY5XSz8QzxRxQiWPVykbqXfQ==", false, "Password verification returns false with correct password, correct salt, but incorrect hashed password");
        Password_Verify("<PASSWORD>", "ZgZ8/RVYJjg=:67iHRqR2TmD7JQNNDMRIek/vcNmJlwBOXBaENDJsa+8fOsyW6A6k6/jZLvalXI9suIngb0J/ruyrHAkCCUkQ6w==", true, "Password verification returns true with correct password, correct salt, and correct hashed password");

        // isValid tests
        Password_IsValid(null, false, "isValid method returns false given null");
        Password_IsValid("", false, "isValid method returns false given empty string");
        Password_IsValid("<PASSWORD>", false, "isValid returns false given password shorter than 6 characters");
        Password_IsValid("<PASSWORD>", true, "isValid returns true given password longer than 6 characters, with uppercase and with numbers");
      `
    },
    {
      name: 'Password_HashWithInvalidInput_IllegalArgumentException',
      args: [
        { class: 'String', name: 'input'    },
        { class: 'String', name: 'message'  }
      ],
      javaCode: `
        try {
          Password.hash(input);
          test(false, "Password hashing with null should throw an IllegalArgumentException");
        } catch ( Throwable t ) {
          test(t instanceof IllegalArgumentException, message);
        }
      `
    },
    {
      name: 'Password_HashWithValidInput_Succeeds',
      args: [
        { class: 'String', name: 'input' }
      ],
      javaCode: `
        try {
          test(! SafetyUtil.isEmpty(Password.hash(input)), "Password hashing with non empty string produces output");
        } catch ( Throwable t ) {
          test(false, "Password hashing with valid input should not throw an exception");
        }
      `
    },
    {
      name: 'Password_Verify',
      args: [
        { class: 'String',  name: 'password' },
        { class: 'String',  name: 'hash'     },
        { class: 'Boolean', name: 'expected' },
        { class: 'String',  name: 'message'  }
      ],
      javaCode: `
        test(Password.verify(password, hash) == expected, message);
      `
    },
    {
      name: 'Password_IsValid',
      args: [
        { class: 'String',  name: 'input'    },
        { class: 'Boolean', name: 'expected' },
        { class: 'String',  name: 'message'  },
      ],
      javaCode: `
        test(Password.isValid(input) == expected, message);
      `
    }
  ]
});<commit_msg>Change Password test to reflect updates to isValid method<commit_after>/**
 * @license
 * Copyright 2018 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

foam.CLASS({
  package: 'foam.util',
  name: 'PasswordTest',
  extends: 'foam.nanos.test.Test',

  javaImports: [
    'foam.core.X'
  ],

  methods: [
    {
      name: 'runTest',
      javaCode: `
        // hashing tests
        Password_HashWithInvalidInput_IllegalArgumentException(null, "Password hashing with null throws an IllegalArgumentException");;
        Password_HashWithInvalidInput_IllegalArgumentException("", "Password hashing with empty string input throws an IllegalArgumentException");
        Password_HashWithValidInput_Succeeds("Testing123");

        // verify tests - null/empty input
        Password_Verify(null, null, false, "Password verification returns false with password and hash both null");
        Password_Verify("", null, false, "Password verification returns false with password empty and hash null");
        Password_Verify(null, "", false, "Password verification returns false with password null and hash empty");
        Password_Verify("", "", false, "Password verification returns false with password and hash both empty");

        // verify tests - correct password invalid hashed password
        Password_Verify("<PASSWORD>", null, false, "Password verification returns false with correct password and null hashed password");
        Password_Verify("<PASSWORD>", "", false, "Password verification returns false with correct password and empty hashed password");
        Password_Verify("<PASSWORD>", "<PASSWORD>", false, "Password verification returns false with correct password and garbage data for hashed password");
        Password_Verify("<PASSWORD>", "ZgZ8/RVYJjc=:67iHRqR2TmD7JQNNDMRIek/vcNmJlwBOXBaENDJsa+8fOsyW6A6k6/jZLvalXI9suIngb0J/ruyrHAkCCUkQ6w==", false, "Password verification returns false with correct password, incorrect salt but correct hashed password");
        Password_Verify("<PASSWORD>", "ZgZ8/RVYJjg=:ZoRxd0SUr9TJposPLwrucP3aZNy1R2Fj2wgBk1uY5ShhNasUJMJV8ayj21NtSOJY5XSz8QzxRxQiWPVykbqXfQ==", false, "Password verification returns false with correct password, correct salt, but incorrect hashed password");
        Password_Verify("<PASSWORD>", "<KEY>=:67iHRqR2TmD7JQNNDMRIek/vcNmJlwBOXBaENDJsa+8fOsyW6A6k6/jZLvalXI9suIngb0J/ruyrHAkCCUkQ6w==", true, "Password verification returns true with correct password, correct salt, and correct hashed password");

        // isValid tests
        Password_IsValid(x, null, false, "isValid method returns false given null");
        Password_IsValid(x, "", false, "isValid method returns false given empty string");
        Password_IsValid(x, "<PASSWORD>", false, "isValid returns false given password shorter than 6 characters");
        Password_IsValid(x, "<PASSWORD>", true, "isValid returns true given password longer than 6 characters");
      `
    },
    {
      name: 'Password_HashWithInvalidInput_IllegalArgumentException',
      args: [
        { class: 'String', name: 'input'    },
        { class: 'String', name: 'message'  }
      ],
      javaCode: `
        try {
          Password.hash(input);
          test(false, "Password hashing with null should throw an IllegalArgumentException");
        } catch ( Throwable t ) {
          test(t instanceof IllegalArgumentException, message);
        }
      `
    },
    {
      name: 'Password_HashWithValidInput_Succeeds',
      args: [
        { class: 'String', name: 'input' }
      ],
      javaCode: `
        try {
          test(! SafetyUtil.isEmpty(Password.hash(input)), "Password hashing with non empty string produces output");
        } catch ( Throwable t ) {
          test(false, "Password hashing with valid input should not throw an exception");
        }
      `
    },
    {
      name: 'Password_Verify',
      args: [
        { class: 'String',  name: 'password' },
        { class: 'String',  name: 'hash'     },
        { class: 'Boolean', name: 'expected' },
        { class: 'String',  name: 'message'  }
      ],
      javaCode: `
        test(Password.verify(password, hash) == expected, message);
      `
    },
    {
      name: 'Password_IsValid',
      args: [
        { javaType: 'foam.core.X',  name: 'x'    },
        { class: 'String',  name: 'input'    },
        { class: 'Boolean', name: 'expected' },
        { class: 'String',  name: 'message'  },
      ],
      javaCode: `
        test(Password.isValid(x, input) == expected, message);
      `
    }
  ]
});
{'fraction_non_alphanumeric': 0.11246267831471857, 'fraction_numerical': 0.0107265288068119, 'mean_word_length': 3.2480037576326914, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4307001', 'n_tokens_mistral': 2654, 'n_tokens_neox': 2587, 'n_words': 707}