/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/EncKrbPrivPartDecoderTest.java

https://github.com/apache/directory-server · Java · 692 lines · 572 code · 90 blank · 30 comment · 0 complexity · bdedd654bd037c7b1fef4de786a9b874 MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. */
  20. package org.apache.directory.shared.kerberos.codec;
  21. import static org.junit.Assert.assertEquals;
  22. import static org.junit.Assert.assertNull;
  23. import static org.junit.Assert.assertTrue;
  24. import static org.junit.Assert.fail;
  25. import java.net.InetAddress;
  26. import java.nio.ByteBuffer;
  27. import java.util.Arrays;
  28. import org.apache.directory.api.asn1.DecoderException;
  29. import org.apache.directory.api.asn1.EncoderException;
  30. import org.apache.directory.api.asn1.ber.Asn1Decoder;
  31. import org.apache.directory.api.util.Strings;
  32. import org.apache.directory.shared.kerberos.codec.encKrbPrivPart.EncKrbPrivPartContainer;
  33. import org.apache.directory.shared.kerberos.components.EncKrbPrivPart;
  34. import org.apache.directory.shared.kerberos.components.HostAddress;
  35. import org.junit.jupiter.api.Test;
  36. /**
  37. * Test cases for EncKrbPrivPart codec.
  38. *
  39. * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  40. */
  41. public class EncKrbPrivPartDecoderTest
  42. {
  43. @Test
  44. public void testDecodeEncKrbPrivPart() throws Exception
  45. {
  46. int streamLen = 0x49;
  47. ByteBuffer stream = ByteBuffer.allocate( streamLen );
  48. stream.put( new byte[]
  49. {
  50. 0x7C, 0x47,
  51. 0x30, 0x45,
  52. ( byte ) 0xA0, 0x4, // user-data
  53. 0x04,
  54. 0x02,
  55. 0x00,
  56. 0x01,
  57. ( byte ) 0xA1,
  58. 0x11, // timestamp
  59. 0x18,
  60. 0xF,
  61. '2',
  62. '0',
  63. '1',
  64. '0',
  65. '1',
  66. '1',
  67. '1',
  68. '9',
  69. '0',
  70. '8',
  71. '0',
  72. '0',
  73. '4',
  74. '3',
  75. 'Z',
  76. ( byte ) 0xA2,
  77. 0x03, // usec
  78. 0x02,
  79. 0x01,
  80. 0x01,
  81. ( byte ) 0xA3,
  82. 0x03, // seq-number
  83. 0x02,
  84. 0x01,
  85. 0x01,
  86. ( byte ) 0xA4,
  87. 0xF, // s-address
  88. 0x30,
  89. 0x0D,
  90. ( byte ) 0xA0,
  91. 0x03,
  92. 0x02,
  93. 0x01,
  94. 0x02,
  95. ( byte ) 0xA1,
  96. 0x06,
  97. 0x04,
  98. 0x04,
  99. 127,
  100. 0,
  101. 0,
  102. 1,
  103. ( byte ) 0xA5,
  104. 0xF, // r-adress
  105. 0x30,
  106. 0x0D,
  107. ( byte ) 0xA0,
  108. 0x03,
  109. 0x02,
  110. 0x01,
  111. 0x02,
  112. ( byte ) 0xA1,
  113. 0x06,
  114. 0x04,
  115. 0x04,
  116. 127,
  117. 0,
  118. 0,
  119. 1
  120. } );
  121. String decoded = Strings.dumpBytes( stream.array() );
  122. stream.flip();
  123. EncKrbPrivPartContainer container = new EncKrbPrivPartContainer( stream );
  124. try
  125. {
  126. Asn1Decoder.decode( stream, container );
  127. }
  128. catch ( DecoderException e )
  129. {
  130. fail();
  131. }
  132. EncKrbPrivPart encKrbPrivPart = container.getEncKrbPrivPart();
  133. String time = "20101119080043Z";
  134. HostAddress ad = new HostAddress( InetAddress.getByName( "127.0.0.1" ) );
  135. assertTrue( Arrays.equals( new byte[]
  136. { 0, 1 }, encKrbPrivPart.getUserData() ) );
  137. assertEquals( time, encKrbPrivPart.getTimestamp().getDate() );
  138. assertEquals( 1, encKrbPrivPart.getUsec() );
  139. assertEquals( 1, encKrbPrivPart.getSeqNumber() );
  140. assertEquals( ad, encKrbPrivPart.getSenderAddress() );
  141. assertEquals( ad, encKrbPrivPart.getRecipientAddress() );
  142. int computedLen = encKrbPrivPart.computeLength();
  143. assertEquals( streamLen, computedLen );
  144. try
  145. {
  146. ByteBuffer bb = ByteBuffer.allocate( computedLen );
  147. encKrbPrivPart.encode( bb );
  148. String encoded = Strings.dumpBytes( bb.array() );
  149. assertEquals( decoded, encoded );
  150. }
  151. catch ( EncoderException e )
  152. {
  153. fail();
  154. }
  155. }
  156. @Test
  157. public void testDecodeEncKrbPrivPartWithoutTimestamp() throws Exception
  158. {
  159. int streamLen = 0x36;
  160. ByteBuffer stream = ByteBuffer.allocate( streamLen );
  161. stream.put( new byte[]
  162. {
  163. 0x7C, 0x34,
  164. 0x30, 0x32,
  165. ( byte ) 0xA0, 0x4, // user-data
  166. 0x04,
  167. 0x02,
  168. 0x00,
  169. 0x01,
  170. // NO timestamp
  171. ( byte ) 0xA2,
  172. 0x03, // usec
  173. 0x02,
  174. 0x01,
  175. 0x01,
  176. ( byte ) 0xA3,
  177. 0x03, // seq-number
  178. 0x02,
  179. 0x01,
  180. 0x01,
  181. ( byte ) 0xA4,
  182. 0xF, // s-address
  183. 0x30,
  184. 0x0D,
  185. ( byte ) 0xA0,
  186. 0x03,
  187. 0x02,
  188. 0x01,
  189. 0x02,
  190. ( byte ) 0xA1,
  191. 0x06,
  192. 0x04,
  193. 0x04,
  194. 127,
  195. 0,
  196. 0,
  197. 1,
  198. ( byte ) 0xA5,
  199. 0xF, // r-adress
  200. 0x30,
  201. 0x0D,
  202. ( byte ) 0xA0,
  203. 0x03,
  204. 0x02,
  205. 0x01,
  206. 0x02,
  207. ( byte ) 0xA1,
  208. 0x06,
  209. 0x04,
  210. 0x04,
  211. 127,
  212. 0,
  213. 0,
  214. 1
  215. } );
  216. String decoded = Strings.dumpBytes( stream.array() );
  217. stream.flip();
  218. EncKrbPrivPartContainer container = new EncKrbPrivPartContainer( stream );
  219. try
  220. {
  221. Asn1Decoder.decode( stream, container );
  222. }
  223. catch ( DecoderException e )
  224. {
  225. fail();
  226. }
  227. EncKrbPrivPart enKrbPrivPart = container.getEncKrbPrivPart();
  228. HostAddress ad = new HostAddress( InetAddress.getByName( "127.0.0.1" ) );
  229. assertTrue( Arrays.equals( new byte[]
  230. { 0, 1 }, enKrbPrivPart.getUserData() ) );
  231. assertNull( enKrbPrivPart.getTimestamp() );
  232. assertEquals( 1, enKrbPrivPart.getUsec() );
  233. assertEquals( 1, enKrbPrivPart.getSeqNumber() );
  234. assertEquals( ad, enKrbPrivPart.getSenderAddress() );
  235. assertEquals( ad, enKrbPrivPart.getRecipientAddress() );
  236. int computedLen = enKrbPrivPart.computeLength();
  237. assertEquals( streamLen, computedLen );
  238. try
  239. {
  240. ByteBuffer bb = ByteBuffer.allocate( computedLen );
  241. enKrbPrivPart.encode( bb );
  242. String encoded = Strings.dumpBytes( bb.array() );
  243. assertEquals( decoded, encoded );
  244. }
  245. catch ( EncoderException e )
  246. {
  247. fail();
  248. }
  249. }
  250. @Test
  251. public void testDecodeEncKrbPrivPartWithoutTimestampAndUsec() throws Exception
  252. {
  253. int streamLen = 0x31;
  254. ByteBuffer stream = ByteBuffer.allocate( streamLen );
  255. stream.put( new byte[]
  256. {
  257. 0x7C, 0x2F,
  258. 0x30, 0x2D,
  259. ( byte ) 0xA0, 0x4, // user-data
  260. 0x04,
  261. 0x02,
  262. 0x00,
  263. 0x01,
  264. // NO timestamp and usec
  265. ( byte ) 0xA3,
  266. 0x03, // seq-number
  267. 0x02,
  268. 0x01,
  269. 0x01,
  270. ( byte ) 0xA4,
  271. 0xF, // s-address
  272. 0x30,
  273. 0x0D,
  274. ( byte ) 0xA0,
  275. 0x03,
  276. 0x02,
  277. 0x01,
  278. 0x02,
  279. ( byte ) 0xA1,
  280. 0x06,
  281. 0x04,
  282. 0x04,
  283. 127,
  284. 0,
  285. 0,
  286. 1,
  287. ( byte ) 0xA5,
  288. 0xF, // r-adress
  289. 0x30,
  290. 0x0D,
  291. ( byte ) 0xA0,
  292. 0x03,
  293. 0x02,
  294. 0x01,
  295. 0x02,
  296. ( byte ) 0xA1,
  297. 0x06,
  298. 0x04,
  299. 0x04,
  300. 127,
  301. 0,
  302. 0,
  303. 1
  304. } );
  305. String decoded = Strings.dumpBytes( stream.array() );
  306. stream.flip();
  307. EncKrbPrivPartContainer container = new EncKrbPrivPartContainer( stream );
  308. try
  309. {
  310. Asn1Decoder.decode( stream, container );
  311. }
  312. catch ( DecoderException e )
  313. {
  314. fail();
  315. }
  316. EncKrbPrivPart encKrbPrivPart = container.getEncKrbPrivPart();
  317. HostAddress ad = new HostAddress( InetAddress.getByName( "127.0.0.1" ) );
  318. assertTrue( Arrays.equals( new byte[]
  319. { 0, 1 }, encKrbPrivPart.getUserData() ) );
  320. assertNull( encKrbPrivPart.getTimestamp() );
  321. assertEquals( 0, encKrbPrivPart.getUsec() );
  322. assertEquals( 1, encKrbPrivPart.getSeqNumber() );
  323. assertEquals( ad, encKrbPrivPart.getSenderAddress() );
  324. assertEquals( ad, encKrbPrivPart.getRecipientAddress() );
  325. int computedLen = encKrbPrivPart.computeLength();
  326. assertEquals( streamLen, computedLen );
  327. try
  328. {
  329. ByteBuffer bb = ByteBuffer.allocate( computedLen );
  330. encKrbPrivPart.encode( bb );
  331. String encoded = Strings.dumpBytes( bb.array() );
  332. assertEquals( decoded, encoded );
  333. }
  334. catch ( EncoderException e )
  335. {
  336. fail();
  337. }
  338. }
  339. @Test
  340. public void testDecodeEncKrbPrivPartWithoutTimestampUsecAndSeqNumber() throws Exception
  341. {
  342. int streamLen = 0x2C;
  343. ByteBuffer stream = ByteBuffer.allocate( streamLen );
  344. stream.put( new byte[]
  345. {
  346. 0x7C, 0x2A,
  347. 0x30, 0x28,
  348. ( byte ) 0xA0, 0x4, // user-data
  349. 0x04,
  350. 0x02,
  351. 0x00,
  352. 0x01,
  353. // NO timestamp, usec and seq-number
  354. ( byte ) 0xA4,
  355. 0xF, // s-address
  356. 0x30,
  357. 0x0D,
  358. ( byte ) 0xA0,
  359. 0x03,
  360. 0x02,
  361. 0x01,
  362. 0x02,
  363. ( byte ) 0xA1,
  364. 0x06,
  365. 0x04,
  366. 0x04,
  367. 127,
  368. 0,
  369. 0,
  370. 1,
  371. ( byte ) 0xA5,
  372. 0xF, // r-adress
  373. 0x30,
  374. 0x0D,
  375. ( byte ) 0xA0,
  376. 0x03,
  377. 0x02,
  378. 0x01,
  379. 0x02,
  380. ( byte ) 0xA1,
  381. 0x06,
  382. 0x04,
  383. 0x04,
  384. 127,
  385. 0,
  386. 0,
  387. 1
  388. } );
  389. String decoded = Strings.dumpBytes( stream.array() );
  390. stream.flip();
  391. EncKrbPrivPartContainer container = new EncKrbPrivPartContainer( stream );
  392. try
  393. {
  394. Asn1Decoder.decode( stream, container );
  395. }
  396. catch ( DecoderException e )
  397. {
  398. e.printStackTrace();
  399. fail();
  400. }
  401. EncKrbPrivPart encKrbPrivPart = container.getEncKrbPrivPart();
  402. HostAddress ad = new HostAddress( InetAddress.getByName( "127.0.0.1" ) );
  403. assertTrue( Arrays.equals( new byte[]
  404. { 0, 1 }, encKrbPrivPart.getUserData() ) );
  405. assertNull( encKrbPrivPart.getTimestamp() );
  406. assertEquals( 0, encKrbPrivPart.getUsec() );
  407. assertEquals( 0, encKrbPrivPart.getSeqNumber() );
  408. assertEquals( ad, encKrbPrivPart.getSenderAddress() );
  409. assertEquals( ad, encKrbPrivPart.getRecipientAddress() );
  410. int computedLen = encKrbPrivPart.computeLength();
  411. assertEquals( streamLen, computedLen );
  412. try
  413. {
  414. ByteBuffer bb = ByteBuffer.allocate( computedLen );
  415. encKrbPrivPart.encode( bb );
  416. String encoded = Strings.dumpBytes( bb.array() );
  417. assertEquals( decoded, encoded );
  418. }
  419. catch ( EncoderException e )
  420. {
  421. fail();
  422. }
  423. }
  424. @Test
  425. public void testDecodeEncKrbPrivPartWithoutSequenceNumber() throws Exception
  426. {
  427. int streamLen = 0x44;
  428. ByteBuffer stream = ByteBuffer.allocate( streamLen );
  429. stream.put( new byte[]
  430. {
  431. 0x7C, 0x42,
  432. 0x30, 0x40,
  433. ( byte ) 0xA0, 0x4, // user-data
  434. 0x04,
  435. 0x02,
  436. 0x00,
  437. 0x01,
  438. ( byte ) 0xA1,
  439. 0x11, // timestamp
  440. 0x18,
  441. 0xF,
  442. '2',
  443. '0',
  444. '1',
  445. '0',
  446. '1',
  447. '1',
  448. '1',
  449. '9',
  450. '0',
  451. '8',
  452. '0',
  453. '0',
  454. '4',
  455. '3',
  456. 'Z',
  457. ( byte ) 0xA2,
  458. 0x03, // usec
  459. 0x02,
  460. 0x01,
  461. 0x01,
  462. // NO seq-number
  463. ( byte ) 0xA4,
  464. 0xF, // s-address
  465. 0x30,
  466. 0x0D,
  467. ( byte ) 0xA0,
  468. 0x03,
  469. 0x02,
  470. 0x01,
  471. 0x02,
  472. ( byte ) 0xA1,
  473. 0x06,
  474. 0x04,
  475. 0x04,
  476. 127,
  477. 0,
  478. 0,
  479. 1,
  480. ( byte ) 0xA5,
  481. 0xF, // r-adress
  482. 0x30,
  483. 0x0D,
  484. ( byte ) 0xA0,
  485. 0x03,
  486. 0x02,
  487. 0x01,
  488. 0x02,
  489. ( byte ) 0xA1,
  490. 0x06,
  491. 0x04,
  492. 0x04,
  493. 127,
  494. 0,
  495. 0,
  496. 1
  497. } );
  498. String decoded = Strings.dumpBytes( stream.array() );
  499. stream.flip();
  500. EncKrbPrivPartContainer container = new EncKrbPrivPartContainer( stream );
  501. try
  502. {
  503. Asn1Decoder.decode( stream, container );
  504. }
  505. catch ( DecoderException e )
  506. {
  507. fail();
  508. }
  509. EncKrbPrivPart encKrbPrivPart = container.getEncKrbPrivPart();
  510. String time = "20101119080043Z";
  511. HostAddress ad = new HostAddress( InetAddress.getByName( "127.0.0.1" ) );
  512. assertTrue( Arrays.equals( new byte[]
  513. { 0, 1 }, encKrbPrivPart.getUserData() ) );
  514. assertEquals( time, encKrbPrivPart.getTimestamp().getDate() );
  515. assertEquals( 1, encKrbPrivPart.getUsec() );
  516. assertEquals( 0, encKrbPrivPart.getSeqNumber() );
  517. assertEquals( ad, encKrbPrivPart.getSenderAddress() );
  518. assertEquals( ad, encKrbPrivPart.getRecipientAddress() );
  519. int computedLen = encKrbPrivPart.computeLength();
  520. assertEquals( streamLen, computedLen );
  521. try
  522. {
  523. ByteBuffer bb = ByteBuffer.allocate( computedLen );
  524. encKrbPrivPart.encode( bb );
  525. String encoded = Strings.dumpBytes( bb.array() );
  526. assertEquals( decoded, encoded );
  527. }
  528. catch ( EncoderException e )
  529. {
  530. fail();
  531. }
  532. }
  533. @Test
  534. public void testDecodeEncKrbPrivPartWithoutOptionalValues() throws Exception
  535. {
  536. int streamLen = 0x1B;
  537. ByteBuffer stream = ByteBuffer.allocate( streamLen );
  538. stream.put( new byte[]
  539. {
  540. 0x7C, 0x19,
  541. 0x30, 0x17,
  542. ( byte ) 0xA0, 0x4, // user-data
  543. 0x04,
  544. 0x02,
  545. 0x00,
  546. 0x01,
  547. // NO timestamp, usec and seq-number
  548. ( byte ) 0xA4,
  549. 0xF, // s-address
  550. 0x30,
  551. 0x0D,
  552. ( byte ) 0xA0,
  553. 0x03,
  554. 0x02,
  555. 0x01,
  556. 0x02,
  557. ( byte ) 0xA1,
  558. 0x06,
  559. 0x04,
  560. 0x04,
  561. 127,
  562. 0,
  563. 0,
  564. 1,
  565. // NO r-address
  566. } );
  567. String decoded = Strings.dumpBytes( stream.array() );
  568. stream.flip();
  569. EncKrbPrivPartContainer container = new EncKrbPrivPartContainer( stream );
  570. try
  571. {
  572. Asn1Decoder.decode( stream, container );
  573. }
  574. catch ( DecoderException e )
  575. {
  576. e.printStackTrace();
  577. fail();
  578. }
  579. EncKrbPrivPart encKrbPrivPart = container.getEncKrbPrivPart();
  580. HostAddress ad = new HostAddress( InetAddress.getByName( "127.0.0.1" ) );
  581. assertTrue( Arrays.equals( new byte[]
  582. { 0, 1 }, encKrbPrivPart.getUserData() ) );
  583. assertNull( encKrbPrivPart.getTimestamp() );
  584. assertEquals( 0, encKrbPrivPart.getUsec() );
  585. assertEquals( 0, encKrbPrivPart.getSeqNumber() );
  586. assertEquals( ad, encKrbPrivPart.getSenderAddress() );
  587. assertNull( encKrbPrivPart.getRecipientAddress() );
  588. int computedLen = encKrbPrivPart.computeLength();
  589. assertEquals( streamLen, computedLen );
  590. try
  591. {
  592. ByteBuffer bb = ByteBuffer.allocate( computedLen );
  593. encKrbPrivPart.encode( bb );
  594. String encoded = Strings.dumpBytes( bb.array() );
  595. assertEquals( decoded, encoded );
  596. }
  597. catch ( EncoderException e )
  598. {
  599. fail();
  600. }
  601. }
  602. }