Reliable Learning for Test-time Attacks and Distribution Shift