The exact laws vary from state to state and country to country. Cupillar lives in Australia.. i have no idea the exact legality.
In america they cannot search without a warrant or resonable cause. They can pat you down, supposedly feeling for weapons. Supposedly they are only supposed to pat down if they think you are a threat but they will always 'think' you are a threat to pat you down.
After they pat you down, if they feel anything funny (baggie, pipe), they will ask you about it. They are very good at getting your to admit what it is, or trap you in a lie.
Always be respectful, but remember you always have the right to remain silent. If you do have something on you, never admit to it. .A cop needs a reason to detain you and must make an effort investigate the crime that they originally stopped you for (ie they cant stop you for speeding then try to search your car) Be firm in your stance that you will not allow a search and that you wish to end the encounter
In California (and probably else where), the smell of marijuana in the air is not a valid reason for a search, so don't fall for it when the cops tell you that.
One time i was stopped by two cops while riding my bike home from a club at 2am. I had no light and i was on the sidewalk. I was sweaty from dancing so I was ripe for a pig snag. I had nothing on me besides my keys.
I get stopped. Pated down. I asked why i was being patted down since i was not indicating a threat to them "just in case" was the response. Cop gets to where my keys are in my pocket. he feels them for a good minute, feeling everything in the pocket.
He asks me what i have in my pocket... I can tell he thinks i have something illegal. I say i dont know. Cop gets in my face, i say i dont know. I get handcuffed, told im not going anywhere until i tell them. i still say i dont know, cause i dont.
eventually it comes out the cop thinks i have a crack pipe on me. i have never owned a crack pipe. I'm tripping out and say I have a weed pipe on me because i figure i must have one since the cops are freaking on me.
Turns out it was my fucking mini flashlight on my keychain. The fuckers got me to lie about a weed pipe i didnt have and to let them search me. They are sneaky, look out.