|
| 1 | +local parse_openssl_time = require "resty.auto-ssl.utils.parse_openssl_time" |
| 2 | +local stdlib = require "posix.stdlib" |
| 3 | + |
| 4 | +describe("parse_openssl_time", function() |
| 5 | + local orig_tz |
| 6 | + before_each(function() |
| 7 | + orig_tz = os.getenv("TZ") |
| 8 | + end) |
| 9 | + after_each(function() |
| 10 | + stdlib.setenv("TZ", orig_tz) |
| 11 | + end) |
| 12 | + |
| 13 | + it("parses basic openssl time", function() |
| 14 | + local timestamp, err = parse_openssl_time("Jun 22 00:00:00 2036 GMT") |
| 15 | + assert.Nil(err) |
| 16 | + assert.equal(2097705600, timestamp) |
| 17 | + end) |
| 18 | + |
| 19 | + it("parses single digit days", function() |
| 20 | + local timestamp, err = parse_openssl_time("Mar 7 12:00:00 2020 GMT") |
| 21 | + assert.Nil(err) |
| 22 | + assert.equal(1583582400, timestamp) |
| 23 | + end) |
| 24 | + |
| 25 | + it("parses prefixed output from openssl", function() |
| 26 | + local timestamp, err = parse_openssl_time("notAfter=Dec 17 17:55:50 2019 GMT") |
| 27 | + assert.Nil(err) |
| 28 | + assert.equal(1576605350, timestamp) |
| 29 | + end) |
| 30 | + |
| 31 | + it("parses times with milliseconds", function() |
| 32 | + local timestamp, err = parse_openssl_time("Jul 31 22:20:50.123 2017 GMT") |
| 33 | + assert.Nil(err) |
| 34 | + assert.equal(1501539650, timestamp) |
| 35 | + end) |
| 36 | + |
| 37 | + it("parses times with 1 fractional digit for seconds", function() |
| 38 | + local timestamp, err = parse_openssl_time("Jul 31 22:20:50.1 2017 GMT") |
| 39 | + assert.Nil(err) |
| 40 | + assert.equal(1501539650, timestamp) |
| 41 | + end) |
| 42 | + |
| 43 | + it("parses times without GMT suffix", function() |
| 44 | + local timestamp, err = parse_openssl_time("Nov 28 20:21:47 2019") |
| 45 | + assert.Nil(err) |
| 46 | + assert.equal(1574972507, timestamp) |
| 47 | + end) |
| 48 | + |
| 49 | + it("returns error for unknown data", function() |
| 50 | + local timestamp, err = parse_openssl_time("Bad time value") |
| 51 | + assert.Nil(timestamp) |
| 52 | + assert.equal("could not parse openssl time string: Bad time value", err) |
| 53 | + end) |
| 54 | + |
| 55 | + it("returns error for unknown month", function() |
| 56 | + local timestamp, err = parse_openssl_time("Abc 22 00:00:00 2036 GMT") |
| 57 | + assert.Nil(timestamp) |
| 58 | + assert.equal("could not parse month in openssl time string: Abc 22 00:00:00 2036 GMT", err) |
| 59 | + end) |
| 60 | + |
| 61 | + it("months are case sensitive", function() |
| 62 | + local timestamp, err = parse_openssl_time("jan 22 00:00:00 2036 GMT") |
| 63 | + assert.Nil(timestamp) |
| 64 | + assert.equal("could not parse month in openssl time string: jan 22 00:00:00 2036 GMT", err) |
| 65 | + end) |
| 66 | + |
| 67 | + it("ignores the system time zone and always outputs in UTC unix timestamps", function() |
| 68 | + stdlib.setenv("TZ", "Pacific/Honolulu") |
| 69 | + -- Sanity check to ensure "os.time" behavior is picking up the TZ that is |
| 70 | + -- set (which is incorrect for our purposes), and that our values differ. |
| 71 | + local local_time = os.time({ |
| 72 | + year = 2036, |
| 73 | + month = 6, |
| 74 | + day = 22, |
| 75 | + hour = 0, |
| 76 | + min = 0, |
| 77 | + sec = 0, |
| 78 | + }) |
| 79 | + assert.equal(2097741600, local_time) |
| 80 | + local timestamp, err = parse_openssl_time("Jun 22 00:00:00 2036 GMT") |
| 81 | + assert.Nil(err) |
| 82 | + assert.equal(2097705600, timestamp) |
| 83 | + assert.Not.equal(timestamp, local_time) |
| 84 | + |
| 85 | + stdlib.setenv("TZ", "Asia/Kolkata") |
| 86 | + local_time = os.time({ |
| 87 | + year = 2036, |
| 88 | + month = 6, |
| 89 | + day = 22, |
| 90 | + hour = 0, |
| 91 | + min = 0, |
| 92 | + sec = 0, |
| 93 | + }) |
| 94 | + assert.equal(2097685800, local_time) |
| 95 | + timestamp, err = parse_openssl_time("Jun 22 00:00:00 2036 GMT") |
| 96 | + assert.Nil(err) |
| 97 | + assert.equal(2097705600, timestamp) |
| 98 | + assert.Not.equal(timestamp, local_time) |
| 99 | + end) |
| 100 | + |
| 101 | + -- Based on the eras from |
| 102 | + -- http://howardhinnant.github.io/date_algorithms.html#civil_from_days, along |
| 103 | + -- with other boundaries (epoch, Y2038, etc). |
| 104 | + it("parses various historical, future, and boundary times", function() |
| 105 | + local timestamp, err = parse_openssl_time("Mar 1 00:00:00 -0800 GMT") |
| 106 | + assert.Nil(err) |
| 107 | + assert.equal(-87407596800, timestamp) |
| 108 | + |
| 109 | + timestamp, err = parse_openssl_time("Feb 29 00:00:00 -0400 GMT") |
| 110 | + assert.Nil(err) |
| 111 | + assert.equal(-74784902400, timestamp) |
| 112 | + |
| 113 | + timestamp, err = parse_openssl_time("Mar 1 00:00:00 -0400 GMT") |
| 114 | + assert.Nil(err) |
| 115 | + assert.equal(-74784816000, timestamp) |
| 116 | + |
| 117 | + timestamp, err = parse_openssl_time("Jan 1 00:00:00 0000 GMT") |
| 118 | + assert.Nil(err) |
| 119 | + assert.equal(-62167219200, timestamp) |
| 120 | + |
| 121 | + timestamp, err = parse_openssl_time("Feb 29 00:00:00 0000 GMT") |
| 122 | + assert.Nil(err) |
| 123 | + assert.equal(-62162121600, timestamp) |
| 124 | + |
| 125 | + timestamp, err = parse_openssl_time("Mar 1 00:00:00 0000 GMT") |
| 126 | + assert.Nil(err) |
| 127 | + assert.equal(-62162035200, timestamp) |
| 128 | + |
| 129 | + timestamp, err = parse_openssl_time("Feb 29 00:00:00 0400 GMT") |
| 130 | + assert.Nil(err) |
| 131 | + assert.equal(-49539340800, timestamp) |
| 132 | + |
| 133 | + timestamp, err = parse_openssl_time("Mar 1 00:00:00 0400 GMT") |
| 134 | + assert.Nil(err) |
| 135 | + assert.equal(-49539254400, timestamp) |
| 136 | + |
| 137 | + timestamp, err = parse_openssl_time("Feb 29 00:00:00 0800 GMT") |
| 138 | + assert.Nil(err) |
| 139 | + assert.equal(-36916560000, timestamp) |
| 140 | + |
| 141 | + timestamp, err = parse_openssl_time("Mar 1 00:00:00 0800 GMT") |
| 142 | + assert.Nil(err) |
| 143 | + assert.equal(-36916473600, timestamp) |
| 144 | + |
| 145 | + timestamp, err = parse_openssl_time("Feb 29 00:00:00 1200 GMT") |
| 146 | + assert.Nil(err) |
| 147 | + assert.equal(-24293779200, timestamp) |
| 148 | + |
| 149 | + timestamp, err = parse_openssl_time("Mar 1 00:00:00 1200 GMT") |
| 150 | + assert.Nil(err) |
| 151 | + assert.equal(-24293692800, timestamp) |
| 152 | + |
| 153 | + timestamp, err = parse_openssl_time("Feb 29 00:00:00 1600 GMT") |
| 154 | + assert.Nil(err) |
| 155 | + assert.equal(-11670998400, timestamp) |
| 156 | + |
| 157 | + timestamp, err = parse_openssl_time("Mar 1 00:00:00 1600 GMT") |
| 158 | + assert.Nil(err) |
| 159 | + assert.equal(-11670912000, timestamp) |
| 160 | + |
| 161 | + timestamp, err = parse_openssl_time("Jan 1 00:00:00 1970 GMT") |
| 162 | + assert.Nil(err) |
| 163 | + assert.equal(0, timestamp) |
| 164 | + |
| 165 | + timestamp, err = parse_openssl_time("Feb 29 00:00:00 2000 GMT") |
| 166 | + assert.Nil(err) |
| 167 | + assert.equal(951782400, timestamp) |
| 168 | + |
| 169 | + timestamp, err = parse_openssl_time("Mar 1 00:00:00 2000 GMT") |
| 170 | + assert.Nil(err) |
| 171 | + assert.equal(951868800, timestamp) |
| 172 | + |
| 173 | + timestamp, err = parse_openssl_time("Jan 18 00:00:00 2038 GMT") |
| 174 | + assert.Nil(err) |
| 175 | + assert.equal(2147385600, timestamp) |
| 176 | + |
| 177 | + timestamp, err = parse_openssl_time("Jan 19 00:00:00 2038 GMT") |
| 178 | + assert.Nil(err) |
| 179 | + assert.equal(2147472000, timestamp) |
| 180 | + |
| 181 | + timestamp, err = parse_openssl_time("Jan 20 00:00:00 2038 GMT") |
| 182 | + assert.Nil(err) |
| 183 | + assert.equal(2147558400, timestamp) |
| 184 | + |
| 185 | + timestamp, err = parse_openssl_time("Feb 29 00:00:00 2400 GMT") |
| 186 | + assert.Nil(err) |
| 187 | + assert.equal(13574563200, timestamp) |
| 188 | + end) |
| 189 | +end) |
0 commit comments