569bd8c1fd9e275a09618e8b2b39d07c9b46d8ec
[vpp.git] / test / ext / vom_test.cpp
1 /*
2  * Test suite for class VppOM
3  *
4  * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
5  *
6  * This program and the accompanying materials are made available under the
7  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
8  * and is available at http://www.eclipse.org/legal/epl-v10.html
9  */
10 #define BOOST_TEST_MODULE "VPP OBJECT MODEL"
11 #define BOOST_TEST_DYN_LINK
12
13 #include <boost/test/unit_test.hpp>
14 #include <boost/assign/list_inserter.hpp>
15
16
17 #include <iostream>
18 #include <deque>
19
20 #include "vom/om.hpp"
21 #include "vom/interface.hpp"
22 #include "vom/interface_cmds.hpp"
23 #include "vom/bond_interface_cmds.hpp"
24 #include "vom/bond_group_binding.hpp"
25 #include "vom/bond_group_binding_cmds.hpp"
26 #include "vom/l2_binding.hpp"
27 #include "vom/l2_binding_cmds.hpp"
28 #include "vom/l2_vtr_cmds.hpp"
29 #include "vom/l2_xconnect.hpp"
30 #include "vom/l2_xconnect_cmds.hpp"
31 #include "vom/l3_binding.hpp"
32 #include "vom/l3_binding_cmds.hpp"
33 #include "vom/bridge_domain.hpp"
34 #include "vom/bridge_domain_entry.hpp"
35 #include "vom/bridge_domain_arp_entry.hpp"
36 #include "vom/bridge_domain_cmds.hpp"
37 #include "vom/bridge_domain_entry_cmds.hpp"
38 #include "vom/bridge_domain_arp_entry_cmds.hpp"
39 #include "vom/prefix.hpp"
40 #include "vom/route.hpp"
41 #include "vom/route_cmds.hpp"
42 #include "vom/mroute_cmds.hpp"
43 #include "vom/route_domain.hpp"
44 #include "vom/route_domain_cmds.hpp"
45 #include "vom/vxlan_tunnel.hpp"
46 #include "vom/vxlan_tunnel_cmds.hpp"
47 #include "vom/sub_interface.hpp"
48 #include "vom/sub_interface_cmds.hpp"
49 #include "vom/acl_ethertype.hpp"
50 #include "vom/acl_ethertype_cmds.hpp"
51 #include "vom/acl_l2_list.hpp"
52 #include "vom/acl_l3_list.hpp"
53 #include "vom/acl_binding.hpp"
54 #include "vom/acl_list_cmds.hpp"
55 #include "vom/acl_binding_cmds.hpp"
56 #include "vom/acl_l3_rule.hpp"
57 #include "vom/acl_l2_rule.hpp"
58 #include "vom/arp_proxy_config.hpp"
59 #include "vom/arp_proxy_binding.hpp"
60 #include "vom/arp_proxy_config_cmds.hpp"
61 #include "vom/arp_proxy_binding_cmds.hpp"
62 #include "vom/igmp_binding.hpp"
63 #include "vom/igmp_binding_cmds.hpp"
64 #include "vom/igmp_listen.hpp"
65 #include "vom/igmp_listen_cmds.hpp"
66 #include "vom/ip_punt_redirect.hpp"
67 #include "vom/ip_punt_redirect_cmds.hpp"
68 #include "vom/ip_unnumbered.hpp"
69 #include "vom/ip_unnumbered_cmds.hpp"
70 #include "vom/interface_ip6_nd.hpp"
71 #include "vom/interface_span.hpp"
72 #include "vom/interface_span_cmds.hpp"
73 #include "vom/neighbour.hpp"
74 #include "vom/neighbour_cmds.hpp"
75 #include "vom/nat_static.hpp"
76 #include "vom/nat_static_cmds.hpp"
77 #include "vom/nat_binding.hpp"
78 #include "vom/nat_binding_cmds.hpp"
79 #include "vom/pipe.hpp"
80 #include "vom/pipe_cmds.hpp"
81
82 using namespace boost;
83 using namespace VOM;
84
85 /**
86  * An expectation exception
87  */
88 class ExpException
89 {
90 public:
91     ExpException(unsigned int number)
92     {
93         // a neat place to add a break point
94         std::cout << "  ExpException here: " << number << std::endl;
95     }
96 };
97
98 class MockListener : public interface::event_listener,
99                      public interface::stat_listener
100 {
101     void handle_interface_stat(const interface& itf)
102     {
103     }
104     void handle_interface_event(std::vector<VOM::interface::event> events)
105     {
106     }
107 };
108
109 class MockCmdQ : public HW::cmd_q
110 {
111 public:
112     MockCmdQ():
113         m_strict_order(true)
114     {
115     }
116     virtual ~MockCmdQ()
117     {
118     }
119     void expect(cmd *f)
120     {
121         m_exp_queue.push_back(f);
122     }
123     void enqueue(cmd *f)
124     {
125         m_act_queue.push_back(f);
126     }
127     void enqueue(std::queue<cmd*> &cmds)
128     {
129         while (cmds.size())
130         {
131             m_act_queue.push_back(cmds.front());
132             cmds.pop();
133         }
134     }
135     void enqueue(std::shared_ptr<cmd> f)
136     {
137         m_act_queue.push_back(f.get());
138     }
139
140     void dequeue(cmd *f)
141     {
142     }
143
144     void dequeue(std::shared_ptr<cmd> cmd)
145     {
146     }
147
148     void strict_order(bool on)
149     {
150         m_strict_order = on;
151     }
152
153     bool is_empty()
154     {
155         return ((0 == m_exp_queue.size()) &&
156                 (0 == m_act_queue.size()));
157     }
158
159     rc_t write()
160     {
161         cmd *f_exp, *f_act;
162         rc_t rc = rc_t::OK;
163
164         while (m_act_queue.size())
165         {
166             bool matched = false;
167             auto it_exp = m_exp_queue.begin();
168             auto it_act = m_act_queue.begin();
169
170             f_act = *it_act;
171
172             std::cout << " Act: " << f_act->to_string() << std::endl;
173             while (it_exp != m_exp_queue.end())
174             {
175                 f_exp = *it_exp;
176                 try
177                 {
178                     std::cout << "  Exp: " << f_exp->to_string() << std::endl;
179
180                     if (typeid(*f_exp) != typeid(*f_act))
181                     {
182                         throw ExpException(1);
183                     }
184
185                     if (typeid(*f_exp) == typeid(interface_cmds::af_packet_create_cmd))
186                     {
187                         rc = handle_derived<interface_cmds::af_packet_create_cmd>(f_exp, f_act);
188                     }
189                     else if (typeid(*f_exp) == typeid(interface_cmds::loopback_create_cmd))
190                     {
191                         rc = handle_derived<interface_cmds::loopback_create_cmd>(f_exp, f_act);
192                     }
193                     else if (typeid(*f_exp) == typeid(interface_cmds::vhost_create_cmd))
194                     {
195                         rc = handle_derived<interface_cmds::vhost_create_cmd>(f_exp, f_act);
196                     }
197                     else if (typeid(*f_exp) == typeid(bond_interface_cmds::create_cmd))
198                     {
199                        rc = handle_derived<bond_interface_cmds::create_cmd>(f_exp, f_act);
200                     }
201                     else if (typeid(*f_exp) == typeid(interface_cmds::loopback_delete_cmd))
202                     {
203                         rc = handle_derived<interface_cmds::loopback_delete_cmd>(f_exp, f_act);
204                     }
205                     else if (typeid(*f_exp) == typeid(interface_cmds::af_packet_delete_cmd))
206                     {
207                         rc = handle_derived<interface_cmds::af_packet_delete_cmd>(f_exp, f_act);
208                     }
209                     else if (typeid(*f_exp) == typeid(interface_cmds::vhost_delete_cmd))
210                     {
211                        rc = handle_derived<interface_cmds::vhost_delete_cmd>(f_exp, f_act);
212                     }
213                     else if (typeid(*f_exp) == typeid(bond_interface_cmds::delete_cmd))
214                     {
215                        rc = handle_derived<bond_interface_cmds::delete_cmd>(f_exp, f_act);
216                     }
217                     else if (typeid(*f_exp) == typeid(interface_cmds::state_change_cmd))
218                     {
219                         rc = handle_derived<interface_cmds::state_change_cmd>(f_exp, f_act);
220                     }
221                     else if (typeid(*f_exp) == typeid(interface_cmds::set_table_cmd))
222                     {
223                         rc = handle_derived<interface_cmds::set_table_cmd>(f_exp, f_act);
224                     }
225                     else if (typeid(*f_exp) == typeid(interface_cmds::set_mac_cmd))
226                     {
227                         rc = handle_derived<interface_cmds::set_mac_cmd>(f_exp, f_act);
228                     }
229                     else if (typeid(*f_exp) == typeid(interface_cmds::set_tag))
230                     {
231                         rc = handle_derived<interface_cmds::set_tag>(f_exp, f_act);
232                     }
233                     else if (typeid(*f_exp) == typeid(bond_group_binding_cmds::bind_cmd))
234                     {
235                        rc = handle_derived<bond_group_binding_cmds::bind_cmd>(f_exp, f_act);
236                     }
237                     else if (typeid(*f_exp) == typeid(bond_group_binding_cmds::unbind_cmd))
238                     {
239                        rc = handle_derived<bond_group_binding_cmds::unbind_cmd>(f_exp, f_act);
240                     }
241                     else if (typeid(*f_exp) == typeid(route_domain_cmds::create_cmd))
242                     {
243                         rc = handle_derived<route_domain_cmds::create_cmd>(f_exp, f_act);
244                     }
245                     else if (typeid(*f_exp) == typeid(route_domain_cmds::delete_cmd))
246                     {
247                         rc = handle_derived<route_domain_cmds::delete_cmd>(f_exp, f_act);
248                     }
249                     else if (typeid(*f_exp) == typeid(route::ip_route_cmds::update_cmd))
250                     {
251                         rc = handle_derived<route::ip_route_cmds::update_cmd>(f_exp, f_act);
252                     }
253                     else if (typeid(*f_exp) == typeid(route::ip_route_cmds::delete_cmd))
254                     {
255                         rc = handle_derived<route::ip_route_cmds::delete_cmd>(f_exp, f_act);
256                     }
257                     else if (typeid(*f_exp) == typeid(route::ip_mroute_cmds::update_cmd))
258                     {
259                         rc = handle_derived<route::ip_mroute_cmds::update_cmd>(f_exp, f_act);
260                     }
261                     else if (typeid(*f_exp) == typeid(route::ip_mroute_cmds::delete_cmd))
262                     {
263                         rc = handle_derived<route::ip_mroute_cmds::delete_cmd>(f_exp, f_act);
264                     }
265                     else if (typeid(*f_exp) == typeid(neighbour_cmds::create_cmd))
266                     {
267                         rc = handle_derived<neighbour_cmds::create_cmd>(f_exp, f_act);
268                     }
269                     else if (typeid(*f_exp) == typeid(neighbour_cmds::delete_cmd))
270                     {
271                         rc = handle_derived<neighbour_cmds::delete_cmd>(f_exp, f_act);
272                     }
273                     else if (typeid(*f_exp) == typeid(l3_binding_cmds::bind_cmd))
274                     {
275                         rc = handle_derived<l3_binding_cmds::bind_cmd>(f_exp, f_act);
276                     }
277                     else if (typeid(*f_exp) == typeid(l3_binding_cmds::unbind_cmd))
278                     {
279                         rc = handle_derived<l3_binding_cmds::unbind_cmd>(f_exp, f_act);
280                     }
281                     else if (typeid(*f_exp) == typeid(bridge_domain_cmds::create_cmd))
282                     {
283                         rc = handle_derived<bridge_domain_cmds::create_cmd>(f_exp, f_act);
284                     }
285                     else if (typeid(*f_exp) == typeid(bridge_domain_cmds::delete_cmd))
286                     {
287                         rc = handle_derived<bridge_domain_cmds::delete_cmd>(f_exp, f_act);
288                     }
289                     else if (typeid(*f_exp) == typeid(bridge_domain_entry_cmds::create_cmd))
290                     {
291                         rc = handle_derived<bridge_domain_entry_cmds::create_cmd>(f_exp, f_act);
292                     }
293                     else if (typeid(*f_exp) == typeid(bridge_domain_entry_cmds::delete_cmd))
294                     {
295                         rc = handle_derived<bridge_domain_entry_cmds::delete_cmd>(f_exp, f_act);
296                     }
297                     else if (typeid(*f_exp) == typeid(bridge_domain_arp_entry_cmds::create_cmd))
298                     {
299                         rc = handle_derived<bridge_domain_arp_entry_cmds::create_cmd>(f_exp, f_act);
300                     }
301                     else if (typeid(*f_exp) == typeid(bridge_domain_arp_entry_cmds::delete_cmd))
302                     {
303                         rc = handle_derived<bridge_domain_arp_entry_cmds::delete_cmd>(f_exp, f_act);
304                     }
305                     else if (typeid(*f_exp) == typeid(l2_binding_cmds::bind_cmd))
306                     {
307                         rc = handle_derived<l2_binding_cmds::bind_cmd>(f_exp, f_act);
308                     }
309                     else if (typeid(*f_exp) == typeid(l2_binding_cmds::unbind_cmd))
310                     {
311                         rc = handle_derived<l2_binding_cmds::unbind_cmd>(f_exp, f_act);
312                     }
313                     else if (typeid(*f_exp) == typeid(l2_vtr_cmds::set_cmd))
314                     {
315                         rc = handle_derived<l2_vtr_cmds::set_cmd>(f_exp, f_act);
316                     }
317                     else if (typeid(*f_exp) == typeid(l2_xconnect_cmds::bind_cmd))
318                     {
319                         rc = handle_derived<l2_xconnect_cmds::bind_cmd>(f_exp, f_act);
320                     }
321                     else if (typeid(*f_exp) == typeid(l2_xconnect_cmds::unbind_cmd))
322                     {
323                         rc = handle_derived<l2_xconnect_cmds::unbind_cmd>(f_exp, f_act);
324                     }
325                     else if (typeid(*f_exp) == typeid(vxlan_tunnel_cmds::create_cmd))
326                     {
327                         rc = handle_derived<vxlan_tunnel_cmds::create_cmd>(f_exp, f_act);
328                     }
329                     else if (typeid(*f_exp) == typeid(vxlan_tunnel_cmds::delete_cmd))
330                     {
331                         rc = handle_derived<vxlan_tunnel_cmds::delete_cmd>(f_exp, f_act);
332                     }
333                     else if (typeid(*f_exp) == typeid(sub_interface_cmds::create_cmd))
334                     {
335                         rc = handle_derived<sub_interface_cmds::create_cmd>(f_exp, f_act);
336                     }
337                     else if (typeid(*f_exp) == typeid(sub_interface_cmds::delete_cmd))
338                     {
339                         rc = handle_derived<sub_interface_cmds::delete_cmd>(f_exp, f_act);
340                     }
341                     else if (typeid(*f_exp) == typeid(ACL::acl_ethertype_cmds::bind_cmd))
342                     {
343                         rc = handle_derived<ACL::acl_ethertype_cmds::bind_cmd>(f_exp, f_act);
344                     }
345                     else if (typeid(*f_exp) == typeid(ACL::acl_ethertype_cmds::unbind_cmd))
346                     {
347                         rc = handle_derived<ACL::acl_ethertype_cmds::unbind_cmd>(f_exp, f_act);
348                     }
349                     else if (typeid(*f_exp) == typeid(ACL::list_cmds::l3_update_cmd))
350                     {
351                         rc = handle_derived<ACL::list_cmds::l3_update_cmd>(f_exp, f_act);
352                     }
353                     else if (typeid(*f_exp) == typeid(ACL::list_cmds::l3_delete_cmd))
354                     {
355                         rc = handle_derived<ACL::list_cmds::l3_delete_cmd>(f_exp, f_act);
356                     }
357                     else if (typeid(*f_exp) == typeid(ACL::binding_cmds::l3_bind_cmd))
358                     {
359                         rc = handle_derived<ACL::binding_cmds::l3_bind_cmd>(f_exp, f_act);
360                     }
361                     else if (typeid(*f_exp) == typeid(ACL::binding_cmds::l3_unbind_cmd))
362                     {
363                         rc = handle_derived<ACL::binding_cmds::l3_unbind_cmd>(f_exp, f_act);
364                     }
365                     else if (typeid(*f_exp) == typeid(ACL::list_cmds::l2_update_cmd))
366                     {
367                         rc = handle_derived<ACL::list_cmds::l2_update_cmd>(f_exp, f_act);
368                     }
369                     else if (typeid(*f_exp) == typeid(ACL::list_cmds::l2_delete_cmd))
370                     {
371                         rc = handle_derived<ACL::list_cmds::l2_delete_cmd>(f_exp, f_act);
372                     }
373                     else if (typeid(*f_exp) == typeid(ACL::binding_cmds::l2_bind_cmd))
374                     {
375                         rc = handle_derived<ACL::binding_cmds::l2_bind_cmd>(f_exp, f_act);
376                     }
377                     else if (typeid(*f_exp) == typeid(ACL::binding_cmds::l2_unbind_cmd))
378                     {
379                         rc = handle_derived<ACL::binding_cmds::l2_unbind_cmd>(f_exp, f_act);
380                     }
381                     else if (typeid(*f_exp) == typeid(arp_proxy_binding_cmds::bind_cmd))
382                     {
383                         rc = handle_derived<arp_proxy_binding_cmds::bind_cmd>(f_exp, f_act);
384                     }
385                     else if (typeid(*f_exp) == typeid(arp_proxy_binding_cmds::unbind_cmd))
386                     {
387                         rc = handle_derived<arp_proxy_binding_cmds::unbind_cmd>(f_exp, f_act);
388                     }
389                     else if (typeid(*f_exp) == typeid(arp_proxy_config_cmds::config_cmd))
390                     {
391                         rc = handle_derived<arp_proxy_config_cmds::config_cmd>(f_exp, f_act);
392                     }
393                     else if (typeid(*f_exp) == typeid(arp_proxy_config_cmds::unconfig_cmd))
394                     {
395                         rc = handle_derived<arp_proxy_config_cmds::unconfig_cmd>(f_exp, f_act);
396                     }
397                     else if (typeid(*f_exp) == typeid(igmp_binding_cmds::bind_cmd))
398                     {
399                         rc = handle_derived<igmp_binding_cmds::bind_cmd>(f_exp, f_act);
400                     }
401                     else if (typeid(*f_exp) == typeid(igmp_binding_cmds::unbind_cmd))
402                     {
403                         rc = handle_derived<igmp_binding_cmds::unbind_cmd>(f_exp, f_act);
404                     }
405                     else if (typeid(*f_exp) == typeid(igmp_listen_cmds::listen_cmd))
406                     {
407                         rc = handle_derived<igmp_listen_cmds::listen_cmd>(f_exp, f_act);
408                     }
409                     else if (typeid(*f_exp) == typeid(igmp_listen_cmds::unlisten_cmd))
410                     {
411                         rc = handle_derived<igmp_listen_cmds::unlisten_cmd>(f_exp, f_act);
412                     }
413                     else if (typeid(*f_exp) == typeid(ip_punt_redirect_cmds::config_cmd))
414                     {
415                         rc = handle_derived<ip_punt_redirect_cmds::config_cmd>(f_exp, f_act);
416                     }
417                     else if (typeid(*f_exp) == typeid(ip_punt_redirect_cmds::unconfig_cmd))
418                     {
419                         rc = handle_derived<ip_punt_redirect_cmds::unconfig_cmd>(f_exp, f_act);
420                     }
421                     else if (typeid(*f_exp) == typeid(ip_unnumbered_cmds::config_cmd))
422                     {
423                         rc = handle_derived<ip_unnumbered_cmds::config_cmd>(f_exp, f_act);
424                     }
425                     else if (typeid(*f_exp) == typeid(ip_unnumbered_cmds::unconfig_cmd))
426                     {
427                         rc = handle_derived<ip_unnumbered_cmds::unconfig_cmd>(f_exp, f_act);
428                     }
429                     else if (typeid(*f_exp) == typeid(ip6nd_ra_config::config_cmd))
430                     {
431                         rc = handle_derived<ip6nd_ra_config::config_cmd>(f_exp, f_act);
432                     }
433                     else if (typeid(*f_exp) == typeid(ip6nd_ra_config::unconfig_cmd))
434                     {
435                         rc = handle_derived<ip6nd_ra_config::unconfig_cmd>(f_exp, f_act);
436                     }
437                     else if (typeid(*f_exp) == typeid(ip6nd_ra_prefix::config_cmd))
438                     {
439                         rc = handle_derived<ip6nd_ra_prefix::config_cmd>(f_exp, f_act);
440                     }
441                     else if (typeid(*f_exp) == typeid(ip6nd_ra_prefix::unconfig_cmd))
442                     {
443                         rc = handle_derived<ip6nd_ra_prefix::unconfig_cmd>(f_exp, f_act);
444                     }
445                     else if (typeid(*f_exp) == typeid(interface_span_cmds::config_cmd))
446                     {
447                         rc = handle_derived<interface_span_cmds::config_cmd>(f_exp, f_act);
448                     }
449                     else if (typeid(*f_exp) == typeid(interface_span_cmds::unconfig_cmd))
450                     {
451                         rc = handle_derived<interface_span_cmds::unconfig_cmd>(f_exp, f_act);
452                     }
453                     else if (typeid(*f_exp) == typeid(nat_static_cmds::create_44_cmd))
454                     {
455                         rc = handle_derived<nat_static_cmds::create_44_cmd>(f_exp, f_act);
456                     }
457                     else if (typeid(*f_exp) == typeid(nat_static_cmds::delete_44_cmd))
458                     {
459                         rc = handle_derived<nat_static_cmds::delete_44_cmd>(f_exp, f_act);
460                     }
461                     else if (typeid(*f_exp) == typeid(nat_binding_cmds::bind_44_input_cmd))
462                     {
463                         rc = handle_derived<nat_binding_cmds::bind_44_input_cmd>(f_exp, f_act);
464                     }
465                     else if (typeid(*f_exp) == typeid(nat_binding_cmds::unbind_44_input_cmd))
466                     {
467                         rc = handle_derived<nat_binding_cmds::unbind_44_input_cmd>(f_exp, f_act);
468                     }
469                     else if (typeid(*f_exp) == typeid(interface_cmds::events_cmd))
470                     {
471                         rc = handle_derived<interface_cmds::events_cmd>(f_exp, f_act);
472                     }
473                     else if (typeid(*f_exp) == typeid(pipe_cmds::create_cmd))
474                     {
475                         rc = handle_derived<pipe_cmds::create_cmd>(f_exp, f_act);
476                     }
477                     else if (typeid(*f_exp) == typeid(pipe_cmds::delete_cmd))
478                     {
479                         rc = handle_derived<pipe_cmds::delete_cmd>(f_exp, f_act);
480                     }
481                     else
482                     {
483                         throw ExpException(2);
484                     }
485
486                     // if we get here then we found the match.
487                     m_exp_queue.erase(it_exp);
488                     m_act_queue.erase(it_act);
489                     delete f_exp;
490                     delete f_act;
491
492                     // return any injected failures to the agent
493                     if (rc_t::OK != rc && rc_t::NOOP != rc)
494                     {
495                         return (rc);
496                     }
497
498                     matched = true;
499                     break;
500                 }
501                 catch (ExpException &e)
502                 {
503                     // The expected and actual do not match
504                     if (m_strict_order)
505                     {
506                         // in strict ordering mode this is fatal, so rethrow
507                         throw e;
508                     }
509                     else
510                     {
511                         // move the iterator onto the next in the expected list and
512                         // check for a match
513                         ++it_exp;
514                     }
515                 }
516             }
517
518             if (!matched)
519                 throw ExpException(3);
520         }
521
522         return (rc);
523     }
524 private:
525
526     template <typename T>
527     rc_t handle_derived(const cmd *f_exp, cmd *f_act)
528     {
529         const T *i_exp;
530         T *i_act;
531
532         i_exp = dynamic_cast<const T*>(f_exp);
533         i_act = dynamic_cast<T*>(f_act);
534         if (!(*i_exp == *i_act))
535         {
536             throw ExpException(4);
537         }
538         // pass the data and return code to the agent
539         i_act->item() = i_exp->item();
540
541         return (i_act->item().rc());
542     }
543
544     // The Q to push the expectations on
545     std::deque<cmd*> m_exp_queue;
546
547     // the queue to push the actual events on
548     std::deque<cmd*> m_act_queue;
549
550     // control whether the expected queue is strictly ordered.
551     bool m_strict_order;
552 };
553
554 class VppInit {
555 public:
556     std::string name;
557     MockCmdQ *f;
558
559     VppInit()
560         : name("vpp-ut"),
561           f(new MockCmdQ())
562     {
563         HW::init(f);
564         OM::init();
565         logger().set(log_level_t::DEBUG);
566     }
567
568     ~VppInit() {
569         delete f;
570     }
571 };
572
573 BOOST_AUTO_TEST_SUITE(vom)
574
575 #define TRY_CHECK_RC(stmt)                    \
576 {                                             \
577     try {                                     \
578         BOOST_CHECK(rc_t::OK == stmt);        \
579     }                                         \
580     catch (ExpException &e)                   \
581     {                                         \
582         BOOST_CHECK(false);                   \
583     }                                         \
584     BOOST_CHECK(vi.f->is_empty());            \
585 }
586
587 #define TRY_CHECK(stmt)                       \
588 {                                             \
589     try {                                     \
590         stmt;                                 \
591     }                                         \
592     catch (ExpException &e)                   \
593     {                                         \
594         BOOST_CHECK(false);                   \
595     }                                         \
596     BOOST_CHECK(vi.f->is_empty());            \
597 }
598
599 #define ADD_EXPECT(stmt)                      \
600     vi.f->expect(new stmt)
601
602 #define STRICT_ORDER_OFF()                        \
603     vi.f->strict_order(false)
604
605 BOOST_AUTO_TEST_CASE(test_interface) {
606     VppInit vi;
607     const std::string go = "GeorgeOrwell";
608     const std::string js = "JohnSteinbeck";
609     rc_t rc = rc_t::OK;
610
611     /*
612      * George creates and deletes the interface
613      */
614     std::string itf1_name = "afpacket1";
615     interface itf1(itf1_name,
616                    interface::type_t::AFPACKET,
617                    interface::admin_state_t::UP);
618
619     /*
620      * set the expectation for a afpacket interface create.
621      *  2 is the interface handle VPP [mock] assigns
622      */
623     HW::item<handle_t> hw_ifh(2, rc_t::OK);
624     ADD_EXPECT(interface_cmds::af_packet_create_cmd(hw_ifh, itf1_name));
625
626     HW::item<interface::admin_state_t> hw_as_up(interface::admin_state_t::UP, rc_t::OK);
627     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh));
628
629     TRY_CHECK_RC(OM::write(go, itf1));
630
631     HW::item<interface::admin_state_t> hw_as_down(interface::admin_state_t::DOWN, rc_t::OK);
632     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh));
633     ADD_EXPECT(interface_cmds::af_packet_delete_cmd(hw_ifh, itf1_name));
634
635     TRY_CHECK(OM::remove(go));
636
637     /*
638      * George creates the interface, then John brings it down.
639      * George's remove is a no-op, sice John also owns the interface
640      */
641     interface itf1b(itf1_name,
642                     interface::type_t::AFPACKET,
643                     interface::admin_state_t::DOWN);
644
645     ADD_EXPECT(interface_cmds::af_packet_create_cmd(hw_ifh, itf1_name));
646     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh));
647     TRY_CHECK_RC(OM::write(go, itf1));
648
649     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh));
650     TRY_CHECK_RC(OM::write(js, itf1b));
651
652     TRY_CHECK(OM::remove(go));
653
654     ADD_EXPECT(interface_cmds::af_packet_delete_cmd(hw_ifh, itf1_name));
655     TRY_CHECK(OM::remove(js));
656
657     /*
658      * George adds an interface, then we flush all of Geroge's state
659      */
660     ADD_EXPECT(interface_cmds::af_packet_create_cmd(hw_ifh, itf1_name));
661     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh));
662     TRY_CHECK_RC(OM::write(go, itf1));
663
664     TRY_CHECK(OM::mark(go));
665
666     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh));
667     ADD_EXPECT(interface_cmds::af_packet_delete_cmd(hw_ifh, itf1_name));
668     TRY_CHECK(OM::sweep(go));
669
670     /*
671      * George adds an interface. mark stale. update the same interface. sweep
672      * and expect no delete
673      */
674     ADD_EXPECT(interface_cmds::af_packet_create_cmd(hw_ifh, itf1_name));
675     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh));
676     TRY_CHECK_RC(OM::write(go, itf1b));
677
678     TRY_CHECK(OM::mark(go));
679
680     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh));
681     TRY_CHECK_RC(OM::write(go, itf1));
682
683     TRY_CHECK(OM::sweep(go));
684
685     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh));
686     ADD_EXPECT(interface_cmds::af_packet_delete_cmd(hw_ifh, itf1_name));
687     TRY_CHECK(OM::remove(go));
688
689     /*
690      * George adds an insterface, then we mark that state. Add a second interface
691      * an flush the first that is now stale.
692      */
693     ADD_EXPECT(interface_cmds::af_packet_create_cmd(hw_ifh, itf1_name));
694     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh));
695     TRY_CHECK_RC(OM::write(go, itf1));
696
697     TRY_CHECK(OM::mark(go));
698
699     std::string itf2_name = "afpacket2";
700     std::string itf2_tag = "uuid-of-afpacket2-interface";
701     interface itf2(itf2_name,
702                    interface::type_t::AFPACKET,
703                    interface::admin_state_t::UP,
704                    itf2_tag);
705     HW::item<handle_t> hw_ifh2(3, rc_t::OK);
706
707     ADD_EXPECT(interface_cmds::af_packet_create_cmd(hw_ifh2, itf2_name));
708     ADD_EXPECT(interface_cmds::set_tag(hw_ifh2, itf2_tag));
709     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh2));
710     TRY_CHECK_RC(OM::write(go, itf2));
711
712     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh));
713     ADD_EXPECT(interface_cmds::af_packet_delete_cmd(hw_ifh, itf1_name));
714     TRY_CHECK(OM::sweep(go));
715
716     TRY_CHECK(OM::mark(go));
717
718     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh2));
719     ADD_EXPECT(interface_cmds::af_packet_delete_cmd(hw_ifh2, itf2_name));
720     TRY_CHECK(OM::sweep(go));
721
722
723     std::string itf3_name = "/PATH/TO/vhost_user1.sock";
724     std::string itf3_tag = "uuid-of-vhost_user1-interface";
725     interface itf3(itf3_name,
726                    interface::type_t::VHOST,
727                    interface::admin_state_t::UP,
728                    itf3_tag);
729     HW::item<handle_t> hw_ifh3(4, rc_t::OK);
730
731     ADD_EXPECT(interface_cmds::vhost_create_cmd(hw_ifh3, itf3_name, itf3_tag));
732     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh3));
733     TRY_CHECK_RC(OM::write(go, itf3));
734
735     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh3));
736     ADD_EXPECT(interface_cmds::vhost_delete_cmd(hw_ifh3, itf3_name));
737     TRY_CHECK(OM::remove(go));
738 }
739
740 BOOST_AUTO_TEST_CASE(test_bvi) {
741     VppInit vi;
742     const std::string ernest = "ErnestHemmingway";
743     const std::string graham = "GrahamGreene";
744     rc_t rc = rc_t::OK;
745     l3_binding *l3;
746
747     HW::item<interface::admin_state_t> hw_as_up(interface::admin_state_t::UP,
748                                                 rc_t::OK);
749     HW::item<interface::admin_state_t> hw_as_down(interface::admin_state_t::DOWN,
750                                                   rc_t::OK);
751
752     /*
753      * Enrest creates a BVI with address 10.10.10.10/24
754      */
755     route::prefix_t pfx_10("10.10.10.10", 24);
756
757     const std::string bvi_name = "bvi1";
758     interface itf(bvi_name,
759                   interface::type_t::BVI,
760                   interface::admin_state_t::UP);
761     HW::item<handle_t> hw_ifh(4, rc_t::OK);
762     HW::item<route::prefix_t> hw_pfx_10(pfx_10, rc_t::OK);
763
764     ADD_EXPECT(interface_cmds::loopback_create_cmd(hw_ifh, bvi_name));
765     ADD_EXPECT(interface_cmds::set_tag(hw_ifh, bvi_name));
766     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh));
767     TRY_CHECK_RC(OM::write(ernest, itf));
768
769     l3 = new l3_binding(itf, pfx_10);
770     HW::item<bool> hw_l3_bind(true, rc_t::OK);
771     HW::item<bool> hw_l3_unbind(false, rc_t::OK);
772     ADD_EXPECT(l3_binding_cmds::bind_cmd(hw_l3_bind, hw_ifh.data(), pfx_10));
773     TRY_CHECK_RC(OM::write(ernest, *l3));
774
775     // change the MAC address on the BVI
776     interface itf_new_mac(bvi_name,
777                           interface::type_t::BVI,
778                           interface::admin_state_t::UP);
779     l2_address_t l2_addr({0,1,2,3,4,5});
780     HW::item<l2_address_t> hw_mac(l2_addr, rc_t::OK);
781     itf_new_mac.set(l2_addr);
782     ADD_EXPECT(interface_cmds::set_mac_cmd(hw_mac, hw_ifh));
783     TRY_CHECK_RC(OM::write(ernest, itf_new_mac));
784
785     // create/write the interface to the OM again but with an unset MAC
786     // this should not generate a MAC address update
787     TRY_CHECK_RC(OM::write(ernest, itf));
788
789     // change the MAC address on the BVI - again
790     interface itf_new_mac2(bvi_name,
791                            interface::type_t::BVI,
792                            interface::admin_state_t::UP);
793     l2_address_t l2_addr2({0,1,2,3,4,6});
794     HW::item<l2_address_t> hw_mac2(l2_addr2, rc_t::OK);
795     itf_new_mac2.set(l2_addr2);
796     ADD_EXPECT(interface_cmds::set_mac_cmd(hw_mac2, hw_ifh));
797     TRY_CHECK_RC(OM::write(ernest, itf_new_mac2));
798
799     delete l3;
800     ADD_EXPECT(l3_binding_cmds::unbind_cmd(hw_l3_unbind, hw_ifh.data(), pfx_10));
801     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh));
802     ADD_EXPECT(interface_cmds::loopback_delete_cmd(hw_ifh));
803     TRY_CHECK(OM::remove(ernest));
804
805     /*
806      * Graham creates a BVI with address 10.10.10.10/24 in Routing Domain
807      */
808     route_domain rd(1);
809     HW::item<bool> hw_rd4_create(true, rc_t::OK);
810     HW::item<bool> hw_rd4_delete(false, rc_t::OK);
811     HW::item<bool> hw_rd6_create(true, rc_t::OK);
812     HW::item<bool> hw_rd6_delete(false, rc_t::OK);
813     HW::item<route::table_id_t> hw_rd4_bind(1, rc_t::OK);
814     HW::item<route::table_id_t> hw_rd4_unbind(route::DEFAULT_TABLE, rc_t::OK);
815     HW::item<route::table_id_t> hw_rd6_bind(1, rc_t::OK);
816     HW::item<route::table_id_t> hw_rd6_unbind(route::DEFAULT_TABLE, rc_t::OK);
817     ADD_EXPECT(route_domain_cmds::create_cmd(hw_rd4_create, l3_proto_t::IPV4, 1));
818     ADD_EXPECT(route_domain_cmds::create_cmd(hw_rd6_create, l3_proto_t::IPV6, 1));
819     TRY_CHECK_RC(OM::write(graham, rd));
820
821     const std::string bvi2_name = "bvi2";
822     interface *itf2 = new interface(bvi2_name,
823                                     interface::type_t::BVI,
824                                     interface::admin_state_t::UP,
825                                     rd);
826     HW::item<handle_t> hw_ifh2(5, rc_t::OK);
827
828     ADD_EXPECT(interface_cmds::loopback_create_cmd(hw_ifh2, bvi2_name));
829     ADD_EXPECT(interface_cmds::set_tag(hw_ifh2, bvi2_name));
830     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh2));
831     ADD_EXPECT(interface_cmds::set_table_cmd(hw_rd4_bind, l3_proto_t::IPV4, hw_ifh2));
832     ADD_EXPECT(interface_cmds::set_table_cmd(hw_rd6_bind, l3_proto_t::IPV6, hw_ifh2));
833
834     TRY_CHECK_RC(OM::write(graham, *itf2));
835
836     l3 = new l3_binding(*itf2, pfx_10);
837     ADD_EXPECT(l3_binding_cmds::bind_cmd(hw_l3_bind, hw_ifh2.data(), pfx_10));
838     TRY_CHECK_RC(OM::write(graham, *l3));
839
840     delete l3;
841     delete itf2;
842
843     ADD_EXPECT(l3_binding_cmds::unbind_cmd(hw_l3_unbind, hw_ifh2.data(), pfx_10));
844     ADD_EXPECT(interface_cmds::set_table_cmd(hw_rd4_unbind, l3_proto_t::IPV4, hw_ifh2));
845     ADD_EXPECT(interface_cmds::set_table_cmd(hw_rd6_unbind, l3_proto_t::IPV6, hw_ifh2));
846     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh2));
847     ADD_EXPECT(interface_cmds::loopback_delete_cmd(hw_ifh2));
848     ADD_EXPECT(route_domain_cmds::delete_cmd(hw_rd4_delete, l3_proto_t::IPV4, 1));
849     ADD_EXPECT(route_domain_cmds::delete_cmd(hw_rd6_delete, l3_proto_t::IPV6, 1));
850     TRY_CHECK(OM::remove(graham));
851 }
852
853 BOOST_AUTO_TEST_CASE(test_bond) {
854     VppInit vi;
855     const std::string cb = "CarolBerg";
856     rc_t rc = rc_t::OK;
857
858     /*
859      * creates the interfaces
860      */
861     std::string itf1_name = "afpacket1";
862     interface itf1(itf1_name,
863                    interface::type_t::AFPACKET,
864                    interface::admin_state_t::UP);
865
866     HW::item<handle_t> hw_ifh(2, rc_t::OK);
867     ADD_EXPECT(interface_cmds::af_packet_create_cmd(hw_ifh, itf1_name));
868
869     HW::item<interface::admin_state_t> hw_as_up(interface::admin_state_t::UP, rc_t::OK);
870     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh));
871
872     TRY_CHECK_RC(OM::write(cb, itf1));
873
874     std::string itf2_name = "afpacket2";
875     interface itf2(itf2_name,
876                    interface::type_t::AFPACKET,
877                    interface::admin_state_t::UP);
878
879
880     HW::item<handle_t> hw_ifh2(4, rc_t::OK);
881     ADD_EXPECT(interface_cmds::af_packet_create_cmd(hw_ifh2, itf2_name));
882     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh2));
883
884     TRY_CHECK_RC(OM::write(cb, itf2));
885
886     std::string bond_name = "bond";
887     bond_interface bond_itf(bond_name, interface::admin_state_t::UP,
888                                  bond_interface::mode_t::LACP);
889
890     HW::item<handle_t> hw_ifh3(6, rc_t::OK);
891     ADD_EXPECT(bond_interface_cmds::create_cmd(hw_ifh3, bond_name,
892       bond_interface::mode_t::LACP, bond_interface::lb_t::L2, l2_address_t::ZERO));
893     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh3));
894
895     TRY_CHECK_RC(OM::write(cb, bond_itf));
896
897     bond_member *bm1 = new bond_member(itf1, bond_member::mode_t::ACTIVE,
898                                          bond_member::rate_t::SLOW);
899     bond_member *bm2 = new bond_member(itf2, bond_member::mode_t::ACTIVE,
900                                          bond_member::rate_t::SLOW);
901     bond_group_binding *bgb = new bond_group_binding(bond_itf, {*bm1, *bm2});
902
903     HW::item<bool> bond_hw_bind(true, rc_t::OK);
904     ADD_EXPECT(bond_group_binding_cmds::bind_cmd(bond_hw_bind, hw_ifh3.data(), *bm1));
905     ADD_EXPECT(bond_group_binding_cmds::bind_cmd(bond_hw_bind, hw_ifh3.data(), *bm2));
906
907     TRY_CHECK_RC(OM::write(cb, *bgb));
908
909     delete bgb;
910     delete bm2;
911     delete bm1;
912
913     STRICT_ORDER_OFF();
914     HW::item<interface::admin_state_t> hw_as_down(interface::admin_state_t::DOWN, rc_t::OK);
915     ADD_EXPECT(bond_group_binding_cmds::unbind_cmd(bond_hw_bind, hw_ifh.data()));
916     ADD_EXPECT(bond_group_binding_cmds::unbind_cmd(bond_hw_bind, hw_ifh2.data()));
917     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh2));
918     ADD_EXPECT(interface_cmds::af_packet_delete_cmd(hw_ifh2, itf2_name));
919     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh3));
920     ADD_EXPECT(bond_interface_cmds::delete_cmd(hw_ifh3));
921     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh));
922     ADD_EXPECT(interface_cmds::af_packet_delete_cmd(hw_ifh, itf1_name));
923
924     TRY_CHECK(OM::remove(cb));
925 }
926
927 BOOST_AUTO_TEST_CASE(test_bridge) {
928     VppInit vi;
929     const std::string franz = "FranzKafka";
930     const std::string dante = "Dante";
931     const std::string jkr = "jkrowling";
932     rc_t rc = rc_t::OK;
933
934     /*
935      * Franz creates an interface, Bridge-domain, then binds the two
936      */
937
938     // interface create
939     std::string itf1_name = "afpacket1";
940     interface itf1(itf1_name,
941                    interface::type_t::AFPACKET,
942                    interface::admin_state_t::UP);
943
944     HW::item<handle_t> hw_ifh(3, rc_t::OK);
945     HW::item<interface::admin_state_t> hw_as_up(interface::admin_state_t::UP,
946                                                 rc_t::OK);
947     ADD_EXPECT(interface_cmds::af_packet_create_cmd(hw_ifh, itf1_name));
948     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh));
949
950     TRY_CHECK_RC(OM::write(franz, itf1));
951
952     // bridge-domain create
953     bridge_domain bd1(33);
954
955     HW::item<uint32_t> hw_bd(33, rc_t::OK);
956     ADD_EXPECT(bridge_domain_cmds::create_cmd(hw_bd,
957                                               bridge_domain::learning_mode_t::ON,
958                                               bridge_domain::arp_term_mode_t::ON,
959                                               bridge_domain::flood_mode_t::ON,
960                                               bridge_domain::mac_age_mode_t::OFF));
961
962     TRY_CHECK_RC(OM::write(franz, bd1));
963
964     // L2-interface create and bind
965     // this needs to be delete'd before the flush below, since it too maintains
966     // references to the BD and Interface
967     l2_binding *l2itf = new l2_binding(itf1, bd1);
968     HW::item<bool> hw_l2_bind(true, rc_t::OK);
969
970     ADD_EXPECT(l2_binding_cmds::bind_cmd(hw_l2_bind,
971                                          hw_ifh.data(),
972                                          hw_bd.data(),
973                                          l2_binding::l2_port_type_t::L2_PORT_TYPE_NORMAL));
974     TRY_CHECK_RC(OM::write(franz, *l2itf));
975
976     /*
977      * Dante adds an interface to the same BD
978      */
979     std::string itf2_name = "afpacket2";
980     interface itf2(itf2_name,
981                    interface::type_t::AFPACKET,
982                    interface::admin_state_t::UP);
983
984     HW::item<handle_t> hw_ifh2(4, rc_t::OK);
985     ADD_EXPECT(interface_cmds::af_packet_create_cmd(hw_ifh2, itf2_name));
986     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh2));
987     TRY_CHECK_RC(OM::write(dante, itf2));
988
989     // BD add is a no-op since it exists
990     TRY_CHECK_RC(OM::write(dante, bd1));
991
992     l2_binding *l2itf2 = new l2_binding(itf2, bd1);
993     HW::item<l2_vtr::option_t> hw_set_vtr(l2_vtr::option_t::POP_1, rc_t::OK);
994     l2itf2->set(l2_vtr::option_t::POP_1, 68);
995
996     ADD_EXPECT(l2_binding_cmds::bind_cmd(hw_l2_bind,
997                                          hw_ifh2.data(),
998                                          hw_bd.data(),
999                                          l2_binding::l2_port_type_t::L2_PORT_TYPE_NORMAL));
1000     ADD_EXPECT(l2_vtr_cmds::set_cmd(hw_set_vtr, hw_ifh2.data(), 68));
1001     TRY_CHECK_RC(OM::write(dante, *l2itf2));
1002
1003     // Add some static entries to the bridge-domain
1004     HW::item<bool> hw_be1(true, rc_t::OK);
1005     mac_address_t mac1({0,1,2,3,4,5});
1006     bridge_domain_entry *be1 = new bridge_domain_entry(bd1, mac1, itf2);
1007     ADD_EXPECT(bridge_domain_entry_cmds::create_cmd(hw_be1, mac1, bd1.id(), hw_ifh2.data(),
1008                                                     false));
1009     TRY_CHECK_RC(OM::write(dante, *be1));
1010
1011     // Add some entries to the bridge-domain ARP termination table
1012     HW::item<bool> hw_bea1(true, rc_t::OK);
1013     boost::asio::ip::address ip1 = boost::asio::ip::address::from_string("10.10.10.10");
1014
1015     bridge_domain_arp_entry *bea1 = new bridge_domain_arp_entry(bd1, ip1, mac1);
1016     ADD_EXPECT(bridge_domain_arp_entry_cmds::create_cmd(hw_be1, bd1.id(), mac1, ip1));
1017     TRY_CHECK_RC(OM::write(dante, *bea1));
1018
1019     // flush Franz's state
1020     delete l2itf;
1021     HW::item<interface::admin_state_t> hw_as_down(interface::admin_state_t::DOWN,
1022                                                   rc_t::OK);
1023     ADD_EXPECT(l2_binding_cmds::unbind_cmd(hw_l2_bind,
1024                                            hw_ifh.data(),
1025                                            hw_bd.data(),
1026                                            l2_binding::l2_port_type_t::L2_PORT_TYPE_NORMAL));
1027     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh));
1028     ADD_EXPECT(interface_cmds::af_packet_delete_cmd(hw_ifh, itf1_name));
1029     TRY_CHECK(OM::remove(franz));
1030
1031     // flush Dante's state - the order the interface and BD are deleted
1032     // is an uncontrollable artifact of the C++ object destruction.
1033     delete l2itf2;
1034     delete be1;
1035     delete bea1;
1036     STRICT_ORDER_OFF();
1037     ADD_EXPECT(bridge_domain_arp_entry_cmds::delete_cmd(hw_be1, bd1.id(), mac1, ip1));
1038     ADD_EXPECT(bridge_domain_entry_cmds::delete_cmd(hw_be1, mac1, bd1.id(), false));
1039     ADD_EXPECT(l2_binding_cmds::unbind_cmd(hw_l2_bind,
1040                                            hw_ifh2.data(),
1041                                            hw_bd.data(),
1042                                            l2_binding::l2_port_type_t::L2_PORT_TYPE_NORMAL));
1043
1044     ADD_EXPECT(bridge_domain_cmds::delete_cmd(hw_bd));
1045     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh2));
1046     ADD_EXPECT(interface_cmds::af_packet_delete_cmd(hw_ifh2, itf2_name));
1047     TRY_CHECK(OM::remove(dante));
1048
1049     // test the BVI entry in l2fib
1050     bridge_domain bd2(99);
1051
1052     HW::item<uint32_t> hw_bd2(99, rc_t::OK);
1053     ADD_EXPECT(bridge_domain_cmds::create_cmd(hw_bd2,
1054                                               bridge_domain::learning_mode_t::ON,
1055                                               bridge_domain::arp_term_mode_t::ON,
1056                                               bridge_domain::flood_mode_t::ON,
1057                                               bridge_domain::mac_age_mode_t::OFF));
1058
1059     TRY_CHECK_RC(OM::write(jkr, bd2));
1060
1061     std::string itf3_name = "bvi";
1062     interface itf3(itf3_name,
1063                    interface::type_t::BVI,
1064                    interface::admin_state_t::UP);
1065
1066     HW::item<handle_t> hw_ifh3(5, rc_t::OK);
1067     ADD_EXPECT(interface_cmds::loopback_create_cmd(hw_ifh3, itf3_name));
1068     ADD_EXPECT(interface_cmds::set_tag(hw_ifh3, itf3_name));
1069     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh3));
1070     TRY_CHECK_RC(OM::write(jkr, itf3));
1071
1072     l2_binding *l2itf3 = new l2_binding(itf3, bd2);
1073     ADD_EXPECT(l2_binding_cmds::bind_cmd(hw_l2_bind,
1074                                          hw_ifh3.data(),
1075                                          hw_bd2.data(),
1076                                          l2_binding::l2_port_type_t::L2_PORT_TYPE_BVI));
1077     TRY_CHECK_RC(OM::write(jkr, *l2itf3));
1078
1079     HW::item<bool> hw_be2(true, rc_t::OK);
1080     mac_address_t mac2({0,1,2,3,4,5});
1081     bridge_domain_entry *be2 = new bridge_domain_entry(bd2, mac2, itf3);
1082     ADD_EXPECT(bridge_domain_entry_cmds::create_cmd(hw_be2, mac2, bd2.id(), hw_ifh3.data(), true));
1083     TRY_CHECK_RC(OM::write(jkr, *be2));
1084
1085     delete l2itf3;
1086     delete be2;
1087     STRICT_ORDER_OFF();
1088     ADD_EXPECT(l2_binding_cmds::unbind_cmd(hw_l2_bind,
1089                                            hw_ifh3.data(),
1090                                            hw_bd2.data(),
1091                                            l2_binding::l2_port_type_t::L2_PORT_TYPE_BVI));
1092     ADD_EXPECT(bridge_domain_entry_cmds::delete_cmd(hw_be2, mac2, bd2.id(), true));
1093     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh3));
1094     ADD_EXPECT(interface_cmds::loopback_delete_cmd(hw_ifh3));
1095     ADD_EXPECT(bridge_domain_cmds::delete_cmd(hw_bd2));
1096     TRY_CHECK(OM::remove(jkr));
1097 }
1098
1099 BOOST_AUTO_TEST_CASE(test_l2_xconnect) {
1100     VppInit vi;
1101     const std::string nicholas = "NicholasAbercrombie";
1102     rc_t rc = rc_t::OK;
1103
1104     /*
1105      * Interface 1
1106      */
1107     std::string itf1_name = "host1";
1108     interface itf1(itf1_name,
1109                    interface::type_t::AFPACKET,
1110                    interface::admin_state_t::UP);
1111     HW::item<handle_t> hw_ifh(2, rc_t::OK);
1112     HW::item<interface::admin_state_t> hw_as_up(interface::admin_state_t::UP, rc_t::OK);
1113     ADD_EXPECT(interface_cmds::af_packet_create_cmd(hw_ifh, itf1_name));
1114     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh));
1115     TRY_CHECK_RC(OM::write(nicholas, itf1));
1116
1117     /*
1118      * Interface 2
1119      */
1120     std::string itf2_name = "host2";
1121     interface itf2(itf2_name,
1122                    interface::type_t::AFPACKET,
1123                    interface::admin_state_t::UP);
1124
1125     HW::item<handle_t> hw_ifh2(4, rc_t::OK);
1126     ADD_EXPECT(interface_cmds::af_packet_create_cmd(hw_ifh2, itf2_name));
1127     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh2));
1128     TRY_CHECK_RC(OM::write(nicholas, itf2));
1129
1130     l2_xconnect *l2_xconn = new l2_xconnect(itf1, itf2);
1131     HW::item<bool> xconnect_east(true, rc_t::OK);
1132     HW::item<bool> xconnect_west(true, rc_t::OK);
1133     HW::item<bool> xconnect_east_unbind(false, rc_t::OK);
1134     HW::item<bool> xconnect_west_unbind(false, rc_t::OK);
1135     ADD_EXPECT(l2_xconnect_cmds::bind_cmd(xconnect_east, hw_ifh.data(), hw_ifh2.data()));
1136     ADD_EXPECT(l2_xconnect_cmds::bind_cmd(xconnect_west, hw_ifh2.data(), hw_ifh.data()));
1137     TRY_CHECK_RC(OM::write(nicholas, *l2_xconn));
1138
1139     delete l2_xconn;
1140
1141     HW::item<interface::admin_state_t> hw_as_down(interface::admin_state_t::DOWN, rc_t::OK);
1142     STRICT_ORDER_OFF();
1143     ADD_EXPECT(l2_xconnect_cmds::unbind_cmd(xconnect_east_unbind, hw_ifh.data(), hw_ifh2.data()));
1144     ADD_EXPECT(l2_xconnect_cmds::unbind_cmd(xconnect_west_unbind, hw_ifh2.data(), hw_ifh.data()));
1145     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh2));
1146     ADD_EXPECT(interface_cmds::af_packet_delete_cmd(hw_ifh2, itf2_name));
1147     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh));
1148     ADD_EXPECT(interface_cmds::af_packet_delete_cmd(hw_ifh, itf1_name));
1149
1150     TRY_CHECK(OM::remove(nicholas));
1151 }
1152
1153 BOOST_AUTO_TEST_CASE(test_vxlan) {
1154     VppInit vi;
1155     const std::string franz = "FranzKafka";
1156     rc_t rc = rc_t::OK;
1157
1158     /*
1159      * Franz creates an interface, Bridge-domain, then binds the two
1160      */
1161
1162     // VXLAN create
1163     vxlan_tunnel::endpoint_t ep(boost::asio::ip::address::from_string("10.10.10.10"),
1164                                 boost::asio::ip::address::from_string("10.10.10.11"),
1165                                 322);
1166
1167     vxlan_tunnel vxt(ep.src, ep.dst, ep.vni);
1168
1169     HW::item<handle_t> hw_vxt(3, rc_t::OK);
1170     ADD_EXPECT(vxlan_tunnel_cmds::create_cmd(hw_vxt, "don't-care", ep,
1171                                              handle_t::INVALID));
1172
1173     TRY_CHECK_RC(OM::write(franz, vxt));
1174
1175     // bridge-domain create
1176     bridge_domain bd1(33, bridge_domain::learning_mode_t::OFF,
1177                       bridge_domain::arp_term_mode_t::OFF,
1178                       bridge_domain::flood_mode_t::OFF,
1179                       bridge_domain::mac_age_mode_t::ON);
1180
1181     HW::item<uint32_t> hw_bd(33, rc_t::OK);
1182     ADD_EXPECT(bridge_domain_cmds::create_cmd(hw_bd,
1183                                               bridge_domain::learning_mode_t::OFF,
1184                                               bridge_domain::arp_term_mode_t::OFF,
1185                                               bridge_domain::flood_mode_t::OFF,
1186                                               bridge_domain::mac_age_mode_t::ON));
1187
1188     TRY_CHECK_RC(OM::write(franz, bd1));
1189
1190     // L2-interface create and bind
1191     // this needs to be delete'd before the flush below, since it too maintains
1192     // references to the BD and Interface
1193     l2_binding *l2itf = new l2_binding(vxt, bd1);
1194     HW::item<bool> hw_l2_bind(true, rc_t::OK);
1195
1196     ADD_EXPECT(l2_binding_cmds::bind_cmd(hw_l2_bind,
1197                                          hw_vxt.data(),
1198                                          hw_bd.data(),
1199                                          l2_binding::l2_port_type_t::L2_PORT_TYPE_NORMAL));
1200     TRY_CHECK_RC(OM::write(franz, *l2itf));
1201
1202     // flush Franz's state
1203     delete l2itf;
1204     HW::item<handle_t> hw_vxtdel(3, rc_t::NOOP);
1205     STRICT_ORDER_OFF();
1206     ADD_EXPECT(l2_binding_cmds::unbind_cmd(hw_l2_bind,
1207                                            hw_vxt.data(),
1208                                            hw_bd.data(),
1209                                            l2_binding::l2_port_type_t::L2_PORT_TYPE_NORMAL));
1210     ADD_EXPECT(bridge_domain_cmds::delete_cmd(hw_bd));
1211     ADD_EXPECT(vxlan_tunnel_cmds::delete_cmd(hw_vxtdel, ep));
1212     TRY_CHECK(OM::remove(franz));
1213 }
1214
1215 BOOST_AUTO_TEST_CASE(test_vlan) {
1216     VppInit vi;
1217     const std::string noam = "NoamChomsky";
1218     rc_t rc = rc_t::OK;
1219
1220     std::string itf1_name = "host1";
1221     interface itf1(itf1_name,
1222                    interface::type_t::AFPACKET,
1223                    interface::admin_state_t::UP);
1224
1225     HW::item<handle_t> hw_ifh(2, rc_t::OK);
1226     ADD_EXPECT(interface_cmds::af_packet_create_cmd(hw_ifh, itf1_name));
1227
1228     HW::item<interface::admin_state_t> hw_as_up(interface::admin_state_t::UP, rc_t::OK);
1229     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh));
1230
1231     TRY_CHECK_RC(OM::write(noam, itf1));
1232
1233     sub_interface *vl33 = new sub_interface(itf1,
1234                                             interface::admin_state_t::UP,
1235                                             33);
1236
1237     HW::item<handle_t> hw_vl33(3, rc_t::OK);
1238     ADD_EXPECT(sub_interface_cmds::create_cmd(hw_vl33, itf1_name+".33", hw_ifh.data(), 33));
1239     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_vl33));
1240
1241     TRY_CHECK_RC(OM::write(noam, *vl33));
1242
1243     delete vl33;
1244     HW::item<interface::admin_state_t> hw_as_down(interface::admin_state_t::DOWN, rc_t::OK);
1245     HW::item<handle_t> hw_vl33_down(3, rc_t::NOOP);
1246     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_vl33));
1247     ADD_EXPECT(sub_interface_cmds::delete_cmd(hw_vl33_down));
1248     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh));
1249     ADD_EXPECT(interface_cmds::af_packet_delete_cmd(hw_ifh, itf1_name));
1250
1251     TRY_CHECK(OM::remove(noam));
1252 }
1253
1254 BOOST_AUTO_TEST_CASE(test_acl) {
1255     VppInit vi;
1256     const std::string fyodor = "FyodorDostoyevsky";
1257     const std::string leo = "LeoTolstoy";
1258     rc_t rc = rc_t::OK;
1259
1260     /*
1261      * Fyodor adds an ACL in the input direction
1262      */
1263     std::string itf1_name = "host1";
1264     interface itf1(itf1_name,
1265                    interface::type_t::AFPACKET,
1266                    interface::admin_state_t::UP);
1267     HW::item<handle_t> hw_ifh(2, rc_t::OK);
1268     HW::item<interface::admin_state_t> hw_as_up(interface::admin_state_t::UP, rc_t::OK);
1269     ADD_EXPECT(interface_cmds::af_packet_create_cmd(hw_ifh, itf1_name));
1270     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh));
1271     TRY_CHECK_RC(OM::write(fyodor, itf1));
1272
1273     ACL::ethertype_rule_t e1(ethertype_t::ARP, direction_t::INPUT);
1274     ACL::ethertype_rule_t e2(ethertype_t::ARP, direction_t::OUTPUT);
1275     ACL::ethertype_rule_t e3(ethertype_t::IPV4, direction_t::INPUT);
1276     ACL::acl_ethertype::ethertype_rules_t l_e = {e1, e2, e3};
1277     ACL::acl_ethertype *a_e = new ACL::acl_ethertype(itf1, l_e);
1278     HW::item<bool> ae_binding(true, rc_t::OK);
1279     ADD_EXPECT(ACL::acl_ethertype_cmds::bind_cmd(ae_binding, hw_ifh.data(), l_e));
1280     TRY_CHECK_RC(OM::write(fyodor, *a_e));
1281
1282     route::prefix_t src("10.10.10.10", 32);
1283     ACL::l3_rule r1(10, ACL::action_t::PERMIT, src, route::prefix_t::ZERO);
1284     ACL::l3_rule r2(20, ACL::action_t::DENY, route::prefix_t::ZERO, route::prefix_t::ZERO);
1285
1286     std::string acl_name = "acl1";
1287     ACL::l3_list acl1(acl_name);
1288     acl1.insert(r2);
1289     acl1.insert(r1);
1290     ACL::l3_list::rules_t rules = {r1, r2};
1291
1292     HW::item<handle_t> hw_acl(2, rc_t::OK);
1293     ADD_EXPECT(ACL::list_cmds::l3_update_cmd(hw_acl, acl_name, rules));
1294     TRY_CHECK_RC(OM::write(fyodor, acl1));
1295
1296     ACL::l3_rule r3(30, ACL::action_t::PERMIT, route::prefix_t::ZERO, route::prefix_t::ZERO);
1297     ACL::l3_list acl2(acl_name);
1298     acl2.insert(r3);
1299     ACL::l3_list::rules_t rules2 = {r3};
1300     ADD_EXPECT(ACL::list_cmds::l3_update_cmd(hw_acl, acl_name, rules2));
1301     TRY_CHECK_RC(OM::write(fyodor, acl2));
1302
1303     ACL::l3_binding *l3b = new ACL::l3_binding(direction_t::INPUT, itf1, acl1);
1304     HW::item<bool> hw_binding(true, rc_t::OK);
1305     ADD_EXPECT(ACL::binding_cmds::l3_bind_cmd(hw_binding, direction_t::INPUT,
1306                                          hw_ifh.data(), hw_acl.data()));
1307     TRY_CHECK_RC(OM::write(fyodor, *l3b));
1308
1309     /**
1310      * Leo adds an L2 ACL in the output direction
1311      */
1312     TRY_CHECK_RC(OM::write(leo, itf1));
1313
1314     std::string l2_acl_name = "l2_acl1";
1315     mac_address_t mac({0x0, 0x0, 0x1, 0x2, 0x3, 0x4});
1316     mac_address_t mac_mask({0xff, 0xff, 0xff, 0x0, 0x0, 0x0});
1317     ACL::l2_rule l2_r1(10, ACL::action_t::PERMIT, src, mac, mac_mask);
1318     ACL::l2_rule l2_r2(20, ACL::action_t::DENY, src, {}, {});
1319
1320     ACL::l2_list l2_acl(l2_acl_name);
1321     l2_acl.insert(l2_r2);
1322     l2_acl.insert(l2_r1);
1323
1324     ACL::l2_list::rules_t l2_rules = {l2_r1, l2_r2};
1325
1326     HW::item<handle_t> l2_hw_acl(3, rc_t::OK);
1327     ADD_EXPECT(ACL::list_cmds::l2_update_cmd(l2_hw_acl, l2_acl_name, l2_rules));
1328     TRY_CHECK_RC(OM::write(leo, l2_acl));
1329
1330     ACL::l2_binding *l2b = new ACL::l2_binding(direction_t::OUTPUT, itf1, l2_acl);
1331     HW::item<bool> l2_hw_binding(true, rc_t::OK);
1332     ADD_EXPECT(ACL::binding_cmds::l2_bind_cmd(l2_hw_binding, direction_t::OUTPUT,
1333                                        hw_ifh.data(), l2_hw_acl.data()));
1334     TRY_CHECK_RC(OM::write(leo, *l2b));
1335
1336     delete l2b;
1337     ADD_EXPECT(ACL::binding_cmds::l2_unbind_cmd(l2_hw_binding, direction_t::OUTPUT,
1338                                                 hw_ifh.data(), l2_hw_acl.data()));
1339     ADD_EXPECT(ACL::list_cmds::l2_delete_cmd(l2_hw_acl));
1340     TRY_CHECK(OM::remove(leo));
1341
1342     delete l3b;
1343     delete a_e;
1344     HW::item<interface::admin_state_t> hw_as_down(interface::admin_state_t::DOWN,
1345                                                   rc_t::OK);
1346     STRICT_ORDER_OFF();
1347     ADD_EXPECT(ACL::binding_cmds::l3_unbind_cmd(hw_binding, direction_t::INPUT,
1348                                          hw_ifh.data(), hw_acl.data()));
1349     ADD_EXPECT(ACL::list_cmds::l3_delete_cmd(hw_acl));
1350     ADD_EXPECT(ACL::acl_ethertype_cmds::unbind_cmd(ae_binding, hw_ifh.data()));
1351     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh));
1352     ADD_EXPECT(interface_cmds::af_packet_delete_cmd(hw_ifh, itf1_name));
1353
1354     TRY_CHECK(OM::remove(fyodor));
1355 }
1356
1357 BOOST_AUTO_TEST_CASE(test_igmp) {
1358     VppInit vi;
1359     const std::string Isaiah = "IsaiahBerlin";
1360     rc_t rc = rc_t::OK;
1361
1362     boost::asio::ip::address_v4 gaddr = boost::asio::ip::address_v4::from_string("232.0.0.1");
1363     boost::asio::ip::address_v4 saddr1 = boost::asio::ip::address_v4::from_string("192.168.0.20");
1364     boost::asio::ip::address_v4 saddr2 = boost::asio::ip::address_v4::from_string("192.168.0.30");
1365
1366     std::string itf3_name = "host3";
1367     interface itf3(itf3_name,
1368                    interface::type_t::AFPACKET,
1369                    interface::admin_state_t::UP);
1370     HW::item<handle_t> hw_ifh(2, rc_t::OK);
1371     HW::item<interface::admin_state_t> hw_as_up(interface::admin_state_t::UP, rc_t::OK);
1372     ADD_EXPECT(interface_cmds::af_packet_create_cmd(hw_ifh, itf3_name));
1373     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh));
1374     TRY_CHECK_RC(OM::write(Isaiah, itf3));
1375
1376     igmp_binding *ib = new igmp_binding(itf3);
1377     HW::item<bool> hw_binding(true, rc_t::OK);
1378     ADD_EXPECT(igmp_binding_cmds::bind_cmd(hw_binding, hw_ifh.data()));
1379     TRY_CHECK_RC(OM::write(Isaiah, *ib));
1380
1381     igmp_listen::src_addrs_t saddrs = {saddr1, saddr2};
1382
1383     igmp_listen *il = new igmp_listen(*ib, gaddr, saddrs);
1384     HW::item<bool> hw_as_listen(true, rc_t::OK);
1385     ADD_EXPECT(igmp_listen_cmds::listen_cmd(hw_as_listen, hw_ifh.data(), gaddr, saddrs));
1386     TRY_CHECK_RC(OM::write(Isaiah, *il));
1387
1388     delete il;
1389     delete ib;
1390
1391     HW::item<interface::admin_state_t> hw_as_down(interface::admin_state_t::DOWN,
1392                                                   rc_t::OK);
1393     STRICT_ORDER_OFF();
1394     ADD_EXPECT(igmp_listen_cmds::unlisten_cmd(hw_as_listen, hw_ifh.data(), gaddr));
1395     ADD_EXPECT(igmp_binding_cmds::unbind_cmd(hw_binding, hw_ifh.data()));
1396     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh));
1397     ADD_EXPECT(interface_cmds::af_packet_delete_cmd(hw_ifh, itf3_name));
1398
1399     TRY_CHECK(OM::remove(Isaiah));
1400 }
1401
1402 BOOST_AUTO_TEST_CASE(test_arp_proxy) {
1403     VppInit vi;
1404     const std::string kurt = "KurtVonnegut";
1405     rc_t rc = rc_t::OK;
1406
1407     asio::ip::address_v4 low  = asio::ip::address_v4::from_string("10.0.0.0");
1408     asio::ip::address_v4 high = asio::ip::address_v4::from_string("10.0.0.255");
1409
1410     arp_proxy_config ap(low, high);
1411     HW::item<bool> hw_ap_cfg(true, rc_t::OK);
1412     ADD_EXPECT(arp_proxy_config_cmds::config_cmd(hw_ap_cfg, low, high));
1413     TRY_CHECK_RC(OM::write(kurt, ap));
1414
1415     std::string itf3_name = "host3";
1416     interface itf3(itf3_name,
1417                    interface::type_t::AFPACKET,
1418                    interface::admin_state_t::UP);
1419     HW::item<handle_t> hw_ifh(2, rc_t::OK);
1420     HW::item<interface::admin_state_t> hw_as_up(interface::admin_state_t::UP, rc_t::OK);
1421     ADD_EXPECT(interface_cmds::af_packet_create_cmd(hw_ifh, itf3_name));
1422     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh));
1423     TRY_CHECK_RC(OM::write(kurt, itf3));
1424
1425     arp_proxy_binding *apb = new arp_proxy_binding(itf3);
1426     HW::item<bool> hw_binding(true, rc_t::OK);
1427     ADD_EXPECT(arp_proxy_binding_cmds::bind_cmd(hw_binding, hw_ifh.data()));
1428     TRY_CHECK_RC(OM::write(kurt, *apb));
1429
1430     delete apb;
1431
1432     HW::item<interface::admin_state_t> hw_as_down(interface::admin_state_t::DOWN,
1433                                                   rc_t::OK);
1434     STRICT_ORDER_OFF();
1435     ADD_EXPECT(arp_proxy_binding_cmds::unbind_cmd(hw_binding, hw_ifh.data()));
1436     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh));
1437     ADD_EXPECT(interface_cmds::af_packet_delete_cmd(hw_ifh, itf3_name));
1438     ADD_EXPECT(arp_proxy_config_cmds::unconfig_cmd(hw_ap_cfg, low, high));
1439
1440     TRY_CHECK(OM::remove(kurt));
1441 }
1442
1443 BOOST_AUTO_TEST_CASE(test_ip_punt_redirect) {
1444     VppInit vi;
1445     const std::string eliot = "EliotReed";
1446     rc_t rc = rc_t::OK;
1447
1448     /*
1449      * Interface 1 is the tx interface
1450      */
1451     std::string itf1_name = "tx-itf";
1452     interface itf1(itf1_name,
1453                    interface::type_t::AFPACKET,
1454                    interface::admin_state_t::UP);
1455     HW::item<handle_t> hw_ifh(2, rc_t::OK);
1456     HW::item<interface::admin_state_t> hw_as_up(interface::admin_state_t::UP, rc_t::OK);
1457     ADD_EXPECT(interface_cmds::af_packet_create_cmd(hw_ifh, itf1_name));
1458     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh));
1459     TRY_CHECK_RC(OM::write(eliot, itf1));
1460
1461     boost::asio::ip::address addr = boost::asio::ip::address::from_string("192.168.0.20");
1462
1463     /*
1464      * Interface 2 is the rx interface
1465      */
1466     std::string itf2_name = "rx-itf";
1467     interface itf2(itf2_name,
1468                    interface::type_t::AFPACKET,
1469                    interface::admin_state_t::UP);
1470
1471     HW::item<handle_t> hw_ifh2(4, rc_t::OK);
1472     ADD_EXPECT(interface_cmds::af_packet_create_cmd(hw_ifh2, itf2_name));
1473     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh2));
1474     TRY_CHECK_RC(OM::write(eliot, itf2));
1475
1476     ip_punt_redirect *ip_punt = new ip_punt_redirect(itf2, itf1, addr);
1477     HW::item<bool> hw_ip_cfg(true, rc_t::OK);
1478     HW::item<bool> hw_ip_uncfg(false, rc_t::OK);
1479     ADD_EXPECT(ip_punt_redirect_cmds::config_cmd(hw_ip_cfg, hw_ifh2.data(), hw_ifh.data(), addr));
1480     TRY_CHECK_RC(OM::write(eliot, *ip_punt));
1481
1482     delete ip_punt;
1483
1484     HW::item<interface::admin_state_t> hw_as_down(interface::admin_state_t::DOWN, rc_t::OK);
1485     STRICT_ORDER_OFF();
1486     ADD_EXPECT(ip_punt_redirect_cmds::unconfig_cmd(hw_ip_uncfg, hw_ifh2.data(), hw_ifh.data(), addr));
1487     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh));
1488     ADD_EXPECT(interface_cmds::af_packet_delete_cmd(hw_ifh, itf1_name));
1489     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh2));
1490     ADD_EXPECT(interface_cmds::af_packet_delete_cmd(hw_ifh2, itf2_name));
1491
1492     TRY_CHECK(OM::remove(eliot));
1493 }
1494
1495 BOOST_AUTO_TEST_CASE(test_ip_unnumbered) {
1496     VppInit vi;
1497     const std::string eric = "EricAmbler";
1498     rc_t rc = rc_t::OK;
1499
1500     /*
1501      * Interface 1 has the L3 address
1502      */
1503     std::string itf1_name = "host1";
1504     interface itf1(itf1_name,
1505                    interface::type_t::AFPACKET,
1506                    interface::admin_state_t::UP);
1507     HW::item<handle_t> hw_ifh(2, rc_t::OK);
1508     HW::item<interface::admin_state_t> hw_as_up(interface::admin_state_t::UP, rc_t::OK);
1509     ADD_EXPECT(interface_cmds::af_packet_create_cmd(hw_ifh, itf1_name));
1510     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh));
1511     TRY_CHECK_RC(OM::write(eric, itf1));
1512
1513     route::prefix_t pfx_10("10.10.10.10", 24);
1514     l3_binding *l3 = new l3_binding(itf1, pfx_10);
1515     HW::item<bool> hw_l3_bind(true, rc_t::OK);
1516     HW::item<bool> hw_l3_unbind(false, rc_t::OK);
1517     ADD_EXPECT(l3_binding_cmds::bind_cmd(hw_l3_bind, hw_ifh.data(), pfx_10));
1518     TRY_CHECK_RC(OM::write(eric, *l3));
1519
1520     /*
1521      * Interface 2 is unnumbered
1522      */
1523     std::string itf2_name = "host2";
1524     interface itf2(itf2_name,
1525                    interface::type_t::AFPACKET,
1526                    interface::admin_state_t::UP);
1527
1528     HW::item<handle_t> hw_ifh2(4, rc_t::OK);
1529     ADD_EXPECT(interface_cmds::af_packet_create_cmd(hw_ifh2, itf2_name));
1530     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh2));
1531     TRY_CHECK_RC(OM::write(eric, itf2));
1532
1533     ip_unnumbered *ipun = new ip_unnumbered(itf2, itf1);
1534     HW::item<bool> hw_ip_cfg(true, rc_t::OK);
1535     HW::item<bool> hw_ip_uncfg(false, rc_t::OK);
1536     ADD_EXPECT(ip_unnumbered_cmds::config_cmd(hw_ip_cfg, hw_ifh2.data(), hw_ifh.data()));
1537     TRY_CHECK_RC(OM::write(eric, *ipun));
1538
1539     delete l3;
1540     delete ipun;
1541
1542     HW::item<interface::admin_state_t> hw_as_down(interface::admin_state_t::DOWN, rc_t::OK);
1543     STRICT_ORDER_OFF();
1544     ADD_EXPECT(ip_unnumbered_cmds::unconfig_cmd(hw_ip_uncfg, hw_ifh2.data(), hw_ifh.data()));
1545     ADD_EXPECT(l3_binding_cmds::unbind_cmd(hw_l3_unbind, hw_ifh.data(), pfx_10));
1546     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh2));
1547     ADD_EXPECT(interface_cmds::af_packet_delete_cmd(hw_ifh2, itf2_name));
1548     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh));
1549     ADD_EXPECT(interface_cmds::af_packet_delete_cmd(hw_ifh, itf1_name));
1550
1551     TRY_CHECK(OM::remove(eric));
1552 }
1553
1554 BOOST_AUTO_TEST_CASE(test_ip6nd) {
1555     VppInit vi;
1556     const std::string paulo = "PauloCoelho";
1557     rc_t rc = rc_t::OK;
1558
1559     /*
1560      * ra config
1561      */
1562     std::string itf_name = "host_ip6nd";
1563     interface itf(itf_name,
1564                    interface::type_t::AFPACKET,
1565                    interface::admin_state_t::UP);
1566     HW::item<handle_t> hw_ifh(3, rc_t::OK);
1567     HW::item<interface::admin_state_t> hw_as_up(interface::admin_state_t::UP, rc_t::OK);
1568     ADD_EXPECT(interface_cmds::af_packet_create_cmd(hw_ifh, itf_name));
1569     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh));
1570     TRY_CHECK_RC(OM::write(paulo, itf));
1571
1572     route::prefix_t pfx_10("fd8f:69d8:c12c:ca62::3", 128);
1573     l3_binding *l3 = new l3_binding(itf, pfx_10);
1574     HW::item<bool> hw_l3_bind(true, rc_t::OK);
1575     HW::item<bool> hw_l3_unbind(false, rc_t::OK);
1576     ADD_EXPECT(l3_binding_cmds::bind_cmd(hw_l3_bind, hw_ifh.data(), pfx_10));
1577     TRY_CHECK_RC(OM::write(paulo, *l3));
1578
1579     ra_config ra(0, 1, 0, 4);
1580     ip6nd_ra_config *ip6ra = new ip6nd_ra_config(itf, ra);
1581     HW::item<bool> hw_ip6nd_ra_config_config(true, rc_t::OK);
1582     HW::item<bool> hw_ip6nd_ra_config_unconfig(false, rc_t::OK);
1583     ADD_EXPECT(ip6nd_ra_config::config_cmd(hw_ip6nd_ra_config_config, hw_ifh.data(), ra));
1584     TRY_CHECK_RC(OM::write(paulo, *ip6ra));
1585
1586     /*
1587      * ra prefix
1588      */
1589     ra_prefix ra_pfx(pfx_10, 0, 0, 2592000, 604800);
1590     ip6nd_ra_prefix *ip6pfx = new ip6nd_ra_prefix(itf, ra_pfx);
1591     HW::item<bool> hw_ip6nd_ra_prefix_config(true, rc_t::OK);
1592     HW::item<bool> hw_ip6nd_ra_prefix_unconfig(false, rc_t::OK);
1593     ADD_EXPECT(ip6nd_ra_prefix::config_cmd(hw_ip6nd_ra_prefix_config, hw_ifh.data(), ra_pfx));
1594     TRY_CHECK_RC(OM::write(paulo, *ip6pfx));
1595
1596     delete ip6pfx;
1597
1598     ADD_EXPECT(ip6nd_ra_prefix::unconfig_cmd(hw_ip6nd_ra_prefix_unconfig, hw_ifh.data(), ra_pfx));
1599
1600     delete ip6ra;
1601     delete l3;
1602
1603     HW::item<interface::admin_state_t> hw_as_down(interface::admin_state_t::DOWN, rc_t::OK);
1604
1605     STRICT_ORDER_OFF();
1606     ADD_EXPECT(ip6nd_ra_config::unconfig_cmd(hw_ip6nd_ra_config_unconfig, hw_ifh.data(), ra));
1607     ADD_EXPECT(l3_binding_cmds::unbind_cmd(hw_l3_unbind, hw_ifh.data(), pfx_10));
1608     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh));
1609     ADD_EXPECT(interface_cmds::af_packet_delete_cmd(hw_ifh, itf_name));
1610
1611     TRY_CHECK(OM::remove(paulo));
1612 }
1613
1614 BOOST_AUTO_TEST_CASE(test_interface_span) {
1615     VppInit vi;
1616     const std::string elif = "ElifShafak";
1617     rc_t rc = rc_t::OK;
1618
1619     /*
1620      * Interface 1 to be mirrored
1621      */
1622     std::string itf1_name = "port-from";
1623     interface itf1(itf1_name,
1624                    interface::type_t::AFPACKET,
1625                    interface::admin_state_t::UP);
1626     HW::item<handle_t> hw_ifh(2, rc_t::OK);
1627     HW::item<interface::admin_state_t> hw_as_up(interface::admin_state_t::UP, rc_t::OK);
1628     ADD_EXPECT(interface_cmds::af_packet_create_cmd(hw_ifh, itf1_name));
1629     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh));
1630     TRY_CHECK_RC(OM::write(elif, itf1));
1631
1632     /*
1633      * Interface 2 where traffic is mirrored
1634      */
1635     std::string itf2_name = "port-to";
1636     interface itf2(itf2_name,
1637                    interface::type_t::AFPACKET,
1638                    interface::admin_state_t::UP);
1639
1640     HW::item<handle_t> hw_ifh2(4, rc_t::OK);
1641     HW::item<interface::admin_state_t> hw_as_up2(interface::admin_state_t::UP, rc_t::OK);
1642
1643     ADD_EXPECT(interface_cmds::af_packet_create_cmd(hw_ifh2, itf2_name));
1644     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up2, hw_ifh2));
1645     TRY_CHECK_RC(OM::write(elif, itf2));
1646
1647     interface_span *itf_span = new interface_span(itf1, itf2, interface_span::state_t::TX_RX_ENABLED);
1648     HW::item<bool> hw_is_cfg(true, rc_t::OK);
1649     HW::item<bool> hw_is_uncfg(true, rc_t::OK);
1650     ADD_EXPECT(interface_span_cmds::config_cmd(hw_is_cfg, hw_ifh.data(), hw_ifh2.data(), interface_span::state_t::TX_RX_ENABLED));
1651     TRY_CHECK_RC(OM::write(elif, *itf_span));
1652
1653     HW::item<interface::admin_state_t> hw_as_down(interface::admin_state_t::DOWN, rc_t::OK);
1654     HW::item<interface::admin_state_t> hw_as_down2(interface::admin_state_t::DOWN, rc_t::OK);
1655
1656     delete itf_span;
1657     STRICT_ORDER_OFF();
1658     ADD_EXPECT(interface_span_cmds::unconfig_cmd(hw_is_uncfg, hw_ifh.data(), hw_ifh2.data()));
1659     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh));
1660     ADD_EXPECT(interface_cmds::af_packet_delete_cmd(hw_ifh, itf1_name));
1661     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down2, hw_ifh2));
1662     ADD_EXPECT(interface_cmds::af_packet_delete_cmd(hw_ifh2, itf2_name));
1663
1664     TRY_CHECK(OM::remove(elif));
1665 }
1666
1667 BOOST_AUTO_TEST_CASE(test_routing) {
1668     VppInit vi;
1669     const std::string ian = "IanFleming";
1670     rc_t rc = rc_t::OK;
1671
1672     /*
1673      * non-default route domain
1674      */
1675     route_domain rd4(1);
1676     HW::item<bool> hw_rd4_create(true, rc_t::OK);
1677     HW::item<bool> hw_rd4_delete(false, rc_t::OK);
1678     HW::item<bool> hw_rd6_create(true, rc_t::OK);
1679     HW::item<bool> hw_rd6_delete(false, rc_t::OK);
1680     HW::item<route::table_id_t> hw_rd4_bind(1, rc_t::OK);
1681     HW::item<route::table_id_t> hw_rd4_unbind(route::DEFAULT_TABLE, rc_t::OK);
1682     HW::item<route::table_id_t> hw_rd6_bind(1, rc_t::OK);
1683     HW::item<route::table_id_t> hw_rd7_unbind(route::DEFAULT_TABLE, rc_t::OK);
1684     ADD_EXPECT(route_domain_cmds::create_cmd(hw_rd4_create, l3_proto_t::IPV4, 1));
1685     ADD_EXPECT(route_domain_cmds::create_cmd(hw_rd6_create, l3_proto_t::IPV6, 1));
1686     TRY_CHECK_RC(OM::write(ian, rd4));
1687
1688     /*
1689      * a couple of interfaces
1690      */
1691     std::string itf1_name = "af1";
1692     interface itf1(itf1_name,
1693                    interface::type_t::AFPACKET,
1694                    interface::admin_state_t::UP);
1695     HW::item<handle_t> hw_ifh(2, rc_t::OK);
1696     HW::item<interface::admin_state_t> hw_as_up(interface::admin_state_t::UP, rc_t::OK);
1697     HW::item<interface::admin_state_t> hw_as_down(interface::admin_state_t::DOWN, rc_t::OK);
1698     ADD_EXPECT(interface_cmds::af_packet_create_cmd(hw_ifh, itf1_name));
1699     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh));
1700     TRY_CHECK_RC(OM::write(ian, itf1));
1701
1702     std::string itf2_name = "af2";
1703     interface *itf2 = new interface(itf2_name,
1704                                     interface::type_t::AFPACKET,
1705                                     interface::admin_state_t::UP,
1706                                     rd4);
1707
1708     HW::item<handle_t> hw_ifh2(4, rc_t::OK);
1709     HW::item<interface::admin_state_t> hw_as_up2(interface::admin_state_t::UP, rc_t::OK);
1710     HW::item<interface::admin_state_t> hw_as_down2(interface::admin_state_t::DOWN, rc_t::OK);
1711     ADD_EXPECT(interface_cmds::af_packet_create_cmd(hw_ifh2, itf2_name));
1712     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up2, hw_ifh2));
1713     ADD_EXPECT(interface_cmds::set_table_cmd(hw_rd4_bind, l3_proto_t::IPV4, hw_ifh2));
1714     ADD_EXPECT(interface_cmds::set_table_cmd(hw_rd6_bind, l3_proto_t::IPV6, hw_ifh2));
1715     TRY_CHECK_RC(OM::write(ian, *itf2));
1716
1717     /*
1718      * prefix on each interface
1719      */
1720     route::prefix_t pfx_10("10.10.10.10", 24);
1721     l3_binding *l3_10 = new l3_binding(itf1, pfx_10);
1722     HW::item<bool> hw_l3_10_bind(true, rc_t::OK);
1723     HW::item<bool> hw_l3_10_unbind(false, rc_t::OK);
1724     ADD_EXPECT(l3_binding_cmds::bind_cmd(hw_l3_10_bind, hw_ifh.data(), pfx_10));
1725     TRY_CHECK_RC(OM::write(ian, *l3_10));
1726     route::prefix_t pfx_11("11.11.11.11", 24);
1727     l3_binding *l3_11 = new l3_binding(*itf2, pfx_11);
1728     HW::item<bool> hw_l3_11_bind(true, rc_t::OK);
1729     HW::item<bool> hw_l3_11_unbind(false, rc_t::OK);
1730     ADD_EXPECT(l3_binding_cmds::bind_cmd(hw_l3_11_bind, hw_ifh2.data(), pfx_11));
1731     TRY_CHECK_RC(OM::write(ian, *l3_11));
1732
1733     /*
1734      * A route via interface 1 in the default table
1735      */
1736     route::prefix_t pfx_5("5.5.5.5", 32);
1737     boost::asio::ip::address nh_9 = boost::asio::ip::address::from_string("10.10.10.9");
1738     route::path *path_9 = new route::path(nh_9, itf1);
1739     boost::asio::ip::address nh_10 = boost::asio::ip::address::from_string("10.10.10.11");
1740     route::path *path_10 = new route::path(nh_10, itf1);
1741     route::ip_route *route_5 = new route::ip_route(pfx_5);
1742     route_5->add(*path_10);
1743     route_5->add(*path_9);
1744     HW::item<bool> hw_route_5(true, rc_t::OK);
1745     ADD_EXPECT(route::ip_route_cmds::update_cmd(hw_route_5, 0, pfx_5, *path_9));
1746     ADD_EXPECT(route::ip_route_cmds::update_cmd(hw_route_5, 0, pfx_5, *path_10));
1747     TRY_CHECK_RC(OM::write(ian, *route_5));
1748
1749     route_5->remove(*path_9);
1750     ADD_EXPECT(route::ip_route_cmds::delete_cmd(hw_route_5, 0, pfx_5, *path_9));
1751     TRY_CHECK_RC(OM::write(ian, *route_5));
1752
1753     delete path_9;
1754
1755     /*
1756      * A route via interface 2 in the non-default table
1757      */
1758     boost::asio::ip::address nh_11 = boost::asio::ip::address::from_string("11.11.11.10");
1759     route::path *path_11 = new route::path(nh_11, *itf2);
1760     boost::asio::ip::address nh_12 = boost::asio::ip::address::from_string("11.11.11.12");
1761     route::path *path_12 = new route::path(nh_12, *itf2);
1762     route::ip_route *route_5_2 = new route::ip_route(rd4, pfx_5);
1763     route_5_2->add(*path_11);
1764     HW::item<bool> hw_route_5_2(true, rc_t::OK);
1765     ADD_EXPECT(route::ip_route_cmds::update_cmd(hw_route_5_2, 1, pfx_5, *path_11));
1766     TRY_CHECK_RC(OM::write(ian, *route_5_2));
1767
1768     route_5_2->add(*path_12);
1769     ADD_EXPECT(route::ip_route_cmds::update_cmd(hw_route_5_2, 1, pfx_5, *path_12));
1770     TRY_CHECK_RC(OM::write(ian, *route_5_2));
1771
1772     /*
1773      * An ARP entry for the neighbour on itf1
1774      */
1775     HW::item<bool> hw_neighbour(true, rc_t::OK);
1776     mac_address_t mac_n({0,1,2,4,5,6});
1777     neighbour *ne = new neighbour(itf1, nh_10, mac_n);
1778     ADD_EXPECT(neighbour_cmds::create_cmd(hw_neighbour, hw_ifh.data(),
1779                                           mac_n, nh_10,
1780                                           neighbour::flags_t::STATIC));
1781     TRY_CHECK_RC(OM::write(ian, *ne));
1782
1783     /*
1784      * A DVR route
1785      */
1786     route::prefix_t pfx_6("6.6.6.6", 32);
1787     route::path *path_l2 = new route::path(*itf2, nh_proto_t::ETHERNET);
1788     route::ip_route *route_dvr = new route::ip_route(pfx_6);
1789     route_dvr->add(*path_l2);
1790     HW::item<bool> hw_route_dvr(true, rc_t::OK);
1791     ADD_EXPECT(route::ip_route_cmds::update_cmd(hw_route_dvr, 0, pfx_6, *path_l2));
1792     TRY_CHECK_RC(OM::write(ian, *route_dvr));
1793
1794     /*
1795      * a multicast route
1796      */
1797     route::mprefix_t mpfx_4(boost::asio::ip::address::from_string("232.1.1.1"), 32);
1798     route::ip_mroute *mroute_4 = new route::ip_mroute(mpfx_4);
1799
1800     route::path *mp1 = new route::path(itf1, nh_proto_t::IPV4);
1801     route::path *mp2 = new route::path(*itf2, nh_proto_t::IPV4);
1802     mroute_4->add(*mp1, route::itf_flags_t::FORWARD);
1803     mroute_4->add(*mp1, route::itf_flags_t::ACCEPT);
1804     mroute_4->add(*mp2, route::itf_flags_t::FORWARD);
1805     HW::item<bool> hw_mroute_4(true, rc_t::OK);
1806     ADD_EXPECT(route::ip_mroute_cmds::update_cmd(hw_mroute_4, 0, mpfx_4,
1807                                                  *mp1, route::itf_flags_t::FORWARD));
1808     ADD_EXPECT(route::ip_mroute_cmds::update_cmd(hw_mroute_4, 0, mpfx_4,
1809                                                  *mp2, route::itf_flags_t::FORWARD));
1810     ADD_EXPECT(route::ip_mroute_cmds::update_cmd(hw_mroute_4, 0, mpfx_4,
1811                                                  *mp1, route::itf_flags_t::ACCEPT));
1812     TRY_CHECK_RC(OM::write(ian, *mroute_4));
1813
1814     STRICT_ORDER_OFF();
1815     // delete the stack objects that hold references to others
1816     // so the OM::remove is the call that removes the last reference
1817     delete l3_11;
1818     delete l3_10;
1819     delete itf2;
1820     delete route_5;
1821     delete route_5_2;
1822     delete route_dvr;
1823     delete ne;
1824     delete mroute_4;
1825
1826     ADD_EXPECT(route::ip_mroute_cmds::delete_cmd(hw_mroute_4, 0, mpfx_4,
1827                                                  *mp1, route::itf_flags_t::FORWARD));
1828     ADD_EXPECT(route::ip_mroute_cmds::delete_cmd(hw_mroute_4, 0, mpfx_4,
1829                                                  *mp2, route::itf_flags_t::FORWARD));
1830     ADD_EXPECT(route::ip_mroute_cmds::delete_cmd(hw_mroute_4, 0, mpfx_4,
1831                                                  *mp1, route::itf_flags_t::ACCEPT));
1832
1833     delete mp1;
1834     delete mp2;
1835
1836     ADD_EXPECT(neighbour_cmds::delete_cmd(hw_neighbour, hw_ifh.data(),
1837                                           mac_n, nh_10,
1838                                           neighbour::flags_t::STATIC));
1839     ADD_EXPECT(route::ip_route_cmds::delete_cmd(hw_route_dvr, 0, pfx_6, *path_l2));
1840     ADD_EXPECT(route::ip_route_cmds::delete_cmd(hw_route_5_2, 1, pfx_5, *path_11));
1841     ADD_EXPECT(route::ip_route_cmds::delete_cmd(hw_route_5_2, 1, pfx_5, *path_12));
1842     ADD_EXPECT(route::ip_route_cmds::delete_cmd(hw_route_5, 0, pfx_5, *path_10));
1843
1844     delete path_10;
1845     delete path_11;
1846     delete path_12;
1847     delete path_l2;
1848
1849     ADD_EXPECT(l3_binding_cmds::unbind_cmd(hw_l3_10_unbind, hw_ifh.data(), pfx_10));
1850     ADD_EXPECT(l3_binding_cmds::unbind_cmd(hw_l3_11_unbind, hw_ifh2.data(), pfx_11));
1851     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh));
1852     ADD_EXPECT(interface_cmds::af_packet_delete_cmd(hw_ifh, itf1_name));
1853     ADD_EXPECT(interface_cmds::set_table_cmd(hw_rd4_unbind, l3_proto_t::IPV4, hw_ifh2));
1854     ADD_EXPECT(interface_cmds::set_table_cmd(hw_rd4_unbind, l3_proto_t::IPV6, hw_ifh2));
1855     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down2, hw_ifh2));
1856     ADD_EXPECT(interface_cmds::af_packet_delete_cmd(hw_ifh2, itf2_name));
1857     ADD_EXPECT(route_domain_cmds::delete_cmd(hw_rd4_delete, l3_proto_t::IPV4, 1));
1858     ADD_EXPECT(route_domain_cmds::delete_cmd(hw_rd6_delete, l3_proto_t::IPV6, 1));
1859
1860     TRY_CHECK(OM::remove(ian));
1861 }
1862
1863 BOOST_AUTO_TEST_CASE(test_nat) {
1864     VppInit vi;
1865     const std::string gs = "GeorgeSimenon";
1866     rc_t rc = rc_t::OK;
1867
1868     /*
1869      * Inside Interface
1870      */
1871     std::string itf_in_name = "inside";
1872     interface itf_in(itf_in_name,
1873                      interface::type_t::AFPACKET,
1874                      interface::admin_state_t::UP);
1875     HW::item<handle_t> hw_ifh(2, rc_t::OK);
1876     HW::item<interface::admin_state_t> hw_as_up(interface::admin_state_t::UP, rc_t::OK);
1877     HW::item<interface::admin_state_t> hw_as_down(interface::admin_state_t::DOWN, rc_t::OK);
1878     ADD_EXPECT(interface_cmds::af_packet_create_cmd(hw_ifh, itf_in_name));
1879     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh));
1880     TRY_CHECK_RC(OM::write(gs, itf_in));
1881
1882     /*
1883      * outside
1884      */
1885     std::string itf_out_name = "port-to";
1886     interface itf_out(itf_out_name,
1887                    interface::type_t::AFPACKET,
1888                    interface::admin_state_t::UP);
1889
1890     HW::item<handle_t> hw_ifh2(4, rc_t::OK);
1891     HW::item<interface::admin_state_t> hw_as_up2(interface::admin_state_t::UP, rc_t::OK);
1892     HW::item<interface::admin_state_t> hw_as_down2(interface::admin_state_t::DOWN, rc_t::OK);
1893
1894     ADD_EXPECT(interface_cmds::af_packet_create_cmd(hw_ifh2, itf_out_name));
1895     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up2, hw_ifh2));
1896     TRY_CHECK_RC(OM::write(gs, itf_out));
1897
1898     /*
1899      * A NAT static mapping
1900      */
1901     boost::asio::ip::address in_addr = boost::asio::ip::address::from_string("10.0.0.1");
1902     boost::asio::ip::address_v4 out_addr = boost::asio::ip::address_v4::from_string("1.1.1.1");
1903
1904     nat_static ns(in_addr, out_addr);
1905     HW::item<bool> hw_ns(true, rc_t::OK);
1906
1907     ADD_EXPECT(nat_static_cmds::create_44_cmd(hw_ns, 0, in_addr.to_v4(), out_addr));
1908     TRY_CHECK_RC(OM::write(gs, ns));
1909
1910     /*
1911      * bind nat inside and out
1912      */
1913     nat_binding *nb_in = new nat_binding(itf_in,
1914                                          direction_t::INPUT,
1915                                          l3_proto_t::IPV4,
1916                                          nat_binding::zone_t::INSIDE);
1917     HW::item<bool> hw_nb_in(true, rc_t::OK);
1918
1919     ADD_EXPECT(nat_binding_cmds::bind_44_input_cmd(hw_nb_in,
1920                                                    hw_ifh.data().value(),
1921                                                    nat_binding::zone_t::INSIDE));
1922     TRY_CHECK_RC(OM::write(gs, *nb_in));
1923
1924     nat_binding *nb_out = new nat_binding(itf_out,
1925                                           direction_t::INPUT,
1926                                           l3_proto_t::IPV4,
1927                                           nat_binding::zone_t::OUTSIDE);
1928     HW::item<bool> hw_nb_out(true, rc_t::OK);
1929
1930     ADD_EXPECT(nat_binding_cmds::bind_44_input_cmd(hw_nb_out,
1931                                                    hw_ifh2.data().value(),
1932                                                    nat_binding::zone_t::OUTSIDE));
1933     TRY_CHECK_RC(OM::write(gs, *nb_out));
1934
1935
1936     STRICT_ORDER_OFF();
1937     delete nb_in;
1938     delete nb_out;
1939     ADD_EXPECT(nat_binding_cmds::unbind_44_input_cmd(hw_nb_in,
1940                                                      hw_ifh.data().value(),
1941                                                      nat_binding::zone_t::INSIDE));
1942     ADD_EXPECT(nat_binding_cmds::unbind_44_input_cmd(hw_nb_out,
1943                                                      hw_ifh2.data().value(),
1944                                                      nat_binding::zone_t::OUTSIDE));
1945     ADD_EXPECT(nat_static_cmds::delete_44_cmd(hw_ns, 0, in_addr.to_v4(), out_addr));
1946     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh));
1947     ADD_EXPECT(interface_cmds::af_packet_delete_cmd(hw_ifh, itf_in_name));
1948     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down2, hw_ifh2));
1949     ADD_EXPECT(interface_cmds::af_packet_delete_cmd(hw_ifh2, itf_out_name));
1950
1951     TRY_CHECK(OM::remove(gs));
1952 }
1953
1954 BOOST_AUTO_TEST_CASE(test_interface_events) {
1955     VppInit vi;
1956     MockListener ml;
1957
1958     HW::item<bool> hw_want(true, rc_t::OK);
1959
1960     ADD_EXPECT(interface_cmds::events_cmd(ml));
1961     cmd* itf = new interface_cmds::events_cmd(ml);
1962
1963     HW::enqueue(itf);
1964     HW::write();
1965 }
1966
1967 BOOST_AUTO_TEST_CASE(test_interface_route_domain_change) {
1968     VppInit vi;
1969     const std::string rene = "ReneGoscinny";
1970     rc_t rc = rc_t::OK;
1971
1972     /*
1973      * Create an interface with two IP addresses
1974      */
1975     std::string itf1_name = "host1";
1976     interface itf1(itf1_name,
1977                    interface::type_t::AFPACKET,
1978                    interface::admin_state_t::UP);
1979     HW::item<handle_t> hw_ifh1(2, rc_t::OK);
1980     HW::item<interface::admin_state_t> hw_as_up(interface::admin_state_t::UP, rc_t::OK);
1981     HW::item<interface::admin_state_t> hw_as_down(interface::admin_state_t::DOWN, rc_t::OK);
1982     ADD_EXPECT(interface_cmds::af_packet_create_cmd(hw_ifh1, itf1_name));
1983     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_ifh1));
1984     TRY_CHECK_RC(OM::write(rene, itf1));
1985
1986     route::prefix_t pfx_10("10.10.10.10", 24);
1987     l3_binding *l3_1 = new l3_binding(itf1, pfx_10);
1988     HW::item<bool> hw_l3_bind1(true, rc_t::OK);
1989     HW::item<bool> hw_l3_unbind1(false, rc_t::OK);
1990     ADD_EXPECT(l3_binding_cmds::bind_cmd(hw_l3_bind1, hw_ifh1.data(), pfx_10));
1991     TRY_CHECK_RC(OM::write(rene, *l3_1));
1992
1993     route::prefix_t pfx_11("10.10.11.11", 24);
1994     l3_binding *l3_2 = new l3_binding(itf1, pfx_11);
1995     HW::item<bool> hw_l3_bind2(true, rc_t::OK);
1996     HW::item<bool> hw_l3_unbind2(false, rc_t::OK);
1997     ADD_EXPECT(l3_binding_cmds::bind_cmd(hw_l3_bind2, hw_ifh1.data(), pfx_11));
1998     TRY_CHECK_RC(OM::write(rene, *l3_2));
1999
2000     route_domain rd(1);
2001     HW::item<bool> hw_rd_create(true, rc_t::OK);
2002     HW::item<bool> hw_rd_delete(false, rc_t::OK);
2003     HW::item<route::table_id_t> hw_rd_bind(1, rc_t::OK);
2004     HW::item<route::table_id_t> hw_rd_unbind(route::DEFAULT_TABLE, rc_t::OK);
2005     ADD_EXPECT(route_domain_cmds::create_cmd(hw_rd_create, l3_proto_t::IPV4, 1));
2006     ADD_EXPECT(route_domain_cmds::create_cmd(hw_rd_create, l3_proto_t::IPV6, 1));
2007     TRY_CHECK_RC(OM::write(rene, rd));
2008
2009     /*
2010      * update the interface to change to a new route-domain
2011      * expect that the l3-bindings are removed and readded.
2012      */
2013     interface *itf2 = new interface(itf1_name,
2014                                     interface::type_t::AFPACKET,
2015                                     interface::admin_state_t::UP,
2016                                     rd);
2017     ADD_EXPECT(l3_binding_cmds::unbind_cmd(hw_l3_unbind1, hw_ifh1.data(), pfx_10));
2018     ADD_EXPECT(l3_binding_cmds::unbind_cmd(hw_l3_unbind2, hw_ifh1.data(), pfx_11));
2019     ADD_EXPECT(interface_cmds::set_table_cmd(hw_rd_bind, l3_proto_t::IPV4, hw_ifh1));
2020     ADD_EXPECT(interface_cmds::set_table_cmd(hw_rd_bind, l3_proto_t::IPV6, hw_ifh1));
2021     ADD_EXPECT(l3_binding_cmds::bind_cmd(hw_l3_bind1, hw_ifh1.data(), pfx_10));
2022     ADD_EXPECT(l3_binding_cmds::bind_cmd(hw_l3_bind2, hw_ifh1.data(), pfx_11));
2023     TRY_CHECK_RC(OM::write(rene, *itf2));
2024
2025     /*
2026      * mve the interface back to the default route-domain
2027      */
2028     interface itf3(itf1_name,
2029                    interface::type_t::AFPACKET,
2030                    interface::admin_state_t::UP);
2031     ADD_EXPECT(l3_binding_cmds::unbind_cmd(hw_l3_unbind1, hw_ifh1.data(), pfx_10));
2032     ADD_EXPECT(l3_binding_cmds::unbind_cmd(hw_l3_unbind2, hw_ifh1.data(), pfx_11));
2033     ADD_EXPECT(interface_cmds::set_table_cmd(hw_rd_unbind, l3_proto_t::IPV4, hw_ifh1));
2034     ADD_EXPECT(interface_cmds::set_table_cmd(hw_rd_unbind, l3_proto_t::IPV6, hw_ifh1));
2035     ADD_EXPECT(l3_binding_cmds::bind_cmd(hw_l3_bind1, hw_ifh1.data(), pfx_10));
2036     ADD_EXPECT(l3_binding_cmds::bind_cmd(hw_l3_bind2, hw_ifh1.data(), pfx_11));
2037     TRY_CHECK_RC(OM::write(rene, itf3));
2038
2039     delete l3_1;
2040     delete l3_2;
2041     delete itf2;
2042
2043     STRICT_ORDER_OFF();
2044     ADD_EXPECT(l3_binding_cmds::unbind_cmd(hw_l3_unbind1, hw_ifh1.data(), pfx_10));
2045     ADD_EXPECT(l3_binding_cmds::unbind_cmd(hw_l3_unbind2, hw_ifh1.data(), pfx_11));
2046     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_ifh1));
2047     ADD_EXPECT(interface_cmds::af_packet_delete_cmd(hw_ifh1, itf1_name));
2048     ADD_EXPECT(route_domain_cmds::delete_cmd(hw_rd_delete, l3_proto_t::IPV4, 1));
2049     ADD_EXPECT(route_domain_cmds::delete_cmd(hw_rd_delete, l3_proto_t::IPV6, 1));
2050
2051     TRY_CHECK(OM::remove(rene));
2052 }
2053
2054 BOOST_AUTO_TEST_CASE(test_prefixes) {
2055     route::prefix_t p6_s_16(boost::asio::ip::address::from_string("2001::"), 16);
2056
2057     BOOST_CHECK(p6_s_16.mask() == boost::asio::ip::address::from_string("ffff::"));
2058
2059     route::prefix_t p6_s_17(boost::asio::ip::address::from_string("2001:ff00::"), 17);
2060
2061     BOOST_CHECK(p6_s_17.mask() == boost::asio::ip::address::from_string("ffff:8000::"));
2062     BOOST_CHECK(p6_s_17.low().address() == boost::asio::ip::address::from_string("2001:8000::"));
2063
2064     route::prefix_t p6_s_15(boost::asio::ip::address::from_string("2001:ff00::"), 15);
2065     BOOST_CHECK(p6_s_15.mask() == boost::asio::ip::address::from_string("fffe::"));
2066     BOOST_CHECK(p6_s_15.low().address() == boost::asio::ip::address::from_string("2000::"));
2067
2068     route::prefix_t p4_s_16(boost::asio::ip::address::from_string("192.168.0.0"), 16);
2069
2070     BOOST_CHECK(p4_s_16.mask() == boost::asio::ip::address::from_string("255.255.0.0"));
2071
2072     route::prefix_t p4_s_17(boost::asio::ip::address::from_string("192.168.127.0"), 17);
2073
2074     BOOST_CHECK(p4_s_17.mask() == boost::asio::ip::address::from_string("255.255.128.0"));
2075     BOOST_CHECK(p4_s_17.low().address() == boost::asio::ip::address::from_string("192.168.0.0"));
2076     BOOST_CHECK(p4_s_17.high().address() == boost::asio::ip::address::from_string("192.168.127.255"));
2077
2078     route::prefix_t p4_s_15(boost::asio::ip::address::from_string("192.168.255.255"), 15);
2079
2080     BOOST_CHECK(p4_s_15.mask() == boost::asio::ip::address::from_string("255.254.0.0"));
2081     BOOST_CHECK(p4_s_15.low().address() == boost::asio::ip::address::from_string("192.168.0.0"));
2082     BOOST_CHECK(p4_s_15.high().address() == boost::asio::ip::address::from_string("192.169.255.255"));
2083
2084     route::prefix_t p4_s_32(boost::asio::ip::address::from_string("192.168.1.1"), 32);
2085
2086     BOOST_CHECK(p4_s_32.mask() == boost::asio::ip::address::from_string("255.255.255.255"));
2087     BOOST_CHECK(p4_s_32.low().address() == boost::asio::ip::address::from_string("192.168.1.1"));
2088     BOOST_CHECK(p4_s_32.high().address() == boost::asio::ip::address::from_string("192.168.1.1"));
2089
2090 }
2091
2092 BOOST_AUTO_TEST_CASE(test_pipes) {
2093     VppInit vi;
2094     const std::string gk = "GKChesterton";
2095
2096     const std::string pipe_name_1 = "pipe1";
2097     VOM::pipe pipe1(1, interface::admin_state_t::UP);
2098     HW::item<handle_t> hw_hdl(4, rc_t::OK);
2099     HW::item<pipe::handle_pair_t> hw_hdl_pair(std::make_pair(5,6), rc_t::OK);
2100
2101     HW::item<interface::admin_state_t> hw_as_up(interface::admin_state_t::UP,
2102                                                 rc_t::OK);
2103     HW::item<interface::admin_state_t> hw_as_down(interface::admin_state_t::DOWN,
2104                                                   rc_t::OK);
2105     ADD_EXPECT(pipe_cmds::create_cmd(hw_hdl, pipe_name_1, 1, hw_hdl_pair));
2106     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_up, hw_hdl));
2107     TRY_CHECK_RC(OM::write(gk, pipe1));
2108
2109     pipe1.set_ends(hw_hdl_pair.data());
2110
2111     // put each end of the pipe in a BD
2112     bridge_domain bd1(33, bridge_domain::learning_mode_t::OFF,
2113                       bridge_domain::arp_term_mode_t::OFF,
2114                       bridge_domain::flood_mode_t::OFF,
2115                       bridge_domain::mac_age_mode_t::ON);
2116
2117     HW::item<uint32_t> hw_bd(33, rc_t::OK);
2118     ADD_EXPECT(bridge_domain_cmds::create_cmd(hw_bd,
2119                                               bridge_domain::learning_mode_t::OFF,
2120                                               bridge_domain::arp_term_mode_t::OFF,
2121                                               bridge_domain::flood_mode_t::OFF,
2122                                               bridge_domain::mac_age_mode_t::ON));
2123
2124     TRY_CHECK_RC(OM::write(gk, bd1));
2125
2126     l2_binding *l2_1 = new l2_binding(*pipe1.east(), bd1);
2127     HW::item<bool> hw_l2_1_bind(true, rc_t::OK);
2128
2129     ADD_EXPECT(l2_binding_cmds::bind_cmd(hw_l2_1_bind,
2130                                          pipe1.east()->handle(),
2131                                          hw_bd.data(),
2132                                          l2_binding::l2_port_type_t::L2_PORT_TYPE_NORMAL));
2133     TRY_CHECK_RC(OM::write(gk, *l2_1));
2134
2135     l2_binding *l2_2 = new l2_binding(*pipe1.west(), bd1);
2136     HW::item<bool> hw_l2_2_bind(true, rc_t::OK);
2137
2138     ADD_EXPECT(l2_binding_cmds::bind_cmd(hw_l2_2_bind,
2139                                          pipe1.west()->handle(),
2140                                          hw_bd.data(),
2141                                          l2_binding::l2_port_type_t::L2_PORT_TYPE_NORMAL));
2142     TRY_CHECK_RC(OM::write(gk, *l2_2));
2143
2144     STRICT_ORDER_OFF();
2145
2146     delete l2_1;
2147     delete l2_2;
2148     ADD_EXPECT(l2_binding_cmds::unbind_cmd(hw_l2_1_bind,
2149                                            pipe1.east()->handle(),
2150                                            hw_bd.data(),
2151                                            l2_binding::l2_port_type_t::L2_PORT_TYPE_NORMAL));
2152     ADD_EXPECT(l2_binding_cmds::unbind_cmd(hw_l2_1_bind,
2153                                            pipe1.west()->handle(),
2154                                            hw_bd.data(),
2155                                            l2_binding::l2_port_type_t::L2_PORT_TYPE_NORMAL));
2156     ADD_EXPECT(interface_cmds::state_change_cmd(hw_as_down, hw_hdl));
2157     ADD_EXPECT(pipe_cmds::delete_cmd(hw_hdl, hw_hdl_pair));
2158     ADD_EXPECT(bridge_domain_cmds::delete_cmd(hw_bd));
2159     TRY_CHECK(OM::remove(gk));
2160 }
2161
2162 BOOST_AUTO_TEST_SUITE_END()