1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * CLI/command dummy handling tester
5 * Copyright (C) 2015 by David Lamparter,
6 * for Open Source Routing / NetDEF, Inc.
15 #include "ospf6d/ospf6_lsa.h"
16 #include "ospf6d/ospf6_lsdb.h"
18 #include "tests/lib/cli/common_cli.h"
19 #include "tests/ospf6d/test_lsdb_clippy.c"
21 static struct ospf6_lsdb
*lsdb
;
23 static struct ospf6_lsa
**lsas
= NULL
;
24 static size_t lsa_count
= 0;
26 static void lsa_check_resize(size_t len
)
28 struct ospf6_lsa
**templsas
;
32 templsas
= realloc(lsas
, len
* sizeof(lsas
[0]));
37 memset(lsas
+ lsa_count
, 0, sizeof(lsas
[0]) * (len
- lsa_count
));
42 DEFPY(lsa_set
, lsa_set_cmd
,
43 "lsa set (0-999999)$idx {type (0-65535)|id A.B.C.D|adv A.B.C.D}",
46 "LSA index in array\n"
51 "Advertising router\n"
52 "Advertising router\n")
54 struct ospf6_lsa_header hdr
;
55 memset(&hdr
, 0, sizeof(hdr
));
56 hdr
.type
= htons(type
);
58 hdr
.adv_router
= adv
.s_addr
;
60 lsa_check_resize(idx
+ 1);
62 ospf6_lsa_unlock(lsas
[idx
]);
63 lsas
[idx
] = ospf6_lsa_create_headeronly(&hdr
);
64 ospf6_lsa_lock(lsas
[idx
]);
68 DEFPY(lsa_drop
, lsa_drop_cmd
,
69 "lsa drop (0-999999)$idx",
72 "LSA index in array\n")
74 if ((size_t)idx
>= lsa_count
)
76 if (lsas
[idx
]->lock
!= 1)
77 vty_out(vty
, "refcount at %u\n", lsas
[idx
]->lock
);
78 ospf6_lsa_unlock(lsas
[idx
]);
84 DEFPY(lsdb_add
, lsdb_add_cmd
,
85 "lsdb add (0-999999)$idx",
87 "insert LSA into LSDB\n"
88 "LSA index in array\n")
90 ospf6_lsdb_add(lsas
[idx
], lsdb
);
94 DEFPY(lsdb_remove
, lsdb_remove_cmd
,
95 "lsdb remove (0-999999)$idx",
97 "remove LSA from LSDB\n"
98 "LSA index in array\n")
100 ospf6_lsdb_remove(lsas
[idx
], lsdb
);
104 static void lsa_show_oneline(struct vty
*vty
, struct ospf6_lsa
*lsa
)
106 char adv_router
[64], id
[64];
109 vty_out(vty
, "lsa = NULL\n");
112 inet_ntop(AF_INET
, &lsa
->header
->id
, id
, sizeof(id
));
113 inet_ntop(AF_INET
, &lsa
->header
->adv_router
, adv_router
,
115 vty_out(vty
, "type %u adv %s id %s\n", ntohs(lsa
->header
->type
),
119 DEFPY(lsdb_walk
, lsdb_walk_cmd
,
124 struct ospf6_lsa
*lsa
, *lsanext
;
127 for (ALL_LSDB(lsdb
, lsa
, lsanext
)) {
128 lsa_show_oneline(vty
, lsa
);
131 vty_out(vty
, "%u entries.\n", cnt
);
135 DEFPY(lsdb_walk_type
, lsdb_walk_type_cmd
,
136 "lsdb walk type (0-65535)",
142 struct ospf6_lsa
*lsa
;
145 for (ALL_LSDB_TYPED(lsdb
, type
, lsa
)) {
146 lsa_show_oneline(vty
, lsa
);
149 vty_out(vty
, "%u entries.\n", cnt
);
153 DEFPY(lsdb_walk_type_adv
, lsdb_walk_type_adv_cmd
,
154 "lsdb walk type (0-65535) adv A.B.C.D",
159 "advertising router ID\n"
160 "advertising router ID\n")
162 struct ospf6_lsa
*lsa
;
165 for (ALL_LSDB_TYPED_ADVRTR(lsdb
, type
, adv
.s_addr
, lsa
)) {
166 lsa_show_oneline(vty
, lsa
);
169 vty_out(vty
, "%u entries.\n", cnt
);
173 DEFPY(lsdb_get
, lsdb_get_cmd
,
174 "lsdb <get-next|get> type (0-65535) adv A.B.C.D id A.B.C.D",
176 "get entry's successor\n"
179 "advertising router ID\n"
180 "advertising router ID\n"
184 struct ospf6_lsa
*lsa
;
186 if (!strcmp(argv
[1]->text
, "get-next"))
187 lsa
= ospf6_lsdb_lookup_next(type
, id
.s_addr
, adv
.s_addr
, lsdb
);
189 lsa
= ospf6_lsdb_lookup(type
, id
.s_addr
, adv
.s_addr
, lsdb
);
190 lsa_show_oneline(vty
, lsa
);
194 DEFPY(lsa_refcounts
, lsa_refcounts_cmd
,
197 "show reference counts\n")
199 for (size_t i
= 0; i
< lsa_count
; i
++)
201 vty_out(vty
, "[%zu] %u\n", i
, lsas
[i
]->lock
);
205 DEFPY(lsdb_create
, lsdb_create_cmd
,
211 ospf6_lsdb_delete(lsdb
);
212 lsdb
= ospf6_lsdb_create(NULL
);
216 DEFPY(lsdb_delete
, lsdb_delete_cmd
,
221 ospf6_lsdb_delete(lsdb
);
227 struct zebra_privs_t ospf6d_privs
;
229 void test_init(int argc
, char **argv
)
233 install_element(ENABLE_NODE
, &lsa_set_cmd
);
234 install_element(ENABLE_NODE
, &lsa_refcounts_cmd
);
235 install_element(ENABLE_NODE
, &lsa_drop_cmd
);
237 install_element(ENABLE_NODE
, &lsdb_create_cmd
);
238 install_element(ENABLE_NODE
, &lsdb_delete_cmd
);
240 install_element(ENABLE_NODE
, &lsdb_add_cmd
);
241 install_element(ENABLE_NODE
, &lsdb_remove_cmd
);
242 install_element(ENABLE_NODE
, &lsdb_walk_cmd
);
243 install_element(ENABLE_NODE
, &lsdb_walk_type_cmd
);
244 install_element(ENABLE_NODE
, &lsdb_walk_type_adv_cmd
);
245 install_element(ENABLE_NODE
, &lsdb_get_cmd
);