Most countries have a customs system where every n-th passenger is selected randomly for a compliance check. This is the most rigorous and thorough check they do, and more for evaluating the effectiveness of their other processes. I had the good fortune to be selected for a "1 in a million" check once, which took 3 hours elapsed -- so it could be worse. And yes, this applies equally to citizens etc. and no court order is needed. On phones and laptops they are looking for a wide range of illegal items. Questioning the legality of the inspection or becoming nervous during it leads to suspicion that there is something to hide which only makes it last longer.