He wasn't that stupid, he just didn't time it well enough.
Steroid use is pretty much standard in athletics now. Most of the athletes know that they can get the benefits of the steroids (building more muscle) and avoid the sanctions of their use by discontinuing the administration of the drugs a certain amount of time ahead of competition. What most try to do is to use the drugs right up to the last possible moment they can just prior to an event where they will be tested and stop in time for the drug to be out of their system enough to not be detected. that window of opportunity is no narrow that they don't always succeed. that is probably what happend to Landis.
Another problem for athletes is that the tests do not test directly for the drugs themselves. They test for the steroids' effects on related hormones. One cannot always predict just when the effected hormone ratios will return to normal since other factors can affect those ratios as well. One powerlifter was banned for life from the sport because his hormones were not normal and the sport's governing body presumed he was taking drugs. This was when they began the testing. He denied taking the drugs and even provided proof that the test results were normal for him. But the rules were that the hormone ratio he had was presumptive of steroid use, no exceptions allowed, period. so one of the greats of that sport was banned for life right at the begining of testing.
They don't test directly for the drugs because that kind of testing is more invasive (requires veinipuncture rather than pissing in a cup) and expensive than testing the ratio of creatnine to several other hormones in the urine. Considering all the factors which can alter the ratios of creatnine to other hormones in the urine which have nothing to do with the use of steroids (the conditon of the kidneys themselves, the amount of fluid intake by the individual, diseases of the adrenal and other glands, tumors, etc...) I think they should draw the blood and test directly for what they are looking for.