In civil lawsuits, including patent infringement,there is something called discovery where a judge requires the defendant to expose information relevant to the case to the plaintive.
Also, patent owners can attempt to reverse-engineer products to look for infringement. It is not only software. Integrated circuits can be analyzed by pealing away layers. https://electronics.stackexchange.com/questions/13472/is-it-possible-to-reverse-engineer-a-chip-design
See answer to similar question How is patent infringement usually being discovered?