summaryrefslogtreecommitdiff
path: root/shaping/test/gtest_shaper_maat.cpp
blob: 972d5502b573a80ea2713a45959037e792df6218 (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <cstring>
#include <gtest/gtest.h>

#include "stub.h"
#include "shaper_maat.h"

TEST(shaping_rule, parse)
{
	const char *data = "{\"compile_id\": 182,\
                            \"service\": 2,\
                            \"action\": 32,\
                            \"do_blacklist\": 0,\
                            \"do_log\": 1,\
                            \"effective_rage\": 0,\
                            \"user_region\": {\"priority\":1,\"fair_factor\":10,\"profile_chain\":[1,2,3]},\
                            \"is_valid\": \"yes\"\
                        }";

    struct shaping_rule *s_rule = NULL;
    struct shaping_rule *s_rule_dup = NULL;

    shaper_rule_ex_new("TRAFFIC_SHAPING_COMPILE", 0, NULL, data, (void**)&s_rule, 0, NULL);
    EXPECT_EQ(s_rule->id, 182);
    EXPECT_EQ(s_rule->primary_pf_id, 1);
    EXPECT_EQ(s_rule->borrow_pf_num, 2);
    EXPECT_EQ(s_rule->borrow_pf_id_array[0], 2);
    EXPECT_EQ(s_rule->borrow_pf_id_array[1], 3);
    EXPECT_EQ(s_rule->priority, 1);
    EXPECT_EQ(s_rule->ref_cnt, 1);

    shaper_rule_ex_dup(0, (void**)&s_rule_dup, (void**)&s_rule, 0, NULL);
    EXPECT_EQ(s_rule, s_rule_dup);
    EXPECT_EQ(s_rule->ref_cnt, 2);

    shaper_rule_ex_free(0, (void**)&s_rule, 0, NULL);
    shaper_rule_ex_free(0, (void**)&s_rule, 0, NULL);
    EXPECT_TRUE(s_rule == NULL);
}

TEST(shaping_profile, parse)
{   const char *data = "1\t\
                        {\"value\":\"local_host\",\"host_fairness\":1}\t\
                        [{\"direction\":\"incoming\",\"bandwidth\":1024},{\"direction\":\"outgoing\",\"bandwidth\":2048}]\t\
                        {\"enabled\":1,\"algorithm\":\"codel\"}\t\
                        null\t\
                        {}\t\
                        1";
    struct shaping_profile *s_pf = NULL;
    struct shaping_profile *s_pf_dup = NULL;

    shaper_profile_ex_new("TRAFFIC_SHAPING_PROFILE", 0, NULL, data, (void**)&s_pf, 0, NULL);
    EXPECT_EQ(s_pf->id, 1);
    EXPECT_EQ(s_pf->in_limit_bandwidth, 1024);
    EXPECT_EQ(s_pf->out_limit_bandwidth, 2048);
    EXPECT_EQ(s_pf->ref_cnt, 1);

    shaper_profile_ex_dup(0, (void**)&s_pf_dup, (void**)&s_pf, 0, NULL);
    EXPECT_EQ(s_pf, s_pf_dup);
    EXPECT_EQ(s_pf->ref_cnt, 2);

    shaper_profile_ex_free(0, (void**)&s_pf, 0, NULL);
    shaper_profile_ex_free(0, (void**)&s_pf, 0, NULL);
    EXPECT_TRUE(s_pf == NULL);
}

TEST(shaping_flow, update_rule)
{
    struct shaping_thread_ctx ctx;
    struct shaping_flow sf;
    struct shaping_rule_info *rule_info;
    long long rule_ids[] = {1, 2, 3};
    int prioritys[] = {1, 2, 3};
    int profile_nums[] = {1, 2, 3};
    int profile_ids[][MAX_REF_PROFILE] = {{1}, {2, 3}, {4, 5, 6}};

    stub_init();

    stub_set_matched_shaping_rules(3, rule_ids, prioritys, profile_nums, profile_ids);

    ctx.maat_info = (struct shaping_maat_info *)calloc(1, sizeof(struct shaping_maat_info));
    ctx.maat_info->rule_table_id = STUB_MAAT_SHAPING_RULE_TABLE_ID;
    ctx.maat_info->profile_table_id = STUB_MAAT_SHAPING_PROFILE_TABLE_ID;
    memset(&sf, 0, sizeof(sf));
    shaper_rules_update(&ctx, &sf, rule_ids, 3);

    EXPECT_EQ(sf.rule_num, 3);

    rule_info = &sf.matched_rule_infos[0];
    EXPECT_EQ(rule_info->id, 1);
    EXPECT_EQ(rule_info->primary.id, 1);
    EXPECT_EQ(rule_info->primary.priority, 1);
    EXPECT_EQ(rule_info->borrowing_num, 0);

    rule_info = &sf.matched_rule_infos[1];
    EXPECT_EQ(rule_info->id, 2);
    EXPECT_EQ(rule_info->primary.id, 2);
    EXPECT_EQ(rule_info->primary.priority, 2);
    EXPECT_EQ(rule_info->borrowing_num, 1);
    EXPECT_EQ(rule_info->borrowing[0].id, 3);
    EXPECT_EQ(rule_info->borrowing[0].priority, 3);

    rule_info = &sf.matched_rule_infos[2];
    EXPECT_EQ(rule_info->id, 3);
    EXPECT_EQ(rule_info->primary.id, 4);
    EXPECT_EQ(rule_info->primary.priority, 3);
    EXPECT_EQ(rule_info->borrowing_num, 2);
    EXPECT_EQ(rule_info->borrowing[0].id, 5);
    EXPECT_EQ(rule_info->borrowing[0].priority, 4);
    EXPECT_EQ(rule_info->borrowing[1].id, 6);
    EXPECT_EQ(rule_info->borrowing[1].priority, 5);

    free(ctx.maat_info);
}

int main(int argc, char **argv)
{
	::testing::InitGoogleTest(&argc, argv);
	return RUN_ALL_TESTS();
}