aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tunnel/src/test/java/com/wireguard/config/BadConfigExceptionTest.java
blob: 54935510d4725e498802c7387f38d5c344a626f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright © 2020 WireGuard LLC. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

package com.wireguard.config;

import com.wireguard.config.BadConfigException.Location;
import com.wireguard.config.BadConfigException.Reason;
import com.wireguard.config.BadConfigException.Section;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.*;

public class BadConfigExceptionTest {
    private static final String[] CONFIG_NAMES = {
            "invalid-key",
            "invalid-number",
            "invalid-value",
            "missing-attribute",
            "missing-section",
            "syntax-error",
            "unknown-attribute",
            "unknown-section"
    };
    private static final Map<String, InputStream> CONFIG_MAP = new HashMap<>();

    @BeforeClass
    public static void readConfigs() {
        for (final String config: CONFIG_NAMES) {
            CONFIG_MAP.put(config, BadConfigExceptionTest.class.getClassLoader().getResourceAsStream(config + ".conf"));
        }
    }

    @AfterClass
    public static void closeStreams() {
        for (final InputStream inputStream : CONFIG_MAP.values()) {
            try {
                inputStream.close();
            } catch (final IOException ignored) {
            }
        }
    }

    @Test
    public void throws_correctly_with_INVALID_KEY_reason() {
        try {
            Config.parse(CONFIG_MAP.get("invalid-key"));
            fail("Config parsing must fail in this test");
        } catch (final BadConfigException e) {
            assertEquals(e.getReason(), Reason.INVALID_KEY);
            assertEquals(e.getLocation(), Location.PUBLIC_KEY);
            assertEquals(e.getSection(), Section.PEER);
        } catch (final IOException e) {
            e.printStackTrace();
            fail("IOException thrown during test");
        }
    }

    @Test
    public void throws_correctly_with_INVALID_NUMBER_reason() {
        try {
            Config.parse(CONFIG_MAP.get("invalid-number"));
            fail("Config parsing must fail in this test");
        } catch (final BadConfigException e) {
            assertEquals(e.getReason(), Reason.INVALID_NUMBER);
            assertEquals(e.getLocation(), Location.PERSISTENT_KEEPALIVE);
            assertEquals(e.getSection(), Section.PEER);
        } catch (final IOException e) {
            e.printStackTrace();
            fail("IOException thrown during test");
        }
    }

    @Test
    public void throws_correctly_with_INVALID_VALUE_reason() {
        try {
            Config.parse(CONFIG_MAP.get("invalid-value"));
            fail("Config parsing must fail in this test");
        } catch (final BadConfigException e) {
            assertEquals(e.getReason(), Reason.INVALID_VALUE);
            assertEquals(e.getLocation(), Location.DNS);
            assertEquals(e.getSection(), Section.INTERFACE);
        } catch (final IOException e) {
            e.printStackTrace();
            fail("IOException throwing during test");
        }
    }

    @Test
    public void throws_correctly_with_MISSING_ATTRIBUTE_reason() {
        try {
            Config.parse(CONFIG_MAP.get("missing-attribute"));
            fail("Config parsing must fail in this test");
        } catch (final BadConfigException e) {
            assertEquals(e.getReason(), Reason.MISSING_ATTRIBUTE);
            assertEquals(e.getLocation(), Location.PUBLIC_KEY);
            assertEquals(e.getSection(), Section.PEER);
        } catch (final IOException e) {
            e.printStackTrace();
            fail("IOException throwing during test");
        }
    }

    @Test
    public void throws_correctly_with_MISSING_SECTION_reason() {
        try {
            Config.parse(CONFIG_MAP.get("missing-section"));
            fail("Config parsing must fail in this test");
        } catch (final BadConfigException e) {
            assertEquals(e.getReason(), Reason.MISSING_SECTION);
            assertEquals(e.getLocation(), Location.TOP_LEVEL);
            assertEquals(e.getSection(), Section.CONFIG);
        } catch (final IOException e) {
            e.printStackTrace();
            fail("IOException throwing during test");
        }
    }

    @Test
    public void throws_correctly_with_SYNTAX_ERROR_reason() {
        try {
            Config.parse(CONFIG_MAP.get("syntax-error"));
            fail("Config parsing must fail in this test");
        } catch (final BadConfigException e) {
            assertEquals(e.getReason(), Reason.SYNTAX_ERROR);
            assertEquals(e.getLocation(), Location.TOP_LEVEL);
            assertEquals(e.getSection(), Section.PEER);
        } catch (final IOException e) {
            e.printStackTrace();
            fail("IOException throwing during test");
        }
    }

    @Test
    public void throws_correctly_with_UNKNOWN_ATTRIBUTE_reason() {
        try {
            Config.parse(CONFIG_MAP.get("unknown-attribute"));
            fail("Config parsing must fail in this test");
        } catch (final BadConfigException e) {
            assertEquals(e.getReason(), Reason.UNKNOWN_ATTRIBUTE);
            assertEquals(e.getLocation(), Location.TOP_LEVEL);
            assertEquals(e.getSection(), Section.PEER);
        } catch (final IOException e) {
            e.printStackTrace();
            fail("IOException throwing during test");
        }
    }

    @Test
    public void throws_correctly_with_UNKNOWN_SECTION_reason() {
        try {
            Config.parse(CONFIG_MAP.get("unknown-section"));
            fail("Config parsing must fail in this test");
        } catch (final BadConfigException e) {
            assertEquals(e.getReason(), Reason.UNKNOWN_SECTION);
            assertEquals(e.getLocation(), Location.TOP_LEVEL);
            assertEquals(e.getSection(), Section.CONFIG);
        } catch (final IOException e) {
            e.printStackTrace();
            fail("IOException throwing during test");
        }
    }
}