~jamesodhunt/ubuntu/vivid/upstart/bug-1447756

« back to all changes in this revision

Viewing changes to init/tests/test_parse_job.c

  • Committer: Dimitri John Ledkov
  • Date: 2014-07-14 12:48:09 UTC
  • mfrom: (1436.2.6)
  • Revision ID: dimitri.ledkov@canonical.com-20140714124809-qgpmf0cj37x2b2we
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
#include "errors.h"
39
39
#include "apparmor.h"
40
40
 
 
41
#ifdef ENABLE_CGROUPS
 
42
 
 
43
#include "cgroup.h"
 
44
 
 
45
#endif /* ENABLE_CGROUPS */
 
46
 
 
47
#include "test_util_common.h"
 
48
 
41
49
 
42
50
void
43
51
test_parse_job (void)
8818
8826
        nih_free (err);
8819
8827
}
8820
8828
 
 
8829
#ifdef ENABLE_CGROUPS
 
8830
 
 
8831
void
 
8832
test_stanza_cgroup (void)
 
8833
{
 
8834
        JobClass       *job;
 
8835
        NihError       *err;
 
8836
        CGroup         *cgroup;
 
8837
        CGroupName     *cgname;
 
8838
        CGroupSetting  *setting;
 
8839
        size_t          pos;
 
8840
        size_t          lineno;
 
8841
        char            buf[1024];
 
8842
        int             len;
 
8843
        size_t          count;
 
8844
 
 
8845
        TEST_FUNCTION ("stanza_cgroup");
 
8846
 
 
8847
        TEST_FEATURE ("no arguments rejected");
 
8848
 
 
8849
        pos = 0;
 
8850
        lineno = 1;
 
8851
        len = sprintf (buf, "cgroup\n");
 
8852
 
 
8853
        job = parse_job (NULL, NULL, NULL, "test", buf, len, &pos, &lineno);
 
8854
        TEST_EQ_P (job, NULL);
 
8855
 
 
8856
        err = nih_error_get ();
 
8857
        TEST_EQ (err->number, NIH_CONFIG_EXPECTED_TOKEN);
 
8858
 
 
8859
        /* Don't count NL */
 
8860
        TEST_EQ (pos, (size_t)len-1);
 
8861
 
 
8862
        TEST_EQ (lineno, 1);
 
8863
        nih_free (err);
 
8864
 
 
8865
        TEST_FEATURE ("single argument (controller) accepted");
 
8866
 
 
8867
        TEST_ALLOC_FAIL {
 
8868
                pos = 0;
 
8869
                lineno = 1;
 
8870
                len = sprintf (buf, "cgroup perf_event\n");
 
8871
                job = parse_job (NULL, NULL, NULL, "test", buf, len, &pos, &lineno);
 
8872
 
 
8873
                if (test_alloc_failed) {
 
8874
                        TEST_EQ_P (job, NULL);
 
8875
 
 
8876
                        err = nih_error_get ();
 
8877
                        TEST_EQ (err->number, ENOMEM);
 
8878
                        nih_free (err);
 
8879
 
 
8880
                        continue;
 
8881
                }
 
8882
 
 
8883
                TEST_NE_P (job, NULL);
 
8884
 
 
8885
                TEST_LIST_NOT_EMPTY (&job->cgroups);
 
8886
                count = test_list_count (&job->cgroups);
 
8887
                TEST_EQ (count, 1);
 
8888
 
 
8889
                cgroup = (CGroup *)test_list_get_index (&job->cgroups, 0);
 
8890
                TEST_NE_P (cgroup, NULL);
 
8891
                TEST_ALLOC_PARENT (cgroup, job);
 
8892
                TEST_ALLOC_SIZE (cgroup, sizeof (CGroup));
 
8893
                TEST_EQ_STR (cgroup->controller, "perf_event");
 
8894
                TEST_ALLOC_PARENT (cgroup->controller, cgroup);
 
8895
                TEST_ALLOC_SIZE (cgroup->controller, 1+strlen ("perf_event"));
 
8896
 
 
8897
                count = test_list_count (&cgroup->names);
 
8898
                TEST_EQ (count, 1);
 
8899
 
 
8900
                cgname = (CGroupName *)test_list_get_index (&cgroup->names, 0);
 
8901
                TEST_NE_P (cgname, NULL);
 
8902
 
 
8903
                TEST_ALLOC_PARENT (cgname, cgroup);
 
8904
                TEST_ALLOC_SIZE (cgname, sizeof (CGroupName));
 
8905
                TEST_EQ_STR (cgname->name, "$UPSTART_CGROUP");
 
8906
                TEST_ALLOC_PARENT (cgname->name, cgname);
 
8907
                TEST_ALLOC_SIZE (cgname->name, 1+strlen ("$UPSTART_CGROUP"));
 
8908
 
 
8909
                TEST_LIST_EMPTY (&cgname->settings);
 
8910
                TEST_EQ (pos, (size_t)len);
 
8911
                TEST_EQ (lineno, 2);
 
8912
 
 
8913
                nih_free (job);
 
8914
        }
 
8915
 
 
8916
        TEST_FEATURE ("2 arguments (controller + unquoted name) accepted");
 
8917
 
 
8918
        TEST_ALLOC_FAIL {
 
8919
                pos = 0;
 
8920
                lineno = 1;
 
8921
                len = sprintf (buf, "cgroup perf_event foo\n");
 
8922
 
 
8923
                job = parse_job (NULL, NULL, NULL, "test", buf, len, &pos, &lineno);
 
8924
 
 
8925
                if (test_alloc_failed) {
 
8926
                        TEST_EQ_P (job, NULL);
 
8927
 
 
8928
                        err = nih_error_get ();
 
8929
                        TEST_EQ (err->number, ENOMEM);
 
8930
                        nih_free (err);
 
8931
 
 
8932
                        continue;
 
8933
                }
 
8934
 
 
8935
                TEST_NE_P (job, NULL);
 
8936
                TEST_LIST_NOT_EMPTY (&job->cgroups);
 
8937
                count = test_list_count (&job->cgroups);
 
8938
                TEST_EQ (count, 1);
 
8939
 
 
8940
                cgroup = (CGroup *)test_list_get_index (&job->cgroups, 0);
 
8941
                TEST_NE_P (cgroup, NULL);
 
8942
                TEST_ALLOC_PARENT (cgroup, job);
 
8943
                TEST_ALLOC_SIZE (cgroup, sizeof (CGroup));
 
8944
                TEST_EQ_STR (cgroup->controller, "perf_event");
 
8945
                TEST_ALLOC_PARENT (cgroup->controller, cgroup);
 
8946
                TEST_ALLOC_SIZE (cgroup->controller, 1+strlen ("perf_event"));
 
8947
 
 
8948
                count = test_list_count (&cgroup->names);
 
8949
                TEST_EQ (count, 1);
 
8950
 
 
8951
                cgname = (CGroupName *)test_list_get_index (&cgroup->names, 0);
 
8952
                TEST_NE_P (cgname, NULL);
 
8953
 
 
8954
                TEST_ALLOC_PARENT (cgname, cgroup);
 
8955
                TEST_ALLOC_SIZE (cgname, sizeof (CGroupName));
 
8956
                TEST_EQ_STR (cgname->name, "foo");
 
8957
                TEST_ALLOC_PARENT (cgname->name, cgname);
 
8958
                TEST_ALLOC_SIZE (cgname->name, 1+strlen ("foo"));
 
8959
 
 
8960
                TEST_LIST_EMPTY (&cgname->settings);
 
8961
                TEST_EQ (pos, (size_t)len);
 
8962
                TEST_EQ (lineno, 2);
 
8963
 
 
8964
                nih_free (job);
 
8965
        }
 
8966
 
 
8967
        TEST_FEATURE ("2 arguments (controller + quoted name) accepted");
 
8968
 
 
8969
        TEST_ALLOC_FAIL {
 
8970
                pos = 0;
 
8971
                lineno = 1;
 
8972
                len = sprintf (buf, "cgroup perf_event \"foo bar\"\n");
 
8973
 
 
8974
                job = parse_job (NULL, NULL, NULL, "test", buf, len, &pos, &lineno);
 
8975
 
 
8976
                if (test_alloc_failed) {
 
8977
                        TEST_EQ_P (job, NULL);
 
8978
 
 
8979
                        err = nih_error_get ();
 
8980
                        TEST_EQ (err->number, ENOMEM);
 
8981
                        nih_free (err);
 
8982
 
 
8983
                        continue;
 
8984
                }
 
8985
 
 
8986
                TEST_NE_P (job, NULL);
 
8987
                TEST_LIST_NOT_EMPTY (&job->cgroups);
 
8988
                count = test_list_count (&job->cgroups);
 
8989
                TEST_EQ (count, 1);
 
8990
 
 
8991
                cgroup = (CGroup *)test_list_get_index (&job->cgroups, 0);
 
8992
                TEST_NE_P (cgroup, NULL);
 
8993
                TEST_ALLOC_PARENT (cgroup, job);
 
8994
                TEST_ALLOC_SIZE (cgroup, sizeof (CGroup));
 
8995
                TEST_EQ_STR (cgroup->controller, "perf_event");
 
8996
                TEST_ALLOC_PARENT (cgroup->controller, cgroup);
 
8997
                TEST_ALLOC_SIZE (cgroup->controller, 1+strlen ("perf_event"));
 
8998
 
 
8999
                count = test_list_count (&cgroup->names);
 
9000
                TEST_EQ (count, 1);
 
9001
 
 
9002
                cgname = (CGroupName *)test_list_get_index (&cgroup->names, 0);
 
9003
                TEST_NE_P (cgname, NULL);
 
9004
 
 
9005
                TEST_ALLOC_PARENT (cgname, cgroup);
 
9006
                TEST_ALLOC_SIZE (cgname, sizeof (CGroupName));
 
9007
                TEST_EQ_STR (cgname->name, "foo bar");
 
9008
                TEST_ALLOC_PARENT (cgname->name, cgname);
 
9009
                TEST_ALLOC_SIZE (cgname->name, 1+strlen ("foo bar"));
 
9010
 
 
9011
                TEST_LIST_EMPTY (&cgname->settings);
 
9012
                TEST_EQ (pos, (size_t)len);
 
9013
                TEST_EQ (lineno, 2);
 
9014
 
 
9015
                nih_free (job);
 
9016
        }
 
9017
 
 
9018
        TEST_FEATURE ("3 arguments (controller, key and value) accepted");
 
9019
 
 
9020
        TEST_ALLOC_FAIL {
 
9021
                pos = 0;
 
9022
                lineno = 1;
 
9023
                len = sprintf (buf, "cgroup perf_event key1 value1\n");
 
9024
 
 
9025
                job = parse_job (NULL, NULL, NULL, "test", buf, len, &pos, &lineno);
 
9026
 
 
9027
                if (test_alloc_failed) {
 
9028
                        TEST_EQ_P (job, NULL);
 
9029
 
 
9030
                        err = nih_error_get ();
 
9031
                        TEST_EQ (err->number, ENOMEM);
 
9032
                        nih_free (err);
 
9033
 
 
9034
                        continue;
 
9035
                }
 
9036
 
 
9037
                TEST_NE_P (job, NULL);
 
9038
                TEST_LIST_NOT_EMPTY (&job->cgroups);
 
9039
                count = test_list_count (&job->cgroups);
 
9040
                TEST_EQ (count, 1);
 
9041
 
 
9042
                cgroup = (CGroup *)test_list_get_index (&job->cgroups, 0);
 
9043
                TEST_NE_P (cgroup, NULL);
 
9044
                TEST_ALLOC_PARENT (cgroup, job);
 
9045
                TEST_ALLOC_SIZE (cgroup, sizeof (CGroup));
 
9046
                TEST_EQ_STR (cgroup->controller, "perf_event");
 
9047
                TEST_ALLOC_PARENT (cgroup->controller, cgroup);
 
9048
                TEST_ALLOC_SIZE (cgroup->controller, 1+strlen ("perf_event"));
 
9049
 
 
9050
                count = test_list_count (&cgroup->names);
 
9051
                TEST_EQ (count, 1);
 
9052
 
 
9053
                cgname = (CGroupName *)test_list_get_index (&cgroup->names, 0);
 
9054
                TEST_NE_P (cgname, NULL);
 
9055
 
 
9056
                TEST_ALLOC_PARENT (cgname, cgroup);
 
9057
                TEST_ALLOC_SIZE (cgname, sizeof (CGroupName));
 
9058
                TEST_EQ_STR (cgname->name, "$UPSTART_CGROUP");
 
9059
                TEST_ALLOC_PARENT (cgname->name, cgname);
 
9060
                TEST_ALLOC_SIZE (cgname->name, 1+strlen ("$UPSTART_CGROUP"));
 
9061
 
 
9062
                TEST_LIST_NOT_EMPTY (&cgname->settings);
 
9063
 
 
9064
                count = test_list_count (&cgname->settings);
 
9065
                TEST_EQ (count, 1);
 
9066
 
 
9067
                setting = (CGroupSetting *)test_list_get_index (&cgname->settings, 0);
 
9068
                TEST_NE_P (setting, NULL);
 
9069
 
 
9070
                TEST_ALLOC_PARENT (setting, cgname);
 
9071
                TEST_ALLOC_SIZE (setting, sizeof (CGroupSetting));
 
9072
 
 
9073
                TEST_EQ_STR (setting->key, "key1");
 
9074
                TEST_ALLOC_PARENT (setting->key, setting);
 
9075
                TEST_ALLOC_SIZE (setting->key, 1+strlen ("key1"));
 
9076
 
 
9077
                TEST_EQ_STR (setting->value, "value1");
 
9078
                TEST_ALLOC_PARENT (setting->value, setting);
 
9079
                TEST_ALLOC_SIZE (setting->value, 1+strlen ("value1"));
 
9080
 
 
9081
                TEST_EQ (pos, (size_t)len);
 
9082
                TEST_EQ (lineno, 2);
 
9083
 
 
9084
                nih_free (job);
 
9085
        }
 
9086
 
 
9087
        TEST_FEATURE ("4 arguments (controller + unquoted name, key and value) accepted");
 
9088
 
 
9089
        TEST_ALLOC_FAIL {
 
9090
                pos = 0;
 
9091
                lineno = 1;
 
9092
                len = sprintf (buf, "cgroup perf_event foo key1 value1\n");
 
9093
 
 
9094
                job = parse_job (NULL, NULL, NULL, "test", buf, len, &pos, &lineno);
 
9095
 
 
9096
                if (test_alloc_failed) {
 
9097
                        TEST_EQ_P (job, NULL);
 
9098
 
 
9099
                        err = nih_error_get ();
 
9100
                        TEST_EQ (err->number, ENOMEM);
 
9101
                        nih_free (err);
 
9102
 
 
9103
                        continue;
 
9104
                }
 
9105
 
 
9106
                TEST_NE_P (job, NULL);
 
9107
                TEST_LIST_NOT_EMPTY (&job->cgroups);
 
9108
                count = test_list_count (&job->cgroups);
 
9109
                TEST_EQ (count, 1);
 
9110
 
 
9111
                cgroup = (CGroup *)test_list_get_index (&job->cgroups, 0);
 
9112
                TEST_NE_P (cgroup, NULL);
 
9113
                TEST_ALLOC_PARENT (cgroup, job);
 
9114
                TEST_ALLOC_SIZE (cgroup, sizeof (CGroup));
 
9115
                TEST_EQ_STR (cgroup->controller, "perf_event");
 
9116
                TEST_ALLOC_PARENT (cgroup->controller, cgroup);
 
9117
                TEST_ALLOC_SIZE (cgroup->controller, 1+strlen ("perf_event"));
 
9118
 
 
9119
                count = test_list_count (&cgroup->names);
 
9120
                TEST_EQ (count, 1);
 
9121
 
 
9122
                cgname = (CGroupName *)test_list_get_index (&cgroup->names, 0);
 
9123
                TEST_NE_P (cgname, NULL);
 
9124
 
 
9125
                TEST_ALLOC_PARENT (cgname, cgroup);
 
9126
                TEST_ALLOC_SIZE (cgname, sizeof (CGroupName));
 
9127
                TEST_EQ_STR (cgname->name, "foo");
 
9128
                TEST_ALLOC_PARENT (cgname->name, cgname);
 
9129
                TEST_ALLOC_SIZE (cgname->name, 1+strlen ("foo"));
 
9130
 
 
9131
                TEST_LIST_NOT_EMPTY (&cgname->settings);
 
9132
 
 
9133
                count = test_list_count (&cgname->settings);
 
9134
                TEST_EQ (count, 1);
 
9135
 
 
9136
                setting = (CGroupSetting *)test_list_get_index (&cgname->settings, 0);
 
9137
                TEST_NE_P (setting, NULL);
 
9138
 
 
9139
                TEST_ALLOC_PARENT (setting, cgname);
 
9140
                TEST_ALLOC_SIZE (setting, sizeof (CGroupSetting));
 
9141
 
 
9142
                TEST_EQ_STR (setting->key, "key1");
 
9143
                TEST_ALLOC_PARENT (setting->key, setting);
 
9144
                TEST_ALLOC_SIZE (setting->key, 1+strlen ("key1"));
 
9145
 
 
9146
                TEST_EQ_STR (setting->value, "value1");
 
9147
                TEST_ALLOC_PARENT (setting->value, setting);
 
9148
                TEST_ALLOC_SIZE (setting->value, 1+strlen ("value1"));
 
9149
 
 
9150
                TEST_EQ (pos, (size_t)len);
 
9151
                TEST_EQ (lineno, 2);
 
9152
 
 
9153
                nih_free (job);
 
9154
        }
 
9155
 
 
9156
        TEST_FEATURE ("4 arguments (controller + quoted name, unquoted key and quoted value) accepted");
 
9157
 
 
9158
        TEST_ALLOC_FAIL {
 
9159
                pos = 0;
 
9160
                lineno = 1;
 
9161
                len = sprintf (buf, "cgroup perf_event \"a silly name\" key1 \"hello world\"\n");
 
9162
 
 
9163
                job = parse_job (NULL, NULL, NULL, "test", buf, len, &pos, &lineno);
 
9164
 
 
9165
                if (test_alloc_failed) {
 
9166
                        TEST_EQ_P (job, NULL);
 
9167
 
 
9168
                        err = nih_error_get ();
 
9169
                        TEST_EQ (err->number, ENOMEM);
 
9170
                        nih_free (err);
 
9171
 
 
9172
                        continue;
 
9173
                }
 
9174
 
 
9175
                TEST_NE_P (job, NULL);
 
9176
                TEST_LIST_NOT_EMPTY (&job->cgroups);
 
9177
                count = test_list_count (&job->cgroups);
 
9178
                TEST_EQ (count, 1);
 
9179
 
 
9180
                cgroup = (CGroup *)test_list_get_index (&job->cgroups, 0);
 
9181
                TEST_NE_P (cgroup, NULL);
 
9182
                TEST_ALLOC_PARENT (cgroup, job);
 
9183
                TEST_ALLOC_SIZE (cgroup, sizeof (CGroup));
 
9184
                TEST_EQ_STR (cgroup->controller, "perf_event");
 
9185
                TEST_ALLOC_PARENT (cgroup->controller, cgroup);
 
9186
                TEST_ALLOC_SIZE (cgroup->controller, 1+strlen ("perf_event"));
 
9187
 
 
9188
                count = test_list_count (&cgroup->names);
 
9189
                TEST_EQ (count, 1);
 
9190
 
 
9191
                cgname = (CGroupName *)test_list_get_index (&cgroup->names, 0);
 
9192
                TEST_NE_P (cgname, NULL);
 
9193
 
 
9194
                TEST_ALLOC_PARENT (cgname, cgroup);
 
9195
                TEST_ALLOC_SIZE (cgname, sizeof (CGroupName));
 
9196
                TEST_EQ_STR (cgname->name, "a silly name");
 
9197
                TEST_ALLOC_PARENT (cgname->name, cgname);
 
9198
                TEST_ALLOC_SIZE (cgname->name, 1+strlen ("foo"));
 
9199
 
 
9200
                TEST_LIST_NOT_EMPTY (&cgname->settings);
 
9201
 
 
9202
                count = test_list_count (&cgname->settings);
 
9203
                TEST_EQ (count, 1);
 
9204
 
 
9205
                setting = (CGroupSetting *)test_list_get_index (&cgname->settings, 0);
 
9206
                TEST_NE_P (setting, NULL);
 
9207
 
 
9208
                TEST_ALLOC_PARENT (setting, cgname);
 
9209
                TEST_ALLOC_SIZE (setting, sizeof (CGroupSetting));
 
9210
 
 
9211
                TEST_EQ_STR (setting->key, "key1");
 
9212
                TEST_ALLOC_PARENT (setting->key, setting);
 
9213
                TEST_ALLOC_SIZE (setting->key, 1+strlen ("key1"));
 
9214
 
 
9215
                TEST_EQ_STR (setting->value, "hello world");
 
9216
                TEST_ALLOC_PARENT (setting->value, setting);
 
9217
                TEST_ALLOC_SIZE (setting->value, 1+strlen ("hello world"));
 
9218
 
 
9219
                TEST_EQ (pos, (size_t)len);
 
9220
                TEST_EQ (lineno, 2);
 
9221
 
 
9222
                nih_free (job);
 
9223
        }
 
9224
 
 
9225
        /* FIXME: variables are only expanded on job start so cannot
 
9226
         * validate.
 
9227
         */
 
9228
        TEST_FEATURE ("name with embedded variable is accepted");
 
9229
 
 
9230
        TEST_ALLOC_FAIL {
 
9231
                pos = 0;
 
9232
                lineno = 1;
 
9233
                len = sprintf (buf, "cgroup perf_event \"$VARIABLE\" key value\n");
 
9234
 
 
9235
                job = parse_job (NULL, NULL, NULL, "test", buf, len, &pos, &lineno);
 
9236
 
 
9237
                if (test_alloc_failed) {
 
9238
                        TEST_EQ_P (job, NULL);
 
9239
 
 
9240
                        err = nih_error_get ();
 
9241
                        TEST_EQ (err->number, ENOMEM);
 
9242
                        nih_free (err);
 
9243
 
 
9244
                        continue;
 
9245
                }
 
9246
 
 
9247
                TEST_NE_P (job, NULL);
 
9248
                TEST_LIST_NOT_EMPTY (&job->cgroups);
 
9249
                count = test_list_count (&job->cgroups);
 
9250
                TEST_EQ (count, 1);
 
9251
 
 
9252
                cgroup = (CGroup *)test_list_get_index (&job->cgroups, 0);
 
9253
                TEST_NE_P (cgroup, NULL);
 
9254
                TEST_ALLOC_PARENT (cgroup, job);
 
9255
                TEST_ALLOC_SIZE (cgroup, sizeof (CGroup));
 
9256
                TEST_EQ_STR (cgroup->controller, "perf_event");
 
9257
                TEST_ALLOC_PARENT (cgroup->controller, cgroup);
 
9258
                TEST_ALLOC_SIZE (cgroup->controller, 1+strlen ("perf_event"));
 
9259
 
 
9260
                count = test_list_count (&cgroup->names);
 
9261
                TEST_EQ (count, 1);
 
9262
 
 
9263
                cgname = (CGroupName *)test_list_get_index (&cgroup->names, 0);
 
9264
                TEST_NE_P (cgname, NULL);
 
9265
 
 
9266
                TEST_ALLOC_PARENT (cgname, cgroup);
 
9267
                TEST_ALLOC_SIZE (cgname, sizeof (CGroupName));
 
9268
                TEST_EQ_STR (cgname->name, "$VARIABLE");
 
9269
                TEST_ALLOC_PARENT (cgname->name, cgname);
 
9270
                TEST_ALLOC_SIZE (cgname->name, 1+strlen ("$VARIABLE"));
 
9271
 
 
9272
                TEST_LIST_NOT_EMPTY (&cgname->settings);
 
9273
 
 
9274
                count = test_list_count (&cgname->settings);
 
9275
                TEST_EQ (count, 1);
 
9276
 
 
9277
                setting = (CGroupSetting *)test_list_get_index (&cgname->settings, 0);
 
9278
                TEST_NE_P (setting, NULL);
 
9279
 
 
9280
                TEST_ALLOC_PARENT (setting, cgname);
 
9281
                TEST_ALLOC_SIZE (setting, sizeof (CGroupSetting));
 
9282
 
 
9283
                TEST_EQ_STR (setting->key, "key");
 
9284
                TEST_ALLOC_PARENT (setting->key, setting);
 
9285
                TEST_ALLOC_SIZE (setting->key, 1+strlen ("key"));
 
9286
 
 
9287
                TEST_EQ_STR (setting->value, "value");
 
9288
                TEST_ALLOC_PARENT (setting->value, setting);
 
9289
                TEST_ALLOC_SIZE (setting->value, 1+strlen ("value"));
 
9290
 
 
9291
                TEST_EQ (pos, (size_t)len);
 
9292
                TEST_EQ (lineno, 2);
 
9293
 
 
9294
                nih_free (job);
 
9295
        }
 
9296
 
 
9297
        TEST_FEATURE ("duplicate stanza is ignored");
 
9298
 
 
9299
        len = sprintf (buf,
 
9300
                        "cgroup perf_event foo key1 value1\n"
 
9301
                        "cgroup perf_event foo key1 value1\n");
 
9302
 
 
9303
        TEST_ALLOC_FAIL {
 
9304
                pos = 0;
 
9305
                lineno = 1;
 
9306
 
 
9307
                job = parse_job (NULL, NULL, NULL, "test", buf, len, &pos, &lineno);
 
9308
 
 
9309
                if (test_alloc_failed) {
 
9310
                        TEST_EQ_P (job, NULL);
 
9311
 
 
9312
                        err = nih_error_get ();
 
9313
                        TEST_EQ (err->number, ENOMEM);
 
9314
                        nih_free (err);
 
9315
 
 
9316
                        continue;
 
9317
                }
 
9318
 
 
9319
                TEST_NE_P (job, NULL);
 
9320
                TEST_LIST_NOT_EMPTY (&job->cgroups);
 
9321
                count = test_list_count (&job->cgroups);
 
9322
                TEST_EQ (count, 1);
 
9323
 
 
9324
                cgroup = (CGroup *)test_list_get_index (&job->cgroups, 0);
 
9325
                TEST_NE_P (cgroup, NULL);
 
9326
                TEST_ALLOC_PARENT (cgroup, job);
 
9327
                TEST_ALLOC_SIZE (cgroup, sizeof (CGroup));
 
9328
                TEST_EQ_STR (cgroup->controller, "perf_event");
 
9329
                TEST_ALLOC_PARENT (cgroup->controller, cgroup);
 
9330
                TEST_ALLOC_SIZE (cgroup->controller, 1+strlen ("perf_event"));
 
9331
 
 
9332
                count = test_list_count (&cgroup->names);
 
9333
                TEST_EQ (count, 1);
 
9334
 
 
9335
                cgname = (CGroupName *)test_list_get_index (&cgroup->names, 0);
 
9336
                TEST_NE_P (cgname, NULL);
 
9337
 
 
9338
                TEST_ALLOC_PARENT (cgname, cgroup);
 
9339
                TEST_ALLOC_SIZE (cgname, sizeof (CGroupName));
 
9340
                TEST_EQ_STR (cgname->name, "foo");
 
9341
                TEST_ALLOC_PARENT (cgname->name, cgname);
 
9342
                TEST_ALLOC_SIZE (cgname->name, 1+strlen ("foo"));
 
9343
 
 
9344
                TEST_LIST_NOT_EMPTY (&cgname->settings);
 
9345
 
 
9346
                count = test_list_count (&cgname->settings);
 
9347
                TEST_EQ (count, 1);
 
9348
 
 
9349
                setting = (CGroupSetting *)test_list_get_index (&cgname->settings, 0);
 
9350
                TEST_NE_P (setting, NULL);
 
9351
 
 
9352
                TEST_ALLOC_PARENT (setting, cgname);
 
9353
                TEST_ALLOC_SIZE (setting, sizeof (CGroupSetting));
 
9354
 
 
9355
                TEST_EQ_STR (setting->key, "key1");
 
9356
                TEST_ALLOC_PARENT (setting->key, setting);
 
9357
                TEST_ALLOC_SIZE (setting->key, 1+strlen ("key1"));
 
9358
 
 
9359
                TEST_EQ_STR (setting->value, "value1");
 
9360
                TEST_ALLOC_PARENT (setting->value, setting);
 
9361
                TEST_ALLOC_SIZE (setting->value, 1+strlen ("value1"));
 
9362
 
 
9363
                TEST_EQ (pos, (size_t)len);
 
9364
                TEST_EQ (lineno, 3);
 
9365
 
 
9366
                nih_free (job);
 
9367
        }
 
9368
 
 
9369
        TEST_FEATURE ("duplicate equivalent stanza is ignored");
 
9370
 
 
9371
        len = sprintf (buf,
 
9372
                        "cgroup perf_event foo \"key1\" value1\n"
 
9373
                        "cgroup perf_event foo key1 \"value1\"\n");
 
9374
 
 
9375
        TEST_ALLOC_FAIL {
 
9376
                pos = 0;
 
9377
                lineno = 1;
 
9378
 
 
9379
                job = parse_job (NULL, NULL, NULL, "test", buf, len, &pos, &lineno);
 
9380
 
 
9381
                if (test_alloc_failed) {
 
9382
                        TEST_EQ_P (job, NULL);
 
9383
 
 
9384
                        err = nih_error_get ();
 
9385
                        TEST_EQ (err->number, ENOMEM);
 
9386
                        nih_free (err);
 
9387
 
 
9388
                        continue;
 
9389
                }
 
9390
 
 
9391
                TEST_NE_P (job, NULL);
 
9392
                TEST_LIST_NOT_EMPTY (&job->cgroups);
 
9393
                count = test_list_count (&job->cgroups);
 
9394
                TEST_EQ (count, 1);
 
9395
 
 
9396
                cgroup = (CGroup *)test_list_get_index (&job->cgroups, 0);
 
9397
                TEST_NE_P (cgroup, NULL);
 
9398
                TEST_ALLOC_PARENT (cgroup, job);
 
9399
                TEST_ALLOC_SIZE (cgroup, sizeof (CGroup));
 
9400
                TEST_EQ_STR (cgroup->controller, "perf_event");
 
9401
                TEST_ALLOC_PARENT (cgroup->controller, cgroup);
 
9402
                TEST_ALLOC_SIZE (cgroup->controller, 1+strlen ("perf_event"));
 
9403
 
 
9404
                count = test_list_count (&cgroup->names);
 
9405
                TEST_EQ (count, 1);
 
9406
 
 
9407
                cgname = (CGroupName *)test_list_get_index (&cgroup->names, 0);
 
9408
                TEST_NE_P (cgname, NULL);
 
9409
 
 
9410
                TEST_ALLOC_PARENT (cgname, cgroup);
 
9411
                TEST_ALLOC_SIZE (cgname, sizeof (CGroupName));
 
9412
                TEST_EQ_STR (cgname->name, "foo");
 
9413
                TEST_ALLOC_PARENT (cgname->name, cgname);
 
9414
                TEST_ALLOC_SIZE (cgname->name, 1+strlen ("foo"));
 
9415
 
 
9416
                TEST_LIST_NOT_EMPTY (&cgname->settings);
 
9417
 
 
9418
                count = test_list_count (&cgname->settings);
 
9419
                TEST_EQ (count, 1);
 
9420
 
 
9421
                setting = (CGroupSetting *)test_list_get_index (&cgname->settings, 0);
 
9422
                TEST_NE_P (setting, NULL);
 
9423
 
 
9424
                TEST_ALLOC_PARENT (setting, cgname);
 
9425
                TEST_ALLOC_SIZE (setting, sizeof (CGroupSetting));
 
9426
 
 
9427
                TEST_EQ_STR (setting->key, "key1");
 
9428
                TEST_ALLOC_PARENT (setting->key, setting);
 
9429
                TEST_ALLOC_SIZE (setting->key, 1+strlen ("key1"));
 
9430
 
 
9431
                TEST_EQ_STR (setting->value, "value1");
 
9432
                TEST_ALLOC_PARENT (setting->value, setting);
 
9433
                TEST_ALLOC_SIZE (setting->value, 1+strlen ("value1"));
 
9434
 
 
9435
                TEST_EQ (pos, (size_t)len);
 
9436
                TEST_EQ (lineno, 3);
 
9437
 
 
9438
                nih_free (job);
 
9439
        }
 
9440
 
 
9441
        TEST_FEATURE ("multiple names per controller are accepted");
 
9442
 
 
9443
        len = sprintf (buf,
 
9444
                        "cgroup perf_event foo key1 value1\n"
 
9445
                        "cgroup perf_event bar key2 value2\n");
 
9446
 
 
9447
        TEST_ALLOC_FAIL {
 
9448
                pos = 0;
 
9449
                lineno = 1;
 
9450
 
 
9451
                job = parse_job (NULL, NULL, NULL, "test", buf, len, &pos, &lineno);
 
9452
 
 
9453
                if (test_alloc_failed) {
 
9454
                        TEST_EQ_P (job, NULL);
 
9455
 
 
9456
                        err = nih_error_get ();
 
9457
                        TEST_EQ (err->number, ENOMEM);
 
9458
                        nih_free (err);
 
9459
 
 
9460
                        continue;
 
9461
                }
 
9462
 
 
9463
                TEST_NE_P (job, NULL);
 
9464
                TEST_LIST_NOT_EMPTY (&job->cgroups);
 
9465
                count = test_list_count (&job->cgroups);
 
9466
                TEST_EQ (count, 1);
 
9467
 
 
9468
                cgroup = (CGroup *)test_list_get_index (&job->cgroups, 0);
 
9469
                TEST_NE_P (cgroup, NULL);
 
9470
                TEST_ALLOC_PARENT (cgroup, job);
 
9471
                TEST_ALLOC_SIZE (cgroup, sizeof (CGroup));
 
9472
                TEST_EQ_STR (cgroup->controller, "perf_event");
 
9473
                TEST_ALLOC_PARENT (cgroup->controller, cgroup);
 
9474
                TEST_ALLOC_SIZE (cgroup->controller, 1+strlen ("perf_event"));
 
9475
 
 
9476
                count = test_list_count (&cgroup->names);
 
9477
                TEST_EQ (count, 2);
 
9478
 
 
9479
                /* first */
 
9480
                cgname = (CGroupName *)test_list_get_index (&cgroup->names, 0);
 
9481
                TEST_NE_P (cgname, NULL);
 
9482
 
 
9483
                TEST_ALLOC_PARENT (cgname, cgroup);
 
9484
                TEST_ALLOC_SIZE (cgname, sizeof (CGroupName));
 
9485
                TEST_EQ_STR (cgname->name, "foo");
 
9486
                TEST_ALLOC_PARENT (cgname->name, cgname);
 
9487
                TEST_ALLOC_SIZE (cgname->name, 1+strlen ("foo"));
 
9488
 
 
9489
                TEST_LIST_NOT_EMPTY (&cgname->settings);
 
9490
 
 
9491
                count = test_list_count (&cgname->settings);
 
9492
                TEST_EQ (count, 1);
 
9493
 
 
9494
                setting = (CGroupSetting *)test_list_get_index (&cgname->settings, 0);
 
9495
                TEST_NE_P (setting, NULL);
 
9496
 
 
9497
                TEST_ALLOC_PARENT (setting, cgname);
 
9498
                TEST_ALLOC_SIZE (setting, sizeof (CGroupSetting));
 
9499
 
 
9500
                TEST_EQ_STR (setting->key, "key1");
 
9501
                TEST_ALLOC_PARENT (setting->key, setting);
 
9502
                TEST_ALLOC_SIZE (setting->key, 1+strlen ("key1"));
 
9503
 
 
9504
                TEST_EQ_STR (setting->value, "value1");
 
9505
                TEST_ALLOC_PARENT (setting->value, setting);
 
9506
                TEST_ALLOC_SIZE (setting->value, 1+strlen ("value1"));
 
9507
 
 
9508
                /* second */
 
9509
                cgname = (CGroupName *)test_list_get_index (&cgroup->names, 1);
 
9510
                TEST_NE_P (cgname, NULL);
 
9511
 
 
9512
                TEST_ALLOC_PARENT (cgname, cgroup);
 
9513
                TEST_ALLOC_SIZE (cgname, sizeof (CGroupName));
 
9514
                TEST_EQ_STR (cgname->name, "bar");
 
9515
                TEST_ALLOC_PARENT (cgname->name, cgname);
 
9516
                TEST_ALLOC_SIZE (cgname->name, 1+strlen ("bar"));
 
9517
 
 
9518
                TEST_LIST_NOT_EMPTY (&cgname->settings);
 
9519
 
 
9520
                count = test_list_count (&cgname->settings);
 
9521
                TEST_EQ (count, 1);
 
9522
 
 
9523
                setting = (CGroupSetting *)test_list_get_index (&cgname->settings, 0);
 
9524
                TEST_NE_P (setting, NULL);
 
9525
 
 
9526
                TEST_ALLOC_PARENT (setting, cgname);
 
9527
                TEST_ALLOC_SIZE (setting, sizeof (CGroupSetting));
 
9528
 
 
9529
                TEST_EQ_STR (setting->key, "key2");
 
9530
                TEST_ALLOC_PARENT (setting->key, setting);
 
9531
                TEST_ALLOC_SIZE (setting->key, 1+strlen ("key2"));
 
9532
 
 
9533
                TEST_EQ_STR (setting->value, "value2");
 
9534
                TEST_ALLOC_PARENT (setting->value, setting);
 
9535
                TEST_ALLOC_SIZE (setting->value, 1+strlen ("value2"));
 
9536
 
 
9537
                TEST_EQ (pos, (size_t)len);
 
9538
                TEST_EQ (lineno, 3);
 
9539
 
 
9540
                nih_free (job);
 
9541
        }
 
9542
 
 
9543
        TEST_FEATURE ("multiple keys per controller name are accepted");
 
9544
 
 
9545
        len = sprintf (buf,
 
9546
                        "cgroup perf_event foo key1 value1\n"
 
9547
                        "cgroup perf_event foo key2 \"value2\"\n");
 
9548
 
 
9549
        TEST_ALLOC_FAIL {
 
9550
                pos = 0;
 
9551
                lineno = 1;
 
9552
 
 
9553
                job = parse_job (NULL, NULL, NULL, "test", buf, len, &pos, &lineno);
 
9554
 
 
9555
                if (test_alloc_failed) {
 
9556
                        TEST_EQ_P (job, NULL);
 
9557
 
 
9558
                        err = nih_error_get ();
 
9559
                        TEST_EQ (err->number, ENOMEM);
 
9560
                        nih_free (err);
 
9561
 
 
9562
                        continue;
 
9563
                }
 
9564
 
 
9565
                TEST_NE_P (job, NULL);
 
9566
                TEST_LIST_NOT_EMPTY (&job->cgroups);
 
9567
                count = test_list_count (&job->cgroups);
 
9568
                TEST_EQ (count, 1);
 
9569
 
 
9570
                cgroup = (CGroup *)test_list_get_index (&job->cgroups, 0);
 
9571
                TEST_NE_P (cgroup, NULL);
 
9572
                TEST_ALLOC_PARENT (cgroup, job);
 
9573
                TEST_ALLOC_SIZE (cgroup, sizeof (CGroup));
 
9574
                TEST_EQ_STR (cgroup->controller, "perf_event");
 
9575
                TEST_ALLOC_PARENT (cgroup->controller, cgroup);
 
9576
                TEST_ALLOC_SIZE (cgroup->controller, 1+strlen ("perf_event"));
 
9577
 
 
9578
                count = test_list_count (&cgroup->names);
 
9579
                TEST_EQ (count, 1);
 
9580
 
 
9581
                cgname = (CGroupName *)test_list_get_index (&cgroup->names, 0);
 
9582
                TEST_NE_P (cgname, NULL);
 
9583
 
 
9584
                TEST_ALLOC_PARENT (cgname, cgroup);
 
9585
                TEST_ALLOC_SIZE (cgname, sizeof (CGroupName));
 
9586
                TEST_EQ_STR (cgname->name, "foo");
 
9587
                TEST_ALLOC_PARENT (cgname->name, cgname);
 
9588
                TEST_ALLOC_SIZE (cgname->name, 1+strlen ("foo"));
 
9589
 
 
9590
                TEST_LIST_NOT_EMPTY (&cgname->settings);
 
9591
 
 
9592
                count = test_list_count (&cgname->settings);
 
9593
                TEST_EQ (count, 2);
 
9594
 
 
9595
                setting = (CGroupSetting *)test_list_get_index (&cgname->settings, 0);
 
9596
                TEST_NE_P (setting, NULL);
 
9597
 
 
9598
                TEST_ALLOC_PARENT (setting, cgname);
 
9599
                TEST_ALLOC_SIZE (setting, sizeof (CGroupSetting));
 
9600
 
 
9601
                TEST_EQ_STR (setting->key, "key1");
 
9602
                TEST_ALLOC_PARENT (setting->key, setting);
 
9603
                TEST_ALLOC_SIZE (setting->key, 1+strlen ("key1"));
 
9604
 
 
9605
                TEST_EQ_STR (setting->value, "value1");
 
9606
                TEST_ALLOC_PARENT (setting->value, setting);
 
9607
                TEST_ALLOC_SIZE (setting->value, 1+strlen ("value1"));
 
9608
 
 
9609
                setting = (CGroupSetting *)test_list_get_index (&cgname->settings, 1);
 
9610
                TEST_NE_P (setting, NULL);
 
9611
 
 
9612
                TEST_ALLOC_PARENT (setting, cgname);
 
9613
                TEST_ALLOC_SIZE (setting, sizeof (CGroupSetting));
 
9614
 
 
9615
                TEST_EQ_STR (setting->key, "key2");
 
9616
                TEST_ALLOC_PARENT (setting->key, setting);
 
9617
                TEST_ALLOC_SIZE (setting->key, 1+strlen ("key2"));
 
9618
 
 
9619
                TEST_EQ_STR (setting->value, "value2");
 
9620
                TEST_ALLOC_PARENT (setting->value, setting);
 
9621
                TEST_ALLOC_SIZE (setting->value, 1+strlen ("value2"));
 
9622
 
 
9623
                TEST_EQ (pos, (size_t)len);
 
9624
                TEST_EQ (lineno, 3);
 
9625
 
 
9626
                nih_free (job);
 
9627
        }
 
9628
 
 
9629
        TEST_FEATURE ("new stanza overrides old arguments");
 
9630
 
 
9631
        /* value saved for key1 should be "hello world", not "bar" */
 
9632
        len = sprintf (buf,
 
9633
                        "cgroup perf_event foo key1 bar\n"
 
9634
                        "cgroup perf_event foo key1 \"hello world\"\n");
 
9635
 
 
9636
        TEST_ALLOC_FAIL {
 
9637
                pos = 0;
 
9638
                lineno = 1;
 
9639
 
 
9640
                job = parse_job (NULL, NULL, NULL, "test", buf, len, &pos, &lineno);
 
9641
 
 
9642
                if (test_alloc_failed) {
 
9643
                        TEST_EQ_P (job, NULL);
 
9644
 
 
9645
                        err = nih_error_get ();
 
9646
                        TEST_EQ (err->number, ENOMEM);
 
9647
                        nih_free (err);
 
9648
 
 
9649
                        continue;
 
9650
                }
 
9651
 
 
9652
                TEST_NE_P (job, NULL);
 
9653
                TEST_LIST_NOT_EMPTY (&job->cgroups);
 
9654
                count = test_list_count (&job->cgroups);
 
9655
                TEST_EQ (count, 1);
 
9656
 
 
9657
                cgroup = (CGroup *)test_list_get_index (&job->cgroups, 0);
 
9658
                TEST_NE_P (cgroup, NULL);
 
9659
                TEST_ALLOC_PARENT (cgroup, job);
 
9660
                TEST_ALLOC_SIZE (cgroup, sizeof (CGroup));
 
9661
                TEST_EQ_STR (cgroup->controller, "perf_event");
 
9662
                TEST_ALLOC_PARENT (cgroup->controller, cgroup);
 
9663
                TEST_ALLOC_SIZE (cgroup->controller, 1+strlen ("perf_event"));
 
9664
 
 
9665
                count = test_list_count (&cgroup->names);
 
9666
                TEST_EQ (count, 1);
 
9667
 
 
9668
                cgname = (CGroupName *)test_list_get_index (&cgroup->names, 0);
 
9669
                TEST_NE_P (cgname, NULL);
 
9670
 
 
9671
                TEST_ALLOC_PARENT (cgname, cgroup);
 
9672
                TEST_ALLOC_SIZE (cgname, sizeof (CGroupName));
 
9673
                TEST_EQ_STR (cgname->name, "foo");
 
9674
                TEST_ALLOC_PARENT (cgname->name, cgname);
 
9675
                TEST_ALLOC_SIZE (cgname->name, 1+strlen ("foo"));
 
9676
 
 
9677
                TEST_LIST_NOT_EMPTY (&cgname->settings);
 
9678
 
 
9679
                count = test_list_count (&cgname->settings);
 
9680
                TEST_EQ (count, 1);
 
9681
 
 
9682
                setting = (CGroupSetting *)test_list_get_index (&cgname->settings, 0);
 
9683
                TEST_NE_P (setting, NULL);
 
9684
 
 
9685
                TEST_ALLOC_PARENT (setting, cgname);
 
9686
                TEST_ALLOC_SIZE (setting, sizeof (CGroupSetting));
 
9687
 
 
9688
                TEST_EQ_STR (setting->key, "key1");
 
9689
                TEST_ALLOC_PARENT (setting->key, setting);
 
9690
                TEST_ALLOC_SIZE (setting->key, 1+strlen ("key1"));
 
9691
 
 
9692
                TEST_EQ_STR (setting->value, "hello world");
 
9693
                TEST_ALLOC_PARENT (setting->value, setting);
 
9694
                TEST_ALLOC_SIZE (setting->value, 1+strlen ("hello world"));
 
9695
 
 
9696
                TEST_EQ (pos, (size_t)len);
 
9697
                TEST_EQ (lineno, 3);
 
9698
 
 
9699
                nih_free (job);
 
9700
        }
 
9701
}
 
9702
 
 
9703
#endif /* ENABLE_CGROUPS */
 
9704
 
8821
9705
int
8822
9706
main (int   argc,
8823
9707
      char *argv[])
8871
9755
        test_stanza_setgid ();
8872
9756
        test_stanza_usage ();
8873
9757
 
 
9758
#ifdef ENABLE_CGROUPS
 
9759
        test_stanza_cgroup ();
 
9760
#endif /* ENABLE_CGROUPS */
 
9761
 
8874
9762
        return 0;
8875
9763
}