@Test public void testDateStr() { Date date = DateUtil.now().get(); Integer time = new Long(date.getTime()).intValue(); System.out.println(DateUtil.formatDateTime(date)); System.out.println(date.getTime() + ":" + time + ":" + Integer.toHexString(time).toUpperCase()); }
@Test public void test1() { // DateUtil cal = DateUtil.now(); cal.setTime(0, 0, 0, 0); int year = cal.getYear(); int month = cal.getMonth(); // 添加12个月的计划 for (int i = 1; i <= 12; i++) { // 设置月份 cal.setYear(year); cal.setMonth(month + i); // 从月末的一周中找出周四和周五 cal.setDay(-7); int day = cal.getDay(); for (int j = 1; j <= 7; j++) { cal.setDay(day + j); if (cal.getWeek() == 4 || cal.getWeek() == 5) { String str = DateUtil.format(cal.get(), "yyyy-MM-dd HH:mm:ss E"); System.out.println("i=" + i + ", day=" + day + ", date=" + str); } } } }
@Test public void testCreateAuthorizationHeader() { Date now = DateUtil.now(); String method = "POST"; String hostname = "api.tuppari.com"; String path = "/test"; String query = "a=v1&b=v2"; Map<String, String> headers = new HashMap<String, String>(); headers.put("Host", "api.tuppari.com"); headers.put("Content-type", "application/json"); headers.put("X-Tuppari-Operation", "CreateApplication"); Map<String, Object> body = new LinkedHashMap<String, Object>(); body.put("applicationName", "example1"); String accessKeyId = "accessKeyId"; String accessSecretKey = "accessSecretKey"; String authorization = SignUtil.createAuthorizationHeader( method, hostname, path, query, headers, body, now, accessKeyId, accessSecretKey); assertThat( authorization, is( "HMAC-SHA256 Credential=accessKeyId,SignedHeaders=content-type;host;x-tuppari-operation,Signature=050f8711271747d4f63a3caa3ffb420e4cd5a0e9d9dda8ba7e4faad6794c40d0")); }
@Test public void testCreateSignedRequestConfig() throws ParseException { Date now = DateUtil.now(); String method = "POST"; URI uri = URI.create("http://api.tuppari.com/test?a=v1&b=v2"); String operation = "CreateApplication"; Map<String, Object> body = new LinkedHashMap<String, Object>(); body.put("applicationName", "example1"); String accessKeyId = "accessKeyId"; String accessSecretKey = "accessSecretKey"; Map<String, Object> config = SignUtil.createSignedRequestConfig( method, uri, operation, body, accessKeyId, accessSecretKey); assertThat((URI) config.get("uri"), is(uri)); assertThat((String) config.get("body"), is(JSON.encode(body))); Map<String, String> headers = (Map<String, String>) config.get("headers"); assertThat(headers.get("Host"), is("api.tuppari.com")); assertThat(headers.get("Content-Type"), is("application/json")); assertThat(headers.get("X-Tuppari-Date"), is("Thu, 01 Jan 1970 00:00:00 GMT")); assertThat(headers.get("X-Tuppari-Operation"), is("CreateApplication")); assertThat( headers.get("Authorization"), is( "HMAC-SHA256 Credential=accessKeyId,SignedHeaders=content-type;host;x-tuppari-date;x-tuppari-operation,Signature=f35767c9fdba4ba5d5bbbf1c622fceed0dbaeb210303bb56b419c6a51bcf1e5d")); }
@Test public void testCreateSignature_1() throws ParseException { String secretKey = "secretKey1"; String stringToSign = "SHA256\n" + "19700101T000000Z\n" + "152176000cc08c7d9d0558bc3a50368aa38619a695ad20f50bec1344429cb315"; Date now = DateUtil.now(); String host = "api.tuppari.com"; String expectedSignature = "4815ff1681a278e7c852902ea3604f17831a80a78dc0ff82f5142598a034509b"; // Check with the same secret key, siganature returns same signature for (int i = 0; i < 1000; ++i) { String result = SignUtil.createSignature(secretKey, stringToSign, now, host); assertThat(result, is(expectedSignature)); } }
@Test public void testCreateSignature_2() throws ParseException { String secretKey1 = "secretKey1"; String secretKey2 = "secretKey2"; String stringToSign = "SHA256\n" + "19700101T000000Z\n" + "152176000cc08c7d9d0558bc3a50368aa38619a695ad20f50bec1344429cb315"; Date now = DateUtil.now(); String host = "api.tuppari.com"; String expectedSignature = "4815ff1681a278e7c852902ea3604f17831a80a78dc0ff82f5142598a034509b"; String result1 = SignUtil.createSignature(secretKey1, stringToSign, now, host); String result2 = SignUtil.createSignature(secretKey2, stringToSign, now, host); assertThat(result1, not(equalTo(result2))); }
@Test public void testCreateStringToSign() throws ParseException { String canonicalRequest = "POST\n" + "/test\n" + "a=v1&b=v2\n" + "content-type:application/json\n" + "host:api.tuppari.com\n" + "x-tuppari-operation:CreateApplication\n" + "content-type;host;x-tuppari-operation\n" + "8f2d5fe4a93000d3546e578d265fc936806f6ef6dc6f7ee87715e1a5c514c168"; Date now = DateUtil.now(); String result = SignUtil.createStringToSign(canonicalRequest, now); assertThat( result, is( "" + "SHA256\n" + "19700101T000000Z\n" + "152176000cc08c7d9d0558bc3a50368aa38619a695ad20f50bec1344429cb315")); }