Back to Repositories

Validating DKIM Header Generation and Signing in Postal

This test suite validates DKIM (DomainKeys Identified Mail) header generation and signing functionality in Postal server. It ensures proper email authentication by verifying header formatting, signature generation, and compliance with DKIM standards.

Test Coverage Overview

The test suite provides comprehensive coverage of DKIM header generation functionality.

Key areas tested include:
  • Header format validation
  • RSA signature generation
  • Domain verification
  • Timestamp handling
  • Base64 encoding of signatures
Edge cases are handled through multiple test examples loaded from external message files, ensuring robust validation across different scenarios.

Implementation Analysis

The testing approach utilizes RSpec’s describe/it blocks with dynamic test generation based on example files. The implementation leverages instance doubles for domain mocking and time manipulation, allowing controlled testing of signature generation.

Key patterns include:
  • File-based test data management
  • YAML frontmatter parsing
  • Dependency injection through mocking
  • Time freezing for deterministic testing

Technical Details

Testing tools and configuration:
  • RSpec testing framework
  • Rails test helpers
  • OpenSSL for RSA key handling
  • YAML for test data management
  • Custom example message files
  • Time manipulation helpers

Best Practices Demonstrated

The test suite exemplifies several testing best practices for complex email authentication systems.

Notable practices include:
  • Separation of test data from test logic
  • Proper mocking of external dependencies
  • Deterministic timestamp handling
  • Clear expectation formatting
  • Dynamic test generation for multiple scenarios
  • Modular test organization

postalserver/postal

spec/lib/dkim_header_spec.rb

            
# frozen_string_literal: true

require "rails_helper"

describe DKIMHeader do
  examples = Rails.root.join("spec/examples/dkim_signing/*.msg")
  Dir[examples].each do |path|
    contents = File.read(path)
    frontmatter, email = contents.split(/^---\n/m, 2)
    frontmatter = YAML.safe_load(frontmatter)
    email.strip
    it "works with #{path.split('/').last}" do
      mocked_time = Time.at(frontmatter["time"].to_i)
      allow(Time).to receive(:now).and_return(mocked_time)

      domain = instance_double("Domain")
      allow(domain).to receive(:dkim_status).and_return("OK")
      allow(domain).to receive(:name).and_return(frontmatter["domain"])
      allow(domain).to receive(:dkim_key).and_return(OpenSSL::PKey::RSA.new(frontmatter["private_key"]))
      allow(domain).to receive(:dkim_identifier).and_return(frontmatter["dkim_identifier"])

      expectation = "DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r\n" \
                    "\td=#{frontmatter['domain']};\r\n" \
                    "\ts=#{frontmatter['dkim_identifier']}; t=#{mocked_time.to_i};\r\n" \
                    "\tbh=#{frontmatter['bh']};\r\n" \
                    "\th=#{frontmatter['headers']};\r\n" \
                    "\tb=#{frontmatter['b'].scan(/.{1,72}/).join("\r\n\t")}"

      header = described_class.new(domain, email)

      expect(header.dkim_header).to eq expectation
    end
  end
end