libnm-core: add test cases for NMSettingBond
[NetworkManager.git] / libnm-core / tests / test-setting-bond.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  *
17  * Copyright 2016 Red Hat, Inc.
18  */
19
20 #include "nm-default.h"
21
22 #include "nm-utils.h"
23 #include "nm-setting-bond.h"
24 #include "nm-connection.h"
25 #include "nm-simple-connection.h"
26 #include "nm-setting-connection.h"
27 #include "nm-errors.h"
28
29 #include "nm-test-utils.h"
30
31 static void
32 create_bond_connection (NMConnection **con, NMSettingBond **s_bond)
33 {
34         NMSettingConnection *s_con;
35
36         g_assert (con);
37         g_assert (s_bond);
38
39         *con = nmtst_create_minimal_connection ("bond",
40                                                 NULL,
41                                                 NM_SETTING_BOND_SETTING_NAME,
42                                                 &s_con);
43         g_assert (*con);
44         g_assert (s_con);
45
46         g_object_set (s_con, NM_SETTING_CONNECTION_INTERFACE_NAME, "bond0", NULL);
47
48         *s_bond = (NMSettingBond *) nm_setting_bond_new ();
49         g_assert (*s_bond);
50
51         nm_connection_add_setting (*con, NM_SETTING (*s_bond));
52 }
53
54 #define test_verify_options(exp, ...) \
55         G_STMT_START { \
56                 const char *__opts[] = { __VA_ARGS__ , NULL }; \
57                 \
58                 _test_verify_options (__opts, exp); \
59         } G_STMT_END
60
61 static void
62 _test_verify_options (const char **options, gboolean expected_result)
63 {
64         gs_unref_object NMConnection *con = NULL;
65         NMSettingBond *s_bond;
66         GError *error = NULL;
67         gboolean success;
68         const char **option;
69
70         create_bond_connection (&con, &s_bond);
71
72         for (option = options; option[0] && option[1]; option += 2)
73                 g_assert (nm_setting_bond_add_option (s_bond, option[0], option[1]));
74
75         if (expected_result) {
76                 nmtst_assert_connection_verifies_and_normalizable (con);
77                 nmtst_connection_normalize (con);
78                 success = nm_setting_verify ((NMSetting *) s_bond, con, &error);
79                 nmtst_assert_success (success, error);
80         } else {
81                 nmtst_assert_connection_unnormalizable (con,
82                                                         NM_CONNECTION_ERROR,
83                                                         NM_CONNECTION_ERROR_INVALID_PROPERTY);
84         }
85 }
86
87 static void
88 test_verify (void)
89 {
90         test_verify_options (TRUE,
91                              "mode", "3",
92                              "arp_interval", "0");
93         test_verify_options (FALSE,
94                              /* arp_interval not supported in balance-alb mode */
95                              "mode", "balance-alb",
96                              "arp_interval", "1",
97                              "arp_ip_target", "1.2.3.4");
98         test_verify_options (FALSE,
99                              /* arp_ip_target requires arp_interval */
100                              "mode", "balance-rr",
101                              "arp_ip_target", "1.2.3.4");
102         test_verify_options (TRUE,
103                              "mode", "balance-rr",
104                              "arp_interval", "1",
105                              "arp_ip_target", "1.2.3.4");
106         test_verify_options (FALSE,
107                              /* num_grat_arp, num_unsol_na cannot be different */
108                              "mode", "balance-rr",
109                              "num_grat_arp", "3",
110                              "num_unsol_na", "4");
111         test_verify_options (TRUE,
112                              "mode", "balance-rr",
113                              "num_grat_arp", "5",
114                              "num_unsol_na", "5");
115         test_verify_options (TRUE,
116                              "mode", "active-backup",
117                              "primary", "eth0");
118         test_verify_options (FALSE,
119                              /* primary requires mode=active-backup */
120                              "mode", "802.3ad",
121                              "primary", "eth0");
122         test_verify_options (TRUE,
123                              "mode", "802.3ad",
124                              "lacp_rate", "fast");
125         test_verify_options (FALSE,
126                              /* lacp_rate=fast requires mode=802.3ad */
127                              "mode", "balance-rr",
128                              "lacp_rate", "fast");
129         test_verify_options (TRUE,
130                              "mode", "802.3ad",
131                              "ad_actor_system", "ae:00:11:33:44:55");
132 }
133
134 static void
135 test_compare_options (gboolean exp_res, const char **opts1, const char **opts2)
136 {
137         gs_unref_object NMSettingBond *s_bond1 = NULL, *s_bond2 = NULL;
138         const char **p;
139
140         s_bond1 = (NMSettingBond *) nm_setting_bond_new ();
141         g_assert (s_bond1);
142         s_bond2 = (NMSettingBond *) nm_setting_bond_new ();
143         g_assert (s_bond2);
144
145         for (p = opts1; p[0] && p[1]; p += 2)
146                 g_assert (nm_setting_bond_add_option (s_bond1, p[0], p[1]));
147
148         for (p = opts2; p[0] && p[1]; p += 2)
149                 g_assert (nm_setting_bond_add_option (s_bond2, p[0], p[1]));
150
151         g_assert_cmpint (nm_setting_compare ((NMSetting *) s_bond1,
152                                              (NMSetting *) s_bond2,
153                                              NM_SETTING_COMPARE_FLAG_EXACT),
154                          ==,
155                          exp_res);
156 }
157
158 static void
159 test_compare (void)
160 {
161         test_compare_options (TRUE,
162                               ((const char *[]){ "mode", "balance-rr", "miimon", "1", NULL }),
163                               ((const char *[]){ "mode", "balance-rr", "miimon", "1", NULL }));
164         test_compare_options (FALSE,
165                               ((const char *[]){ "mode", "balance-rr", "miimon", "1", NULL }),
166                               ((const char *[]){ "mode", "balance-rr", "miimon", "2", NULL }));
167
168         /* ignore default values */
169         test_compare_options (TRUE,
170                               ((const char *[]){ "miimon", "1", NULL }),
171                               ((const char *[]){ "miimon", "1", "updelay", "0", NULL }));
172
173         /* special handling of num_grat_arp, num_unsol_na */
174         test_compare_options (FALSE,
175                               ((const char *[]){ "num_grat_arp", "2", NULL }),
176                               ((const char *[]){ "num_grat_arp", "1", NULL }));
177         test_compare_options (TRUE,
178                               ((const char *[]){ "num_grat_arp", "3", NULL }),
179                               ((const char *[]){ "num_unsol_na", "3", NULL }));
180         test_compare_options (TRUE,
181                               ((const char *[]){ "num_grat_arp", "4", NULL }),
182                               ((const char *[]){ "num_unsol_na", "4", "num_grat_arp", "4", NULL }));
183 }
184
185 #define TPATH "/libnm/settings/bond/"
186
187 NMTST_DEFINE ();
188
189 int
190 main (int argc, char **argv)
191 {
192         nmtst_init (&argc, &argv, TRUE);
193
194         g_test_add_func (TPATH "verify", test_verify);
195         g_test_add_func (TPATH "compare", test_compare);
196
197         return g_test_run ();
198 }