View Javadoc
1   /*
2    * Copyright (C) 2020-2023 Dipl.-Inform. Kai Hofmann. All rights reserved!
3    */
4   package de.powerstat.validation.values.test;
5   
6   
7   import static org.junit.jupiter.api.Assertions.assertAll;
8   import static org.junit.jupiter.api.Assertions.assertEquals;
9   import static org.junit.jupiter.api.Assertions.assertFalse;
10  import static org.junit.jupiter.api.Assertions.assertNotEquals;
11  import static org.junit.jupiter.api.Assertions.assertThrows;
12  import static org.junit.jupiter.api.Assertions.assertTrue;
13  
14  import org.junit.jupiter.api.Test;
15  import org.junit.jupiter.params.ParameterizedTest;
16  import org.junit.jupiter.params.provider.ValueSource;
17  
18  import de.powerstat.validation.values.Hostname;
19  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
20  
21  
22  /**
23   * Hostname tests.
24   */
25  @SuppressFBWarnings({"EC_NULL_ARG", "RV_NEGATING_RESULT_OF_COMPARETO", "RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT", "SPP_USE_ZERO_WITH_COMPARATOR"})
26  final class HostnameTests
27   {
28    /**
29     * Private ip address 192.168.1.1.
30     */
31    private static final String PRIVATE_IP_192_168_1_1 = "192.168.1.1"; //$NON-NLS-1$
32  
33    /**
34     * Private ip address 192.168.1.2.
35     */
36    private static final String PRIVATE_IP_192_168_1_2 = "192.168.1.2"; //$NON-NLS-1$
37  
38    /**
39     * IPV6 FD.
40     */
41    private static final String FD00 = "fd00:0000:0000:0000:0000:0000:0000:0000"; //$NON-NLS-1$
42  
43    /**
44     * www.powerstat.de constant.
45     */
46    private static final String WWW_POWERSTAT_DE = "www.powerstat.de"; //$NON-NLS-1$
47  
48    /**
49     * nonexistant.example.com.
50     */
51    private static final String NONEXISTANT_EXAMPLE_COM = "nonexistant.example.com"; //$NON-NLS-1$
52  
53    /**
54     * Hostname not as expected constant.
55     */
56    private static final String HOSTNAME_NOT_AS_EXPECTED = "Hostname not as expected"; //$NON-NLS-1$
57  
58    /**
59     * Illegal argument exception expected constant.
60     */
61    private static final String ILLEGAL_ARGUMENT = "Illegal argument exception expected"; //$NON-NLS-1$
62  
63  
64    /**
65     * Default constructor.
66     */
67    /* default */ HostnameTests()
68     {
69      super();
70     }
71  
72  
73    /**
74     * Test Hostname with valid hostnames.
75     *
76     * @param hostname Hostname
77     */
78    @ParameterizedTest
79    @ValueSource(strings = {HostnameTests.WWW_POWERSTAT_DE, "a.de", "www.powerstat012345678901234567890123456789012345678901234567890123.de", "abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdef.com", "192.168.0.1", "00fe:0080:0000:0000:0000:0000:0000:0000"})
80    /* default */ void testHostnameOk0(final String hostname)
81     {
82      final Hostname cleanHostname = Hostname.of(hostname);
83      assertEquals(hostname, cleanHostname.stringValue(), HostnameTests.HOSTNAME_NOT_AS_EXPECTED);
84     }
85  
86  
87    /**
88     * Test Hostname with valid hostname.
89     */
90    @Test
91    /* default */ void testHostnameOk1()
92     {
93      final Hostname cleanHostname = Hostname.of("::"); //$NON-NLS-1$
94      assertEquals("0000:0000:0000:0000:0000:0000:0000:0000", cleanHostname.stringValue(), HostnameTests.HOSTNAME_NOT_AS_EXPECTED); //$NON-NLS-1$
95     }
96  
97  
98    /**
99     * Test Hostname with hostname to short or long, part to long.
100    *
101    * @param hostname Hostname
102    */
103   @ParameterizedTest
104   @ValueSource(strings = {"p", "www..de", "www.powerstat0123456789012345678901234567890123456789012345678901234.de", "www.powerstat1234123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234.de"})
105   /* default */ void testHostnameLength(final String hostname)
106    {
107     assertThrows(IllegalArgumentException.class, () ->
108      {
109       /* final Hostname cleanHostname = */ Hostname.of(hostname);
110      }, HostnameTests.ILLEGAL_ARGUMENT
111     );
112    }
113 
114 
115   /**
116    * Test Hostname with illegal parameters.
117    *
118    * @param hostname Hostname
119    */
120   @ParameterizedTest
121   @ValueSource(strings = {"www.power~stat.de", "www.powerstat.unknown", "ACCOUNTANT", "www.-powerstat.de", "www.powerstat-.de"})
122   /* default */ void testHostnameIllegalParameters(final String hostname)
123    {
124     assertThrows(IllegalArgumentException.class, () ->
125      {
126       /* final Hostname cleanHostname = */ Hostname.of(hostname);
127      }, HostnameTests.ILLEGAL_ARGUMENT
128     );
129    }
130 
131 
132   /**
133    * Test string value.
134    */
135   @Test
136   /* default */ void testStringValue()
137    {
138     final Hostname hostname = Hostname.of(HostnameTests.PRIVATE_IP_192_168_1_1);
139     assertEquals(HostnameTests.PRIVATE_IP_192_168_1_1, hostname.stringValue(), HostnameTests.HOSTNAME_NOT_AS_EXPECTED);
140    }
141 
142 
143   /**
144    * Test get reverse hostname.
145    */
146   @Test
147   /* default */ void testGetReverseHostname1()
148    {
149     final Hostname hostname = Hostname.of(HostnameTests.PRIVATE_IP_192_168_1_1);
150     assertEquals(HostnameTests.PRIVATE_IP_192_168_1_1, hostname.getReverseHostname(), HostnameTests.HOSTNAME_NOT_AS_EXPECTED);
151    }
152 
153 
154   /**
155    * Test get reverse hostname.
156    */
157   @Test
158   /* default */ void testGetReverseHostname2()
159    {
160     final Hostname hostname = Hostname.of(HostnameTests.FD00);
161     assertEquals(HostnameTests.FD00, hostname.getReverseHostname(), HostnameTests.HOSTNAME_NOT_AS_EXPECTED);
162    }
163 
164 
165   /**
166    * Test get reverse hostname.
167    */
168   @Test
169   /* default */ void testGetReverseHostname3()
170    {
171     final Hostname hostname = Hostname.of("www.example.com"); //$NON-NLS-1$
172     assertEquals("com.example.www", hostname.getReverseHostname(), HostnameTests.HOSTNAME_NOT_AS_EXPECTED); //$NON-NLS-1$
173    }
174 
175 
176   /**
177    * Exists hostname in DNS.
178    */
179   @Test
180   /* default */ void testExist()
181    {
182     assertTrue(Hostname.of(HostnameTests.WWW_POWERSTAT_DE).exist(), "Should be an existing hostname"); //$NON-NLS-1$
183    }
184 
185 
186   /**
187    * Exists hostname in DNS.
188    */
189   @Test
190   /* default */ void testExistFalse()
191    {
192     assertFalse(Hostname.of(HostnameTests.NONEXISTANT_EXAMPLE_COM).exist(), "Should not be an existing hostname"); //$NON-NLS-1$
193    }
194 
195 
196   /**
197    * Reachable hostname.
198    */
199   @Test
200   /* default */ void testIsReachable()
201    {
202     assertTrue(Hostname.of(HostnameTests.WWW_POWERSTAT_DE).isReachable(1000), "Should be a reachable hostname"); //$NON-NLS-1$
203    }
204 
205 
206   /**
207    * Non reachable hostname.
208    */
209   @Test
210   /* default */ void testIsReachableFalse()
211    {
212     assertFalse(Hostname.of(HostnameTests.NONEXISTANT_EXAMPLE_COM).isReachable(1000), "Should not be a reachable hostname"); //$NON-NLS-1$
213    }
214 
215 
216   /**
217    * Test hash code.
218    */
219   @Test
220   /* default */ void testHashCode()
221    {
222     final Hostname hostname1 = Hostname.of(HostnameTests.PRIVATE_IP_192_168_1_1);
223     final Hostname hostname2 = Hostname.of(HostnameTests.PRIVATE_IP_192_168_1_1);
224     final Hostname hostname3 = Hostname.of(HostnameTests.PRIVATE_IP_192_168_1_2);
225     assertAll("testHashCode", //$NON-NLS-1$
226       () -> assertEquals(hostname1.hashCode(), hostname2.hashCode(), "hashCodes are not equal"), //$NON-NLS-1$
227       () -> assertNotEquals(hostname1.hashCode(), hostname3.hashCode(), "hashCodes are equal") //$NON-NLS-1$
228     );
229    }
230 
231 
232   /**
233    * Test equals.
234    */
235   @Test
236   @SuppressWarnings("java:S5785")
237   /* default */ void testEquals()
238    {
239     final Hostname hostname1 = Hostname.of(HostnameTests.PRIVATE_IP_192_168_1_1);
240     final Hostname hostname2 = Hostname.of(HostnameTests.PRIVATE_IP_192_168_1_1);
241     final Hostname hostname3 = Hostname.of(HostnameTests.PRIVATE_IP_192_168_1_2);
242     final Hostname hostname4 = Hostname.of(HostnameTests.PRIVATE_IP_192_168_1_1);
243     assertAll("testEquals", //$NON-NLS-1$
244       () -> assertTrue(hostname1.equals(hostname1), "hostname11 is not equal"), //$NON-NLS-1$
245       () -> assertTrue(hostname1.equals(hostname2), "hostname12 are not equal"), //$NON-NLS-1$
246       () -> assertTrue(hostname2.equals(hostname1), "hostname21 are not equal"), //$NON-NLS-1$
247       () -> assertTrue(hostname2.equals(hostname4), "hostname24 are not equal"), //$NON-NLS-1$
248       () -> assertTrue(hostname1.equals(hostname4), "hostname14 are not equal"), //$NON-NLS-1$
249       () -> assertFalse(hostname1.equals(hostname3), "hostname13 are equal"), //$NON-NLS-1$
250       () -> assertFalse(hostname3.equals(hostname1), "hostname31 are equal"), //$NON-NLS-1$
251       () -> assertFalse(hostname1.equals(null), "hostname10 is equal") //$NON-NLS-1$
252     );
253    }
254 
255 
256   /**
257    * Test toString.
258    */
259   @Test
260   /* default */ void testToString()
261    {
262     final Hostname hostname = Hostname.of(HostnameTests.PRIVATE_IP_192_168_1_1);
263     assertEquals("Hostname[hostname=192.168.1.1]", hostname.toString(), "toString not equal"); //$NON-NLS-1$ //$NON-NLS-2$
264    }
265 
266 
267   /**
268    * Test compareTo.
269    */
270   @Test
271   @SuppressWarnings("java:S5785")
272   /* default */ void testCompareTo()
273    {
274     final Hostname hostname1 = Hostname.of(HostnameTests.PRIVATE_IP_192_168_1_1);
275     final Hostname hostname2 = Hostname.of(HostnameTests.PRIVATE_IP_192_168_1_1);
276     final Hostname hostname3 = Hostname.of(HostnameTests.PRIVATE_IP_192_168_1_2);
277     final Hostname hostname4 = Hostname.of("192.168.1.3"); //$NON-NLS-1$
278     final Hostname hostname5 = Hostname.of(HostnameTests.PRIVATE_IP_192_168_1_1);
279     assertAll("testCompareTo", //$NON-NLS-1$
280       () -> assertTrue(hostname1.compareTo(hostname2) == -hostname2.compareTo(hostname1), "reflexive1"), //$NON-NLS-1$
281       () -> assertTrue(hostname1.compareTo(hostname3) == -hostname3.compareTo(hostname1), "reflexive2"), //$NON-NLS-1$
282       () -> assertTrue((hostname4.compareTo(hostname3) > 0) && (hostname3.compareTo(hostname1) > 0) && (hostname4.compareTo(hostname1) > 0), "transitive1"), //$NON-NLS-1$
283       () -> assertTrue((hostname1.compareTo(hostname2) == 0) && (Math.abs(hostname1.compareTo(hostname5)) == Math.abs(hostname2.compareTo(hostname5))), "sgn1"), //$NON-NLS-1$
284       () -> assertTrue((hostname1.compareTo(hostname2) == 0) && hostname1.equals(hostname2), "equals") //$NON-NLS-1$
285     );
286    }
287 
288  }