/tests/LightSaml/Tests/Validator/Model/NameId/NameIdValidatorTest.php

https://github.com/lightSAML/lightSAML · PHP · 438 lines · 316 code · 122 blank · 0 comment · 0 complexity · 57a85886adfe64102503a0f86fa2828b MD5 · raw file

  1. <?php
  2. namespace LightSaml\Tests\Validator\Model\NameId;
  3. use LightSaml\Model\Assertion\NameID;
  4. use LightSaml\SamlConstants;
  5. use LightSaml\Tests\BaseTestCase;
  6. use LightSaml\Validator\Model\NameId\NameIdValidator;
  7. class NameIdValidatorTest extends BaseTestCase
  8. {
  9. public function test_ok_if_no_format()
  10. {
  11. $nameId = new NameID();
  12. $validator = new NameIdValidator();
  13. $validator->validateNameId($nameId);
  14. $this->assertTrue(true);
  15. }
  16. public function test_invalid_format()
  17. {
  18. $this->expectExceptionMessage("NameID element has Format attribute 'invalid format' which is not a wellformed absolute uri");
  19. $this->expectException(\LightSaml\Error\LightSamlValidationException::class);
  20. $nameId = new NameID();
  21. $nameId->setFormat('invalid format');
  22. $validator = new NameIdValidator();
  23. $validator->validateNameId($nameId);
  24. $this->assertTrue(true);
  25. }
  26. public function test_valid_email_format()
  27. {
  28. $nameId = new NameID();
  29. $nameId->setFormat(SamlConstants::NAME_ID_FORMAT_EMAIL)
  30. ->setValue('email@domain.com');
  31. $validator = new NameIdValidator();
  32. $validator->validateNameId($nameId);
  33. $this->assertTrue(true);
  34. }
  35. public function test_invalid_email_format()
  36. {
  37. $this->expectExceptionMessage("Value of NameID is not a valid email address according to the IETF RFC 2822 specification");
  38. $this->expectException(\LightSaml\Error\LightSamlValidationException::class);
  39. $nameId = new NameID();
  40. $nameId->setFormat(SamlConstants::NAME_ID_FORMAT_EMAIL)
  41. ->setValue('not_an_email');
  42. $validator = new NameIdValidator();
  43. $validator->validateNameId($nameId);
  44. $this->assertTrue(true);
  45. }
  46. public function test_empty_email_format()
  47. {
  48. $this->expectExceptionMessage("NameID with Email Format attribute MUST contain a Value that contains more than whitespace characters");
  49. $this->expectException(\LightSaml\Error\LightSamlValidationException::class);
  50. $nameId = new NameID();
  51. $nameId->setFormat(SamlConstants::NAME_ID_FORMAT_EMAIL);
  52. $validator = new NameIdValidator();
  53. $validator->validateNameId($nameId);
  54. $this->assertTrue(true);
  55. }
  56. public function test_valid_x509_subject_format()
  57. {
  58. $nameId = new NameID();
  59. $nameId->setFormat(SamlConstants::NAME_ID_FORMAT_X509_SUBJECT_NAME)
  60. ->setValue('CN=mt.evo.team,O=BOS,C=RS');
  61. $validator = new NameIdValidator();
  62. $validator->validateNameId($nameId);
  63. $this->assertTrue(true);
  64. }
  65. public function test_empty_x509_subject_format()
  66. {
  67. $this->expectExceptionMessage("NameID with X509SubjectName Format attribute MUST contain a Value that contains more than whitespace characters");
  68. $this->expectException(\LightSaml\Error\LightSamlValidationException::class);
  69. $nameId = new NameID();
  70. $nameId->setFormat(SamlConstants::NAME_ID_FORMAT_X509_SUBJECT_NAME);
  71. $validator = new NameIdValidator();
  72. $validator->validateNameId($nameId);
  73. $this->assertTrue(true);
  74. }
  75. public function test_valid_windows_format_with_domain()
  76. {
  77. $nameId = new NameID();
  78. $nameId->setFormat(SamlConstants::NAME_ID_FORMAT_WINDOWS)
  79. ->setValue('DomainName\UserName');
  80. $validator = new NameIdValidator();
  81. $validator->validateNameId($nameId);
  82. $this->assertTrue(true);
  83. }
  84. public function test_valid_windows_format_with_out_domain()
  85. {
  86. $nameId = new NameID();
  87. $nameId->setFormat(SamlConstants::NAME_ID_FORMAT_WINDOWS)
  88. ->setValue('UserName');
  89. $validator = new NameIdValidator();
  90. $validator->validateNameId($nameId);
  91. $this->assertTrue(true);
  92. }
  93. public function test_empty_windows_format()
  94. {
  95. $this->expectExceptionMessage("NameID with Windows Format attribute MUST contain a Value that contains more than whitespace characters");
  96. $this->expectException(\LightSaml\Error\LightSamlValidationException::class);
  97. $nameId = new NameID();
  98. $nameId->setFormat(SamlConstants::NAME_ID_FORMAT_WINDOWS);
  99. $validator = new NameIdValidator();
  100. $validator->validateNameId($nameId);
  101. $this->assertTrue(true);
  102. }
  103. public function test_valid_kerberos_format_full()
  104. {
  105. $nameId = new NameID();
  106. $nameId->setFormat(SamlConstants::NAME_ID_FORMAT_KERBEROS)
  107. ->setValue('name/instance@REALM');
  108. $validator = new NameIdValidator();
  109. $validator->validateNameId($nameId);
  110. $this->assertTrue(true);
  111. }
  112. public function test_valid_kerberos_format_short()
  113. {
  114. $nameId = new NameID();
  115. $nameId->setFormat(SamlConstants::NAME_ID_FORMAT_KERBEROS)
  116. ->setValue('name@REALM');
  117. $validator = new NameIdValidator();
  118. $validator->validateNameId($nameId);
  119. $this->assertTrue(true);
  120. }
  121. public function test_invalid_kerberos_format()
  122. {
  123. $this->expectExceptionMessage("NameID with Kerberos Format attribute MUST contain a Value that contains a '@'");
  124. $this->expectException(\LightSaml\Error\LightSamlValidationException::class);
  125. $nameId = new NameID();
  126. $nameId->setFormat(SamlConstants::NAME_ID_FORMAT_KERBEROS)
  127. ->setValue('name');
  128. $validator = new NameIdValidator();
  129. $validator->validateNameId($nameId);
  130. $this->assertTrue(true);
  131. }
  132. public function test_invalid_kerberos_format_short()
  133. {
  134. $this->expectExceptionMessage("NameID with Kerberos Format attribute MUST contain a Value with at least 3 characters");
  135. $this->expectException(\LightSaml\Error\LightSamlValidationException::class);
  136. $nameId = new NameID();
  137. $nameId->setFormat(SamlConstants::NAME_ID_FORMAT_KERBEROS)
  138. ->setValue('a@');
  139. $validator = new NameIdValidator();
  140. $validator->validateNameId($nameId);
  141. $this->assertTrue(true);
  142. }
  143. public function test_invalid_kerberos_format_empty()
  144. {
  145. $this->expectExceptionMessage("NameID with Kerberos Format attribute MUST contain a Value that contains more than whitespace characters");
  146. $this->expectException(\LightSaml\Error\LightSamlValidationException::class);
  147. $nameId = new NameID();
  148. $nameId->setFormat(SamlConstants::NAME_ID_FORMAT_KERBEROS);
  149. $validator = new NameIdValidator();
  150. $validator->validateNameId($nameId);
  151. $this->assertTrue(true);
  152. }
  153. public function test_valid_entity_format()
  154. {
  155. $nameId = new NameID();
  156. $nameId->setFormat(SamlConstants::NAME_ID_FORMAT_ENTITY)
  157. ->setValue('some:entity');
  158. $validator = new NameIdValidator();
  159. $validator->validateNameId($nameId);
  160. $this->assertTrue(true);
  161. }
  162. public function test_invalid_entity_format_empty()
  163. {
  164. $this->expectExceptionMessage("NameID with Entity Format attribute MUST contain a Value that contains more than whitespace characters");
  165. $this->expectException(\LightSaml\Error\LightSamlValidationException::class);
  166. $nameId = new NameID();
  167. $nameId->setFormat(SamlConstants::NAME_ID_FORMAT_ENTITY);
  168. $validator = new NameIdValidator();
  169. $validator->validateNameId($nameId);
  170. $this->assertTrue(true);
  171. }
  172. public function test_invalid_entity_format_long()
  173. {
  174. $this->expectExceptionMessage("NameID with Entity Format attribute MUST have a Value that contains no more than 1024 characters");
  175. $this->expectException(\LightSaml\Error\LightSamlValidationException::class);
  176. $nameId = new NameID();
  177. $nameId->setFormat(SamlConstants::NAME_ID_FORMAT_ENTITY)
  178. ->setValue(str_pad('long_string', 1030, 'x'));
  179. $validator = new NameIdValidator();
  180. $validator->validateNameId($nameId);
  181. $this->assertTrue(true);
  182. }
  183. public function test_invalid_entity_format_with_name_qualifier()
  184. {
  185. $this->expectExceptionMessage("NameID with Entity Format attribute MUST NOT set the NameQualifier attribute");
  186. $this->expectException(\LightSaml\Error\LightSamlValidationException::class);
  187. $nameId = new NameID();
  188. $nameId->setFormat(SamlConstants::NAME_ID_FORMAT_ENTITY)
  189. ->setValue('some:entity')
  190. ->setNameQualifier('name:qualifier');
  191. $validator = new NameIdValidator();
  192. $validator->validateNameId($nameId);
  193. $this->assertTrue(true);
  194. }
  195. public function test_invalid_entity_format_with_sp_name_qualifier()
  196. {
  197. $this->expectExceptionMessage("NameID with Entity Format attribute MUST NOT set the SPNameQualifier attribute");
  198. $this->expectException(\LightSaml\Error\LightSamlValidationException::class);
  199. $nameId = new NameID();
  200. $nameId->setFormat(SamlConstants::NAME_ID_FORMAT_ENTITY)
  201. ->setValue('some:entity')
  202. ->setSPNameQualifier('sp:name:qualifier');
  203. $validator = new NameIdValidator();
  204. $validator->validateNameId($nameId);
  205. $this->assertTrue(true);
  206. }
  207. public function test_invalid_entity_format_with_sp_provided_id()
  208. {
  209. $this->expectExceptionMessage("NameID with Entity Format attribute MUST NOT set the SPProvidedID attribute");
  210. $this->expectException(\LightSaml\Error\LightSamlValidationException::class);
  211. $nameId = new NameID();
  212. $nameId->setFormat(SamlConstants::NAME_ID_FORMAT_ENTITY)
  213. ->setValue('some:entity')
  214. ->setSPProvidedID('sp:provided:id');
  215. $validator = new NameIdValidator();
  216. $validator->validateNameId($nameId);
  217. $this->assertTrue(true);
  218. }
  219. public function test_valid_persistent_format()
  220. {
  221. $nameId = new NameID();
  222. $nameId->setFormat(SamlConstants::NAME_ID_FORMAT_PERSISTENT)
  223. ->setValue('12345678');
  224. $validator = new NameIdValidator();
  225. $validator->validateNameId($nameId);
  226. $this->assertTrue(true);
  227. }
  228. public function test_valid_persistent_format_with_other_attributes()
  229. {
  230. $nameId = new NameID();
  231. $nameId->setFormat(SamlConstants::NAME_ID_FORMAT_PERSISTENT)
  232. ->setValue('12345678')
  233. ->setSPProvidedID('sp:provided:id')
  234. ->setSPNameQualifier('sp:name:qualifier')
  235. ->setNameQualifier('name:qualifier')
  236. ;
  237. $validator = new NameIdValidator();
  238. $validator->validateNameId($nameId);
  239. $this->assertTrue(true);
  240. }
  241. public function test_invalid_persistent_format_empty()
  242. {
  243. $this->expectExceptionMessage("NameID with Persistent Format attribute MUST contain a Value that contains more than whitespace characters");
  244. $this->expectException(\LightSaml\Error\LightSamlValidationException::class);
  245. $nameId = new NameID();
  246. $nameId->setFormat(SamlConstants::NAME_ID_FORMAT_PERSISTENT);
  247. $validator = new NameIdValidator();
  248. $validator->validateNameId($nameId);
  249. $this->assertTrue(true);
  250. }
  251. public function test_invalid_persistent_format_long()
  252. {
  253. $this->expectExceptionMessage("NameID with Persistent Format attribute MUST have a Value that contains no more than 256 characters");
  254. $this->expectException(\LightSaml\Error\LightSamlValidationException::class);
  255. $nameId = new NameID();
  256. $nameId->setFormat(SamlConstants::NAME_ID_FORMAT_PERSISTENT)
  257. ->setValue(str_pad('a', 260, 'x'));
  258. $validator = new NameIdValidator();
  259. $validator->validateNameId($nameId);
  260. $this->assertTrue(true);
  261. }
  262. public function test_valid_transient_format()
  263. {
  264. $nameId = new NameID();
  265. $nameId->setFormat(SamlConstants::NAME_ID_FORMAT_TRANSIENT)
  266. ->setValue('1234567890123456');
  267. $validator = new NameIdValidator();
  268. $validator->validateNameId($nameId);
  269. $this->assertTrue(true);
  270. }
  271. public function test_valid_transient_format_with_other_attributes()
  272. {
  273. $nameId = new NameID();
  274. $nameId->setFormat(SamlConstants::NAME_ID_FORMAT_TRANSIENT)
  275. ->setValue('1234567890123456')
  276. ->setSPProvidedID('sp:provided:id')
  277. ->setSPNameQualifier('sp:name:qualifier')
  278. ->setNameQualifier('name:qualifier')
  279. ;
  280. $validator = new NameIdValidator();
  281. $validator->validateNameId($nameId);
  282. $this->assertTrue(true);
  283. }
  284. public function test_invalid_transient_format_empty()
  285. {
  286. $this->expectExceptionMessage("NameID with Transient Format attribute MUST contain a Value that contains more than whitespace characters");
  287. $this->expectException(\LightSaml\Error\LightSamlValidationException::class);
  288. $nameId = new NameID();
  289. $nameId->setFormat(SamlConstants::NAME_ID_FORMAT_TRANSIENT);
  290. $validator = new NameIdValidator();
  291. $validator->validateNameId($nameId);
  292. $this->assertTrue(true);
  293. }
  294. public function test_invalid_transient_format_long()
  295. {
  296. $this->expectExceptionMessage("NameID with Transient Format attribute MUST have a Value that contains no more than 256 characters");
  297. $this->expectException(\LightSaml\Error\LightSamlValidationException::class);
  298. $nameId = new NameID();
  299. $nameId->setFormat(SamlConstants::NAME_ID_FORMAT_TRANSIENT)
  300. ->setValue(str_pad('a', 260, 'x'));
  301. $validator = new NameIdValidator();
  302. $validator->validateNameId($nameId);
  303. $this->assertTrue(true);
  304. }
  305. public function test_invalid_transient_format_short()
  306. {
  307. $this->expectExceptionMessage("NameID '123456789012345' with Transient Format attribute MUST have a Value with at least 16 characters (the equivalent of 128 bits)");
  308. $this->expectException(\LightSaml\Error\LightSamlValidationException::class);
  309. $nameId = new NameID();
  310. $nameId->setFormat(SamlConstants::NAME_ID_FORMAT_TRANSIENT)
  311. ->setValue('123456789012345');
  312. $validator = new NameIdValidator();
  313. $validator->validateNameId($nameId);
  314. $this->assertTrue(true);
  315. }
  316. }