Back to Repositories

Testing Hill Cipher Encryption Implementation in javascript-algorithms

This test suite validates the Hill Cipher implementation in JavaScript, focusing on encryption functionality and error handling. It ensures proper validation of input parameters and verifies the correct encryption of messages using the Hill Cipher algorithm.

Test Coverage Overview

The test suite provides comprehensive coverage of the Hill Cipher implementation.

Key areas tested include:
  • Error handling for unimplemented decryption
  • Input validation for non-letter characters
  • Key string length validation
  • Message and key length relationship verification
  • Actual encryption functionality with known test cases

Implementation Analysis

The testing approach utilizes Jest’s expect assertions and error checking capabilities. The implementation follows a systematic pattern of testing both error conditions and successful encryption scenarios.

Notable patterns include:
  • Arrow function wrapping for exception testing
  • Multiple test cases for encryption verification
  • Structured error message validation

Technical Details

Testing infrastructure includes:
  • Jest testing framework
  • ES6 module imports
  • Describe/It block structure
  • Error throwing and catching mechanisms
  • String manipulation validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Comprehensive error case coverage
  • Clear test case descriptions
  • Isolated test scenarios
  • Meaningful error messages
  • Multiple positive test cases with different inputs

trekhleb/javascript-algorithms

src/algorithms/cryptography/hill-cipher/_test_/hillCipher.test.js

            
import { hillCipherEncrypt, hillCipherDecrypt } from '../hillCipher';

describe('hillCipher', () => {
  it('should throw an exception when trying to decipher', () => {
    expect(hillCipherDecrypt).toThrowError('This method is not implemented yet');
  });

  it('should throw an error when message or keyString contains none letter character', () => {
    const invalidCharacterInMessage = () => {
      hillCipherEncrypt('hell3', 'helloworld');
    };
    const invalidCharacterInKeyString = () => {
      hillCipherEncrypt('hello', 'hel12world');
    };
    expect(invalidCharacterInMessage).toThrowError(
      'The message and key string can only contain letters',
    );
    expect(invalidCharacterInKeyString).toThrowError(
      'The message and key string can only contain letters',
    );
  });

  it('should throw an error when the length of the keyString has a square root which is not integer', () => {
    const invalidLengthOfKeyString = () => {
      hillCipherEncrypt('ab', 'ab');
    };
    expect(invalidLengthOfKeyString).toThrowError(
      'Invalid key string length. The square root of the key string must be an integer',
    );
  });

  it('should throw an error when the length of the keyString does not equal to the power of length of the message', () => {
    const invalidLengthOfKeyString = () => {
      hillCipherEncrypt('ab', 'aaabbbccc');
    };
    expect(invalidLengthOfKeyString).toThrowError(
      'Invalid key string length. The key length must be a square of message length',
    );
  });

  it('should encrypt passed message using Hill Cipher', () => {
    expect(hillCipherEncrypt('ACT', 'GYBNQKURP')).toBe('POH');
    expect(hillCipherEncrypt('CAT', 'GYBNQKURP')).toBe('FIN');
    expect(hillCipherEncrypt('GFG', 'HILLMAGIC')).toBe('SWK');
  });
});