Skip to content

Commit 51580e5

Browse files
committed
Add test for SHA sum lengths
Check for wrong SHA sum lengths in release posts, an error that happens from time to time (mainly truncated or wrapped SHA512).
1 parent 6144c5c commit 51580e5

8 files changed

+118
-0
lines changed

lib/linter.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ def check
8282
errors[doc] << "missing translator variable" if doc.translator_missing?
8383
errors[doc] << "missing date variable" if doc.date_missing?
8484
end
85+
86+
if doc.release_post?
87+
errors[doc] << "invalid SHA1 length" if doc.sha1_length_invalid?
88+
errors[doc] << "invalid SHA256 length" if doc.sha256_length_invalid?
89+
errors[doc] << "invalid SHA512 length" if doc.sha512_length_invalid?
90+
else
91+
end
8592
end
8693
end
8794

lib/linter/document.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ def post?
1919
filename.match? %r{/_posts/}
2020
end
2121

22+
def release_post?
23+
post? && filename.match?(%r{released\.md})
24+
end
25+
2226
# posts from before the migration to the Jekyll site
2327
# (they follow different rules; e.g. they have no YAML date variable,
2428
# filenames of translations differ from original `en' post, ...)
@@ -91,6 +95,27 @@ def trailing_whitespace?
9195
content.match?(/ $/)
9296
end
9397

98+
def sha1_length_invalid?
99+
matchdata = content.match(/SHA1: *(?<sha>[0-9a-f]*)/)
100+
return nil unless matchdata
101+
102+
matchdata[:sha].size != 40
103+
end
104+
105+
def sha256_length_invalid?
106+
matchdata = content.match(/SHA256: *(?<sha>[0-9a-f]*)/)
107+
return nil unless matchdata
108+
109+
matchdata[:sha].size != 64
110+
end
111+
112+
def sha512_length_invalid?
113+
matchdata = content.match(/SHA512: *(?<sha>[0-9a-f]*)/)
114+
return nil unless matchdata
115+
116+
matchdata[:sha].size != 128
117+
end
118+
94119
private
95120

96121
def read_yaml_and_content(filename)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
layout: news_post
3+
title: "Release Post"
4+
author: "stomar"
5+
translator:
6+
date: 2019-01-01 12:00:00 +0000
7+
lang: en
8+
---
9+
10+
Content
11+
12+
Sums, SHA1 too short
13+
14+
SHA1: abcdef78901234567890
15+
SHA256: abcdef7890123456789012345678901234567890123456789012345678901234
16+
SHA512: abcdef78901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
layout: news_post
3+
title: "Release Post"
4+
author: "stomar"
5+
translator:
6+
date: 2019-01-02 12:00:00 +0000
7+
lang: en
8+
---
9+
10+
Content
11+
12+
Sums, SHA256 too long
13+
14+
SHA1: abcdef7890123456789012345678901234567890
15+
SHA256: abcdef7890123456789012345678901234567890123456789012345678901234567890
16+
SHA512: abcdef78901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
layout: news_post
3+
title: "Release Post"
4+
author: "stomar"
5+
translator:
6+
date: 2019-01-03 12:00:00 +0000
7+
lang: en
8+
---
9+
10+
Content
11+
12+
Sums, SHA512 wrapped
13+
14+
SHA1: abcdef7890123456789012345678901234567890
15+
SHA256: abcdef7890123456789012345678901234567890123456789012345678901234
16+
SHA512: abcdef789012345678901234567890123456789012345678901234567890
17+
12345678901234567890123456789012345678901234567890123456789012345678
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
layout: news_post
3+
title: "Release Post"
4+
author: "stomar"
5+
translator:
6+
date: 2019-01-01 12:00:00 +0000
7+
lang: en
8+
---
9+
10+
Content
11+
12+
Sums:
13+
14+
SHA1: abcdef7890123456789012345678901234567890
15+
SHA256: abcdef7890123456789012345678901234567890123456789012345678901234
16+
SHA512: abcdef78901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
layout: news_post
3+
title: "Release Post"
4+
author: "stomar"
5+
translator:
6+
date: 2019-01-01 12:00:00 +0000
7+
lang: en
8+
---
9+
10+
Content
11+
12+
Sums:
13+
14+
SHA1: abcdef7890123456789012345678901234567890
15+
SHA256: abcdef7890123456789012345678901234567890123456789012345678901234

test/output_errors.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,9 @@ en/_posts/2018-01-14-date-variable-wrong-offset.md
5858
date variable with wrong time zone offset (not +0000)
5959
en/_posts/2018-01-15-date-mismatch.md
6060
filename not matching date variable
61+
en/_posts/2019-01-01-invalid-sha1-length-released.md
62+
invalid SHA1 length
63+
en/_posts/2019-01-02-invalid-sha256-length-released.md
64+
invalid SHA256 length
65+
en/_posts/2019-01-03-invalid-sha512-length-released.md
66+
invalid SHA512 length

0 commit comments

Comments
 (0)