File: test_ht_conn.c

package info (click to toggle)
netproc 0.6.6-0.1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 908 kB
  • sloc: ansic: 7,876; makefile: 101; sh: 12
file content (106 lines) | stat: -rw-r--r-- 2,530 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

#include "unity/unity.h"

#include "../src/connection.c"

// #include "../src/m_error.h"
//
#include <stdlib.h>
#include <time.h>

static connection_t *
create_conn_fake ( void )
{
  connection_t *conn = calloc ( 1, sizeof *conn );

  if ( conn )
    {
      conn->inode = rand ();
      conn->tuple.l3.local.ip = rand ();
      conn->tuple.l3.remote.ip = rand ();
      conn->tuple.l4.local_port = rand () % 0xffff;
      conn->tuple.l4.remote_port = rand () % 0xffff;
      conn->refs_active = 3;
    }

  return conn;
}

#define NUM_CONN 16

static void
test_insert ( void )
{
  for ( int i = 0; i < NUM_CONN; i++ )
    {
      connection_t *conn = create_conn_fake ();
      TEST_ASSERT_NOT_NULL ( conn );

      connection_insert ( conn );
      TEST_ASSERT_EQUAL_INT ( 2 * ( i + 1 ),
                              hashtable_get_nentries ( ht_connections ) );
    }
}

static void
test_search ( void )
{
  connection_t *conn = create_conn_fake ();
  TEST_ASSERT_NOT_NULL ( conn );

  TEST_ASSERT_NULL ( connection_get_by_inode ( conn->inode ) );
  TEST_ASSERT_NULL ( connection_get_by_tuple ( &conn->tuple ) );

  connection_insert ( conn );

  TEST_ASSERT_EQUAL ( 2 * ( NUM_CONN + 1 ),
                      hashtable_get_nentries ( ht_connections ) );

  connection_t *tmp;

  tmp = connection_get_by_inode ( conn->inode );
  TEST_ASSERT_EQUAL_PTR ( conn, tmp );

  tmp = connection_get_by_tuple ( &conn->tuple );
  TEST_ASSERT_EQUAL_PTR ( conn, tmp );
}

static void
test_delete ( void )
{
  hashtable_foreach_remove ( ht_connections, remove_inactives_conns, NULL );
  TEST_ASSERT_EQUAL ( NUM_CONN * 2 + 2,
                      hashtable_get_nentries ( ht_connections ) );

  // removed conns only next update
  hashtable_foreach_remove ( ht_connections, remove_inactives_conns, NULL );
  TEST_ASSERT_EQUAL ( 0, hashtable_get_nentries ( ht_connections ) );
}

static void
test_conn_update ( void )
{
  TEST_ASSERT_TRUE ( connection_update ( TCP | UDP ) );
}

void
test_ht_conn ( void )
{
  srand ( time ( NULL ) );

  TEST_ASSERT_TRUE ( connection_init () );

  test_insert ();
  test_search ();
  test_delete ();

  test_conn_update ();
  size_t hold = hashtable_get_nentries ( ht_connections );
  hashtable_foreach_remove ( ht_connections, remove_inactives_conns, NULL );
  TEST_ASSERT_EQUAL ( hold, hashtable_get_nentries ( ht_connections ) );

  hashtable_foreach_remove ( ht_connections, remove_inactives_conns, NULL );
  TEST_ASSERT_EQUAL ( 0, hashtable_get_nentries ( ht_connections ) );

  connection_free ();
}