The necessary expenses, incurred by the auditor and treasurer in the defense and prosecution of any action brought by or against them to test the validity or constitutionality of any act of the Legislature or of the board of supervisors or of any order providing for the payment of any funds held in the county treasury are county charges.