# File lib/rubygems/validator.rb, line 84
  def alien
    errors = {}

    Gem::SourceIndex.from_installed_gems.each do |gem_name, gem_spec|
      errors[gem_name] ||= []

      gem_path = File.join(Gem.dir, "cache", gem_spec.full_name) + ".gem"
      spec_path = File.join(Gem.dir, "specifications", gem_spec.full_name) + ".gemspec"
      gem_directory = File.join(Gem.dir, "gems", gem_spec.full_name)

      installed_files = find_files_for_gem(gem_directory)

      unless File.exist? spec_path then
        errors[gem_name] << ErrorData.new(spec_path, "Spec file doesn't exist for installed gem")
      end

      begin
        verify_gem_file(gem_path)

        open gem_path, Gem.binary_mode do |file|
          format = Gem::Format.from_file_by_path(gem_path)
          format.file_entries.each do |entry, data|
            # Found this file.  Delete it from list
            installed_files.delete remove_leading_dot_dir(entry['path'])

            next unless data # HACK `gem check -a mkrf`

            open File.join(gem_directory, entry['path']), Gem.binary_mode do |f|
              unless Gem::MD5.hexdigest(f.read).to_s ==
                Gem::MD5.hexdigest(data).to_s then
                errors[gem_name] << ErrorData.new(entry['path'], "installed file doesn't match original from gem")
              end
            end
          end
        end
      rescue Gem::VerificationError => e
        errors[gem_name] << ErrorData.new(gem_path, e.message)
      end

      # Clean out directories that weren't explicitly included in the gemspec
      # FIXME: This still allows arbitrary incorrect directories.
      installed_files.delete_if {|potential_directory|
        File.directory?(File.join(gem_directory, potential_directory))
      }
      if(installed_files.size > 0) then
        errors[gem_name] << ErrorData.new(gem_path, "Unmanaged files in gem: #{installed_files.inspect}")
      end
    end

    errors
  end