185b528e0SShwetha/*
285b528e0SShwetha * Copyright (c) 2016 Cisco and/or its affiliates.
385b528e0SShwetha * Licensed under the Apache License, Version 2.0 (the "License");
485b528e0SShwetha * you may not use this file except in compliance with the License.
585b528e0SShwetha * You may obtain a copy of the License at:
685b528e0SShwetha *
785b528e0SShwetha *     http://www.apache.org/licenses/LICENSE-2.0
885b528e0SShwetha *
985b528e0SShwetha * Unless required by applicable law or agreed to in writing, software
1085b528e0SShwetha * distributed under the License is distributed on an "AS IS" BASIS,
1185b528e0SShwetha * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1285b528e0SShwetha * See the License for the specific language governing permissions and
1385b528e0SShwetha * limitations under the License.
1485b528e0SShwetha */
1585b528e0SShwetha/*
1685b528e0SShwetha *------------------------------------------------------------------
1785b528e0SShwetha * pot_test.c - test harness for pot plugin
1885b528e0SShwetha *------------------------------------------------------------------
1985b528e0SShwetha */
2085b528e0SShwetha
2185b528e0SShwetha#include <vat/vat.h>
2285b528e0SShwetha#include <vlibapi/api.h>
2385b528e0SShwetha#include <vlibmemory/api.h>
2459b2565cSDave Barach
2585b528e0SShwetha#include <vppinfra/error.h>
262d6b2d6dSDave Barach
272d6b2d6dSDave Barach#define __plugin_msg_base pot_test_main.msg_id_base
28fe6bdfd8SDave Barach#include <vlibapi/vat_helper_macros.h>
2985b528e0SShwetha
3085b528e0SShwetha/* Declare message IDs */
31ab96454eSOle Troan#include <ioam/lib-pot/pot.api_enum.h>
32ab96454eSOle Troan#include <ioam/lib-pot/pot.api_types.h>
3385b528e0SShwetha
3485b528e0SShwethatypedef struct {
3585b528e0SShwetha    /* API message ID base */
3685b528e0SShwetha    u16 msg_id_base;
3785b528e0SShwetha    vat_main_t *vat_main;
3885b528e0SShwetha} pot_test_main_t;
3985b528e0SShwetha
4085b528e0SShwethapot_test_main_t pot_test_main;
4185b528e0SShwetha
4285b528e0SShwethastatic int api_pot_profile_add (vat_main_t *vam)
4385b528e0SShwetha{
4485b528e0SShwetha#define MAX_BITS 64
4585b528e0SShwetha    unformat_input_t *input = vam->input;
4685b528e0SShwetha    vl_api_pot_profile_add_t *mp;
4785b528e0SShwetha    u8 *name = NULL;
4885b528e0SShwetha    u64 prime = 0;
4985b528e0SShwetha    u64 secret_share = 0;
5085b528e0SShwetha    u64 secret_key = 0;
5185b528e0SShwetha    u32  bits = MAX_BITS;
5285b528e0SShwetha    u64 lpc = 0, poly2 = 0;
5385b528e0SShwetha    u8 id = 0;
5485b528e0SShwetha    int rv = 0;
5556c7b01eSJon Loeliger    int ret;
5685b528e0SShwetha
5785b528e0SShwetha    while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
5885b528e0SShwetha      {
5985b528e0SShwetha        if (unformat(input, "name %s", &name))
6085b528e0SShwetha	  ;
6185b528e0SShwetha	else if(unformat(input, "id %d", &id))
6285b528e0SShwetha	  ;
6385b528e0SShwetha        else if (unformat(input, "validator-key 0x%Lx", &secret_key))
6485b528e0SShwetha          ;
6585b528e0SShwetha        else if (unformat(input, "prime-number 0x%Lx", &prime))
6685b528e0SShwetha          ;
6785b528e0SShwetha        else if (unformat(input, "secret-share 0x%Lx", &secret_share))
6885b528e0SShwetha          ;
6985b528e0SShwetha        else if (unformat(input, "polynomial-public 0x%Lx", &poly2))
7085b528e0SShwetha          ;
7185b528e0SShwetha        else if (unformat(input, "lpc 0x%Lx", &lpc))
7285b528e0SShwetha          ;
7385b528e0SShwetha        else if (unformat(input, "bits-in-random %u", &bits))
7485b528e0SShwetha	  {
7585b528e0SShwetha	    if (bits > MAX_BITS)
7685b528e0SShwetha	      bits = MAX_BITS;
7785b528e0SShwetha	  }
7885b528e0SShwetha        else
7985b528e0SShwetha  	break;
8085b528e0SShwetha      }
8185b528e0SShwetha
8285b528e0SShwetha    if (!name)
8385b528e0SShwetha      {
8485b528e0SShwetha        errmsg ("name required\n");
8585b528e0SShwetha        rv = -99;
8685b528e0SShwetha        goto OUT;
8785b528e0SShwetha      }
8821a43846SDave Barach
890fa66d61SOle Troan    M2(POT_PROFILE_ADD, mp, sizeof(vl_api_string_t) + vec_len(name));
9085b528e0SShwetha
912dbee936SJakub Grajciar    vl_api_vec_to_api_string(name, &mp->list_name);
9285b528e0SShwetha    mp->secret_share = clib_host_to_net_u64(secret_share);
9385b528e0SShwetha    mp->polynomial_public = clib_host_to_net_u64(poly2);
9485b528e0SShwetha    mp->lpc = clib_host_to_net_u64(lpc);
9585b528e0SShwetha    mp->prime = clib_host_to_net_u64(prime);
9685b528e0SShwetha    if (secret_key != 0)
9785b528e0SShwetha      {
9885b528e0SShwetha        mp->secret_key = clib_host_to_net_u64(secret_key);
9985b528e0SShwetha        mp->validator = 1;
10085b528e0SShwetha      }
10185b528e0SShwetha    else
10285b528e0SShwetha      {
10385b528e0SShwetha	mp->validator = 0;
10485b528e0SShwetha      }
10585b528e0SShwetha    mp->id = id;
10685b528e0SShwetha    mp->max_bits = bits;
10721a43846SDave Barach
1087bc770ceSJon Loeliger    S(mp);
10956c7b01eSJon Loeliger    W (ret);
11056c7b01eSJon Loeliger    return ret;
11121a43846SDave Barach
11285b528e0SShwethaOUT:
11385b528e0SShwetha    vec_free(name);
11485b528e0SShwetha    return(rv);
11585b528e0SShwetha}
11685b528e0SShwetha
11785b528e0SShwethastatic int api_pot_profile_activate (vat_main_t *vam)
11885b528e0SShwetha{
11985b528e0SShwetha#define MAX_BITS 64
12085b528e0SShwetha    unformat_input_t *input = vam->input;
12185b528e0SShwetha    vl_api_pot_profile_activate_t *mp;
12285b528e0SShwetha    u8 *name = NULL;
12385b528e0SShwetha    u8 id = 0;
12485b528e0SShwetha    int rv = 0;
12556c7b01eSJon Loeliger    int ret;
12621a43846SDave Barach
12785b528e0SShwetha    while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
12885b528e0SShwetha      {
12985b528e0SShwetha        if (unformat(input, "name %s", &name))
13085b528e0SShwetha	  ;
13185b528e0SShwetha	else if(unformat(input, "id %d", &id))
13285b528e0SShwetha	  ;
13385b528e0SShwetha        else
13485b528e0SShwetha  	break;
13585b528e0SShwetha      }
13685b528e0SShwetha
13785b528e0SShwetha    if (!name)
13885b528e0SShwetha      {
13985b528e0SShwetha        errmsg ("name required\n");
14085b528e0SShwetha        rv = -99;
14185b528e0SShwetha        goto OUT;
14285b528e0SShwetha      }
14321a43846SDave Barach
1440fa66d61SOle Troan    M2(POT_PROFILE_ACTIVATE, mp, sizeof(vl_api_string_t) + vec_len(name));
1452dbee936SJakub Grajciar    vl_api_vec_to_api_string(name, &mp->list_name);
14685b528e0SShwetha    mp->id = id;
14721a43846SDave Barach
1487bc770ceSJon Loeliger    S(mp);
14956c7b01eSJon Loeliger    W (ret);
15056c7b01eSJon Loeliger    return ret;
15121a43846SDave Barach
15285b528e0SShwethaOUT:
15385b528e0SShwetha    vec_free(name);
15485b528e0SShwetha    return(rv);
15585b528e0SShwetha}
15685b528e0SShwetha
15785b528e0SShwetha
15885b528e0SShwethastatic int api_pot_profile_del (vat_main_t *vam)
15985b528e0SShwetha{
16085b528e0SShwetha    vl_api_pot_profile_del_t *mp;
16156c7b01eSJon Loeliger    int ret;
16221a43846SDave Barach
1638a2aea3fSJon Loeliger    M(POT_PROFILE_DEL, mp);
1647bc770ceSJon Loeliger    S(mp);
16556c7b01eSJon Loeliger    W (ret);
16656c7b01eSJon Loeliger    return ret;
16785b528e0SShwetha}
16885b528e0SShwetha
169b4bc55b9SSagar Srivastavstatic int api_pot_profile_show_config_dump (vat_main_t *vam)
170b4bc55b9SSagar Srivastav{
171b4bc55b9SSagar Srivastav    unformat_input_t *input = vam->input;
172b4bc55b9SSagar Srivastav    vl_api_pot_profile_show_config_dump_t *mp;
173b4bc55b9SSagar Srivastav    u8 id = 0;
17456c7b01eSJon Loeliger    int ret;
175b4bc55b9SSagar Srivastav
176b4bc55b9SSagar Srivastav    while(unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
177b4bc55b9SSagar Srivastav    {
178b4bc55b9SSagar Srivastav      if(unformat(input,"id %d",&id));
179b4bc55b9SSagar Srivastav      else
180b4bc55b9SSagar Srivastav        break;
181b4bc55b9SSagar Srivastav    }
1828a2aea3fSJon Loeliger    M(POT_PROFILE_SHOW_CONFIG_DUMP, mp);
183b4bc55b9SSagar Srivastav
184b4bc55b9SSagar Srivastav    mp->id = id;
185b4bc55b9SSagar Srivastav
1867bc770ceSJon Loeliger    S(mp);
18756c7b01eSJon Loeliger    W (ret);
18856c7b01eSJon Loeliger    return ret;
189b4bc55b9SSagar Srivastav}
1900fa66d61SOle Troanstatic int vl_api_pot_profile_show_config_details_t_handler (vat_main_t *vam)
19185b528e0SShwetha{
1920fa66d61SOle Troan  return -1;
19385b528e0SShwetha}
19485b528e0SShwetha
1950fa66d61SOle Troan/* Override generated plugin register symbol */
1960fa66d61SOle Troan#define vat_plugin_register pot_vat_plugin_register
1970fa66d61SOle Troan#include <ioam/lib-pot/pot.api_test.c>
198