-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathLocalAuthList.cpp
More file actions
967 lines (772 loc) · 38.3 KB
/
LocalAuthList.cpp
File metadata and controls
967 lines (772 loc) · 38.3 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
// matth-x/MicroOcpp
// Copyright Matthias Akstaller 2019 - 2024
// MIT License
#include <MicroOcpp/Version.h>
#if MO_ENABLE_LOCAL_AUTH
#include <MicroOcpp.h>
#include <MicroOcpp/Core/Connection.h>
#include <catch2/catch.hpp>
#include "./helpers/testHelper.h"
#include <MicroOcpp/Core/Context.h>
#include <MicroOcpp/Operations/CustomOperation.h>
#include <MicroOcpp/Core/Request.h>
#include <MicroOcpp/Core/FilesystemAdapter.h>
#include <MicroOcpp/Core/FilesystemUtils.h>
#include <MicroOcpp/Core/Configuration.h>
#include <MicroOcpp/Model/Authorization/AuthorizationService.h>
#define BASE_TIME "2023-01-01T00:00:00.000Z"
using namespace MicroOcpp;
void generateAuthList(JsonArray out, size_t size, bool compact) {
for (size_t i = 0; i < size; i++) {
JsonObject authData = out.createNestedObject();
JsonObject idTagInfo;
if (compact) {
//flat structure
idTagInfo = authData;
} else {
//nested idTagInfo
idTagInfo = authData["idTagInfo"].to<JsonObject>();
}
char buf [IDTAG_LEN_MAX + 1];
sprintf(buf, "mIdTag%zu", i);
authData[AUTHDATA_KEY_IDTAG(compact)] = buf;
idTagInfo[AUTHDATA_KEY_STATUS(compact)] = "Accepted";
}
}
TEST_CASE( "LocalAuth" ) {
printf("\nRun %s\n", "LocalAuth");
//clean state
auto filesystem = makeDefaultFilesystemAdapter(FilesystemOpt::Use_Mount_FormatOnFail);
FilesystemUtils::remove_if(filesystem, [] (const char*) {return true;});
//initialize Context with dummy socket
LoopbackConnection loopback;
mocpp_set_timer(custom_timer_cb);
mocpp_initialize(loopback, ChargerCredentials("test-runner1234"));
auto& model = getOcppContext()->getModel();
auto authService = model.getAuthorizationService();
auto connector = model.getConnector(1);
model.getClock().setTime(BASE_TIME);
loop();
//enable local auth
declareConfiguration<bool>("LocalAuthListEnabled", true)->setBool(true);
//set Authorize timeout after which the charger is considered offline
const unsigned long AUTH_TIMEOUT_MS = 60000;
MicroOcpp::declareConfiguration<int>(MO_CONFIG_EXT_PREFIX "AuthorizationTimeout", -1)->setInt(AUTH_TIMEOUT_MS / 1000);
//fetch local auth configs
auto localAuthorizeOffline = declareConfiguration<bool>("LocalAuthorizeOffline", false);
auto localPreAuthorize = declareConfiguration<bool>("LocalPreAuthorize", false);
SECTION("Basic local auth - LocalPreAuthorize") {
localAuthorizeOffline->setBool(false);
localPreAuthorize->setBool(true);
//set local list
StaticJsonDocument<256> localAuthList;
localAuthList[0]["idTag"] = "mIdTag";
localAuthList[0]["idTagInfo"]["status"] = "Accepted";
REQUIRE( authService->updateLocalList(localAuthList.as<JsonArray>(), 1, false) );
REQUIRE( authService->getLocalListSize() == 1 );
REQUIRE( authService->getLocalAuthorization("mIdTag") != nullptr );
REQUIRE( authService->getLocalAuthorization("mIdTag")->getAuthorizationStatus() == AuthorizationStatus::Accepted );
//check TX notification
bool checkTxAuthorized = false;
setTxNotificationOutput([&checkTxAuthorized] (Transaction*, TxNotification txNotification) {
if (txNotification == TxNotification_Authorized) {
checkTxAuthorized = true;
}
});
//begin transaction and delay Authorize request - tx should start immediately
loopback.setOnline(false); //Authorize delayed by short offline period
REQUIRE( connector->getStatus() == ChargePointStatus_Available );
beginTransaction("mIdTag");
loop();
REQUIRE( connector->getStatus() == ChargePointStatus_Charging );
REQUIRE( checkTxAuthorized );
loopback.setOnline(true);
endTransaction();
loop();
//begin transaction delay Authorize request, but idTag doesn't match local list - tx should start when online again
checkTxAuthorized = false;
loopback.setOnline(false);
REQUIRE( connector->getStatus() == ChargePointStatus_Available );
beginTransaction("wrong idTag");
loop();
REQUIRE( connector->getStatus() == ChargePointStatus_Preparing );
REQUIRE( !checkTxAuthorized );
loopback.setOnline(true);
loop();
REQUIRE( connector->getStatus() == ChargePointStatus_Charging );
REQUIRE( checkTxAuthorized );
endTransaction();
loop();
}
SECTION("Basic local auth - LocalAuthorizeOffline") {
localAuthorizeOffline->setBool(true);
localPreAuthorize->setBool(false);
//set local list
StaticJsonDocument<256> localAuthList;
localAuthList[0]["idTag"] = "mIdTag";
localAuthList[0]["idTagInfo"]["status"] = "Accepted";
authService->updateLocalList(localAuthList.as<JsonArray>(), 1, false);
//check TX notification
bool checkTxAuthorized = false;
setTxNotificationOutput([&checkTxAuthorized] (Transaction*, TxNotification txNotification) {
if (txNotification == TxNotification_Authorized) {
checkTxAuthorized = true;
}
});
//make charger offline and begin tx - tx should begin after Authorize timeout
loopback.setOnline(false);
REQUIRE( connector->getStatus() == ChargePointStatus_Available );
unsigned long t_before = mocpp_tick_ms();
beginTransaction("mIdTag");
loop();
REQUIRE( mocpp_tick_ms() - t_before < AUTH_TIMEOUT_MS ); //if this fails, increase AUTH_TIMEOUT_MS
REQUIRE( connector->getStatus() == ChargePointStatus_Preparing );
REQUIRE( !checkTxAuthorized );
mtime += AUTH_TIMEOUT_MS - (mocpp_tick_ms() - t_before); //increment clock so that auth timeout is exceeded
loop();
REQUIRE( connector->getStatus() == ChargePointStatus_Charging );
REQUIRE( checkTxAuthorized );
loopback.setOnline(true);
endTransaction();
loop();
//make charger offline and begin tx, but idTag doesn't match - tx should be aborted
bool checkTxTimeout = false;
setTxNotificationOutput([&checkTxTimeout] (Transaction*, TxNotification txNotification) {
if (txNotification == TxNotification_AuthorizationTimeout) {
checkTxTimeout = true;
}
});
loopback.setOnline(false);
REQUIRE( connector->getStatus() == ChargePointStatus_Available );
t_before = mocpp_tick_ms();
beginTransaction("wrong idTag");
loop();
REQUIRE( connector->getStatus() == ChargePointStatus_Preparing );
REQUIRE( !checkTxTimeout );
mtime += AUTH_TIMEOUT_MS - (mocpp_tick_ms() - t_before); //increment clock so that auth timeout is exceeded
loop();
REQUIRE( connector->getStatus() == ChargePointStatus_Available );
REQUIRE( checkTxTimeout );
loopback.setOnline(true);
loop();
}
SECTION("Basic local auth - AllowOfflineTxForUnknownId") {
localAuthorizeOffline->setBool(false);
localPreAuthorize->setBool(false);
MicroOcpp::declareConfiguration<bool>("AllowOfflineTxForUnknownId", true)->setBool(true);
//check TX notification
bool checkTxAuthorized = false;
setTxNotificationOutput([&checkTxAuthorized] (Transaction*, TxNotification txNotification) {
if (txNotification == TxNotification_Authorized) {
checkTxAuthorized = true;
}
});
//make charger offline and begin tx - tx should begin after Authorize timeout
loopback.setOnline(false);
REQUIRE( connector->getStatus() == ChargePointStatus_Available );
unsigned long t_before = mocpp_tick_ms();
beginTransaction("unknownIdTag");
loop();
REQUIRE( connector->getStatus() == ChargePointStatus_Preparing );
REQUIRE( !checkTxAuthorized );
mtime += AUTH_TIMEOUT_MS - (mocpp_tick_ms() - t_before); //increment clock so that auth timeout is exceeded
loop();
REQUIRE( connector->getStatus() == ChargePointStatus_Charging );
REQUIRE( checkTxAuthorized );
loopback.setOnline(true);
endTransaction();
loop();
}
SECTION("Local auth - check WS online status") {
localAuthorizeOffline->setBool(false);
localPreAuthorize->setBool(false);
MicroOcpp::declareConfiguration<bool>("AllowOfflineTxForUnknownId", true)->setBool(true);
//check TX notification
bool checkTxAuthorized = false;
setTxNotificationOutput([&checkTxAuthorized] (Transaction*, TxNotification txNotification) {
if (txNotification == TxNotification_Authorized) {
checkTxAuthorized = true;
}
});
//disconnect WS and begin tx - charger should enter offline mode immediately
loopback.setConnected(false);
REQUIRE( connector->getStatus() == ChargePointStatus_Available );
beginTransaction("unknownIdTag");
loop();
REQUIRE( connector->getStatus() == ChargePointStatus_Charging );
REQUIRE( checkTxAuthorized );
loopback.setConnected(true);
endTransaction();
loop();
}
SECTION("Local auth list entry expired / unauthorized") {
localPreAuthorize->setBool(true);
//set local list with expired / unauthorized entry
StaticJsonDocument<512> localAuthList;
localAuthList[0]["idTag"] = "mIdTagExpired";
localAuthList[0]["idTagInfo"]["status"] = "Accepted";
localAuthList[0]["idTagInfo"]["expiryDate"] = BASE_TIME; //is in past
localAuthList[1]["idTag"] = "mIdTagUnauthorized";
localAuthList[1]["idTagInfo"]["status"] = "Blocked";
authService->updateLocalList(localAuthList.as<JsonArray>(), 1, false);
REQUIRE( authService->getLocalAuthorization("mIdTagExpired") );
REQUIRE( authService->getLocalAuthorization("mIdTagUnauthorized") );
REQUIRE( authService->getLocalAuthorization("mIdTagExpired") );
//begin transaction and delay Authorize request - cannot PreAuthorize because entry is expired
loopback.setOnline(false); //Authorize delayed by short offline period
REQUIRE( connector->getStatus() == ChargePointStatus_Available );
beginTransaction("mIdTagExpired");
loop();
REQUIRE( connector->getStatus() == ChargePointStatus_Preparing );
loopback.setOnline(true);
loop();
REQUIRE( connector->getStatus() == ChargePointStatus_Charging );
endTransaction();
loop();
//begin transaction and delay Authorize request - cannot PreAuthorize because entry is unauthorized
loopback.setOnline(false); //Authorize delayed by short offline period
REQUIRE( connector->getStatus() == ChargePointStatus_Available );
beginTransaction("mIdTagUnauthorized");
loop();
REQUIRE( connector->getStatus() == ChargePointStatus_Preparing );
loopback.setOnline(true);
loop();
REQUIRE( connector->getStatus() == ChargePointStatus_Charging );
endTransaction();
loop();
}
SECTION("Mix local authorization modes") {
localAuthorizeOffline->setBool(true);
localPreAuthorize->setBool(true);
MicroOcpp::declareConfiguration<bool>("AllowOfflineTxForUnknownId", true)->setBool(true);
//set local list with accepted and accepted entry
StaticJsonDocument<512> localAuthList;
localAuthList[0]["idTag"] = "mIdTagExpired";
localAuthList[0]["idTagInfo"]["status"] = "Accepted";
localAuthList[0]["idTagInfo"]["expiryDate"] = BASE_TIME; //is in past
localAuthList[1]["idTag"] = "mIdTagAccepted";
localAuthList[1]["idTagInfo"]["status"] = "Accepted";
authService->updateLocalList(localAuthList.as<JsonArray>(), 1, false);
REQUIRE( authService->getLocalAuthorization("mIdTagExpired") );
REQUIRE( authService->getLocalAuthorization("mIdTagAccepted") );
//begin transaction and delay Authorize request - tx should start immediately
loopback.setOnline(false);
REQUIRE( connector->getStatus() == ChargePointStatus_Available );
beginTransaction("mIdTagAccepted");
loop();
REQUIRE( connector->getStatus() == ChargePointStatus_Charging );
loopback.setOnline(true);
endTransaction();
loop();
//begin transaction, but idTag is expired - AllowOfflineTxForUnknownId must not apply
loopback.setOnline(false);
REQUIRE( connector->getStatus() == ChargePointStatus_Available );
unsigned long t_before = mocpp_tick_ms();
beginTransaction("mIdTagExpired");
loop();
REQUIRE( mocpp_tick_ms() - t_before < AUTH_TIMEOUT_MS ); //if this fails, increase AUTH_TIMEOUT_MS
REQUIRE( connector->getStatus() == ChargePointStatus_Preparing );
mtime += AUTH_TIMEOUT_MS - (mocpp_tick_ms() - t_before); //increment clock so that auth timeout is exceeded
loop();
REQUIRE( connector->getStatus() == ChargePointStatus_Available );
loopback.setOnline(true);
loop();
}
SECTION("DeAuthorize locally authorized tx") {
localAuthorizeOffline->setBool(false);
localPreAuthorize->setBool(true);
//check TX notification
bool checkTxAuthorized = false;
setTxNotificationOutput([&checkTxAuthorized] (Transaction*, TxNotification txNotification) {
if (txNotification == TxNotification_Authorized) {
checkTxAuthorized = true;
}
});
//set local list
StaticJsonDocument<256> localAuthList;
localAuthList[0]["idTag"] = "mIdTag";
localAuthList[0]["idTagInfo"]["status"] = "Accepted";
authService->updateLocalList(localAuthList.as<JsonArray>(), 1, false);
//patch Authorize so it will reject all idTags
getOcppContext()->getOperationRegistry().registerOperation("Authorize", [] () {
return new Ocpp16::CustomOperation("Authorize",
[] (JsonObject) {}, //ignore req
[] () {
//create conf
auto doc = makeJsonDoc("UnitTests", 2 * JSON_OBJECT_SIZE(1));
auto payload = doc->to<JsonObject>();
payload["idTagInfo"]["status"] = "Blocked";
return doc;
});});
//begin transaction and delay Authorize request - tx should start immediately
loopback.setOnline(false); //Authorize delayed by short offline period
REQUIRE( connector->getStatus() == ChargePointStatus_Available );
beginTransaction("mIdTag");
loop();
REQUIRE( connector->getStatus() == ChargePointStatus_Charging );
REQUIRE( checkTxAuthorized );
//check TX notification
bool checkTxRejected = false;
setTxNotificationOutput([&checkTxRejected] (Transaction*, TxNotification txNotification) {
if (txNotification == TxNotification_AuthorizationRejected) {
checkTxRejected = true;
}
});
loopback.setOnline(true);
loop();
REQUIRE( connector->getStatus() == ChargePointStatus_Available );
REQUIRE( checkTxRejected );
loop();
}
SECTION("LocalListConflict") {
//patch Authorize so it will reject all idTags
bool checkAuthorize = false;
getOcppContext()->getOperationRegistry().registerOperation("Authorize", [&checkAuthorize] () {
return new Ocpp16::CustomOperation("Authorize",
[&checkAuthorize] (JsonObject) {
checkAuthorize = true;
},
[] () {
//create conf
auto doc = makeJsonDoc("UnitTests", 2 * JSON_OBJECT_SIZE(1));
auto payload = doc->to<JsonObject>();
payload["idTagInfo"]["status"] = "Blocked";
return doc;
});});
//patch StartTransaction so it will DeAuthorize all txs
bool checkStartTx = false;
getOcppContext()->getOperationRegistry().registerOperation("StartTransaction", [&checkStartTx] () {
return new Ocpp16::CustomOperation("StartTransaction",
[&checkStartTx] (JsonObject) {
checkStartTx = true;
},
[] () {
//create conf
auto doc = makeJsonDoc("UnitTests",
JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(1));
auto payload = doc->to<JsonObject>();
payload["idTagInfo"]["status"] = "Blocked";
payload["transactionId"] = 1000;
return doc;
});});
//check resulting StatusNotification message
bool checkLocalListConflict = false;
getOcppContext()->getOperationRegistry().registerOperation("StatusNotification", [&checkLocalListConflict] () {
return new Ocpp16::CustomOperation("StatusNotification",
[&checkLocalListConflict] (JsonObject payload) {
if (payload["connectorId"] == 0 &&
!strcmp(payload["errorCode"] | "_Undefined", "LocalListConflict")) {
checkLocalListConflict = true;
}
},
[] () {
//create conf
return createEmptyDocument();
});});
//set local list
StaticJsonDocument<256> localAuthList;
localAuthList[0]["idTag"] = "mIdTag";
localAuthList[0]["idTagInfo"]["status"] = "Accepted";
authService->updateLocalList(localAuthList.as<JsonArray>(), 1, false);
//send Authorize and StartTx and check if they trigger LocalListConflict
beginTransaction("mIdTag");
loop();
REQUIRE( checkLocalListConflict );
REQUIRE( checkAuthorize );
REQUIRE( !checkStartTx );
checkLocalListConflict = false;
checkAuthorize = false;
beginTransaction_authorized("mIdTag");
loop();
REQUIRE( checkLocalListConflict );
REQUIRE( !checkAuthorize );
REQUIRE( checkStartTx );
}
SECTION("Update local list") {
REQUIRE( authService->getLocalListSize() == 0 ); //idle, empty local list
//set local list
int localListVersion = 42;
StaticJsonDocument<256> localAuthList;
localAuthList[0]["idTag"] = "mIdTag";
localAuthList[0]["idTagInfo"]["status"] = "Accepted";
authService->updateLocalList(localAuthList.as<JsonArray>(), localListVersion, false);
REQUIRE( authService->getLocalListVersion() == localListVersion );
REQUIRE( authService->getLocalListSize() == 1 );
REQUIRE( authService->getLocalAuthorization("mIdTag") != nullptr );
//overwrite list
localListVersion++;
localAuthList.clear();
localAuthList[0]["idTag"] = "mIdTag2";
localAuthList[0]["idTagInfo"]["status"] = "Accepted";
authService->updateLocalList(localAuthList.as<JsonArray>(), localListVersion, false);
REQUIRE( authService->getLocalListVersion() == localListVersion );
REQUIRE( authService->getLocalListSize() == 1 );
REQUIRE( authService->getLocalAuthorization("mIdTag") == nullptr );
REQUIRE( authService->getLocalAuthorization("mIdTag2") != nullptr );
//reset list
localListVersion++;
localAuthList.clear();
localAuthList.to<JsonArray>();
authService->updateLocalList(localAuthList.as<JsonArray>(), localListVersion, false);
REQUIRE( authService->getLocalListVersion() == 0 ); //localListVersion is ignored - empty list always resets version
REQUIRE( authService->getLocalListSize() == 0 );
//consecutive updates in Differential mode
localListVersion = 1;
localAuthList.clear();
localAuthList[0]["idTag"] = "mIdTag";
localAuthList[0]["idTagInfo"]["status"] = "Accepted";
authService->updateLocalList(localAuthList.as<JsonArray>(), localListVersion, true);
REQUIRE( authService->getLocalListVersion() == localListVersion );
REQUIRE( authService->getLocalListSize() == 1 );
//append further entry
localListVersion++;
localAuthList.clear();
localAuthList[0]["idTag"] = "mIdTag2";
localAuthList[0]["idTagInfo"]["status"] = "Accepted";
authService->updateLocalList(localAuthList.as<JsonArray>(), localListVersion, true);
REQUIRE( authService->getLocalListVersion() == localListVersion );
REQUIRE( authService->getLocalListSize() == 2 );
//overwrite previous entry
REQUIRE( authService->getLocalAuthorization("mIdTag")->getAuthorizationStatus() == AuthorizationStatus::Accepted );
localListVersion++;
localAuthList.clear();
localAuthList[0]["idTag"] = "mIdTag";
localAuthList[0]["idTagInfo"]["status"] = "Blocked";
authService->updateLocalList(localAuthList.as<JsonArray>(), localListVersion, true);
REQUIRE( authService->getLocalListVersion() == localListVersion );
REQUIRE( authService->getLocalListSize() == 2 );
REQUIRE( authService->getLocalAuthorization("mIdTag")->getAuthorizationStatus() == AuthorizationStatus::Blocked );
//empty update keeps previous entries
localListVersion++;
localAuthList.clear();
localAuthList.to<JsonArray>();
authService->updateLocalList(localAuthList.as<JsonArray>(), localListVersion, true);
REQUIRE( authService->getLocalListVersion() == localListVersion );
REQUIRE( authService->getLocalListSize() == 2 );
//delete entries
localListVersion++;
localAuthList.clear();
localAuthList[0]["idTag"] = "mIdTag";
authService->updateLocalList(localAuthList.as<JsonArray>(), localListVersion, true);
REQUIRE( authService->getLocalListVersion() == localListVersion );
REQUIRE( authService->getLocalListSize() == 1 );
localListVersion++;
localAuthList.clear();
localAuthList[0]["idTag"] = "mIdTag2";
authService->updateLocalList(localAuthList.as<JsonArray>(), localListVersion, true);
REQUIRE( authService->getLocalListVersion() == 0 );
REQUIRE( authService->getLocalListSize() == 0 );
}
SECTION("LocalList persistency") {
int listVersion = 42;
StaticJsonDocument<512> localAuthList;
localAuthList[0]["idTag"] = "mIdTagMinimal";
localAuthList[0]["idTagInfo"]["status"] = "Accepted";
localAuthList[1]["idTag"] = "mIdTagFull";
localAuthList[1]["idTagInfo"]["expiryDate"] = BASE_TIME;
localAuthList[1]["idTagInfo"]["parentIdTag"] = "mParentIdTag";
localAuthList[1]["idTagInfo"]["status"] = "Blocked";
authService->updateLocalList(localAuthList.as<JsonArray>(), listVersion, false);
mocpp_deinitialize();
mocpp_initialize(loopback, ChargerCredentials("test-runner1234"));
authService = getOcppContext()->getModel().getAuthorizationService();
REQUIRE( authService->getLocalListVersion() == listVersion );
REQUIRE( authService->getLocalListSize() == 2 );
auto auth0 = authService->getLocalAuthorization("mIdTagMinimal");
REQUIRE( auth0 != nullptr );
REQUIRE( !strcmp(auth0->getIdTag(), "mIdTagMinimal") );
REQUIRE( auth0->getExpiryDate() == nullptr );
REQUIRE( auth0->getParentIdTag() == nullptr );
REQUIRE( auth0->getAuthorizationStatus() == AuthorizationStatus::Accepted );
auto auth1 = authService->getLocalAuthorization("mIdTagFull");
REQUIRE( auth1 != nullptr );
REQUIRE( !strcmp(auth1->getIdTag(), "mIdTagFull") );
REQUIRE( auth1->getExpiryDate() != nullptr );
Timestamp baseTimeParsed;
baseTimeParsed.setTime(BASE_TIME);
REQUIRE( *auth1->getExpiryDate() == baseTimeParsed );
REQUIRE( !strcmp(auth1->getParentIdTag(), "mParentIdTag") );
REQUIRE( auth1->getAuthorizationStatus() == AuthorizationStatus::Blocked );
}
SECTION("SendLocalList") {
int listVersion = 42;
size_t listSize = 2;
auto populatedEntryIdTag = makeString("UnitTests"); //local auth list entry to be fully populated
//Full update - happy path
bool checkAccepted = false;
getOcppContext()->initiateRequest(makeRequest(
new Ocpp16::CustomOperation("SendLocalList",
[&listVersion, &listSize, &populatedEntryIdTag] () {
//create req
auto doc = makeJsonDoc("UnitTests",
4096);
auto payload = doc->to<JsonObject>();
payload["listVersion"] = listVersion;
generateAuthList(payload["localAuthorizationList"].to<JsonArray>(), listSize, false);
//fully populate first entry
populatedEntryIdTag = payload["localAuthorizationList"][0]["idTag"] | "_Undefined";
payload["localAuthorizationList"][0]["idTagInfo"]["expiryDate"] = BASE_TIME;
payload["localAuthorizationList"][0]["idTagInfo"]["parentIdTag"] = "mParentIdTag";
payload["localAuthorizationList"][0]["idTagInfo"]["status"] = "Blocked";
payload["updateType"] = "Full";
return doc;
},
[&checkAccepted] (JsonObject payload) {
//process conf
checkAccepted = !strcmp(payload["status"] | "_Undefined", "Accepted");
})));
loop();
REQUIRE( authService->getLocalListVersion() == listVersion );
REQUIRE( authService->getLocalListSize() == listSize );
REQUIRE( !populatedEntryIdTag.empty() );
auto localAuth = authService->getLocalAuthorization(populatedEntryIdTag.c_str());
REQUIRE( localAuth != nullptr );
Timestamp baseTimeParsed;
baseTimeParsed.setTime(BASE_TIME);
REQUIRE( localAuth->getExpiryDate() );
REQUIRE( *localAuth->getExpiryDate() == baseTimeParsed );
REQUIRE( !strcmp(localAuth->getIdTag(), populatedEntryIdTag.c_str()) );
REQUIRE( !strcmp(localAuth->getParentIdTag(), "mParentIdTag") );
REQUIRE( localAuth->getAuthorizationStatus() == AuthorizationStatus::Blocked );
REQUIRE( checkAccepted );
//Differential update - happy path
listVersion++;
listSize++; //add one extra entry and update all others
checkAccepted = false;
getOcppContext()->initiateRequest(makeRequest(
new Ocpp16::CustomOperation("SendLocalList",
[&listVersion, &listSize] () {
//create req
auto doc = makeJsonDoc("UnitTests",
1024);
auto payload = doc->to<JsonObject>();
payload["listVersion"] = listVersion;
generateAuthList(payload["localAuthorizationList"].to<JsonArray>(), listSize, false);
payload["updateType"] = "Differential";
return doc;
},
[&checkAccepted] (JsonObject payload) {
//process conf
checkAccepted = !strcmp(payload["status"] | "_Undefined", "Accepted");
})));
loop();
REQUIRE( authService->getLocalListVersion() == listVersion );
REQUIRE( authService->getLocalListSize() == listSize );
REQUIRE( checkAccepted );
//Differential update - version mismatch
size_t listSizeInvalid = listSize + 1;
bool checkVersionMismatch = false;
getOcppContext()->initiateRequest(makeRequest(
new Ocpp16::CustomOperation("SendLocalList",
[&listVersion, &listSizeInvalid] () {
//create req
auto doc = makeJsonDoc("UnitTests",
1024);
auto payload = doc->to<JsonObject>();
payload["listVersion"] = listVersion;
generateAuthList(payload["localAuthorizationList"].to<JsonArray>(), listSizeInvalid, false);
payload["updateType"] = "Differential";
return doc;
},
[&checkVersionMismatch] (JsonObject payload) {
//process conf
checkVersionMismatch = !strcmp(payload["status"] | "_Undefined", "VersionMismatch");
})));
loop();
REQUIRE( authService->getLocalListVersion() == listVersion );
REQUIRE( authService->getLocalListSize() == listSize );
REQUIRE( checkVersionMismatch );
//Boundary check - maximum entries per SendLocalList
listVersion = 42;
listSize = (size_t) declareConfiguration<int>("SendLocalListMaxLength", -1)->getInt();
checkAccepted = false;
getOcppContext()->initiateRequest(makeRequest(
new Ocpp16::CustomOperation("SendLocalList",
[&listVersion, &listSize] () {
//create req
auto doc = makeJsonDoc("UnitTests",
4096);
auto payload = doc->to<JsonObject>();
payload["listVersion"] = listVersion;
generateAuthList(payload["localAuthorizationList"].to<JsonArray>(), listSize, false);
payload["updateType"] = "Full";
return doc;
},
[&checkAccepted] (JsonObject payload) {
//process conf
checkAccepted = !strcmp(payload["status"] | "_Undefined", "Accepted");
})));
loop();
REQUIRE( authService->getLocalListVersion() == listVersion );
REQUIRE( authService->getLocalListSize() == listSize );
REQUIRE( checkAccepted );
//Boundary check - maximum entries per SendLocalList in Differential mode
listVersion++;
checkAccepted = false;
getOcppContext()->initiateRequest(makeRequest(
new Ocpp16::CustomOperation("SendLocalList",
[&listVersion, &listSize] () {
//create req
auto doc = makeJsonDoc("UnitTests",
4096);
auto payload = doc->to<JsonObject>();
payload["listVersion"] = listVersion;
generateAuthList(payload["localAuthorizationList"].to<JsonArray>(), listSize, false);
payload["updateType"] = "Differential";
return doc;
},
[&checkAccepted] (JsonObject payload) {
//process conf
checkAccepted = !strcmp(payload["status"] | "_Undefined", "Accepted");
})));
loop();
REQUIRE( authService->getLocalListVersion() == listVersion );
REQUIRE( authService->getLocalListSize() == listSize );
REQUIRE( checkAccepted );
//Boundary check - exceed maximum entries per SendLocalList
int listVersionInvalid = listVersion + 1;
listSizeInvalid = listSize + 1;
bool errOccurence = false;
getOcppContext()->initiateRequest(makeRequest(
new Ocpp16::CustomOperation("SendLocalList",
[&listVersionInvalid, &listSizeInvalid] () {
//create req
auto doc = makeJsonDoc("UnitTests",
4096);
auto payload = doc->to<JsonObject>();
payload["listVersion"] = listVersionInvalid;
generateAuthList(payload["localAuthorizationList"].to<JsonArray>(), listSizeInvalid, false);
payload["updateType"] = "Full";
return doc;
},
[] (JsonObject) { }, //ignore conf
[&errOccurence] (const char *errorCode, const char*, JsonObject) {
errOccurence = !strcmp(errorCode, "OccurenceConstraintViolation");
return true;
})));
loop();
REQUIRE( authService->getLocalListVersion() == listVersion );
REQUIRE( authService->getLocalListSize() == listSize );
REQUIRE( errOccurence );
//Boundary check - exceed maximum local list size by multiple Differerntial updates
//clear local list
StaticJsonDocument<64> emptyDoc;
emptyDoc.to<JsonArray>();
authService->updateLocalList(emptyDoc.as<JsonArray>(), 1, false);
size_t localAuthListMaxLength = (size_t) declareConfiguration<int>("LocalAuthListMaxLength", -1)->getInt();
//send Differential lists with 2 entries: update an existing entry and add a new entry
for (size_t i = 1; i < localAuthListMaxLength; i++) {
//Full update - happy path
bool checkAccepted = false;
getOcppContext()->initiateRequest(makeRequest(
new Ocpp16::CustomOperation("SendLocalList",
[&i] () {
//create req
auto doc = makeJsonDoc("UnitTests",
1024);
auto payload = doc->to<JsonObject>();
payload["listVersion"] = (int) i;
char buf [IDTAG_LEN_MAX + 1];
sprintf(buf, "mIdTag%zu", i-1);
payload["localAuthorizationList"][0]["idTag"] = buf;
payload["localAuthorizationList"][0]["idTagInfo"]["status"] = "Accepted";
sprintf(buf, "mIdTag%zu", i);
payload["localAuthorizationList"][1]["idTag"] = buf;
payload["localAuthorizationList"][1]["idTagInfo"]["status"] = "Accepted";
payload["updateType"] = "Differential";
return doc;
},
[&checkAccepted] (JsonObject payload) {
//process conf
checkAccepted = !strcmp(payload["status"] | "_Undefined", "Accepted");
})));
loop();
REQUIRE( authService->getLocalListVersion() == (int)i );
REQUIRE( authService->getLocalListSize() == i + 1 );
REQUIRE( checkAccepted );
}
//now exceed local list by sending overflow entry
listVersion = authService->getLocalListVersion();
listVersionInvalid = listVersion + 1;
listSize = authService->getLocalListSize();
bool checkFailed = false;
getOcppContext()->initiateRequest(makeRequest(
new Ocpp16::CustomOperation("SendLocalList",
[&listVersionInvalid] () {
//create req
auto doc = makeJsonDoc("UnitTests",
1024);
auto payload = doc->to<JsonObject>();
payload["listVersion"] = listVersionInvalid;
//update already existing entry
char buf [IDTAG_LEN_MAX + 1];
sprintf(buf, "mIdTag%zu", 0UL);
payload["localAuthorizationList"][0]["idTag"] = buf;
payload["localAuthorizationList"][0]["idTagInfo"]["status"] = "Accepted";
//additional overflowing entry
payload["localAuthorizationList"][1]["idTag"] = "overflow idTag";
payload["localAuthorizationList"][1]["idTagInfo"]["status"] = "Accepted";
payload["updateType"] = "Differential";
return doc;
},
[&checkFailed] (JsonObject payload) {
//process conf
checkFailed = !strcmp(payload["status"] | "_Undefined", "Failed");
})));
loop();
REQUIRE( authService->getLocalListVersion() == listVersion );
REQUIRE( authService->getLocalListSize() == listSize );
REQUIRE( checkFailed );
}
SECTION("GetLocalListVersion") {
int localListVersion = 42;
StaticJsonDocument<256> localAuthList;
localAuthList[0]["idTag"] = "mIdTag";
localAuthList[0]["idTagInfo"]["status"] = "Accepted";
authService->updateLocalList(localAuthList.as<JsonArray>(), localListVersion, false);
int checkListVerion = -1;
getOcppContext()->initiateRequest(makeRequest(
new Ocpp16::CustomOperation("GetLocalListVersion",
[] () {
//create req
return createEmptyDocument();
},
[&checkListVerion] (JsonObject payload) {
//process conf
checkListVerion = payload["listVersion"] | -1;
})));
loop();
REQUIRE( checkListVerion == localListVersion );
}
SECTION("Id in Local Authorization List should not send Authorize.req (TC_008_1_CS & TC_008_2_CS)") {
localAuthorizeOffline->setBool(true);
localPreAuthorize->setBool(true);
//set local list
StaticJsonDocument<256> localAuthList;
localAuthList[0]["idTag"] = "mIdTag";
localAuthList[0]["idTagInfo"]["status"] = "Accepted";
authService->updateLocalList(localAuthList.as<JsonArray>(), 1, false);
//patch Authorize so we can check if it is called
bool checkAuthorize = false;
getOcppContext()->getOperationRegistry().registerOperation("Authorize", [&checkAuthorize] () {
return new Ocpp16::CustomOperation("Authorize",
[&checkAuthorize] (JsonObject) {
checkAuthorize = true;
},
[] () {
return createEmptyDocument();
});});
//begin transaction
//Authorize should be skipped because of LocalPreAuthorize & LocalAuthorizeOffline
loopback.setOnline(true);
REQUIRE( connector->getStatus() == ChargePointStatus_Available );
beginTransaction("mIdTag");
loop();
REQUIRE( !checkAuthorize ); // Ensure Authorize.req was NOT sent
REQUIRE( connector->getStatus() == ChargePointStatus_Charging );
endTransaction();
loop();
}
mocpp_deinitialize();
}
#endif //MO_ENABLE_LOCAL_AUTH