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      private static DatagramPacket getDatagramPacket() throws UnknownHostException {
40          final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
41          byteStream.write(0);
42          byteStream.write(1);
43  
44          try {
45              byteStream.write("fileName".getBytes(StandardCharsets.US_ASCII));
46              byteStream.write(0);
47              byteStream.write("octet".getBytes(StandardCharsets.US_ASCII));
48              byteStream.write(0);
49  
50              byteStream.write("blksize".getBytes(StandardCharsets.US_ASCII));
51              byteStream.write(0);
52              byteStream.write("1024".getBytes(StandardCharsets.US_ASCII));
53              byteStream.write(0);
54          } catch (final IOException e) {
55              throw new RuntimeException("Error creating TFTP request packet", e);
56          }
57  
58          final byte[] data = byteStream.toByteArray();
59          return new DatagramPacket(data, data.length, InetAddress.getLocalHost(), 0);
60      }
61  
62      @Test
63      void testGetOptions() throws UnknownHostException, TFTPPacketException {
64          final DatagramPacket datagramPacket = getDatagramPacket();
65          final TFTPReadRequestPacket requestPacket = new TFTPReadRequestPacket(datagramPacket);
66          assertNotNull(requestPacket.toString());
67          final Map<String, String> options = requestPacket.getOptions();
68          assertEquals(1, options.size());
69          assertEquals("1024", options.get("blksize"));
70      }
71  
72      @Test
73      void testNewDatagram() throws TFTPPacketException, UnknownHostException {
74          final DatagramPacket datagramPacket = getDatagramPacket();
75  
76          final TFTPReadRequestPacket requestPacket = new TFTPReadRequestPacket(datagramPacket);
77          final DatagramPacket newDatagram = requestPacket.newDatagram();
78  
79          assertNotNull(newDatagram);
80          assertEquals(datagramPacket.getAddress(), newDatagram.getAddress());
81          assertEquals(datagramPacket.getPort(), newDatagram.getPort());
82          assertEquals(datagramPacket.getLength(), newDatagram.getLength());
83          assertArrayEquals(datagramPacket.getData(), newDatagram.getData());
84  
85          final byte[] data = new byte[datagramPacket.getLength()];
86          final DatagramPacket newDatagram2 = new DatagramPacket(data, data.length, InetAddress.getLocalHost(), 0);
87          requestPacket.newDatagram(newDatagram2, data);
88  
89          assertEquals(datagramPacket.getAddress(), newDatagram2.getAddress());
90          assertEquals(datagramPacket.getPort(), newDatagram2.getPort());
91          assertArrayEquals(datagramPacket.getData(), data);
92      }
93  }