-
-
Notifications
You must be signed in to change notification settings - Fork 720
Expand file tree
/
Copy pathtest_detect.py
More file actions
1327 lines (1122 loc) · 58.1 KB
/
test_detect.py
File metadata and controls
1327 lines (1122 loc) · 58.1 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
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# ScanCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/scancode-toolkit for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
import os
import pytest
from commoncode.testcase import FileBasedTesting
from licensedcode import cache
from licensedcode import index
from licensedcode import match_aho
from licensedcode import match_seq
from licensedcode.legalese import build_dictionary_from_iterable
from licensedcode.match import LicenseMatch
from licensedcode.models import load_rules
from licensedcode.spans import Span
from licensedcode.tracing import get_texts
from licensedcode_test_utils import mini_legalese
from licensedcode_test_utils import create_rule_from_text_and_expression
from licensedcode_test_utils import create_rule_from_text_file_and_expression
TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
"""
Test the core license detection mechanics.
"""
def MiniLicenseIndex(*args, **kwargs):
return index.LicenseIndex(*args, _legalese=mini_legalese, **kwargs)
class TestIndexMatch(FileBasedTesting):
test_data_dir = TEST_DATA_DIR
def test_match_does_not_return_matches_for_empty_query(self):
idx = MiniLicenseIndex([create_rule_from_text_and_expression(text='A one. A two. license A three.')])
matches = idx.match(query_string='')
assert matches == []
matches = idx.match(query_string=None)
assert matches == []
def test_match_does_not_return_matches_for_junk_queries(self):
idx = MiniLicenseIndex([create_rule_from_text_and_expression(text='A one. a license two. license A three.')])
assert idx.match(query_string=u'some other junk') == []
assert idx.match(query_string=u'some junk') == []
def test_match_return_one_match_with_correct_offsets(self):
idx = MiniLicenseIndex([
create_rule_from_text_and_expression(text='A one. a license two. A three.',
license_expression='abc')]
)
querys = u'some junk. A one. A license two. A three.'
# 0 1 2 3 4 5 6 7 8
matches = idx.match(query_string=querys)
assert len(matches) == 1
match = matches[0]
qtext, itext = get_texts(match)
assert qtext == 'one. A license two. A three.'
assert itext == 'one license two three'
assert match.qspan == Span(0, 3)
assert match.ispan == Span(0, 3)
def test_match_can_match_exactly_rule_text_used_as_query(self):
test_file = self.get_test_loc('detect/mit/mit.c')
rule = create_rule_from_text_file_and_expression(text_file=test_file, license_expression='mit')
idx = MiniLicenseIndex([rule])
matches = idx.match(test_file)
assert len(matches) == 1
match = matches[0]
assert match.rule == rule
assert match.qspan == Span(0, 85)
assert match.ispan == Span(0, 85)
assert match.coverage() == 100
assert match.score() == 100
def test_match_matches_correctly_simple_exact_query_1(self):
tf1 = self.get_test_loc('detect/mit/mit.c')
ftr = create_rule_from_text_file_and_expression(text_file=tf1, license_expression='mit')
idx = MiniLicenseIndex([ftr])
query_doc = self.get_test_loc('detect/mit/mit2.c')
matches = idx.match(query_doc)
assert len(matches) == 1
match = matches[0]
assert match.rule == ftr
assert match.qspan == Span(0, 85)
assert match.ispan == Span(0, 85)
def test_match_matches_correctly_simple_exact_query_across_query_runs(self):
tf1 = self.get_test_loc('detect/mit/mit.c')
ftr = create_rule_from_text_file_and_expression(text_file=tf1, license_expression='mit')
idx = MiniLicenseIndex([ftr])
query_doc = self.get_test_loc('detect/mit/mit3.c')
matches = idx.match(query_doc)
assert len(matches) == 1
match = matches[0]
qtext, itext = get_texts(match)
expected_qtext = '''
Permission is hereby granted, free of charge, to any person obtaining a copy
// of this
software and associated documentation files (the "Software"), to deal
// in
#,.,
///
THE SOFTWARE WITHOUT RESTRICTION, INCLUDING WITHOUT LIMITATION THE RIGHTS
// TO USE, COPY, MODIFY, MERGE, PUBLISH, DISTRIBUTE, SUBLICENSE, AND/OR SELL
// COPIES #,.,
///
of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
#,.,
///
// The above copyright notice and this permission notice shall be included in
#,.,
///
// all copies or substantial portions#,.,
///
of the Software.
'''
assert ' '.join(qtext.split()) == ' '.join(expected_qtext.split())
expected_itext = u'''
Permission is hereby granted free of charge to any person obtaining
copy of this software and associated documentation files the Software to
deal in the Software without restriction including without limitation
the rights to use copy modify merge publish distribute sublicense and or
sell copies of the Software and to permit persons to whom the Software
is furnished to do so subject to the following conditions The above
copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software
'''.lower()
assert ' '.join(itext.split()) == ' '.join(expected_itext.split())
def test_match_with_surrounding_junk_should_return_an_exact_match(self):
tf1 = self.get_test_loc('detect/mit/mit.c')
ftr = create_rule_from_text_file_and_expression(text_file=tf1, license_expression='mit')
idx = MiniLicenseIndex([ftr])
query_loc = self.get_test_loc('detect/mit/mit4.c')
matches = idx.match(query_loc)
assert len(matches) == 1
match = matches[0]
qtext, itext = get_texts(match)
expected_qtext = u'''
Permission "[add] [text]" is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright "[add] [text]" notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
'''.split()
assert qtext.split() == expected_qtext
expected_itext = u'''
permission is hereby granted free of charge to any person obtaining
copy of this software and associated documentation files the software to
deal in the software without restriction including without limitation the
rights to use copy modify merge publish distribute sublicense and or sell
copies of the software and to permit persons to whom the software is
furnished to do so subject to the following conditions the above
copyright notice and this permission notice shall be included in all
copies or substantial portions of the software
'''.lower().split()
assert itext.split() == expected_itext
assert match.qspan == Span(0, 85)
assert match.ispan == Span(0, 85)
assert match.score() == 95.56
def test_match_to_single_word_does_not_have_zero_score(self):
idx = MiniLicenseIndex(
[create_rule_from_text_and_expression(text='LGPL', license_expression='lgpl-2.0')]
)
matches = idx.match(query_string='LGPL')
assert len(matches) == 1
assert matches[0].score() == 5.0
def test_match_to_threshold_words_has_hundred_score(self):
threshold = 18
idx = MiniLicenseIndex(
[create_rule_from_text_and_expression(text=' LGPL ' * threshold, license_expression='lgpl-2.0')]
)
matches = idx.match(query_string=' LGPL ' * threshold)
assert len(matches) == 1
assert matches[0].score() == 100.0
def test_match_can_match_approximately(self):
rule_file = self.get_test_loc('approx/mit/mit.c')
rule = create_rule_from_text_file_and_expression(text_file=rule_file, license_expression='mit')
idx = MiniLicenseIndex([rule])
query_doc = self.get_test_loc('approx/mit/mit4.c')
matches = idx.match(query_doc)
assert len(matches) == 2
m1 = matches[0]
m2 = matches[1]
assert m1.rule == rule
assert m2.rule == rule
assert m1.coverage() == 100
assert m2.coverage() == 100
assert m1.score() == 95.56
assert m2.score() == 93.48
def test_match_return_correct_positions_with_short_index_and_queries(self):
idx = MiniLicenseIndex(
[create_rule_from_text_and_expression(text='MIT License', license_expression='mit')]
)
matches = idx.match(query_string='MIT License')
assert len(matches) == 1
qtext, itext = get_texts(matches[0])
assert qtext == 'MIT License'
assert itext == 'mit license'
assert matches[0].qspan == Span(0, 1)
assert matches[0].ispan == Span(0, 1)
matches = idx.match(query_string='MIT MIT License')
assert len(matches) == 1
qtext, itext = get_texts(matches[0])
assert qtext == 'MIT License'
assert itext == 'mit license'
assert Span(1, 2) == matches[0].qspan
assert Span(0, 1) == matches[0].ispan
query_doc1 = 'do you think I am a mit license MIT License, yes, I think so'
# # 0 1 2 3
matches = idx.match(query_string=query_doc1)
assert len(matches) == 2
qtext, itext = get_texts(matches[0])
assert qtext == 'mit license'
assert itext == 'mit license'
assert matches[0].qspan == Span(0, 1)
assert matches[0].ispan == Span(0, 1)
qtext, itext = get_texts(matches[1])
assert qtext == 'MIT License,'
assert itext == 'mit license'
assert matches[1].qspan == Span(2, 3)
assert matches[1].ispan == Span(0, 1)
query_doc2 = '''do you think I am a mit license
MIT License
yes, I think so'''
matches = idx.match(query_string=query_doc2)
assert len(matches) == 2
qtext, itext = get_texts(matches[0])
assert qtext == 'mit license'
assert itext == 'mit license'
assert matches[0].qspan == Span(0, 1)
assert matches[0].ispan == Span(0, 1)
qtext, itext = get_texts(matches[1])
assert qtext == 'MIT License'
assert itext == 'mit license'
assert matches[1].qspan == Span(2, 3)
assert matches[1].ispan == Span(0, 1)
def test_match_simple_rule(self):
tf1 = self.get_test_loc('detect/mit/t1.txt')
ftr = create_rule_from_text_file_and_expression(text_file=tf1, license_expression='bsd-original')
idx = MiniLicenseIndex([ftr])
query_doc = self.get_test_loc('detect/mit/t2.txt')
matches = idx.match(query_doc)
assert len(matches) == 1
match = matches[0]
assert match.qspan == Span(0, 240)
assert match.ispan == Span(0, 240)
assert match.lines() == (1, 27,)
assert match.coverage() == 100
assert match.score() == 100
def test_match_works_with_special_characters_1(self):
test_file = self.get_test_loc('detect/specialcharacter/kerberos.txt')
idx = MiniLicenseIndex([create_rule_from_text_file_and_expression(text_file=test_file, license_expression='kerberos')])
assert len(idx.match(test_file)) == 1
def test_match_works_with_special_characters_2(self):
test_file = self.get_test_loc('detect/specialcharacter/kerberos1.txt')
idx = MiniLicenseIndex([create_rule_from_text_file_and_expression(text_file=test_file, license_expression='kerberos')])
assert len(idx.match(test_file)) == 1
def test_match_works_with_special_characters_3(self):
test_file = self.get_test_loc('detect/specialcharacter/kerberos2.txt')
idx = MiniLicenseIndex(
[create_rule_from_text_file_and_expression(text_file=test_file, license_expression='kerberos')]
)
assert len(idx.match(test_file)) == 1
def test_match_works_with_special_characters_4(self):
test_file = self.get_test_loc('detect/specialcharacter/kerberos3.txt')
idx = MiniLicenseIndex([create_rule_from_text_file_and_expression(text_file=test_file, license_expression='kerberos')])
assert len(idx.match(test_file)) == 1
def test_overlap_detection1(self):
# test this containment relationship between test and index licenses:
# * Index licenses:
# +-license 2 --------+
# | +-license 1 --+ |
# +-------------------+
#
# * License texts to detect:
# +- license 3 -----------+
# | +-license 2 --------+ |
# | | +-license 1 --+ | |
# | +-------------------+ |
# +-----------------------+
#
# +-license 4 --------+
# | +-license 1 --+ |
# +-------------------+
# setup index
license1 = '''Redistribution and use permitted.'''
license2 = '''Redistributions of source must retain copyright.
Redistribution and use permitted.
Redistributions in binary form is permitted.'''
license3 = '''
this license source
Redistributions of source must retain copyright.
Redistribution and use permitted.
Redistributions in binary form is permitted.
has a permitted license'''
license4 = '''My Redistributions is permitted.
Redistribution and use permitted.
Use is permitted too.'''
rule1 = create_rule_from_text_and_expression(text=license1, license_expression='overlap')
rule2 = create_rule_from_text_and_expression(text=license2, license_expression='overlap')
rule3 = create_rule_from_text_and_expression(text=license3, license_expression='overlap')
rule4 = create_rule_from_text_and_expression(text=license4, license_expression='overlap')
idx = MiniLicenseIndex([rule1, rule2, rule3, rule4])
querys = 'Redistribution and use bla permitted.'
# test : license1 is in the index and contains no other rule. should return rule1 at exact coverage.
matches = idx.match(query_string=querys)
assert len(matches) == 1
match = matches[0]
assert match.qspan == Span(0, 3)
assert match.rule == rule1
qtext, _itext = get_texts(match)
assert qtext == 'Redistribution and use [bla] permitted.'
def test_overlap_detection2(self):
# test this containment relationship between test and index licenses:
# * Index licenses:
# +-license 2 --------+
# | +-license 1 --+ |
# +-------------------+
# setup index
license1 = '''Redistribution and use permitted.'''
license2 = '''Redistributions of source must retain copyright.
Redistribution and use permitted.
Redistributions in binary form is permitted.'''
rule1 = create_rule_from_text_and_expression(text=license1, license_expression='overlap')
rule2 = create_rule_from_text_and_expression(text=license2, license_expression='overlap')
idx = MiniLicenseIndex([rule1, rule2])
# test : license2 contains license1: return license2 as exact coverage
querys = 'Redistribution and use bla permitted.'
matches = idx.match(query_string=querys)
assert len(matches) == 1
match = matches[0]
assert match.rule == rule1
qtext, _itext = get_texts(match)
assert qtext == 'Redistribution and use [bla] permitted.'
def test_overlap_detection2_exact(self):
# test this containment relationship between test and index licenses:
# * Index licenses:
# +-license 2 --------+
# | +-license 1 --+ |
# +-------------------+
# setup index
license1 = '''Redistribution and use permitted.'''
license2 = '''Redistributions of source must retain copyright.
Redistribution and use permitted.
Redistributions in binary form is permitted.'''
rule1 = create_rule_from_text_and_expression(text=license1, license_expression='overlap')
rule2 = create_rule_from_text_and_expression(text=license2, license_expression='overlap')
idx = MiniLicenseIndex([rule1, rule2])
# test : license2 contains license1: return license2 as exact coverage
querys = 'Redistribution and use bla permitted.'
matches = idx.match(query_string=querys)
assert len(matches) == 1
match = matches[0]
assert match.rule == rule1
qtext, _itext = get_texts(match)
assert qtext == 'Redistribution and use [bla] permitted.'
def test_overlap_detection3(self):
# test this containment relationship between test and index licenses:
# * Index licenses:
# +-license 2 --------+
# | +-license 1 --+ |
# +-------------------+
#
# * License texts to detect:
# +- license 3 -----------+
# | +-license 2 --------+ |
# | | +-license 1 --+ | |
# | +-------------------+ |
# +-----------------------+
#
# setup index
license1 = '''Redistribution and use permitted.'''
license2 = '''Redistributions of source must retain copyright.
Redistribution and use permitted.
Redistributions in binary form is permitted.'''
rule1 = create_rule_from_text_and_expression(text=license1, license_expression='overlap')
rule2 = create_rule_from_text_and_expression(text=license2, license_expression='overlap')
idx = MiniLicenseIndex([rule1, rule2])
querys = '''My source.
Redistributions of source must retain copyright.
Redistribution and use permitted.
Redistributions in binary form is permitted.
My code.'''
# test : querys contains license2 that contains license1: return license2 as exact coverage
matches = idx.match(query_string=querys)
assert len(matches) == 1
match = matches[0]
assert match.rule == rule2
qtext, _itext = get_texts(match)
expected = '''
Redistributions of source must retain copyright.
Redistribution and use permitted.
Redistributions in binary form is permitted.'''.split()
assert qtext.split() == expected
def test_overlap_detection4(self):
# test this containment relationship between test and index licenses:
# * Index licenses:
# +-license 2 --------+
# | +-license 1 --+ |
# +-------------------+
#
# +-license 4 --------+
# | +-license 1 --+ |
# +-------------------+
# setup index
license1 = '''Redistribution and use permitted.'''
license2 = '''Redistributions of source must retain copyright.
Redistribution and use permitted.
Redistributions in binary form is permitted.'''
rule1 = create_rule_from_text_and_expression(text=license1, license_expression='overlap')
rule2 = create_rule_from_text_and_expression(text=license2, license_expression='overlap')
idx = MiniLicenseIndex([rule1, rule2])
querys = '''My source.
Redistribution and use permitted.
My code.'''
# test : querys contains license1: return license1 as exact coverage
matches = idx.match(query_string=querys)
assert len(matches) == 1
match = matches[0]
assert match.rule == rule1
qtext, _itext = get_texts(match)
assert qtext == 'Redistribution and use permitted.'
def test_overlap_detection5(self):
# test this containment relationship between test and index licenses:
# * Index licenses:
# +-license 2 --------+
# | +-license 1 --+ |
# +-------------------+
#
# +-license 4 --------+
# | +-license 1 --+ |
# +-------------------+
# setup index
license1 = '''Redistribution and use permitted for MIT license.'''
license2 = '''Redistributions of source must retain copyright.
Redistribution and use permitted for MIT license.
Redistributions in binary form is permitted.'''
rule1 = create_rule_from_text_and_expression(text=license1, license_expression='overlap')
rule2 = create_rule_from_text_and_expression(text=license2, license_expression='overlap')
idx = MiniLicenseIndex([rule1, rule2])
querys = '''My source.
Redistribution and use permitted for MIT license.
My code.'''
# test : querys contains license1: return license1 as exact coverage
matches = idx.match(query_string=querys)
assert len(matches) == 1
match = matches[0]
assert match.rule == rule1
qtext, _itext = get_texts(match)
assert qtext == 'Redistribution and use permitted for MIT license.'
def test_fulltext_detection_works_with_partial_overlap_from_location(self):
test_doc = self.get_test_loc('detect/templates/license3.txt')
idx = MiniLicenseIndex([create_rule_from_text_file_and_expression(text_file=test_doc, license_expression='mylicense')])
query_loc = self.get_test_loc('detect/templates/license4.txt')
matches = idx.match(query_loc)
assert len(matches) == 1
match = matches[0]
assert match.qspan == Span(0, 41)
assert match.ispan == Span(0, 41)
assert match.coverage() == 100
assert match.score() == 100
qtext, _itext = get_texts(match)
expected = '''
is free software; you can redistribute it and/or # modify it under
the terms of the GNU Lesser General Public # License as published by
the Free Software Foundation; either # version 2.1 of the License,
or (at your option) any later version.'''
assert ' '.join(qtext.split()) == ' '.join(expected.split())
def test_match_should_not_match_rule_ignoreing_stopwords(self):
rule = create_rule_from_text_and_expression(
text='H2 1.0',
license_expression='h2-1.0',
is_required_phrase=True,
)
idx = MiniLicenseIndex([rule])
matches = idx.match(query_string='Manifest-Version: 1.0')
# we should have NO matches but since h2 is a stopword .... it is ignored!
try:
assert matches == []
except AssertionError:
pass
class TestIndexPartialMatch(FileBasedTesting):
test_data_dir = TEST_DATA_DIR
def test_match_can_match_with_plain_rule_simple(self):
tf1_text = u'''X11 License
Copyright (C) 1996 X Consortium
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: The above copyright
notice and this permission notice shall be included in all copies or
substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS",
WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE. Except as contained in this notice, the
name of the X Consortium shall not be used in advertising or otherwise to
promote the sale, use or other dealings in this Software without prior
written authorization from the X Consortium. X Window System is a trademark
of X Consortium, Inc.
'''
rule = create_rule_from_text_and_expression(text=tf1_text, license_expression='x-consortium')
idx = MiniLicenseIndex([rule])
query_loc = self.get_test_loc('detect/simple_detection/x11-xconsortium_text.txt')
matches = idx.match(query_loc)
assert len(matches) == 1
match = matches[0]
assert match.qspan == Span(0, 213)
def test_match_can_match_with_plain_rule_simple2(self):
rule_text = u'''X11 License
Copyright (C) 1996 X Consortium
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: The above copyright
notice and this permission notice shall be included in all copies or
substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS",
WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE. Except as contained in this notice, the
name of the X Consortium shall not be used in advertising or otherwise to
promote the sale, use or other dealings in this Software without prior
written authorization from the X Consortium. X Window System is a trademark
of X Consortium, Inc.
'''
rule = create_rule_from_text_and_expression(text=rule_text, license_expression='x-consortium')
idx = MiniLicenseIndex([rule])
query_loc = self.get_test_loc('detect/simple_detection/x11-xconsortium_text.txt')
matches = idx.match(location=query_loc)
assert len(matches) == 1
expected_qtext = u'''
X11 License
Copyright (C) 1996 X Consortium
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: The above copyright
notice and this permission notice shall be included in all copies or
substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS",
WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE. Except as contained in this notice, the
name of the X Consortium shall not be used in advertising or otherwise to
promote the sale, use or other dealings in this Software without prior
written authorization from the X Consortium. X Window System is a trademark
of X Consortium, Inc.
'''.split()
match = matches[0]
qtext, _itext = get_texts(match)
assert qtext.split() == expected_qtext
def test_match_can_match_with_simple_rule_template2(self):
rule_text = u'''
IN NO EVENT SHALL THE
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''
rule = create_rule_from_text_and_expression(text=rule_text, license_expression='x-consortium')
idx = MiniLicenseIndex([rule])
query_string = u'''
IN NO EVENT SHALL THE Y CORP
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''
matches = idx.match(query_string=query_string)
assert len(matches) == 1
match = matches[0]
qtext, itext = get_texts(match)
expected_qtokens = u'''
IN NO EVENT SHALL THE [Y] [CORP] BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''.split()
assert qtext.split() == expected_qtokens
expected_itokens = u'''
IN NO EVENT SHALL THE BE LIABLE FOR ANY CLAIM DAMAGES OR OTHER LIABILITY
WHETHER IN AN ACTION OF CONTRACT TORT OR OTHERWISE ARISING FROM OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
'''.lower().split()
assert itext.split() == expected_itokens
def test_match_can_match_discontinuous_rule_text_1(self):
test_text = u'''Redistributions in binary form must
reproduce the above copyright notice'''
rule = create_rule_from_text_and_expression(text=test_text, license_expression='mylicense')
idx = MiniLicenseIndex([rule])
querys = u'''Redistributions in binary form must nexB company
reproduce the word for word above copyright notice.'''
matches = idx.match(query_string=querys)
assert len(matches) == 1
match = matches[0]
assert match.coverage() == 100
assert 36.67 == match.score()
assert Span(0, 9) == match.qspan
assert Span(0, 9) == match.ispan
def test_match_can_match_discontinuous_rule_text_2(self):
test_text = u'''Redistributions in binary form must
reproduce the stipulated above copyright notice'''
rule = create_rule_from_text_and_expression(text=test_text, license_expression='mylicense')
idx = MiniLicenseIndex([rule])
querys = u'''Redistributions in binary form must nexB company
reproduce the stipulated word for word above copyright notice.'''
matches = idx.match(query_string=querys)
assert len(matches) == 1
match = matches[0]
assert match.coverage() == 100
assert match.score() == 41.94
assert match.qspan == Span(0, 10)
assert match.ispan == Span(0, 10)
def test_match_can_match_discontinuous_rule_text_3(self):
test_text = u'''Redistributions in binary form must
reproduce as is stipulated above copyright notice'''
rule = create_rule_from_text_and_expression(text=test_text, license_expression='mylicense')
idx = MiniLicenseIndex([rule])
querys = u'''Redistributions in binary form must nexB company
reproduce as is stipulated the word for word above copyright notice.'''
matches = idx.match(query_string=querys)
assert len(matches) == 1
match = matches[0]
assert match.qspan == Span(0, 11)
assert match.ispan == Span(0, 11)
def test_match_can_match_with_sax_rule_for_public_domain(self):
test_text = '''
I hereby abandon any property rights to , and release all of source
code, compiled code, and documentation contained in this distribution
into the Public Domain.
'''
rule = create_rule_from_text_and_expression(text=test_text, license_expression='public-domain')
legalese = build_dictionary_from_iterable(
set(mini_legalese) |
set(['property', 'abandon', 'rights', ])
)
idx = index.LicenseIndex([rule], _legalese=legalese)
querys = '''
SAX2 is Free!
I hereby abandon any property rights to SAX 2.0 (the Simple API for
XML), and release all of the SAX 2.0 source code, compiled code, and
documentation contained in this distribution into the Public Domain. SAX
comes with NO WARRANTY or guarantee of fitness for any purpose.
SAX2 is Free!
'''
matches = idx.match(query_string=querys)
assert len(matches) == 1
match = matches[0]
qtext, itext = get_texts(match)
expected_qtext = ' '.join(u'''
I hereby abandon any property rights to [SAX] [2].[0] ([the] [Simple]
[API] [for] [XML]), [and] [release] [all] [of] [the] [SAX] [2].[0]
source code, compiled code, and documentation contained in this
distribution into the Public Domain.
'''.split())
assert ' '.join(qtext.split()) == expected_qtext
expected_itext = ' '.join(u'''
I hereby abandon any property rights to
<and> <release> <all> <of>
source code compiled code and documentation contained in this distribution
into the Public Domain
'''.lower().split())
assert ' '.join(itext.split()) == expected_itext
assert match.coverage() == 84
assert match.score() == 84
assert match.qspan == Span(0, 6) | Span(13, 26)
assert match.ispan == Span(0, 6) | Span(11, 24)
def test_match_can_match_with_rule_template_with_gap_near_start_with_few_tokens_before(self):
# failed when a gapped token starts at a beginning of rule with few tokens before
test_file = self.get_test_loc('detect/templates/license7.txt')
rule = create_rule_from_text_file_and_expression(text_file=test_file, license_expression='lic')
legalese = build_dictionary_from_iterable(
set(mini_legalese) |
set(['permission', 'written', 'registered', 'derived', 'damage', 'due'])
)
idx = index.LicenseIndex([rule], _legalese=legalese)
qloc = self.get_test_loc('detect/templates/license8.txt')
matches = idx.match(qloc)
assert len(matches) == 1
match = matches[0]
expected_qtokens = u"""
All Rights Reserved.
Redistribution and use of this software and associated documentation
("Software"), with or without modification, are permitted provided
that the following conditions are met:
1. Redistributions of source code must retain copyright
statements and notices. Redistributions must also contain a
copy of this document.
2. Redistributions in binary form must reproduce the
above copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
3. The name "[groovy]" must not be used to endorse or promote
products derived from this Software without prior written
permission of [The] [Codehaus]. For written permission,
please contact [info]@[codehaus].[org].
4. Products derived from this Software may not be called "[groovy]"
nor may "[groovy]" appear in their names without prior written
permission of [The] [Codehaus]. "[groovy]" is a registered
trademark of [The] [Codehaus].
5. Due credit should be given to [The] [Codehaus] -
[http]://[groovy].[codehaus].[org]/
[THIS] [SOFTWARE] [IS] [PROVIDED] [BY] [THE] [CODEHAUS] [AND] [CONTRIBUTORS]
``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
[THE] [CODEHAUS] OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
""".split()
expected_itokens = u''' All Rights Reserved Redistribution and use of this
software and associated documentation Software with or without modification
are permitted provided that the following conditions are met
1 Redistributions of source code must retain copyright statements and notices
Redistributions must also contain copy of this document
2 Redistributions in binary form must reproduce the above copyright notice
this list of conditions and the following disclaimer in the documentation and
or other materials provided with the distribution
3 The name must not be used to endorse or promote products derived from this
Software without prior written permission of For written permission please
contact
4 Products derived from this Software may not be called nor may appear in
their names without prior written permission of is registered trademark of
5 Due credit should be given to
<THIS> <SOFTWARE> <IS> <PROVIDED> <BY>
AS IS AND ANY EXPRESSED OR IMPLIED WARRANTIES INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR PARTICULAR
PURPOSE ARE DISCLAIMED IN NO EVENT SHALL OR ITS CONTRIBUTORS BE LIABLE FOR
ANY DIRECT INDIRECT INCIDENTAL SPECIAL EXEMPLARY OR CONSEQUENTIAL DAMAGES
INCLUDING BUT NOT LIMITED TO PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES LOSS
OF USE DATA OR PROFITS OR BUSINESS INTERRUPTION HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY WHETHER IN CONTRACT STRICT LIABILITY OR TORT INCLUDING
NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
'''.lower().split()
qtext, itext = get_texts(match)
assert qtext.split() == expected_qtokens
assert itext.split() == expected_itokens
assert match.coverage() == 97.52
assert match.score() == 97.52
expected = Span(2, 97) | Span(99, 124) | Span(126, 129) | Span(131, 137) | Span(147, 175) | Span(177, 250)
assert match.qspan == expected
expected = Span(1, 133) | Span(139, 241)
assert match.ispan == expected
def test_match_can_match_with_index_built_from_rule_directory_with_sun_bcls(self):
rule_dir = self.get_test_loc('detect/rule_template/rules')
idx = MiniLicenseIndex(load_rules(rule_dir))
# at line 151 the query has an extra "Software" word inserted to avoid hash matching
query_loc = self.get_test_loc('detect/rule_template/query.txt')
matches = idx.match(location=query_loc)
assert len(matches) == 1
match = matches[0]
expected = Span(0, 949) | Span(951, 1739)
assert match.qspan == expected
assert match.matcher == match_seq.MATCH_SEQ
class TestMatchAccuracyWithFullIndex(FileBasedTesting):
test_data_dir = TEST_DATA_DIR
def check_position(self, test_path, expected, with_span=True):
"""
Check license detection in file or folder against expected result.
Expected is a list of (license, lines span, qspan span) tuples.
"""
test_location = self.get_test_loc(test_path)
results = []
# FULL INDEX!!
idx = cache.get_index()
matches = idx.match(test_location)
for match in matches:
for detected in match.rule.license_keys():
results.append((detected, match.lines(), with_span and match.qspan or None))
assert results == expected
def test_match_has_correct_positions_basic(self):
idx = cache.get_index()
querys = u'''Licensed under the GNU General Public License (GPL).
Licensed under the GNU General Public License (GPL).
Licensed under the GNU General Public License (GPL).'''
matches = idx.match(query_string=querys)
rule = [r for r in idx.rules_by_rid if r.identifier == 'gpl_69.RULE'][0]
m1 = LicenseMatch(rule=rule, matcher='2-aho', qspan=Span(0, 7), ispan=Span(0, 7), start_line=1, end_line=1)
m2 = LicenseMatch(rule=rule, matcher='2-aho', qspan=Span(8, 15), ispan=Span(0, 7), start_line=2, end_line=2)
m3 = LicenseMatch(rule=rule, matcher='2-aho', qspan=Span(16, 23), ispan=Span(0, 7), start_line=3, end_line=3)
assert matches == [m1, m2, m3]
def test_match_has_correct_line_positions_for_query_with_repeats(self):
expected = [
# licenses, match.lines(), qtext,
([u'apache-2.0'], (1, 2), u'The Apache Software License, Version 2.0\nhttp://www.apache.org/licenses/LICENSE-2.0.txt'),
([u'apache-2.0'], (3, 4), u'The Apache Software License, Version 2.0\nhttp://www.apache.org/licenses/LICENSE-2.0.txt'),
([u'apache-2.0'], (5, 6), u'The Apache Software License, Version 2.0\nhttp://www.apache.org/licenses/LICENSE-2.0.txt'),
([u'apache-2.0'], (7, 8), u'The Apache Software License, Version 2.0\nhttp://www.apache.org/licenses/LICENSE-2.0.txt'),
([u'apache-2.0'], (9, 10), u'The Apache Software License, Version 2.0\nhttp://www.apache.org/licenses/LICENSE-2.0.txt'),
]
test_path = 'positions/license1.txt'
test_location = self.get_test_loc(test_path)
idx = cache.get_index()
matches = idx.match(test_location)
for i, match in enumerate(matches):
ex_lics, ex_lines, ex_qtext = expected[i]
qtext, _itext = get_texts(match)
try:
assert match.rule.license_keys() == ex_lics
assert match.lines() == ex_lines
assert qtext == ex_qtext
except AssertionError:
assert (match.rule.license_keys(), match.lines(), qtext) == expected[i]
def test_match_does_not_return_spurious_match(self):
expected = []
self.check_position('positions/license2.txt', expected)
def test_match_has_correct_line_positions_for_repeats(self):
# we had a weird error where the lines were not computed correctly
# when we had more than one file detected at a time
expected = [
# detected, match.lines(), match.qspan,
(u'apache-2.0', (1, 2), Span(0, 15)),
(u'apache-2.0', (3, 4), Span(16, 31)),
(u'apache-2.0', (5, 6), Span(32, 47)),
(u'apache-2.0', (7, 8), Span(48, 63)),
(u'apache-2.0', (9, 10), Span(64, 79)),
]
self.check_position('positions/license3.txt', expected)
def test_match_returns_correct_lines(self):
test_location = self.get_test_loc('positions/correct_lines')
expected = [('mit', (1, 1))]
results = []
idx = cache.get_index()
matches = idx.match(test_location)
for match in matches: