<commit_before>/*
 * Copyright 2007 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.oauth.signature;

import java.util.List;
import junit.framework.TestCase;
import net.oauth.OAuth;
import net.oauth.OAuthConsumer;
import net.oauth.OAuthMessage;

public class OAuthSignatureMethodTest extends TestCase {

    public static void main(String[] args) throws Exception {
        (new OAuthSignatureMethodTest()).testSign();
    }

    private static final String[][] NORMALS = //
    { { "no parameters", null, "" } //
            , { "absent value", "name", "name=" } //
            , { "one parameter", "a=b", "a=b" } //
            , { "two parameters", "a=b&c=d", "a=b&c=d" } //
            , { "sort names", "x!y=a&x=a", "x=a&x%21y=a" } //
            , { "sort values", "a=x!y&a=x+y", "a=x%20y&a=x%21y" } //
    };

    public void testNormalizeParameters() throws Exception {
        for (int c = 0; c < NORMALS.length;) {
            String[] normal = NORMALS[c++];
            int n = 0;
            String label = normal[n++];
            List<OAuth.Parameter> parameters = OAuth.decodeForm(normal[n++]);
            String expected = normal[n++];
            OAuthSignatureMethod method = OAuthSignatureMethod.newMethod(
                    "HMAC-SHA1", new OAuthConsumer(null, null, null, null));
            String actual = method.normalizeParameters(parameters);
            assertEquals(label, expected, actual);
        }
    }

    private static final String OAUTH_A_BASE_STRING = "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&"
            + "file%3Dvacation.jpg%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26oauth_token%3Dnn<PASSWORD>%26oauth_version%3D1.0%26size%3Doriginal"
            + "&kd94hf93k423kf44&pfkkdhi9sl3r4s00";

    private static final String[][] BASES = //
    // label, signature method, HTTP method, URL, parameters,
    // consumer secret, token secret, expected
    {
            { "simple", "HMAC-SHA1", "GET", "http://example.com", "n=v", "cs",
                    "ts", "GET&http%3A%2F%2Fexample.com&n%3Dv&cs&ts" },
            {
                    "OAuth A request",
                    "HMAC-SHA1",
                    "POST",
                    "https://photos.example.net/request_token",
                    "oauth_version=1.0&oauth_consumer_key=dpf43f3p2l4k3l03&oauth_timestamp=1191242090&oauth_nonce=hsu94j3884jdopsl&oauth_signature_method=PLAINTEXT&oauth_signature=ignored",
                    "kd94hf93k423kf44",
                    null,
                    "POST&https%3A%2F%2Fphotos.example.net%2Frequest_token&"
                            + "oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dhsu94j3884jdopsl%26oauth_signature_method%3DPLAINTEXT%26oauth_timestamp%3D1191242090%26oauth_version%3D1.0"
                            + "&kd94hf93k423kf44&" },
            {
                    "OAuth A access",
                    "HMAC-SHA1",
                    "GET",
                    "http://photos.example.net/photos",
                    "file=vacation.jpg&size=original&oauth_version=1.0&oauth_consumer_key=dpf43f3p2l4k3l03&oauth_token=<PASSWORD>sl2jdk&oauth_timestamp=1191242096&oauth_nonce=kllo9940pd9333jh&oauth_signature=ignored&oauth_signature_method=HMAC-SHA1",
                    "kd94hf93k423kf44", "pfkkdhi9sl3r4s00", OAUTH_A_BASE_STRING } };

    public void testGetBaseString() throws Exception {
        for (int c = 0; c < BASES.length;) {
            String[] base = BASES[c++];
            int b = 0;
            String label = base[b++];
            String methodName = base[b++];
            String httpMethod = base[b++];
            String URL = base[b++];
            String form = base[b++];
            String consumerSecret = base[b++];
            String tokenSecret = base[b++];
            String expected = base[b++];
            OAuthSignatureMethod method = OAuthSignatureMethod.newMethod(
                    methodName, new OAuthConsumer(null, null, consumerSecret,
                            null));
            method.setTokenSecret(tokenSecret);
            List<OAuth.Parameter> parameters = OAuth.decodeForm(form);
            String actual = method.getBaseString(new OAuthMessage(httpMethod,
                    URL, parameters));
            assertEquals(label, expected, actual);
        }
    }

    private static final String[][] SIGNATURES = //
    // label, method, consumer secret, token secret, base string, expected
    {
            { "PLAINTEXT", "PLAINTEXT", "cs", "ts", "bs", "cs&ts" },
            { "HMAC-SHA1", "HMAC-SHA1", "cs", null, "bs",
                    "egQqG5AJep5sJ7anhXju1unge2I=" },
            { "HMAC-SHA1", "HMAC-SHA1", "cs", "ts", "bs",
                    "VZVjXceV7JgPq/dOTnNmEfO0Fv8=" },
            { "OAuth A request", "PLAINTEXT", "kd94hf93k423kf44", null, null,
                    "kd94hf93k423kf44&" },
            { "OAuth A access", "HMAC-SHA1", "kd94hf93k423kf44",
                    "pfkkdhi9sl3r4s00", OAUTH_A_BASE_STRING,
                    "Gcg/323lvAsQ707p+y41y14qWfY=" } };

    public void testSign() throws Exception {
        StringBuffer errors = new StringBuffer();
        for (int c = 0; c < SIGNATURES.length;) {
            String[] signature = SIGNATURES[c++];
            int s = 0;
            String label = signature[s++];
            String methodName = signature[s++];
            String consumerSecret = signature[s++];
            String tokenSecret = signature[s++];
            String baseString = signature[s++];
            String expected = signature[s++];
            String accessorSecret = "wrong";
            OAuthConsumer consumer = new OAuthConsumer(null, null,
                    consumerSecret, null);
            consumer.setProperty(OAuthConsumer.ACCESSOR_SECRET, accessorSecret);
            OAuthSignatureMethod method = OAuthSignatureMethod.newMethod(
                    methodName, consumer);
            method.setTokenSecret(tokenSecret);
            String actual = method.getSignature(baseString);
            if (!expected.equals(actual)) {<commit_msg>OAuth Draft 6 signature base string<commit_after>/*
 * Copyright 2007 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.oauth.signature;

import java.util.List;
import junit.framework.TestCase;
import net.oauth.OAuth;
import net.oauth.OAuthConsumer;
import net.oauth.OAuthMessage;

public class OAuthSignatureMethodTest extends TestCase {

    public static void main(String[] args) throws Exception {
        (new OAuthSignatureMethodTest()).testSign();
    }

    private static final String[][] NORMALS = //
    { { "no parameters", null, "" } //
            , { "absent value", "name", "name=" } //
            , { "one parameter", "a=b", "a=b" } //
            , { "two parameters", "a=b&c=d", "a=b&c=d" } //
            , { "sort names", "x!y=a&x=a", "x=a&x%21y=a" } //
            , { "sort values", "a=x!y&a=x+y", "a=x%20y&a=x%21y" } //
    };

    public void testNormalizeParameters() throws Exception {
        for (int c = 0; c < NORMALS.length;) {
            String[] normal = NORMALS[c++];
            int n = 0;
            String label = normal[n++];
            List<OAuth.Parameter> parameters = OAuth.decodeForm(normal[n++]);
            String expected = normal[n++];
            OAuthSignatureMethod method = OAuthSignatureMethod.newMethod(
                    "HMAC-SHA1", new OAuthConsumer(null, null, null, null));
            String actual = method.normalizeParameters(parameters);
            assertEquals(label, expected, actual);
        }
    }

    private static final String OAUTH_A_BASE_STRING = "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&"
            + "file%3Dvacation.jpg%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26oauth_token%3Dnn<PASSWORD>%26oauth_version%3D1.0%26size%3Doriginal";

    private static final String[][] BASES = //
    // label, signature method, HTTP method, URL, parameters,
    // consumer secret, token secret, expected
    {
            { "simple", "HMAC-SHA1", "GET", "http://example.com", "n=v", "cs",
                    "ts", "GET&http%3A%2F%2Fexample.com&n%3Dv" },
            {
                    "OAuth A request",
                    "HMAC-SHA1",
                    "POST",
                    "https://photos.example.net/request_token",
                    "oauth_version=1.0&oauth_consumer_key=dpf43f3p2l4k3l03&oauth_timestamp=1191242090&oauth_nonce=hsu94j3884jdopsl&oauth_signature_method=PLAINTEXT&oauth_signature=ignored",
                    "kd94hf93k423kf44",
                    null,
                    "POST&https%3A%2F%2Fphotos.example.net%2Frequest_token&"
                            + "oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dhsu94j3884jdopsl%26oauth_signature_method%3DPLAINTEXT%26oauth_timestamp%3D1191242090%26oauth_version%3D1.0" },
            {
                    "OAuth A access",
                    "HMAC-SHA1",
                    "GET",
                    "http://photos.example.net/photos",
                    "file=vacation.jpg&size=original&oauth_version=1.0&oauth_consumer_key=dpf43f3p2l4k3<PASSWORD>&oauth_token=<PASSWORD>&oauth_timestamp=1191242096&oauth_nonce=kllo9940pd9333jh&oauth_signature=ignored&oauth_signature_method=HMAC-SHA1",
                    "kd94hf93k423kf44", "pfkkdhi9sl3r4s00", OAUTH_A_BASE_STRING } };

    public void testGetBaseString() throws Exception {
        for (int c = 0; c < BASES.length;) {
            String[] base = BASES[c++];
            int b = 0;
            String label = base[b++];
            String methodName = base[b++];
            String httpMethod = base[b++];
            String URL = base[b++];
            String form = base[b++];
            String consumerSecret = base[b++];
            String tokenSecret = base[b++];
            String expected = base[b++];
            OAuthSignatureMethod method = OAuthSignatureMethod.newMethod(
                    methodName, new OAuthConsumer(null, null, consumerSecret,
                            null));
            method.setTokenSecret(tokenSecret);
            List<OAuth.Parameter> parameters = OAuth.decodeForm(form);
            String actual = method.getBaseString(new OAuthMessage(httpMethod,
                    URL, parameters));
            assertEquals(label, expected, actual);
        }
    }

    private static final String[][] SIGNATURES = //
    // label, method, consumer secret, token secret, base string, expected
    {
            { "PLAINTEXT", "PLAINTEXT", "cs", "ts", "bs", "cs&ts" },
            { "HMAC-SHA1", "HMAC-SHA1", "cs", null, "bs",
                    "egQqG5AJep5sJ7anhXju1unge2I=" },
            { "HMAC-SHA1", "HMAC-SHA1", "cs", "ts", "bs",
                    "VZVjXceV7JgPq/dOTnNmEfO0Fv8=" },
            { "OAuth A request", "PLAINTEXT", "kd94hf93k423kf44", null, null,
                    "kd94hf93k423kf44&" },
            { "OAuth A access", "HMAC-SHA1", "kd94hf93k423kf44",
                    "pfkkdhi9sl3r4s00", OAUTH_A_BASE_STRING,
                    "tR3+Ty81lMeYAr/Fid0kMTYa/WM=" } };

    public void testSign() throws Exception {
        StringBuffer errors = new StringBuffer();
        for (int c = 0; c < SIGNATURES.length;) {
            String[] signature = SIGNATURES[c++];
            int s = 0;
            String label = signature[s++];
            String methodName = signature[s++];
            String consumerSecret = signature[s++];
            String tokenSecret = signature[s++];
            String baseString = signature[s++];
            String expected = signature[s++];
            String accessorSecret = "wrong";
            OAuthConsumer consumer = new OAuthConsumer(null, null,
                    consumerSecret, null);
            consumer.setProperty(OAuthConsumer.ACCESSOR_SECRET, accessorSecret);
            OAuthSignatureMethod method = OAuthSignatureMethod.newMethod(
                    methodName, consumer);
            method.setTokenSecret(tokenSecret);
            String actual = method.getSignature(baseString);
            if (!expected.equals(actual)) {
{'fraction_non_alphanumeric': 0.12170187175825002, 'fraction_numerical': 0.0402164925204841, 'mean_word_length': 2.330162703379224, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 12, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6079352', 'n_tokens_mistral': 4452, 'n_tokens_neox': 4117, 'n_words': 815}