View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.net.tftp;
19  
20  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  
24  import java.io.ByteArrayOutputStream;
25  import java.io.IOException;
26  import java.net.DatagramPacket;
27  import java.net.InetAddress;
28  import java.net.UnknownHostException;
29  import java.nio.charset.StandardCharsets;
30  import java.util.Map;
31  
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Tests {@link TFTPRequestPacket}.
36   */
37  class TFTPRequestPacketTest {
38  
39      @Test
40      public void testGetOptions() throws UnknownHostException, TFTPPacketException {
41          final DatagramPacket datagramPacket = getDatagramPacket();
42          final TFTPReadRequestPacket requestPacket = new TFTPReadRequestPacket(datagramPacket);
43          assertNotNull(requestPacket.toString());
44          final Map<String, String> options = requestPacket.getOptions();
45          assertEquals(1, options.size());
46          assertEquals("1024", options.get("blksize"));
47      }
48  
49      @Test
50      public void testNewDatagram() throws TFTPPacketException, UnknownHostException {
51          final DatagramPacket datagramPacket = getDatagramPacket();
52  
53          final TFTPReadRequestPacket requestPacket = new TFTPReadRequestPacket(datagramPacket);
54          final DatagramPacket newDatagram = requestPacket.newDatagram();
55  
56          assertNotNull(newDatagram);
57          assertEquals(datagramPacket.getAddress(), newDatagram.getAddress());
58          assertEquals(datagramPacket.getPort(), newDatagram.getPort());
59          assertEquals(datagramPacket.getLength(), newDatagram.getLength());
60          assertArrayEquals(datagramPacket.getData(), newDatagram.getData());
61  
62          final byte[] data = new byte[datagramPacket.getLength()];
63          final DatagramPacket newDatagram2 = new DatagramPacket(data, data.length, InetAddress.getLocalHost(), 0);
64          requestPacket.newDatagram(newDatagram2, data);
65  
66          assertEquals(datagramPacket.getAddress(), newDatagram2.getAddress());
67          assertEquals(datagramPacket.getPort(), newDatagram2.getPort());
68          assertArrayEquals(datagramPacket.getData(), data);
69      }
70  
71      private static DatagramPacket getDatagramPacket() throws UnknownHostException {
72          final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
73          byteStream.write(0);
74          byteStream.write(1);
75  
76          try {
77              byteStream.write("fileName".getBytes(StandardCharsets.US_ASCII));
78              byteStream.write(0);
79              byteStream.write("octet".getBytes(StandardCharsets.US_ASCII));
80              byteStream.write(0);
81  
82              byteStream.write("blksize".getBytes(StandardCharsets.US_ASCII));
83              byteStream.write(0);
84              byteStream.write("1024".getBytes(StandardCharsets.US_ASCII));
85              byteStream.write(0);
86          } catch (final IOException e) {
87              throw new RuntimeException("Error creating TFTP request packet", e);
88          }
89  
90          final byte[] data = byteStream.toByteArray();
91          return new DatagramPacket(data, data.length, InetAddress.getLocalHost(), 0);
92      }
93  }