How could the Buddha know that he had attained enlightenment when he didn't know what it was? Having learned and gained complete mastery from the two most famous teachers of his time, he decided to apply extreme austerities for some six years. With these skills acquired, driving a powerful concentration,  he abandoned it all in favour of a skill he discovered when as a child at the Kings Plowing Ceremony, where he entered the first jhana [Dhyāna] quite effortlessly.

Furthermore, added to this cache of tools, he prior added a powerful determination to not move from that spot, even if his blood should dry up, etc, etc. The subsequent release of this energy resulted in a spectacular display of meditative attainment.

In the first watch of the night investigating Kamma with respect to successive past lives, revealing causal sequence. In the second watch of the night, investigating Kamma with respect to consequences of currently available choices. In the third watch of the night, the realisation of deliverance.

Not much is said about the results of the third watch. It is a fairly common experience where insights coming from seeing a new possibility after examining two different phenomena with a common factor giving rise to a eureka moment.

But what may have happened in this case, such a eureka event further resulting in a realisation that Dukkha had ceased? The Buddha's quest finally achieved.

During the next eight weeks, the problem of describing a way of enabling others to achieve this result, though necessarily not in the same way, given the death of the two teachers, plus avoiding austerities, plus the absence of psychic powers.

In developing a tangible expression for the inexpressible, the Buddha further developed the right view, that Dukkha exists, arises and ceases according to conditions. Enabling the further development of the 'noble eightfold path'. According to tradition, all this happened in an instant, that is to say, the sequence happened very rapidly: the problem is to give it coherent expression. The result: the four noble truths.
In the third watch of the night He had the knowledge of the exhaustion of cankers, knowing that He had eradicated all defilements and made an end of ill.


The Three Supreme Knowledges of Lord Buddha

So the Buddha knew he had overcome defilements and was enlightened due to knowledge of the exhaustion of cankers.

Also with the arising of knowledge of the exhaustion of cankers the Buddha knew what enlightenment was.